demo/events-demo.js
function (data) {
    try {
        // Create an audio buffer to hold the data
        var audioBuffer = audioContext.createBuffer(captureCfg.channels, (data.length / captureCfg.channels),
            captureCfg.sampleRate);

        // Initialize the audio buffer with the data
        if (captureCfg.channels > 1) {
            // For multiple channels (stereo) we assume that the data is interleaved
            for (var i = 0; i < captureCfg.channels; i++) {
                var chdata = [],
                    index = 0;

                while (index < data.length) {
                    chdata.push(data[index + i]);
                    index += parseInt(captureCfg.channels);
                }

                audioBuffer.getChannelData(i).set(chdata);
            }
        }
        else {
            // For just one channels (mono)
            audioBuffer.getChannelData(0).set(data);
        }

        // Create a buffer source based on the audio buffer
        var source = audioContext.createBufferSource();
        source.buffer = audioBuffer;

        // Connect the buffer source to the gain node
        source.connect(micGainNode);

        // Play the audio immediately
        source.start(0);

        // Increase the debug counter for played data
        totalPlayedData += data.length;

        // Return the duration of the sound so that we can play the next sound when ended.
        return audioBuffer.duration;
    }
    catch(e) {
        alert("playAudio exception: " + e);
        return 100;
    }
}
function (data) {
    try {
        // Create an audio buffer to hold the data
        var audioBuffer = audioContext.createBuffer(captureCfg.channels, (data.length / captureCfg.channels),
            captureCfg.sampleRate);

        // Initialize the audio buffer with the data
        if (captureCfg.channels > 1) {
            // For multiple channels (stereo) we assume that the data is interleaved
            for (var i = 0; i < captureCfg.channels; i++) {
                var chdata = [],
                    index = 0;

                while (index < data.length) {
                    chdata.push(data[index + i]);
                    index += parseInt(captureCfg.channels);
                }

                audioBuffer.getChannelData(i).set(chdata);
            }
        }
        else {
            // For just one channels (mono)
            audioBuffer.getChannelData(0).set(data);
        }

        // Create a buffer source based on the audio buffer
        var source = audioContext.createBufferSource();
        source.buffer = audioBuffer;

        // Connect the buffer source to the gain node
        source.connect(micGainNode);

        // Play the audio immediately
        source.start(0);

        // Increase the debug counter for played data
        totalPlayedData += data.length;

        // Return the duration of the sound so that we can play the next sound when ended.
        return audioBuffer.duration;
    }
    catch(e) {
        alert("playAudio exception: " + e);
        return 100;
    }
}
function ( data ) { try { // Create an audio buffer to hold the data var audioBuffer = audioContext . createBuffer ( captureCfg . channels , ( data . length / captureCfg . channels ) , captureCfg . sampleRate ) ; // Initialize the audio buffer with the data if ( captureCfg . channels > 1 ) { // For multiple channels (stereo) we assume that the data is interleaved for ( var i = 0 ; i < captureCfg . channels ; i ++ ) { var chdata = [ ] , index = 0 ; while ( index < data . length ) { chdata . push ( data [ index + i ] ) ; index += parseInt ( captureCfg . channels ) ; } audioBuffer . getChannelData ( i ) . set ( chdata ) ; } } else { // For just one channels (mono) audioBuffer . getChannelData ( 0 ) . set ( data ) ; } // Create a buffer source based on the audio buffer var source = audioContext . createBufferSource ( ) ; source . buffer = audioBuffer ; // Connect the buffer source to the gain node source . connect ( micGainNode ) ; // Play the audio immediately source . start ( 0 ) ; // Increase the debug counter for played data totalPlayedData += data . length ; // Return the duration of the sound so that we can play the next sound when ended. return audioBuffer . duration ; } catch ( e ) { alert ( "playAudio exception: " + e ) ; return 100 ; } }
Play audio from data using the Web Audio API
Play audio from data using the Web Audio API