Revert "audio/internal/go2cpp: Fix C++ side and remove the preparation"

This reverts commit 65f974588c.

Reason: Didn't improve the performance on some devices
This commit is contained in:
Hajime Hoshi 2021-01-17 04:48:11 +09:00
parent 65f974588c
commit 2432530b51

View File

@ -23,12 +23,18 @@ import (
type Context struct {
v js.Value
sampleRate int
channelNum int
bitDepthInBytes int
}
func NewContext(sampleRate int, channelNum, bitDepthInBytes int) *Context {
v := js.Global().Get("go2cpp").Call("createAudio", sampleRate, channelNum, bitDepthInBytes)
return &Context{
v: v,
sampleRate: sampleRate,
channelNum: channelNum,
bitDepthInBytes: bitDepthInBytes,
}
}
@ -103,7 +109,22 @@ func (p *Player) Play() {
p.v.Set("volume", p.volume)
runloop = true
}
p.v.Call("play")
// Prepare the first data as soon as possible, or the audio can get stuck.
// TODO: Get the appropriate buffer size from the C++ side.
buf := make([]byte, p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes/4)
n, err := p.src.Read(buf)
if err != nil && err != io.EOF {
p.setError(err)
return
}
if n > 0 {
dst := js.Global().Get("Uint8Array").New(n)
p.writeImpl(dst, buf[:n])
}
if runloop {
go p.loop()
}