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
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
Tone.Source.call(this);
|
2015-01-06 02:49:21 +00:00
|
|
|
var options = this.optionsObject(arguments, ["url"], Tone.Player.defaults);
|
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:49:21 +00:00
|
|
|
this._buffer = new Tone.Buffer(options.url);
|
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
|
|
|
*/
|
2014-10-02 17:21:22 +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
|
|
|
*/
|
2014-10-02 17:21:22 +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-19 05:40:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set a callback function to invoke when the sample is over
|
|
|
|
*
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2014-09-30 04:28:48 +00:00
|
|
|
this.onended = options.onended;
|
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 = {
|
2014-10-02 17:21:22 +00:00
|
|
|
"onended" : function(){},
|
|
|
|
"loop" : false,
|
|
|
|
"loopStart" : 0,
|
2014-10-02 22:58:24 +00:00
|
|
|
"loopEnd" : -1,
|
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
|
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));
|
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){
|
2015-01-05 02:46:10 +00:00
|
|
|
this._buffer.set(buffer);
|
2014-06-21 22:39:01 +00:00
|
|
|
};
|
|
|
|
|
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)
|
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){
|
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();
|
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;
|
|
|
|
this._source.loopStart = this.toSeconds(this.loopStart);
|
2014-10-03 05:00:48 +00:00
|
|
|
if (this.loopEnd !== -1){
|
2014-10-02 22:58:24 +00:00
|
|
|
this._source.loopEnd = this.toSeconds(this.loopEnd);
|
|
|
|
}
|
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
|
|
|
}
|
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]
|
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
|
|
|
}
|
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
|
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
|
|
|
}
|
2014-06-18 21:01:39 +00:00
|
|
|
};
|
2014-03-11 23:27:46 +00:00
|
|
|
|
2014-10-02 17:21:22 +00:00
|
|
|
/**
|
|
|
|
* set the loop start position
|
|
|
|
* @param {Tone.Time} loopStart the start time
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setLoopStart = function(loopStart){
|
|
|
|
this.loopStart = loopStart;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the loop end position
|
|
|
|
* @param {Tone.Time} loopEnd the loop end time
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setLoopEnd = function(loopEnd){
|
|
|
|
this.loopEnd = loopEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the loop start and end
|
|
|
|
* @param {Tone.Time} loopStart the loop end time
|
|
|
|
* @param {Tone.Time} loopEnd the loop end time
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.setLoopPoints = function(loopStart, loopEnd){
|
|
|
|
this.setLoopStart(loopStart);
|
|
|
|
this.setLoopEnd(loopEnd);
|
|
|
|
};
|
|
|
|
|
2014-09-30 04:28:48 +00:00
|
|
|
/**
|
|
|
|
* set the parameters at once
|
|
|
|
* @param {Object} params
|
|
|
|
*/
|
|
|
|
Tone.Player.prototype.set = function(params){
|
|
|
|
if (!this.isUndef(params.playbackRate)) this.setPlaybackRate(params.playbackRate);
|
|
|
|
if (!this.isUndef(params.onended)) this.onended = params.onended;
|
2014-10-02 17:21:22 +00:00
|
|
|
if (!this.isUndef(params.loop)) this.loop = params.loop;
|
|
|
|
if (!this.isUndef(params.loopStart)) this.setLoopStart(params.loopStart);
|
|
|
|
if (!this.isUndef(params.loopEnd)) this.setLoopEnd(params.loopEnd);
|
2015-01-06 02:49:21 +00:00
|
|
|
if (!this.isUndef(params.buffer)) this.setBuffer(params.buffer);
|
2014-09-30 04:28:48 +00:00
|
|
|
Tone.Source.prototype.set.call(this, params);
|
|
|
|
};
|
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
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;
|
|
|
|
});
|