mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
audio: Refactoring
This commit is contained in:
parent
1327e2239c
commit
fe55e55b7e
@ -85,30 +85,14 @@ func NewContext(sampleRate int) (*Context, error) {
|
|||||||
panic("audio: context is already created")
|
panic("audio: context is already created")
|
||||||
}
|
}
|
||||||
|
|
||||||
ch := make(chan struct{})
|
|
||||||
|
|
||||||
c := &Context{
|
c := &Context{
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
c: newContext(sampleRate, ch),
|
c: newContext(sampleRate),
|
||||||
initCh: ch,
|
initCh: make(chan struct{}),
|
||||||
}
|
}
|
||||||
theContext = c
|
theContext = c
|
||||||
c.mux = newMux()
|
c.mux = newMux()
|
||||||
|
|
||||||
go c.loop()
|
|
||||||
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CurrentContext returns the current context or nil if there is no context.
|
|
||||||
func CurrentContext() *Context {
|
|
||||||
theContextLock.Lock()
|
|
||||||
c := theContext
|
|
||||||
theContextLock.Unlock()
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Context) loop() {
|
|
||||||
h := getHook()
|
h := getHook()
|
||||||
h.OnSuspendAudio(func() {
|
h.OnSuspendAudio(func() {
|
||||||
c.m.Lock()
|
c.m.Lock()
|
||||||
@ -138,6 +122,20 @@ func (c *Context) loop() {
|
|||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go c.loop()
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentContext returns the current context or nil if there is no context.
|
||||||
|
func CurrentContext() *Context {
|
||||||
|
theContextLock.Lock()
|
||||||
|
c := theContext
|
||||||
|
theContextLock.Unlock()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) loop() {
|
||||||
<-c.initCh
|
<-c.initCh
|
||||||
|
|
||||||
defer c.c.Close()
|
defer c.c.Close()
|
||||||
|
@ -93,13 +93,22 @@ func (p *otoPlayer) ensurePlayer() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newContext(sampleRate int, initCh <-chan struct{}) context {
|
func newContext(sampleRate int) context {
|
||||||
if contextForTesting != nil {
|
if contextForTesting != nil {
|
||||||
return contextForTesting
|
return contextForTesting
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ch := make(chan struct{})
|
||||||
|
var once sync.Once
|
||||||
|
hooks.AppendHookOnBeforeUpdate(func() error {
|
||||||
|
once.Do(func() {
|
||||||
|
close(ch)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
return &otoContext{
|
return &otoContext{
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
initCh: initCh,
|
initCh: ch,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user