Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Created July 4, 2018 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hajimehoshi/7c9ee1574e5d28c476a41154d8921d04 to your computer and use it in GitHub Desktop.
Save hajimehoshi/7c9ee1574e5d28c476a41154d8921d04 to your computer and use it in GitHub Desktop.
func (p *player) resample() {
inL, inR := toLR(p.buf)
if len(inL) != len(inR) {
panic("oto: len(inL) must == len(inR)")
}
s := p.context.Get("sampleRate").Int()
if p.sampleRate == s {
p.buf = nil
p.bufL = append(p.bufL, inL...)
p.bufR = append(p.bufR, inR...)
return
}
outLen := len(inL) * s / p.sampleRate
if outLen == 0 {
return
}
ctx := offlineAudioContextClass.New(p.channelNum, outLen, s)
ctx.Set("oncomplete", func(e *js.Object) {
buf := e.Get("renderedBuffer")
outL := buf.Call("getChannelData", 0).Interface().([]float32)
outR := buf.Call("getChannelData", 1).Interface().([]float32)
p.bufL = append(p.bufL, outL...)
p.bufR = append(p.bufR, outR...)
})
buf := ctx.Call("createBuffer", p.channelNum, len(inL), p.sampleRate)
buf.Call("copyToChannel", inL, 0, 0)
buf.Call("copyToChannel", inR, 1, 0)
src := ctx.Call("createBufferSource")
src.Set("buffer", buf)
src.Call("connect", ctx.Get("destination"))
src.Call("start", 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment