On Github meandavejustice / creating_web_audio
WebAudio vs audio tag
<audio src="/path/to/file.ogg" autoplay=""></audio> <audio controls="controls"> <source src="/path/to/file.mp3"> </audio>
var req = new XMLHttpRequest(); req.open("GET",url,true); req.responseType = "arraybuffer"; // <<<<< req.onload = function() { audioContext.decodeAudioData(req.response, function(buf) { source = audioContext.createBufferSource(); source.buffer = buf; source.connect(audioContext.destination); source.start(0); }); }; req.send();
source.start(0); source.stop();
what about pause/replay?
var trackTime = 0; function pause () { trackTime = audioContext.currentTime; source.stop(); } function play() { source.start(audioContext.currentTime + trackTime); }
// lowpass filter var bufferSize = 4096; var effect = (function() { var lastOut = 0.0; var node = audioContext.createScriptProcessor(bufferSize, 1, 1); node.onaudioprocess = function(e) { var input = e.inputBuffer.getChannelData(0); var output = e.outputBuffer.getChannelData(0); for (var i = 0; i < bufferSize; i++) { output[i] = (input[i] + lastOut) / 2.0; lastOut = output[i]; } } return node; })();http://noisehack.com/custom-audio-effects-javascript-web-audio-api/
var fft = ctx.createAnalyser(); fft.fftSize = samples; fft.connect(ctx.destination); // setup canvas and call update() // in load function ctx.decodeAudioData(req.response, function(buf) { var src = ctx.createBufferSource(); src.buffer = buf; play(src); //connect them up into a chain src.connect(fft); } // inside update function var data = new Uint8Array(samples); fft.getByteFrequencyData(data);fft visual demo
Twitter: @meandave2020
IRC: JSON_voorhees