2016-01-31 17:20:55 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Transport", "Tone/component/Volume", "Tone/core/Master",
|
2016-04-18 04:36:08 +00:00
|
|
|
"Tone/type/Type", "Tone/core/TimelineState", "Tone/signal/Signal"],
|
2015-08-18 20:29:39 +00:00
|
|
|
function(Tone){
|
2014-09-04 04:41:40 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2014-06-16 01:00:22 +00:00
|
|
|
/**
|
2015-06-14 02:03:06 +00:00
|
|
|
* @class Base class for sources. Sources have start/stop methods
|
|
|
|
* and the ability to be synced to the
|
2015-09-05 19:15:47 +00:00
|
|
|
* start/stop of Tone.Transport.
|
2014-06-19 05:40:16 +00:00
|
|
|
*
|
2014-06-16 01:00:22 +00:00
|
|
|
* @constructor
|
2015-08-21 19:01:22 +00:00
|
|
|
* @extends {Tone}
|
2015-09-05 19:15:47 +00:00
|
|
|
* @example
|
|
|
|
* //Multiple state change events can be chained together,
|
|
|
|
* //but must be set in the correct order and with ascending times
|
|
|
|
*
|
|
|
|
* // OK
|
|
|
|
* state.start().stop("+0.2");
|
|
|
|
* // AND
|
|
|
|
* state.start().stop("+0.2").start("+0.4").stop("+0.7")
|
|
|
|
*
|
|
|
|
* // BAD
|
|
|
|
* state.stop("+0.2").start();
|
|
|
|
* // OR
|
|
|
|
* state.start("+0.3").stop("+0.2");
|
|
|
|
*
|
2014-06-16 01:00:22 +00:00
|
|
|
*/
|
2015-02-02 01:38:06 +00:00
|
|
|
Tone.Source = function(options){
|
2015-08-18 20:29:39 +00:00
|
|
|
//Sources only have an output and no input
|
2015-08-31 20:55:47 +00:00
|
|
|
Tone.call(this);
|
2015-08-18 20:29:39 +00:00
|
|
|
|
2015-02-02 01:38:06 +00:00
|
|
|
options = this.defaultArg(options, Tone.Source.defaults);
|
2014-06-19 05:40:16 +00:00
|
|
|
|
2015-11-03 01:08:53 +00:00
|
|
|
/**
|
|
|
|
* The output volume node
|
|
|
|
* @type {Tone.Volume}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._volume = this.output = new Tone.Volume(options.volume);
|
|
|
|
|
2015-02-04 15:11:10 +00:00
|
|
|
/**
|
2015-02-27 16:19:45 +00:00
|
|
|
* The volume of the output in decibels.
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Decibels}
|
|
|
|
* @signal
|
2015-02-27 16:19:45 +00:00
|
|
|
* @example
|
|
|
|
* source.volume.value = -6;
|
2015-02-04 15:11:10 +00:00
|
|
|
*/
|
2015-11-03 01:08:53 +00:00
|
|
|
this.volume = this._volume.volume;
|
2015-04-05 18:00:52 +00:00
|
|
|
this._readOnly("volume");
|
2015-02-04 15:11:10 +00:00
|
|
|
|
2015-02-02 02:32:07 +00:00
|
|
|
/**
|
2015-08-18 20:29:39 +00:00
|
|
|
* Keep track of the scheduled state.
|
|
|
|
* @type {Tone.TimelineState}
|
2015-02-02 02:32:07 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-08-18 20:29:39 +00:00
|
|
|
this._state = new Tone.TimelineState(Tone.State.Stopped);
|
2015-04-28 18:33:59 +00:00
|
|
|
|
2015-08-31 19:13:43 +00:00
|
|
|
/**
|
|
|
|
* The synced `start` callback function from the transport
|
|
|
|
* @type {Function}
|
|
|
|
* @private
|
|
|
|
*/
|
2015-09-03 14:24:20 +00:00
|
|
|
this._syncStart = function(time, offset){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
time += this.toSeconds(this._startDelay);
|
|
|
|
this.start(time, offset);
|
|
|
|
}.bind(this);
|
2015-08-31 19:13:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The synced `stop` callback function from the transport
|
|
|
|
* @type {Function}
|
|
|
|
* @private
|
|
|
|
*/
|
2015-09-03 14:24:20 +00:00
|
|
|
this._syncStop = this.stop.bind(this);
|
2015-08-31 19:13:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The offset from the start of the Transport `start`
|
|
|
|
* @type {Time}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._startDelay = 0;
|
|
|
|
|
2015-04-28 18:33:59 +00:00
|
|
|
//make the output explicitly stereo
|
2015-11-03 01:08:53 +00:00
|
|
|
this._volume.output.output.channelCount = 2;
|
|
|
|
this._volume.output.output.channelCountMode = "explicit";
|
2014-06-16 01:00:22 +00:00
|
|
|
};
|
|
|
|
|
2015-08-18 20:29:39 +00:00
|
|
|
Tone.extend(Tone.Source);
|
2014-06-16 01:00:22 +00:00
|
|
|
|
2015-02-02 01:38:06 +00:00
|
|
|
/**
|
2015-02-27 16:19:45 +00:00
|
|
|
* The default parameters
|
2015-02-02 01:38:06 +00:00
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.Source.defaults = {
|
2015-02-04 15:11:10 +00:00
|
|
|
"volume" : 0,
|
2015-02-02 01:38:06 +00:00
|
|
|
};
|
|
|
|
|
2015-02-27 16:19:45 +00:00
|
|
|
/**
|
|
|
|
* Returns the playback state of the source, either "started" or "stopped".
|
2015-05-23 22:57:05 +00:00
|
|
|
* @type {Tone.State}
|
2015-02-23 05:29:07 +00:00
|
|
|
* @readOnly
|
|
|
|
* @memberOf Tone.Source#
|
|
|
|
* @name state
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Tone.Source.prototype, "state", {
|
|
|
|
get : function(){
|
2015-08-18 20:29:39 +00:00
|
|
|
return this._state.getStateAtTime(this.now());
|
2015-02-23 05:29:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-14 20:52:58 +00:00
|
|
|
/**
|
|
|
|
* Mute the output.
|
|
|
|
* @memberOf Tone.Source#
|
|
|
|
* @type {boolean}
|
|
|
|
* @name mute
|
|
|
|
* @example
|
|
|
|
* //mute the output
|
|
|
|
* source.mute = true;
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Tone.Source.prototype, "mute", {
|
|
|
|
get : function(){
|
2016-05-14 21:34:23 +00:00
|
|
|
return this._volume.mute;
|
2016-05-14 20:52:58 +00:00
|
|
|
},
|
|
|
|
set : function(mute){
|
2016-05-14 21:34:23 +00:00
|
|
|
this._volume.mute = mute;
|
2016-05-14 20:52:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-23 05:29:07 +00:00
|
|
|
/**
|
2015-06-20 19:50:57 +00:00
|
|
|
* Start the source at the specified time. If no time is given,
|
|
|
|
* start the source now.
|
|
|
|
* @param {Time} [time=now] When the source should be started.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Source} this
|
2015-02-27 16:19:45 +00:00
|
|
|
* @example
|
2015-06-14 02:03:06 +00:00
|
|
|
* source.start("+0.5"); //starts the source 0.5 seconds from now
|
2014-06-16 01:00:22 +00:00
|
|
|
*/
|
2015-02-02 02:32:07 +00:00
|
|
|
Tone.Source.prototype.start = function(time){
|
2015-02-23 05:29:07 +00:00
|
|
|
time = this.toSeconds(time);
|
2015-08-18 20:29:39 +00:00
|
|
|
if (this._state.getStateAtTime(time) !== Tone.State.Started || this.retrigger){
|
|
|
|
this._state.setStateAtTime(Tone.State.Started, time);
|
|
|
|
if (this._start){
|
|
|
|
this._start.apply(this, arguments);
|
|
|
|
}
|
2015-02-02 02:32:07 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2014-06-16 01:00:22 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-20 19:50:57 +00:00
|
|
|
* Stop the source at the specified time. If no time is given,
|
|
|
|
* stop the source now.
|
|
|
|
* @param {Time} [time=now] When the source should be stopped.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Source} this
|
2015-02-27 16:19:45 +00:00
|
|
|
* @example
|
2015-06-14 02:03:06 +00:00
|
|
|
* source.stop(); // stops the source immediately
|
2014-06-16 01:00:22 +00:00
|
|
|
*/
|
2015-02-02 02:32:07 +00:00
|
|
|
Tone.Source.prototype.stop = function(time){
|
2015-08-16 19:16:36 +00:00
|
|
|
time = this.toSeconds(time);
|
2016-03-03 18:02:38 +00:00
|
|
|
this._state.cancel(time);
|
|
|
|
this._state.setStateAtTime(Tone.State.Stopped, time);
|
|
|
|
if (this._stop){
|
|
|
|
this._stop.apply(this, arguments);
|
2015-02-02 02:32:07 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2015-09-03 14:32:00 +00:00
|
|
|
|
2014-06-19 05:40:16 +00:00
|
|
|
/**
|
2015-02-27 16:19:45 +00:00
|
|
|
* Sync the source to the Transport so that when the transport
|
|
|
|
* is started, this source is started and when the transport is stopped
|
|
|
|
* or paused, so is the source.
|
2014-06-25 16:47:47 +00:00
|
|
|
*
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [delay=0] Delay time before starting the source after the
|
2015-02-27 16:19:45 +00:00
|
|
|
* Transport has started.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Source} this
|
2015-06-14 02:30:33 +00:00
|
|
|
* @example
|
|
|
|
* //sync the source to start 1 measure after the transport starts
|
|
|
|
* source.sync("1m");
|
|
|
|
* //start the transport. the source will start 1 measure later.
|
|
|
|
* Tone.Transport.start();
|
2014-06-19 05:40:16 +00:00
|
|
|
*/
|
2014-06-25 16:47:47 +00:00
|
|
|
Tone.Source.prototype.sync = function(delay){
|
2015-08-31 19:13:43 +00:00
|
|
|
this._startDelay = this.defaultArg(delay, 0);
|
2015-09-03 14:24:20 +00:00
|
|
|
Tone.Transport.on("start", this._syncStart);
|
|
|
|
Tone.Transport.on("stop pause", this._syncStop);
|
2015-02-21 19:06:58 +00:00
|
|
|
return this;
|
2014-06-19 05:40:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-14 01:54:20 +00:00
|
|
|
* Unsync the source to the Transport. See Tone.Source.sync
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Source} this
|
2014-06-19 05:40:16 +00:00
|
|
|
*/
|
|
|
|
Tone.Source.prototype.unsync = function(){
|
2015-08-31 19:13:43 +00:00
|
|
|
this._startDelay = 0;
|
2015-09-03 14:24:20 +00:00
|
|
|
Tone.Transport.off("start", this._syncStart);
|
|
|
|
Tone.Transport.off("stop pause", this._syncStop);
|
2015-02-21 19:06:58 +00:00
|
|
|
return this;
|
2014-06-19 05:40:16 +00:00
|
|
|
};
|
|
|
|
|
2014-08-24 20:24:16 +00:00
|
|
|
/**
|
2015-06-14 02:30:33 +00:00
|
|
|
* Clean up.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @return {Tone.Source} this
|
2014-08-24 20:24:16 +00:00
|
|
|
*/
|
2015-02-02 03:05:24 +00:00
|
|
|
Tone.Source.prototype.dispose = function(){
|
2015-02-02 02:32:07 +00:00
|
|
|
this.stop();
|
2015-08-31 19:13:43 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
|
|
|
this.unsync();
|
2015-04-05 18:41:43 +00:00
|
|
|
this._writable("volume");
|
2015-11-03 01:08:53 +00:00
|
|
|
this._volume.dispose();
|
|
|
|
this._volume = null;
|
2015-04-05 18:41:43 +00:00
|
|
|
this.volume = null;
|
2015-08-18 20:29:39 +00:00
|
|
|
this._state.dispose();
|
|
|
|
this._state = null;
|
2015-09-03 14:24:20 +00:00
|
|
|
this._syncStart = null;
|
|
|
|
this._syncStart = null;
|
2014-08-24 20:24:16 +00:00
|
|
|
};
|
|
|
|
|
2014-06-16 01:00:22 +00:00
|
|
|
return Tone.Source;
|
|
|
|
});
|