audio: Apply delaying when initializing

This commit is contained in:
Hajime Hoshi 2017-01-22 19:43:03 +09:00
parent 7201015d02
commit 8c2064c8b7

View File

@ -23,6 +23,11 @@ import (
"github.com/gopherjs/gopherjs/js"
)
// positionDelay is buffer in sample numbers for p.positionInSamples.
// Without this, adjusting p.positionInSamples with the context's currenTime
// much more often especially on Safari, which is the cause of noise (#307).
const positionDelay = 256
type Player struct {
sampleRate int
channelNum int
@ -73,10 +78,10 @@ func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
// domain page in an iframe.
p.context.Call("resume")
p.context.Call("createBufferSource").Call("start", 0)
p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
p.positionInSamples = int64(p.context.Get("currentTime").Float()*float64(p.sampleRate)) + positionDelay
})
}
p.positionInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
p.positionInSamples = int64(p.context.Get("currentTime").Float()*float64(p.sampleRate)) + positionDelay
return p, nil
}
@ -91,13 +96,9 @@ func toLR(data []byte) ([]int16, []int16) {
}
func (p *Player) Proceed(data []byte) error {
// delay is buffer in sample numbers for p.positionInSamples.
// Without this, adjusting p.positionInSamples with the context's currenTime
// much more often especially on Safari, which is the cause of noise (#307).
const delay = 256
p.bufferedData = append(p.bufferedData, data...)
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
if p.positionInSamples+delay < c {
if p.positionInSamples+positionDelay < c {
p.positionInSamples = c
}
// Heuristic data size which doesn't cause too much noise and too much delay (#299)
@ -118,7 +119,7 @@ func (p *Player) Proceed(data []byte) error {
s := p.context.Call("createBufferSource")
s.Set("buffer", buf)
s.Call("connect", p.context.Get("destination"))
s.Call("start", float64(p.positionInSamples+delay)/float64(p.sampleRate))
s.Call("start", float64(p.positionInSamples+positionDelay)/float64(p.sampleRate))
p.positionInSamples += int64(len(il))
p.bufferedData = p.bufferedData[dataSize:]
}