Skip to content

Instantly share code, notes, and snippets.

@mattandrews
Created December 10, 2015 16:00
Show Gist options
  • Save mattandrews/1ca8a3a003d6dd7b7769 to your computer and use it in GitHub Desktop.
Save mattandrews/1ca8a3a003d6dd7b7769 to your computer and use it in GitHub Desktop.
A Web Audio API example with code mostly taken from http://www.html5rocks.com/en/tutorials/webaudio/intro/ by Boris Smus
// original code from http://www.html5rocks.com/en/tutorials/webaudio/intro/
// with thanks to Boris Smus (https://twitter.com/borismus)
define([], function() {
var context;
var bufferLoader;
var BUFFERS_TO_LOAD;
var BUFFERS = {};
var SOURCES = {};
var maxGain = 1;
var minGain = 0;
function BufferLoader(context, urlList, callback) {
this.context = context;
this.urlList = urlList;
this.onload = callback;
this.bufferList = [];
this.loadCount = 0;
}
BufferLoader.prototype.loadBuffer = function(url, index) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
var loader = this;
request.onload = function() {
// Asynchronously decode the audio file data in request.response
loader
.context
.decodeAudioData(request.response, function(buffer) {
if (!buffer) {
alert('error decoding file data: ' + url);
return;
}
loader.bufferList[index] = buffer;
if (++loader.loadCount == loader.urlList.length)
loader.onload(loader.bufferList);
}, function(error) {
console.error('decodeAudioData error', error);
});
};
request.onerror = function() {
alert('BufferLoader: XHR error');
};
request.send();
};
BufferLoader.prototype.load = function() {
for (var i = 0; i < this.urlList.length; ++i)
this.loadBuffer(this.urlList[i], i);
};
// Loads all sound samples into the buffers object.
function loadBuffers(list, loadedCallback) {
BUFFERS_TO_LOAD = list;
// Array-ify
var names = [];
var paths = [];
for (var name in BUFFERS_TO_LOAD) {
var path = BUFFERS_TO_LOAD[name];
names.push(name);
paths.push(path);
}
bufferLoader = new BufferLoader(context, paths, function(bufferList) {
for (var i = 0; i < bufferList.length; i++) {
var buffer = bufferList[i];
var name = names[i];
BUFFERS[name] = buffer;
SOURCES[name] = createSource(buffer);
}
window.setTimeout(function() {
loadedCallback();
}, 500);
});
bufferLoader.load();
}
function startAll(shouldMute) {
// begin each buffer
for (var s in SOURCES) {
var src = SOURCES[s];
if (shouldMute) {
src.gainNode.gain.value = minGain;
}
src.source.start(0);
}
}
function toggleTrack(track) {
var src = SOURCES[track];
if (src) {
var curGain = src.gainNode.gain.value;
if (curGain === maxGain) { // mute it
src.gainNode.gain.value = minGain;
} else { // max it
src.gainNode.gain.value = maxGain;
}
}
}
function createSource(buffer) {
var source = context.createBufferSource();
var gainNode = context.createGain ? context.createGain() : context.createGainNode();
source.buffer = buffer;
source.loop = true;
source.connect(gainNode);
gainNode.connect(context.destination);
return {
source: source,
gainNode: gainNode
};
}
function init(listOfTracks, loadedCallback) {
try {
// Fix up prefixing
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
} catch (e) {
alert("Web Audio API is not supported in this browser");
}
loadBuffers(listOfTracks, loadedCallback);
}
return {
init: init,
startAll: startAll,
toggleTrack: toggleTrack
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment