audio/internal/readerdriver: Use a channel instead of a callback function

This commit is contained in:
Hajime Hoshi 2021-04-29 18:21:06 +09:00
parent b509ce523d
commit 760072e3a9
2 changed files with 12 additions and 9 deletions

View File

@ -40,10 +40,11 @@ type contextImpl struct {
bitDepthInBytes int
}
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady func()) (Context, error) {
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, chan struct{}, error) {
ready := make(chan struct{})
if js.Global().Get("go2cpp").Truthy() {
defer onReady()
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, nil
close(ready)
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, ready, nil
}
class := js.Global().Get("AudioContext")
@ -51,7 +52,7 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady fun
class = js.Global().Get("webkitAudioContext")
}
if !class.Truthy() {
return nil, errors.New("readerdriver: AudioContext or webkitAudioContext was not found")
return nil, nil, errors.New("readerdriver: AudioContext or webkitAudioContext was not found")
}
options := js.Global().Get("Object").New()
options.Set("sampleRate", sampleRate)
@ -69,7 +70,7 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady fun
if !d.ready {
d.audioContext.Call("resume")
d.ready = true
onReady()
close(ready)
}
js.Global().Get("document").Call("removeEventListener", event, f)
return nil
@ -86,7 +87,7 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady fun
setCallback("keyup")
setCallback("mouseup")
return d, nil
return d, ready, nil
}
type playerImpl struct {

View File

@ -67,12 +67,14 @@ func (p *readerPlayer) ensurePlayer() error {
// is unexpectable.
// e.g. a variable for JVM on Android might not be set.
if p.factory.context == nil {
c, err := readerdriver.NewContext(p.factory.sampleRate, channelNum, bitDepthInBytes, func() {
p.context.setReady()
})
c, ready, err := readerdriver.NewContext(p.factory.sampleRate, channelNum, bitDepthInBytes)
if err != nil {
return err
}
go func() {
<-ready
p.context.setReady()
}()
p.factory.context = c
}
if p.stream == nil {