mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
audio/internal/readerdriver: Use a channel instead of a callback function
This commit is contained in:
parent
b509ce523d
commit
760072e3a9
@ -40,10 +40,11 @@ type contextImpl struct {
|
|||||||
bitDepthInBytes int
|
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() {
|
if js.Global().Get("go2cpp").Truthy() {
|
||||||
defer onReady()
|
close(ready)
|
||||||
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, nil
|
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, ready, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
class := js.Global().Get("AudioContext")
|
class := js.Global().Get("AudioContext")
|
||||||
@ -51,7 +52,7 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady fun
|
|||||||
class = js.Global().Get("webkitAudioContext")
|
class = js.Global().Get("webkitAudioContext")
|
||||||
}
|
}
|
||||||
if !class.Truthy() {
|
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 := js.Global().Get("Object").New()
|
||||||
options.Set("sampleRate", sampleRate)
|
options.Set("sampleRate", sampleRate)
|
||||||
@ -69,7 +70,7 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady fun
|
|||||||
if !d.ready {
|
if !d.ready {
|
||||||
d.audioContext.Call("resume")
|
d.audioContext.Call("resume")
|
||||||
d.ready = true
|
d.ready = true
|
||||||
onReady()
|
close(ready)
|
||||||
}
|
}
|
||||||
js.Global().Get("document").Call("removeEventListener", event, f)
|
js.Global().Get("document").Call("removeEventListener", event, f)
|
||||||
return nil
|
return nil
|
||||||
@ -86,7 +87,7 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int, onReady fun
|
|||||||
setCallback("keyup")
|
setCallback("keyup")
|
||||||
setCallback("mouseup")
|
setCallback("mouseup")
|
||||||
|
|
||||||
return d, nil
|
return d, ready, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type playerImpl struct {
|
type playerImpl struct {
|
||||||
|
@ -67,12 +67,14 @@ func (p *readerPlayer) ensurePlayer() error {
|
|||||||
// is unexpectable.
|
// is unexpectable.
|
||||||
// e.g. a variable for JVM on Android might not be set.
|
// e.g. a variable for JVM on Android might not be set.
|
||||||
if p.factory.context == nil {
|
if p.factory.context == nil {
|
||||||
c, err := readerdriver.NewContext(p.factory.sampleRate, channelNum, bitDepthInBytes, func() {
|
c, ready, err := readerdriver.NewContext(p.factory.sampleRate, channelNum, bitDepthInBytes)
|
||||||
p.context.setReady()
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
<-ready
|
||||||
|
p.context.setReady()
|
||||||
|
}()
|
||||||
p.factory.context = c
|
p.factory.context = c
|
||||||
}
|
}
|
||||||
if p.stream == nil {
|
if p.stream == nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user