I'm trying to figure out a way to stop a web audio script processor node from running, without disconnecting it.
My initial thought was to just set the "onaudioprocess" to "null" to stop it, but when I do this I hear a really short loop of audio playing. My guess is that the audio buffer is not being cleared or something and it's repeatedly playing the same buffer.
I tried some additional techniques like first setting the buffer channel array values all to 0, then setting the "onaudioprocess" to "null", this still produced a looping slice of audio instead of silence.
context = new webkitAudioContext()
scriptProcessor = context.createScriptProcessor()
scriptProcessor.onaudioprocess = (e)->
outBufferL = e.outputBuffer.getChannelData(0)
outBufferR = e.outputBuffer.getChannelData(1)
i = 0
while i < bufferSize
outBufferL[i] = randomNoiseFunc()
outBufferR[i] = randomNoiseFunc()
i++
return null
return null
stopFunc1: ->
scriptProcessor.onaudioprocess = null
stopFunc2: ->
scriptProcessor.onaudioprocess = (e)->
outBufferL = e.outputBuffer.getChannelData(0)
outBufferR = e.outputBuffer.getChannelData(1)
i = 0
while i < bufferSize
outBufferL[i] = 0
outBufferR[i] = 0
i++
scriptProcessor.onaudioprocess = null
return null
return null
Is there a way to do this correctly or am I just thinking about it wrong?
Any help greatly appreciated.
Maybe I'm misunderstanding...
But if you null out onaudioprocess
, it won't stop playback immediately unless you just happen to hit it at the very end of the current buffer.
Let's say your bufferSize
is 2048, and you happen to null out onaudioprocess
half-way through the current buffer duration of 46ms (2048 / 44100 * 1000). You're still going to have the another 23ms of audio that's already been processed by your ScriptProcessor
before you nulled it out.
Best bet is probably to throw a gain node into the path and just mute it on-demand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With