Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 644
Cole Reed Avatar asked Jul 15 '13 07:07

Cole Reed


1 Answers

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.

like image 75
Kevin Ennis Avatar answered Oct 21 '22 21:10

Kevin Ennis