Question

Here is a link to a simple jsFiddle which measures the loudness of a live input using web-audio (it outputs the values as a percentage to the console).

http://jsfiddle.net/XSnsF/

I was planning to have one input and no outputs, since there's no need to delay my audio signal waiting for my custom node to finish working out the volume.

However, it soom became apparent that the scriptProcessor only logs the values if it's connected to context.destination. Am I doing something wrong? Or is this a bug? Or is this the expected behaviour?

function gotStream(stream) {

    var mediaStreamSource = context.createMediaStreamSource(stream);

    var gainNode = context.createGain();
    gainNode.gain.value = 3;

    var levelChecker = context.createScriptProcessor(2048);

    mediaStreamSource.connect(gainNode);
    gainNode.connect(levelChecker);

    //Commenting out the line directly below stops the script processor from working!
    levelChecker.connect(context.destination);
    levelChecker.onaudioprocess = process;

}

function process(e) {
    var buffer = e.inputBuffer.getChannelData(0);

    var maxVal = 0;

    for (var i = 0; i < buffer.length; i++) {

        if (maxVal < buffer[i]) {
            maxVal = buffer[i];
        }
    }

    console.log(Math.round(maxVal * 100) + "%");
}
Was it helpful?

Solution

It's a bug in the Blink & Webkit implementations. From the spec: "audioprocess events are only dispatched if the ScriptProcessorNode has at least one input or one output connected." It doesn't need both.

For now, just connect it to a zero-gain GainNode connected to the audiocontext.destination.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top