Search code examples
javascripthtmlgoogle-chromeweb-audio-api

How can I play audio in reverse with web audio API?


How can I play audio in reverse with the web audio API? I can't seem to find anything in the API docs...


Solution

  • You could do something like this:

    var context = new AudioContext(),
        request = new XMLHttpRequest();
    request.open('GET', 'path/to/audio.mp3', true);
    request.responseType = 'arraybuffer';
    request.addEventListener('load', function(){
        context.decodeAudioData(request.response, function(buffer){
            var source = context.createBufferSource();
            Array.prototype.reverse.call( buffer.getChannelData(0) );
            Array.prototype.reverse.call( buffer.getChannelData(1) );
            source.buffer = buffer;
        });
    });
    

    It's a super simple example - but the point is basically that you can grab the Float32Array instance for each channel in the AudioBuffer and reverse them.