audio: Rename context -> writerContext

This is a preparation of a new type of context with io.Readers.
This commit is contained in:
Hajime Hoshi 2021-01-07 00:26:20 +09:00
parent 9849cb5c2b
commit c42a40a541
3 changed files with 41 additions and 37 deletions

View File

@ -39,6 +39,8 @@ import (
"runtime" "runtime"
"sync" "sync"
"time" "time"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
) )
const ( const (
@ -53,7 +55,7 @@ const (
// //
// For a typical usage example, see examples/wav/main.go. // For a typical usage example, see examples/wav/main.go.
type Context struct { type Context struct {
c context c writerContext
// inited represents whether the audio device is initialized and available or not. // inited represents whether the audio device is initialized and available or not.
// On Android, audio loop cannot be started unless JVM is accessible. After updating one frame, JVM should exist. // On Android, audio loop cannot be started unless JVM is accessible. After updating one frame, JVM should exist.
@ -94,7 +96,7 @@ func NewContext(sampleRate int) *Context {
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
c: newContext(sampleRate), c: newWriterContext(sampleRate),
players: map[*playerImpl]struct{}{}, players: map[*playerImpl]struct{}{},
inited: make(chan struct{}), inited: make(chan struct{}),
semaphore: make(chan struct{}, 1), semaphore: make(chan struct{}, 1),
@ -551,3 +553,32 @@ func (p *playerImpl) SetVolume(volume float64) {
p.volume = volume p.volume = volume
p.m.Unlock() p.m.Unlock()
} }
type hook interface {
OnSuspendAudio(f func())
OnResumeAudio(f func())
AppendHookOnBeforeUpdate(f func() error)
}
var hookForTesting hook
func getHook() hook {
if hookForTesting != nil {
return hookForTesting
}
return &hookImpl{}
}
type hookImpl struct{}
func (h *hookImpl) OnSuspendAudio(f func()) {
hooks.OnSuspendAudio(f)
}
func (h *hookImpl) OnResumeAudio(f func()) {
hooks.OnResumeAudio(f)
}
func (h *hookImpl) AppendHookOnBeforeUpdate(f func() error) {
hooks.AppendHookOnBeforeUpdate(f)
}

View File

@ -21,16 +21,18 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hooks"
) )
type context interface { // writerContext represents a context represented as io.WriteClosers.
// The actual implementation is oto.Context.
type writerContext interface {
NewPlayer() io.WriteCloser NewPlayer() io.WriteCloser
io.Closer io.Closer
} }
var contextForTesting context var writerContextForTesting writerContext
func newContext(sampleRate int) context { func newWriterContext(sampleRate int) writerContext {
if contextForTesting != nil { if writerContextForTesting != nil {
return contextForTesting return writerContextForTesting
} }
ch := make(chan struct{}) ch := make(chan struct{})
@ -43,32 +45,3 @@ func newContext(sampleRate int) context {
}) })
return newOtoContext(sampleRate, ch) return newOtoContext(sampleRate, ch)
} }
type hook interface {
OnSuspendAudio(f func())
OnResumeAudio(f func())
AppendHookOnBeforeUpdate(f func() error)
}
var hookForTesting hook
func getHook() hook {
if hookForTesting != nil {
return hookForTesting
}
return &hookImpl{}
}
type hookImpl struct{}
func (h *hookImpl) OnSuspendAudio(f func()) {
hooks.OnSuspendAudio(f)
}
func (h *hookImpl) OnResumeAudio(f func()) {
hooks.OnResumeAudio(f)
}
func (h *hookImpl) AppendHookOnBeforeUpdate(f func() error) {
hooks.AppendHookOnBeforeUpdate(f)
}

View File

@ -40,7 +40,7 @@ func (p *dummyPlayer) Close() error {
} }
func init() { func init() {
contextForTesting = &dummyContext{} writerContextForTesting = &dummyContext{}
} }
type dummyHook struct { type dummyHook struct {