Tone.js/Tone/source/Source.js

241 lines
5.8 KiB
JavaScript
Raw Normal View History

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){
"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
* 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}
* @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);
/**
* the unmuted volume
* @type {number}
* @private
*/
this._unmutedVolume = 1;
/**
* if the master is muted
* @type {boolean}
* @private
*/
this._muted = false;
/**
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-11-03 01:08:53 +00:00
this.volume = this._volume.volume;
this._readOnly("volume");
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);
/**
* 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);
/**
* 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);
/**
* The offset from the start of the Transport `start`
* @type {Time}
* @private
*/
this._startDelay = 0;
//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 = {
"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".
* @type {Tone.State}
* @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());
}
});
/**
* 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(){
return this._muted;
},
set : function(mute){
if (!this._muted && mute){
this._unmutedVolume = this.volume.value;
//maybe it should ramp here?
this.volume.value = -Infinity;
} else if (this._muted && !mute){
this.volume.value = this._unmutedVolume;
}
this._muted = mute;
}
});
/**
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.
* @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){
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.
* @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);
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.
* @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){
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
* @returns {Tone.Source} this
2014-06-19 05:40:16 +00:00
*/
Tone.Source.prototype.unsync = function(){
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
};
/**
2015-06-14 02:30:33 +00:00
* Clean up.
* @return {Tone.Source} this
*/
2015-02-02 03:05:24 +00:00
Tone.Source.prototype.dispose = function(){
2015-02-02 02:32:07 +00:00
this.stop();
Tone.prototype.dispose.call(this);
this.unsync();
this._writable("volume");
2015-11-03 01:08:53 +00:00
this._volume.dispose();
this._volume = null;
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-06-16 01:00:22 +00:00
return Tone.Source;
});