Re: Web Audio API Proposal

Here's a version of the JavaScriptProcessor API which deals with the sample
data in a non-interleaved way and which allows
the number of input channels to be different from the number of output
channels.  I prefer this way, but would like other people's perspectives:

var context;
var jsProcessor;

function init() {
    context = new AudioContext();
}

function setupJavascriptProcessing() {
    // bufferSize == 4096, number of output channels == 2
    jsProcessor = context.createJavaScriptProcessor(4096, 2);
    jsProcessor.onprocess = process;

    var audio = document.getElementById('audioElement');
    audio.audioSource.connect(jsProcessor);

    jsProcessor.connect(context.destination);
}

// This function gets called periodically to process a single buffer's worth
of audio
function process(event) {
    var inputBuffer = event.inputBuffer; // an AudioBuffer
    var outputBuffer = event.outputBuffer; // an AudioBuffer

    var n = inputBuffer.length; // number of sample-frames
    // it is assumed that outputBuffer.length is guaranteed to equal
inputBuffer.length
    // further, this value will equal the argument passed to
createJavaScriptProcessor()
    // and will never change

    // Often we'll have both stereo input and stereo output, but the API
should handle
    // other cases.
    var numInputChannels = inputBuffer.numberOfChannels;
    var numOutputChannels = outputBuffer.numberOfChannels; // will equal
value passed to createJavaScriptProcessor()

    // If numInputChannels == numOutputChannels == 2
    var inputSamplesL = inputBuffer.getChannelData(0); // a Float32Array
    var inputSamplesR = inputBuffer.getChannelData(1); // a Float32Array
    var outputSamplesL = outputBuffer.getChannelData(0); // a Float32Array
    var outputSamplesR = outputBuffer.getChannelData(1); // a Float32Array


    // DSP magic here where you would process n sample-frames from
inputSamples -> outputSamples...

    // We might need to have a commit() method (or something) here at the
end - hopefully not though...
    event.commit();
}

Received on Thursday, 15 July 2010 23:50:02 UTC