2014-10-21 18:55:29 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Buffer", "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}
|
2015-01-06 02:49:21 +00:00
|
|
|
* @param {string|AudioBuffer} url either the AudioBuffer or the url from
|
|
|
|
* which to load the AudioBuffer
|
2014-06-18 19:42:08 +00:00
|
|
|
*/
|
2014-09-30 04:28:48 +00:00
|
|
|
Tone.Player = function(){
|
2014-08-20 20:51:02 +00:00
|
|
|
|
2015-01-06 02:55:57 +00:00
|
|
|
var options = this.optionsObject(arguments, ["url", "onload"], Tone.Player.defaults);
|
2015-02-02 01:38:06 +00:00
|
|
|
Tone.Source.call(this, options);
|
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
|
2014-12-03 22:20:23 +00:00
|
|
|
* @type {Tone.Buffer}
|
2014-06-19 05:40:16 +00:00
|
|
|
*/
|
2015-01-06 02:55:57 +00:00
|
|
|
this._buffer = new Tone.Buffer(options.url, options.onload.bind(null, this));
|
2014-06-19 05:40:16 +00:00
|
|
|
|
2014-06-25 17:11:29 +00:00
|
|
|
/**
|
|
|
|
* if the buffer should loop once it's over
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2014-10-02 17:21:22 +00:00
|
|
|
this.loop = options.loop;
|
2014-06-25 17:11:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* if 'loop' is true, the loop will start at this position
|
2014-10-02 17:21:22 +00:00
|
|
|
* @type {Tone.Time}
|
2014-06-25 17:11:29 +00:00
|
|
|
*/
|
2015-02-02 01:02:13 +00:00
|
|
|
this._loopStart = options.loopStart;
|
2014-06-25 17:11:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* if 'loop' is true, the loop will end at this position
|
2014-10-02 17:21:22 +00:00
|
|
|
* @type {Tone.Time}
|
2014-06-25 17:11:29 +00:00
|
|
|
*/
|
2015-02-02 01:02:13 +00:00
|
|
|
this._loopEnd = options.loopEnd;
|
2014-06-25 17:11:29 +00:00
|
|
|
|
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}
|
|
|
|
*/
|
2014-10-02 17:21:22 +00:00
|
|
|
this.retrigger = options.retrigger;
|
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-09-30 04:28:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Player.defaults = {
|
2015-01-06 02:55:57 +00:00
|
|
|
"onload" : function(){},
|
2014-10-02 17:21:22 +00:00
|
|
|
"loop" : false,
|
|
|
|
"loopStart" : 0,
|
2015-02-02 01:02:13 +00:00
|
|
|
"loopEnd" : 0,
|
2014-10-02 17:21:22 +00:00
|
|
|
"retrigger" : false
|
2014-09-30 04:28:48 +00:00
|
|
|
};
|
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
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-10-16 18:48:23 +00:00
|
|
|
* @param {string} url the url of the buffer to load.
|
|
|
|
* filetype support depends on the
|
|
|
|
* browser.
|
|
|
|
* @param {function(Tone.Player)=} callback
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
2014-06-18 21:01:39 +00:00
|
|
|
*/
|
2014-06-21 22:39:01 +00:00
|
|
|
Tone.Player.prototype.load = function(url, callback){
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.load(url, callback.bind(this, this));
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
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.
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
2014-06-21 22:39:01 +00:00
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setBuffer = function(buffer){
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.set(buffer);
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-06-21 22:39:01 +00:00
|
|
|
};
|
|
|
|
|
2015-02-02 01:02:13 +00:00
|
|
|
/**
|
|
|
|
* get the buffer
|
|
|
|
* @returns {AudioBuffer} the buffer
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.getBuffer = function(){
|
|
|
|
return this._buffer.get();
|
|
|
|
};
|
|
|
|
|
2014-06-18 21:01:39 +00:00
|
|
|
/**
|
|
|
|
* play the buffer between the desired positions
|
|
|
|
*
|
2014-12-03 22:20:23 +00:00
|
|
|
* @param {Tone.Time} [startTime=now] when the player should start.
|
|
|
|
* @param {Tone.Time} [offset=0] the offset from the beginning of the sample
|
|
|
|
* to start at.
|
|
|
|
* @param {Tone.Time=} duration how long the sample should play. If no duration
|
|
|
|
* is given, it will default to the full length
|
|
|
|
* of the sample (minus any offset)
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
2014-06-18 21:01:39 +00:00
|
|
|
*/
|
|
|
|
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){
|
2015-02-02 01:02:13 +00:00
|
|
|
offset = this.defaultArg(offset, this._loopStart);
|
2014-09-30 03:44:15 +00:00
|
|
|
} 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();
|
2015-01-05 02:46:10 +00:00
|
|
|
this._source.buffer = this._buffer.get();
|
2014-06-25 17:11:29 +00:00
|
|
|
//set the looping properties
|
2014-10-02 17:21:22 +00:00
|
|
|
if (this.loop){
|
|
|
|
this._source.loop = this.loop;
|
2015-02-02 01:02:13 +00:00
|
|
|
this._source.loopStart = this.toSeconds(this._loopStart);
|
|
|
|
if (this._loopEnd !== -1){
|
|
|
|
this._source.loopEnd = this.toSeconds(this._loopEnd);
|
2014-10-02 22:58:24 +00:00
|
|
|
}
|
2014-10-02 17:21:22 +00:00
|
|
|
}
|
2014-06-25 17:11:29 +00:00
|
|
|
//and other properties
|
2014-06-19 05:40:16 +00:00
|
|
|
this._source.playbackRate.value = this._playbackRate;
|
|
|
|
this._source.onended = this._onended.bind(this);
|
2014-12-01 02:32:09 +00:00
|
|
|
this._source.connect(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
|
|
|
}
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
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-12-03 22:20:23 +00:00
|
|
|
* @param {Tone.Time} [time=now]
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
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) {
|
2015-01-05 02:46:10 +00:00
|
|
|
if (this._source){
|
2014-10-08 15:59:31 +00:00
|
|
|
this.state = Tone.Source.State.STOPPED;
|
2014-06-19 05:40:16 +00:00
|
|
|
this._source.stop(this.toSeconds(time));
|
|
|
|
}
|
2014-04-06 00:47:59 +00:00
|
|
|
}
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-11 04:54:40 +00:00
|
|
|
|
2014-10-08 15:59:31 +00:00
|
|
|
/**
|
|
|
|
* internal call when the buffer is done playing
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype._onended = function(){
|
|
|
|
this.state = Tone.Source.State.STOPPED;
|
|
|
|
this.onended();
|
|
|
|
};
|
|
|
|
|
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
|
2014-12-03 22:20:23 +00:00
|
|
|
* @param {Tone.Time=} rampTime the amount of time it takes to
|
2014-06-19 05:40:16 +00:00
|
|
|
* reach the rate
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
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) {
|
2014-10-22 22:24:58 +00:00
|
|
|
this._source.playbackRate.exponentialRampToValueAtTime(rate, this.toSeconds(rampTime));
|
2014-10-21 18:55:29 +00:00
|
|
|
}
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2015-02-02 01:02:13 +00:00
|
|
|
/**
|
|
|
|
* get the playback rate
|
|
|
|
* @returns {number} the playback rate
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.getPlaybackRate = function(){
|
|
|
|
return this._playbackRate;
|
|
|
|
};
|
|
|
|
|
2014-10-02 17:21:22 +00:00
|
|
|
/**
|
|
|
|
* set the loop start position
|
|
|
|
* @param {Tone.Time} loopStart the start time
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
2014-10-02 17:21:22 +00:00
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setLoopStart = function(loopStart){
|
2015-02-02 01:02:13 +00:00
|
|
|
this._loopStart = loopStart;
|
2015-02-01 18:42:51 +00:00
|
|
|
if (this._source){
|
|
|
|
this._source.loopStart = this.toSeconds(loopStart);
|
|
|
|
}
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-10-02 17:21:22 +00:00
|
|
|
};
|
|
|
|
|
2015-02-02 01:02:13 +00:00
|
|
|
/**
|
|
|
|
* returns the loop start position
|
|
|
|
* @returns {Tone.Time} the start time
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.getLoopStart = function(){
|
|
|
|
return this._loopStart;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the loop end position
|
|
|
|
* @returns {Tone.Time} the start time
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.getLoopEnd = function(){
|
|
|
|
return this._loopEnd;
|
|
|
|
};
|
|
|
|
|
2014-10-02 17:21:22 +00:00
|
|
|
/**
|
|
|
|
* set the loop end position
|
|
|
|
* @param {Tone.Time} loopEnd the loop end time
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
2014-10-02 17:21:22 +00:00
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setLoopEnd = function(loopEnd){
|
2015-02-02 01:02:13 +00:00
|
|
|
this._loopEnd = loopEnd;
|
2015-02-01 18:42:51 +00:00
|
|
|
if (this._source){
|
|
|
|
this._source.loopEnd = this.toSeconds(loopEnd);
|
|
|
|
}
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-10-02 17:21:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the loop start and end
|
|
|
|
* @param {Tone.Time} loopStart the loop end time
|
|
|
|
* @param {Tone.Time} loopEnd the loop end time
|
2015-02-01 19:40:47 +00:00
|
|
|
* @returns {Tone.Player} `this`
|
2014-10-02 17:21:22 +00:00
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setLoopPoints = function(loopStart, loopEnd){
|
|
|
|
this.setLoopStart(loopStart);
|
|
|
|
this.setLoopEnd(loopEnd);
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-10-02 17:21:22 +00:00
|
|
|
};
|
|
|
|
|
2014-09-30 04:28:48 +00:00
|
|
|
/**
|
2015-02-02 01:02:13 +00:00
|
|
|
* if 'loop' is true, the loop will start at this position
|
|
|
|
* @memberOf Tone.Player#
|
|
|
|
* @type {Tone.Time}
|
|
|
|
* @name loopStart
|
2014-09-30 04:28:48 +00:00
|
|
|
*/
|
2015-02-02 01:02:13 +00:00
|
|
|
Tone._defineGetterSetter(Tone.Player, "loopStart");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if 'loop' is true, the loop will end at this position
|
|
|
|
* @memberOf Tone.Player#
|
|
|
|
* @type {Tone.Time}
|
|
|
|
* @name loopEnd
|
|
|
|
*/
|
|
|
|
Tone._defineGetterSetter(Tone.Player, "loopEnd");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The playback speed. 1 is normal speed.
|
|
|
|
* @memberOf Tone.Player#
|
|
|
|
* @type {number}
|
|
|
|
* @name playbackRate
|
|
|
|
*/
|
|
|
|
Tone._defineGetterSetter(Tone.Player, "playbackRate");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The audio buffer belonging to the player.
|
|
|
|
* @memberOf Tone.Player#
|
|
|
|
* @type {AudioBuffer}
|
|
|
|
* @name buffer
|
|
|
|
*/
|
|
|
|
Tone._defineGetterSetter(Tone.Player, "buffer");
|
2014-09-30 04:28:48 +00:00
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
/**
|
|
|
|
* dispose and disconnect
|
2015-02-02 01:02:13 +00:00
|
|
|
* @private
|
2014-06-19 05:40:16 +00:00
|
|
|
*/
|
2015-02-02 01:02:13 +00:00
|
|
|
Tone.Player.prototype._dispose = function(){
|
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;
|
|
|
|
}
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.dispose();
|
2014-06-19 05:40:16 +00:00
|
|
|
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;
|
|
|
|
});
|