7
votes

I'm using ScriptProcessorNode's onaudioprocess callback to process the microphone input. By connecting MediaStreamSourceNode to the ScriptProcessorNode, I can get the raw audio data within the onaudioprocess callback function. However, after about 30 seconds (this varies ranging from 10 to 35 sec,) the browser stops calling onaudioprocess. In the following code, the console.log output ('>>') always stops after about 30 sec.

var ctx = new AudioContext();
var BUFFER_LENGTH = 4096;
console.log('Buffer length is + ' + BUFFER_LENGTH);
navigator.webkitGetUserMedia({audio: true}, function (stream) {
    var mediaStreamSource = ctx.createMediaStreamSource(stream);
    var scriptProcessor = ctx.createScriptProcessor(BUFFER_LENGTH, 1, 1);
    scriptProcessor.onaudioprocess = function (e) {
      console.log('>>');
    };
    scriptProcessor.connect(ctx.destination);
}, function(e) {
  console.error('Unable to get audio input source.');
});

I tried all the possible BUFFER_LENGTH (256, 512, 1024, 2048, 4096, 8192, 16384), but the situation didn't change (log stops after 30 sec.) I observed this issue in the latest Chrome Release (Version 35.0.1916.153) and Canary (Version 37.0.2060.3 canary.) Does anyone know any workarounds?

1
I'm wondering if it has to do with the fact that you're returning empty e.outputbuffers in your onaudioprocess callbacks. After 30 seconds on seeing empty buffers Chrome may want to save battery/processing power and turn off the callback. - notthetup
Thanks notthetup, I tried copying the inputBuffer to outputBuffer in onaudioprocess. But it didn't fix the issue. Heres the code: var source = e.inputBuffer.getChannelData(0); var dest = e.outputBuffer.getChannelData(0); dest.set(source); - kuu

1 Answers

23
votes

This looks more like your scriptprocessor object is getting garbage collected. Try saving it in a global variable and see if that fixes the problem.