var hasWebkitAudio = typeof(webkitAudioContext) === "function";
Initialisation
a bit like a canvas 2D or WebGL context
var context = new webkitAudioContext();
Load a sound to get a buffer
function loadSound(url, ctx, onSuccess, onError) {
// init request
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer'; // <= here secret sauce!
// function called once data is loaded
request.onload = function(){
// request.response === encoded... so decode it now
ctx.decodeAudioData(request.response, function(buffer) {
onSuccess && onSuccess(buffer);
}, function(){
onError && onError();
});
}
request.send(); // Start the request
}
Basic sound playing
Plain playing it. No fx whatsoever
function playSound(ctx, buffer) {
// create a 'bufferSource' node
var source = ctx.createBufferSource();
// setup the buffer
source.buffer = buffer;
// connect it to context destination
source.connect(ctx.destination);
// play immediatly
source.noteOn(0);
}
Steps for Basic sound playing
Create a audio context
new webkitAudioContext()
Load the audio buffer
request.responseType = 'arraybuffer';
Connect a buffer source to the context destination
src.connect(ctx.destination);
Trigger a play
source.noteOn(0);
Ok we got basic code...
What about those nodes ?
Routes of Nodes
Nodes and Routing
What is Routing ?
audio processing is a chain of nodes
nodes connected to each other act as "route for sound"
Sound follows this route
a Simple Route
a More Complex Route :)
Nodes and Routing
Relation between the two
a route is a several interconnected nodes
a node is one stage in a sound route
Tuning
The route determines the quality of the played sound
void setPosition(in float x, in float y, in float z);
void setOrientation(in float x, in float y, in float z);// in degree
void setVelocity(in float x, in float y, in float z);
attribute float gain; // linear gain (default 1.0)
attribute float dopplerFactor; // same as OpenAL (default 1.0)
attribute float speedOfSound; // in meters / second (default 343.3)
AudioPannerNode: Directional sound cone
attribute float coneInnerAngle; // gain 1.0 in InnerAngle
attribute float coneOuterAngle; // linear gain decrease
attribute float coneOuterGain; // gain when in OutterGain