2014-10-16 18:48:23 +00:00
|
|
|
define(["Tone/core/Tone"], function(Tone){
|
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
"use strict";
|
2014-10-16 18:48:23 +00:00
|
|
|
/**
|
2014-10-23 02:27:01 +00:00
|
|
|
* @class Buffer loading and storage. Tone.Buffer will load and store the buffers
|
|
|
|
* in the same data structure they were given in the argument. If given
|
|
|
|
* a string, this.buffer will equal an AudioBuffer. If constructed
|
|
|
|
* with an array, the samples will be placed in an array in the same
|
|
|
|
* order.
|
2014-10-21 18:44:02 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-10-23 02:27:01 +00:00
|
|
|
* @param {Object|Array|string} url the urls to be loaded
|
2014-10-21 18:44:02 +00:00
|
|
|
*/
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
Tone.Buffer = function(){
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
var options = this.optionsObject(arguments, ["url", "callback"], Tone.Buffer.defaults);
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
/**
|
2014-10-23 02:27:01 +00:00
|
|
|
* stores the loaded AudioBuffers in the same format they were
|
|
|
|
* given in the constructor
|
|
|
|
* @type {Object|Array|AudioBuffer}
|
2014-10-21 18:44:02 +00:00
|
|
|
*/
|
2014-10-23 02:27:01 +00:00
|
|
|
this.buffers = null;
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
var self = this;
|
|
|
|
if(typeof options.url !== "object") {
|
|
|
|
this._loadBuffer(options.url, options.callback); //it's a string
|
|
|
|
} else { //otherwise it's an array of object map
|
|
|
|
this._loadBuffers(options.url, function(buffer){
|
|
|
|
self.buffer = buffer;
|
|
|
|
options.callback(buffer);
|
|
|
|
});
|
|
|
|
}
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
};
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
Tone.extend(Tone.Buffer);
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Buffer.defaults = {
|
|
|
|
"url" : "",
|
2014-10-23 02:27:01 +00:00
|
|
|
"callback" : function(){}
|
2014-10-16 18:48:23 +00:00
|
|
|
};
|
2014-10-21 18:44:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* makes an xhr reqest for the selected url
|
|
|
|
* Load the audio file as an audio buffer.
|
|
|
|
* Decodes the audio asynchronously and invokes
|
|
|
|
* the callback once the audio buffer loads.
|
|
|
|
* @private
|
|
|
|
* @param {string} url the url of the buffer to load.
|
|
|
|
* filetype support depends on the
|
|
|
|
* browser.
|
|
|
|
* @param {function} callback function
|
|
|
|
*/
|
|
|
|
Tone.Buffer.prototype._loadBuffer = function(url, callback){
|
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open("GET", url, true);
|
|
|
|
request.responseType = "arraybuffer";
|
|
|
|
// decode asynchronously
|
|
|
|
var self = this;
|
|
|
|
request.onload = function() {
|
|
|
|
self.context.decodeAudioData(request.response, function(buff) {
|
|
|
|
if(!buff){
|
|
|
|
console.log("error in buffer data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(buff);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
request.onerror = function() {
|
|
|
|
console.log("error loading buffer");
|
|
|
|
};
|
|
|
|
//send the request
|
|
|
|
request.send();
|
2014-10-16 18:48:23 +00:00
|
|
|
};
|
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
/**
|
|
|
|
* Loads multiple buffers given a collection of urls
|
|
|
|
* @private
|
|
|
|
* @param {Object|Array} urls keyVal object of urls or Array
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
Tone.Buffer.prototype._loadBuffers = function(urls, callback){
|
|
|
|
var loadCounter = {
|
|
|
|
total : 0,
|
|
|
|
loaded : 0
|
|
|
|
};
|
2014-10-23 02:27:01 +00:00
|
|
|
var incrementCount = function(i, buffers){
|
2014-10-21 18:44:02 +00:00
|
|
|
var key = i;
|
|
|
|
return function(loadedBuffer){
|
|
|
|
buffers[key] = loadedBuffer;
|
|
|
|
loadCounter.loaded++;
|
|
|
|
if (loadCounter.total === loadCounter.loaded){
|
|
|
|
callback(buffers);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (Array.isArray(urls)){
|
2014-10-23 02:27:01 +00:00
|
|
|
var len = urls.length;
|
|
|
|
loadCounter.total = len;
|
|
|
|
this.buffer = new Array(len);
|
|
|
|
for (var i = 0; i < len; i++){
|
|
|
|
this._loadBuffer(urls[i], incrementCount(i, this.buffer));
|
2014-10-21 18:44:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loadCounter.total = Object.keys(urls).length;
|
2014-10-23 02:27:01 +00:00
|
|
|
this.buffer = {};
|
2014-10-21 18:44:02 +00:00
|
|
|
for (var key in urls){
|
2014-10-23 02:27:01 +00:00
|
|
|
this._loadBuffer(urls[key], incrementCount(key, this.buffer));
|
2014-10-21 18:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
/**
|
|
|
|
* dispose and disconnect
|
|
|
|
*/
|
|
|
|
Tone.Buffer.prototype.dispose = function(){
|
|
|
|
Tone.prototype.dispose.call(this);
|
2014-10-23 02:27:01 +00:00
|
|
|
this.buffer = null;
|
2014-10-21 18:44:02 +00:00
|
|
|
};
|
2014-10-16 18:48:23 +00:00
|
|
|
|
2014-10-21 18:44:02 +00:00
|
|
|
return Tone.Buffer;
|
2014-10-16 18:48:23 +00:00
|
|
|
});
|