2014-06-19 05:40:16 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Source"], function(Tone){
|
2014-09-04 04:41:40 +00:00
|
|
|
|
|
|
|
"use strict";
|
2014-06-19 05:40:16 +00:00
|
|
|
|
2014-06-18 19:42:08 +00:00
|
|
|
/**
|
2014-07-20 22:17:24 +00:00
|
|
|
* @class Audio file player with start, loop, stop.
|
2014-06-18 19:42:08 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Source}
|
2014-06-21 22:39:01 +00:00
|
|
|
* @param {string=} url if a url is passed in, it will be loaded
|
|
|
|
* and invoke the callback if it also passed
|
|
|
|
* in.
|
2014-06-23 18:20:16 +00:00
|
|
|
* @param {function(Tone.Player)=} onload callback to be invoked
|
2014-06-21 22:39:01 +00:00
|
|
|
* once the url is loaded
|
2014-06-18 19:42:08 +00:00
|
|
|
*/
|
2014-06-23 18:20:16 +00:00
|
|
|
Tone.Player = function(url, onload){
|
2014-08-20 20:51:02 +00:00
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.Source.call(this);
|
2014-03-24 17:41:08 +00:00
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {AudioBufferSourceNode}
|
|
|
|
*/
|
|
|
|
this._source = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the buffer
|
|
|
|
* @private
|
|
|
|
* @type {AudioBuffer}
|
|
|
|
*/
|
|
|
|
this._buffer = null;
|
2014-03-11 04:54:40 +00:00
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the duration of the buffer once it's been loaded
|
|
|
|
* @type {number}
|
2014-08-20 20:51:02 +00:00
|
|
|
* @readOnly
|
2014-06-19 05:40:16 +00:00
|
|
|
*/
|
|
|
|
this.duration = 0;
|
|
|
|
|
2014-06-25 17:11:29 +00:00
|
|
|
/**
|
|
|
|
* if the buffer should loop once it's over
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.loop = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if 'loop' is true, the loop will start at this position
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.loopStart = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if 'loop' is true, the loop will end at this position
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.loopEnd = 0;
|
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
/**
|
|
|
|
* the playback rate
|
|
|
|
* @private
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this._playbackRate = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enabling retrigger will allow a player to be restarted
|
2014-06-25 17:11:29 +00:00
|
|
|
* before the the previous 'start' is done playing
|
2014-06-19 05:40:16 +00:00
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.retrigger = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set a callback function to invoke when the sample is over
|
|
|
|
*
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2014-04-06 00:47:59 +00:00
|
|
|
this.onended = function(){};
|
2014-06-21 22:39:01 +00:00
|
|
|
|
|
|
|
//if there is a url, load it.
|
|
|
|
if (url){
|
2014-06-23 18:20:16 +00:00
|
|
|
this.load(url, onload);
|
2014-06-21 22:39:01 +00:00
|
|
|
}
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.extend(Tone.Player, Tone.Source);
|
2014-03-11 04:54:40 +00:00
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
|
|
|
* makes an xhr reqest for the selected url
|
2014-06-18 19:42:08 +00:00
|
|
|
* Load the audio file as an audio buffer.
|
|
|
|
* Decodes the audio asynchronously and invokes
|
|
|
|
* the callback once the audio buffer loads.
|
2014-06-21 22:39:01 +00:00
|
|
|
*
|
|
|
|
* @param {string} url the url of the buffer to load.
|
|
|
|
* filetype support depends on the
|
|
|
|
* browser.
|
|
|
|
* @param {function(Tone.Player)=} callback
|
2014-06-18 21:01:39 +00:00
|
|
|
*/
|
2014-06-21 22:39:01 +00:00
|
|
|
Tone.Player.prototype.load = function(url, callback){
|
2014-06-19 05:40:16 +00:00
|
|
|
if (!this._buffer){
|
2014-04-06 00:47:59 +00:00
|
|
|
var request = new XMLHttpRequest();
|
2014-06-21 22:39:01 +00:00
|
|
|
request.open("GET", url, true);
|
2014-06-18 21:01:39 +00:00
|
|
|
request.responseType = "arraybuffer";
|
2014-04-06 00:47:59 +00:00
|
|
|
// decode asynchronously
|
|
|
|
var self = this;
|
|
|
|
request.onload = function() {
|
|
|
|
self.context.decodeAudioData(request.response, function(buff) {
|
2014-06-21 22:39:01 +00:00
|
|
|
self.setBuffer(buff);
|
2014-04-06 00:47:59 +00:00
|
|
|
if (callback){
|
|
|
|
callback(self);
|
|
|
|
}
|
|
|
|
});
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-04-06 00:47:59 +00:00
|
|
|
//send the request
|
|
|
|
request.send();
|
|
|
|
} else {
|
|
|
|
if (callback){
|
|
|
|
callback(this);
|
|
|
|
}
|
2014-03-30 21:54:02 +00:00
|
|
|
}
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-06-21 22:39:01 +00:00
|
|
|
/**
|
|
|
|
* set the buffer
|
|
|
|
*
|
|
|
|
* @param {AudioBuffer} buffer the buffer which the player will play.
|
|
|
|
* note: if you switch the buffer after
|
|
|
|
* the player is already started, it will not
|
|
|
|
* take effect until the next time the player
|
|
|
|
* is started.
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setBuffer = function(buffer){
|
|
|
|
this._buffer = buffer;
|
|
|
|
this.duration = buffer.duration;
|
|
|
|
};
|
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
|
|
|
* play the buffer between the desired positions
|
|
|
|
*
|
|
|
|
* @param {Tone.Time=} startTime
|
|
|
|
* @param {Tone.Time=} offset
|
|
|
|
* @param {Tone.Time=} duration
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.start = function(startTime, offset, duration){
|
2014-06-19 05:40:16 +00:00
|
|
|
if (this.state === Tone.Source.State.STOPPED || this.retrigger){
|
|
|
|
if (this._buffer){
|
|
|
|
this.state = Tone.Source.State.STARTED;
|
2014-09-30 03:44:15 +00:00
|
|
|
//if it's a loop the default offset is the loopstart point
|
2014-09-04 04:06:01 +00:00
|
|
|
if (this.loop){
|
2014-09-30 03:44:15 +00:00
|
|
|
offset = this.defaultArg(offset, this.loopStart);
|
|
|
|
} else {
|
|
|
|
//otherwise the default offset is 0
|
|
|
|
offset = this.defaultArg(offset, 0);
|
2014-09-04 04:06:01 +00:00
|
|
|
}
|
2014-06-19 05:40:16 +00:00
|
|
|
duration = this.defaultArg(duration, this._buffer.duration - offset);
|
|
|
|
//make the source
|
|
|
|
this._source = this.context.createBufferSource();
|
|
|
|
this._source.buffer = this._buffer;
|
2014-06-25 17:11:29 +00:00
|
|
|
//set the looping properties
|
|
|
|
this._source.loop = this.loop;
|
|
|
|
this._source.loopStart = this.loopStart;
|
|
|
|
this._source.loopEnd = this.loopEnd;
|
|
|
|
//and other properties
|
2014-06-19 05:40:16 +00:00
|
|
|
this._source.playbackRate.value = this._playbackRate;
|
|
|
|
this._source.onended = this._onended.bind(this);
|
|
|
|
this.chain(this._source, this.output);
|
2014-06-25 17:11:29 +00:00
|
|
|
//start it
|
|
|
|
this._source.start(this.toSeconds(startTime), this.toSeconds(offset), this.toSeconds(duration));
|
2014-06-19 05:40:16 +00:00
|
|
|
}
|
2014-03-30 21:54:02 +00:00
|
|
|
}
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-11 04:54:40 +00:00
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
2014-06-18 19:42:08 +00:00
|
|
|
* Stop playback.
|
|
|
|
*
|
2014-06-19 05:40:16 +00:00
|
|
|
* @param {Tone.Time} time
|
2014-06-18 21:01:39 +00:00
|
|
|
*/
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.Player.prototype.stop = function(time){
|
|
|
|
if (this.state === Tone.Source.State.STARTED) {
|
|
|
|
if (this._buffer && this._source){
|
|
|
|
if (!time){
|
|
|
|
this.state = Tone.Source.State.STOPPED;
|
|
|
|
}
|
|
|
|
this._source.stop(this.toSeconds(time));
|
|
|
|
}
|
2014-04-06 00:47:59 +00:00
|
|
|
}
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-11 04:54:40 +00:00
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
2014-06-19 05:40:16 +00:00
|
|
|
* set the rate at which the file plays
|
|
|
|
*
|
|
|
|
* @param {number} rate
|
|
|
|
* @param {Tone.Time=} rampTime (optional) the amount of time it takes to
|
|
|
|
* reach the rate
|
2014-06-18 21:01:39 +00:00
|
|
|
*/
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.Player.prototype.setPlaybackRate = function(rate, rampTime){
|
|
|
|
this._playbackRate = rate;
|
|
|
|
if (this._source) {
|
|
|
|
if (rampTime){
|
|
|
|
this._source.playbackRate.exponentialRampToValueAtTime(rate, this.toSeconds(rampTime));
|
|
|
|
} else {
|
|
|
|
this._source.playbackRate.value = rampTime;
|
|
|
|
}
|
|
|
|
}
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
|
|
|
* internal call when the buffer is done playing
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.Player.prototype._onended = function(){
|
|
|
|
this.state = Tone.Source.State.STOPPED;
|
|
|
|
this.onended();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dispose and disconnect
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.dispose = function(){
|
2014-08-24 20:24:16 +00:00
|
|
|
Tone.Source.prototype.dispose.call(this);
|
2014-06-20 04:25:10 +00:00
|
|
|
if (this._source !== null){
|
2014-06-19 05:40:16 +00:00
|
|
|
this._source.disconnect();
|
|
|
|
this._source = null;
|
|
|
|
}
|
|
|
|
this._buffer = null;
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-24 17:41:08 +00:00
|
|
|
|
2014-04-06 00:47:59 +00:00
|
|
|
return Tone.Player;
|
|
|
|
});
|