javascript - How can I stop a Web Audio Script Processor and clear the buffer? -


i'm trying figure out way stop web audio script processor node running, without disconnecting it.

my initial thought set "onaudioprocess" "null" stop it, when hear short loop of audio playing. guess audio buffer not being cleared or , it's repeatedly playing same buffer.

i tried additional techniques first setting buffer channel array values 0, setting "onaudioprocess" "null", still produced looping slice of audio instead of silence.

i have code below (coffeescript)

context = new webkitaudiocontext() scriptprocessor = context.createscriptprocessor()  scriptprocessor.onaudioprocess = (e)->   outbufferl = e.outputbuffer.getchanneldata(0)   outbufferr = e.outputbuffer.getchanneldata(1)   = 0   while < buffersize     outbufferl[i] = randomnoisefunc()     outbufferr[i] = randomnoisefunc()     i++   return null return null 

then when want stop it

stopfunc1: ->   scriptprocessor.onaudioprocess = null 

i tried setting buffer channel arrays 0, setting callback null

stopfunc2: ->   scriptprocessor.onaudioprocess = (e)->     outbufferl = e.outputbuffer.getchanneldata(0)     outbufferr = e.outputbuffer.getchanneldata(1)     = 0     while < buffersize       outbufferl[i] = 0       outbufferr[i] = 0       i++     scriptprocessor.onaudioprocess = null     return null   return null 

both techniques result in slice of audio loops fast, instead of no audio.

is there way correctly or thinking wrong?

any appreciated.

maybe i'm misunderstanding...

but if null out onaudioprocess, won't stop playback unless happen hit @ end of current buffer.

let's buffersize 2048, , happen null out onaudioprocess half-way through current buffer duration of 46ms (2048 / 44100 * 1000). you're still going have 23ms of audio that's been processed scriptprocessor before nulled out.

best bet throw gain node path , mute on-demand.


Comments