title: How can I stop a Web Audio Script Processor and clear the buffer?
body: <p>I'm trying to figure out a way to stop a web audio script processor node from running, without disconnecting it.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <h3>I have some code like the below (coffeescript)</h3>
  
  <pre><code>context = new webkitAudioContext()
  scriptProcessor = context.createScriptProcessor()
  
  scriptProcessor.onaudioprocess = (e)-&gt;
    outBufferL = e.outputBuffer.getChannelData(0)
    outBufferR = e.outputBuffer.getChannelData(1)
    i = 0
    while i &lt; bufferSize
      outBufferL[i] = randomNoiseFunc()
      outBufferR[i] = randomNoiseFunc()
      i++
    return null
  return null
  </code></pre>
  
  <h3>Then when I want to stop it</h3>
  
  <pre><code>stopFunc1: -&gt;
    scriptProcessor.onaudioprocess = null
  </code></pre>
  
  <h3>I also tried setting the buffer channel arrays to 0, then setting the callback to null</h3>
  
  <pre><code>stopFunc2: -&gt;
    scriptProcessor.onaudioprocess = (e)-&gt;
      outBufferL = e.outputBuffer.getChannelData(0)
      outBufferR = e.outputBuffer.getChannelData(1)
      i = 0
      while i &lt; bufferSize
        outBufferL[i] = 0
        outBufferR[i] = 0
        i++
      scriptProcessor.onaudioprocess = null
      return null
    return null
  </code></pre>
  
  <h3>Both techniques result in a slice of audio that loops really fast, instead of no audio.</h3>
  
  <p>Is there a way to do this correctly or am I just thinking about it wrong?</p>
  
  <p>Any help greatly appreciated.</p>