Where to start, what to use, what to do, etc.
The main object for the API.
var AC = new webkitAudioContext();
Loading a buffer via a XMLHttpRequest.
// previously defined: AC
var buffer, url, request, error_handler;
url = "music.mp3";
request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
// decode asynchronously
request.onload = function() {
AC.decodeAudioData(request.response, function(b) {
buffer = b;
}, error_handler);
}
// send request
request.send();
To play the sound we need to create a 'source' for the sound.
// previously defined: AC, buffer
var source;
source = AC.createBufferSource();
source.buffer = buffer;
source.connect(AC.destination); // connect to the speakers
source.noteOn(0); // play
Why? Well, because it allows streaming
// previously defined: AC
var audio_element, source;
audio_element = new Audio();
audio_element.src = "music.mp3";
audio_element.autoplay = true;
document.body.appendChild(audio_element);
window.addEventListener("load", function(e) {
source = AC.createMediaElementSource(audio_element);
source.connect(AC.destination);
}, false);
// previously defined: AC, source
var volume_node;
volume_node = AC.createGainNode();
volume_node.gain.value = 0.5; // the volume
source.connect(volume_node);
volume_node.connect(AC.destination);
// previously defined: AC, source, volume_node
var filter_node;
filter_node = AC.createBiquadFilter();
filter_node.type = 0; // low-pass filter
filter_node.frequency.value = 220;
source.connect(volume_node);
volume_node.connect(filter_node);
filter_node.connect(AC.destination);