audio/internal/go2cpp: Avoid unnecessary allocations

This commit is contained in:
Hajime Hoshi 2021-01-25 03:09:13 +09:00
parent c8ba51d26e
commit 68411e9591

View File

@ -75,6 +75,7 @@ type Player struct {
volume float64 volume float64
cond *sync.Cond cond *sync.Cond
err error err error
buf []byte
onWritten js.Func onWritten js.Func
} }
@ -114,15 +115,17 @@ func (p *Player) Play() {
// Prepare the first data as soon as possible, or the audio can get stuck. // Prepare the first data as soon as possible, or the audio can get stuck.
// TODO: Get the appropriate buffer size from the C++ side. // TODO: Get the appropriate buffer size from the C++ side.
buf := make([]byte, p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes/4) if p.buf == nil {
n, err := p.src.Read(buf) p.buf = make([]byte, p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes/4)
}
n, err := p.src.Read(p.buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
p.setError(err) p.setError(err)
return return
} }
if n > 0 { if n > 0 {
dst := js.Global().Get("Uint8Array").New(n) dst := js.Global().Get("Uint8Array").New(n)
p.writeImpl(dst, buf[:n]) p.writeImpl(dst, p.buf[:n])
} }
if runloop { if runloop {