Re: Script Processor onaudioprocess() gives up the ghost?

Thanks, Nick! It looks like the global binding does the trick.
BTW, Kumar has also written brilliantly on this and a collection of 
related issues with ScriptProcessorNode lifetimes:
http://sriku.org/blog/2013/01/30/taming-the-scriptprocessornode/#replacing-gain-node-with-scriptprocessornode

Best,
              - lonce

On 2/20/2013 1:39 AM, Nick Thompson wrote:
> Hey Lonce,
>
> I've hit this problem before as well. The key is that you're defining 
> your scriptProcessor and the onaudioprocess callback within a function 
> namespace because of require.js, so the variables don't land on the 
> global namespace. This becomes problematic because the garbage 
> collector will swing by and clean your node and listener up after your 
> code finishes referencing it. Somebody listed a bug in Chrome to avoid 
> garbage collecting event listeners like this, but I can't find it... 
> Maybe it's been fixed since I last played around with it, in which 
> case I'm not sure what the problem is :)
>
> You should be able to fix it by binding the scriptprocessor or the 
> callback to the window to prevent garbage collection: 
> https://github.com/nick-thompson/flow/blob/master/public/js/flow.js#L96
>
> Best,
> Nick
>
>
>
> On Tue, Feb 19, 2013 at 5:11 AM, lonce <lonce.wyse@zwhome.org 
> <mailto:lonce.wyse@zwhome.org>> wrote:
>
>
>     Hi -
>
>         What might cause callbacks to  onaudioprocess()  for
>     ScriptProcessorNode to suddenly cease?
>
>     I have the simplest architecture:
>          ScriptProcessorNode (generating random noise)  -> Gain node
>     -> audioContext.destination
>
>     When left to run, I get anywhere from 150 to 1500 successsive
>     callbacks to onaudioprocess(), and then it just quits (leaving a
>     dirty buffer for the gain node to read)
>
>     You can hear/see it here:
>     http://animatedsoundworks.com/jsaSound/ (select "JS Node Test"
>     from the drop-down menu, and hit "play" and let it run)
>         You can see the callbacks to onaudioprocess() logged in the
>     console window. Sometime in the first minute, they just stop.
>
>     The core code:
>
>                 // ScriptProcessor to generate noise
>                 var noiseNode =
>     config.audioContext.createScriptProcessor(config.k_bufferLength,
>     1, 1);
>                 noiseNode.onaudioprocess = function (e) {
>                     var outBuffer = e.outputBuffer.getChannelData(0);
>                     for (var i = 0; i < outBuffer.length; i += 1) {
>                         outBuffer[i] = Math.random() * 2 - 1;
>                     }
>                 }
>
>                 var gainLevelNode = config.audioContext.createGainNode();
>
>                 // Connect graph
>                 noiseNode.connect(gainLevelNode);
>                 gainLevelNode.connect(config.audioContext.destination);
>                 gainLevelNode.gain.value = .1;
>
>     To "start" and "stop" the sound, I just set the gain level.
>
>     If I am not doing something properly, I would be glad to know
>     about it!
>     Thanks,
>                      - lonce
>
>
>

Received on Tuesday, 19 February 2013 18:05:46 UTC