audio: reduce dependency on the package oto

Updates #1900
This commit is contained in:
Hajime Hoshi 2021-12-17 15:30:32 +09:00
parent 4c82064bdd
commit 55add3e323
4 changed files with 64 additions and 7 deletions

43
audio/oto.go Normal file
View File

@ -0,0 +1,43 @@
// Copyright 2021 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audio
import (
"io"
"github.com/hajimehoshi/oto/v2"
)
// otoContext is an interface for *oto.Context.
type otoContext interface {
NewPlayer(io.Reader) oto.Player
Suspend() error
Resume() error
Err() error
}
// contextProxy is a proxy between otoContext and context.
type contextProxy struct {
otoContext
}
// contextProxy implements context.
func (c *contextProxy) NewPlayer(r io.Reader) otoPlayer {
return c.otoContext.NewPlayer(r)
}
func otoContextToContext(ctx otoContext) context {
return &contextProxy{ctx}
}

View File

@ -19,12 +19,24 @@ import (
"runtime"
"sync"
"time"
"github.com/hajimehoshi/oto/v2"
)
// otoPlayer is exactly same as the interface oto.Player.
// This is defined in order to remove the dependency on Oto from this file.
type otoPlayer interface {
Pause()
Play()
IsPlaying() bool
Reset()
Volume() float64
SetVolume(volume float64)
UnplayedBufferSize() int
Err() error
io.Closer
}
type context interface {
NewPlayer(io.Reader) oto.Player
NewPlayer(io.Reader) otoPlayer
Suspend() error
Resume() error
Err() error
@ -52,7 +64,7 @@ func newPlayerFactory(sampleRate int) *playerFactory {
type player struct {
context *Context
player oto.Player
player otoPlayer
src io.Reader
stream *timeStream
factory *playerFactory

View File

@ -26,8 +26,9 @@ func newContext(sampleRate, channelNum, bitDepthInBytes int) (context, chan stru
if js.Global().Get("go2cpp").Truthy() {
ready := make(chan struct{})
close(ready)
return go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes), ready, nil
return otoContextToContext(go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)), ready, nil
}
return oto.NewContext(sampleRate, channelNum, bitDepthInBytes)
ctx, ready, err := oto.NewContext(sampleRate, channelNum, bitDepthInBytes)
return otoContextToContext(ctx), ready, err
}

View File

@ -22,5 +22,6 @@ import (
)
func newContext(sampleRate, channelNum, bitDepthInBytes int) (context, chan struct{}, error) {
return oto.NewContext(sampleRate, channelNum, bitDepthInBytes)
ctx, ready, err := oto.NewContext(sampleRate, channelNum, bitDepthInBytes)
return otoContextToContext(ctx), ready, err
}