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.
I have some code like the below (coffeescript)
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
Then when I want to stop it
stopFunc1: ->
scriptProcessor.onaudioprocess = null
I also tried setting the buffer channel arrays to 0, then setting the callback to 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
Both techniques result in a slice of audio that loops really fast, instead of no audio.
Is there a way to do this correctly or am I just thinking about it wrong?
Any help greatly appreciated.