2018-08-27 02:29:17 +00:00
|
|
|
define(["../core/Tone", "../core/Buffer"], function(Tone){
|
2016-06-14 23:57:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class A data structure for holding multiple buffers.
|
|
|
|
*
|
|
|
|
* @param {Object|Array} urls An object literal or array
|
|
|
|
* of urls to load.
|
|
|
|
* @param {Function=} callback The callback to invoke when
|
|
|
|
* the buffers are loaded.
|
|
|
|
* @extends {Tone}
|
|
|
|
* @example
|
|
|
|
* //load a whole bank of piano samples
|
|
|
|
* var pianoSamples = new Tone.Buffers({
|
|
|
|
* "C4" : "path/to/C4.mp3"
|
|
|
|
* "C#4" : "path/to/C#4.mp3"
|
|
|
|
* "D4" : "path/to/D4.mp3"
|
|
|
|
* "D#4" : "path/to/D#4.mp3"
|
|
|
|
* ...
|
|
|
|
* }, function(){
|
|
|
|
* //play one of the samples when they all load
|
|
|
|
* player.buffer = pianoSamples.get("C4");
|
|
|
|
* player.start();
|
|
|
|
* });
|
2017-05-14 02:16:05 +00:00
|
|
|
* @example
|
|
|
|
* //To pass in additional parameters in the second parameter
|
|
|
|
* var buffers = new Tone.Buffers(urls, {
|
|
|
|
* "onload" : callback,
|
|
|
|
* "baseUrl" : "../path/to/audio/"
|
|
|
|
* })
|
2016-06-14 23:57:01 +00:00
|
|
|
*/
|
2017-04-25 02:10:13 +00:00
|
|
|
Tone.Buffers = function(urls){
|
|
|
|
|
|
|
|
//remove the urls from the options
|
2017-05-14 02:16:05 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args.shift();
|
|
|
|
var options = Tone.defaults(args, ["onload", "baseUrl"], Tone.Buffers);
|
2017-04-26 03:08:57 +00:00
|
|
|
Tone.call(this);
|
2016-06-14 23:57:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All of the buffers
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._buffers = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A path which is prefixed before every url.
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2017-04-25 02:10:13 +00:00
|
|
|
this.baseUrl = options.baseUrl;
|
2016-06-14 23:57:01 +00:00
|
|
|
|
|
|
|
this._loadingCount = 0;
|
|
|
|
//add each one
|
2017-05-14 02:16:05 +00:00
|
|
|
for (var key in urls){
|
2016-06-14 23:57:01 +00:00
|
|
|
this._loadingCount++;
|
2017-05-14 02:16:05 +00:00
|
|
|
this.add(key, urls[key], this._bufferLoaded.bind(this, options.onload));
|
2016-06-14 23:57:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Buffers);
|
|
|
|
|
2017-04-25 02:10:13 +00:00
|
|
|
/**
|
|
|
|
* Defaults
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Buffers.defaults = {
|
|
|
|
"onload" : Tone.noOp,
|
|
|
|
"baseUrl" : ""
|
|
|
|
};
|
|
|
|
|
2016-08-09 05:12:34 +00:00
|
|
|
/**
|
|
|
|
* True if the buffers object has a buffer by that name.
|
|
|
|
* @param {String|Number} name The key or index of the
|
|
|
|
* buffer.
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
Tone.Buffers.prototype.has = function(name){
|
|
|
|
return this._buffers.hasOwnProperty(name);
|
|
|
|
};
|
|
|
|
|
2016-06-14 23:57:01 +00:00
|
|
|
/**
|
|
|
|
* Get a buffer by name. If an array was loaded,
|
|
|
|
* then use the array index.
|
|
|
|
* @param {String|Number} name The key or index of the
|
|
|
|
* buffer.
|
|
|
|
* @return {Tone.Buffer}
|
|
|
|
*/
|
|
|
|
Tone.Buffers.prototype.get = function(name){
|
2016-08-09 05:12:34 +00:00
|
|
|
if (this.has(name)){
|
2016-06-14 23:57:01 +00:00
|
|
|
return this._buffers[name];
|
|
|
|
} else {
|
2016-08-09 05:12:34 +00:00
|
|
|
throw new Error("Tone.Buffers: no buffer named "+name);
|
2016-06-14 23:57:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A buffer was loaded. decrement the counter.
|
|
|
|
* @param {Function} callback
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Buffers.prototype._bufferLoaded = function(callback){
|
|
|
|
this._loadingCount--;
|
|
|
|
if (this._loadingCount === 0 && callback){
|
|
|
|
callback(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-19 22:03:05 +00:00
|
|
|
/**
|
|
|
|
* If the buffers are loaded or not
|
|
|
|
* @memberOf Tone.Buffers#
|
|
|
|
* @type {Boolean}
|
|
|
|
* @name loaded
|
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Tone.Buffers.prototype, "loaded", {
|
|
|
|
get : function(){
|
|
|
|
var isLoaded = true;
|
|
|
|
for (var buffName in this._buffers){
|
2016-09-08 14:29:41 +00:00
|
|
|
var buff = this.get(buffName);
|
2016-08-19 22:03:05 +00:00
|
|
|
isLoaded = isLoaded && buff.loaded;
|
|
|
|
}
|
|
|
|
return isLoaded;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-14 23:57:01 +00:00
|
|
|
/**
|
|
|
|
* Add a buffer by name and url to the Buffers
|
|
|
|
* @param {String} name A unique name to give
|
|
|
|
* the buffer
|
|
|
|
* @param {String|Tone.Buffer|Audiobuffer} url Either the url of the bufer,
|
|
|
|
* or a buffer which will be added
|
|
|
|
* with the given name.
|
|
|
|
* @param {Function=} callback The callback to invoke
|
|
|
|
* when the url is loaded.
|
|
|
|
*/
|
|
|
|
Tone.Buffers.prototype.add = function(name, url, callback){
|
2017-04-30 19:03:49 +00:00
|
|
|
callback = Tone.defaultArg(callback, Tone.noOp);
|
2016-06-14 23:57:01 +00:00
|
|
|
if (url instanceof Tone.Buffer){
|
|
|
|
this._buffers[name] = url;
|
|
|
|
callback(this);
|
|
|
|
} else if (url instanceof AudioBuffer){
|
|
|
|
this._buffers[name] = new Tone.Buffer(url);
|
|
|
|
callback(this);
|
2017-04-26 04:24:19 +00:00
|
|
|
} else if (Tone.isString(url)){
|
2016-06-14 23:57:01 +00:00
|
|
|
this._buffers[name] = new Tone.Buffer(this.baseUrl + url, callback);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up.
|
|
|
|
* @return {Tone.Buffers} this
|
|
|
|
*/
|
|
|
|
Tone.Buffers.prototype.dispose = function(){
|
2017-04-25 02:10:13 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2016-06-14 23:57:01 +00:00
|
|
|
for (var name in this._buffers){
|
|
|
|
this._buffers[name].dispose();
|
|
|
|
}
|
|
|
|
this._buffers = null;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Buffers;
|
2017-10-26 20:02:01 +00:00
|
|
|
});
|