audio: refactoring

This commit is contained in:
Hajime Hoshi 2024-03-16 21:39:00 +09:00
parent 9faa3f4601
commit 9cc017412f
2 changed files with 2 additions and 32 deletions

View File

@ -60,11 +60,6 @@ const (
type Context struct {
playerFactory *playerFactory
// 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.
inited chan struct{}
initedOnce sync.Once
sampleRate int
err error
ready bool
@ -100,7 +95,6 @@ func NewContext(sampleRate int) *Context {
sampleRate: sampleRate,
playerFactory: newPlayerFactory(sampleRate),
playingPlayers: map[*playerImpl]struct{}{},
inited: make(chan struct{}),
semaphore: make(chan struct{}, 1),
}
theContext = c
@ -128,10 +122,6 @@ func NewContext(sampleRate int) *Context {
})
h.AppendHookOnBeforeUpdate(func() error {
c.initedOnce.Do(func() {
close(c.inited)
})
var err error
theContextLock.Lock()
if theContext != nil {
@ -316,18 +306,6 @@ func (c *Context) SampleRate() int {
return c.sampleRate
}
func (c *Context) acquireSemaphore() {
c.semaphore <- struct{}{}
}
func (c *Context) releaseSemaphore() {
<-c.semaphore
}
func (c *Context) waitUntilInited() {
<-c.inited
}
// Player is an audio player which has one stream.
//
// Even when all references to a Player object is gone,

View File

@ -30,20 +30,12 @@ func newContext(sampleRate int) (context, chan struct{}, error) {
return &contextProxy{ctx}, ready, err
}
// 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
*oto.Context
}
// NewPlayer implements context.
func (c *contextProxy) NewPlayer(r io.Reader) player {
return c.otoContext.NewPlayer(r)
return c.Context.NewPlayer(r)
}