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){
|
2016-09-20 03:02:42 +00:00
|
|
|
|
|
|
|
// this.createInsOuts(0, 1);
|
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);
|
2016-05-14 21:46:21 +00:00
|
|
|
this._state.memory = 10;
|
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
|
|
|
|
*/
|
2016-08-16 03:12:12 +00:00
|
|
|
this._synced = false;
|
2015-08-31 19:13:43 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-16 03:12:12 +00:00
|
|
|
* Keep track of all of the scheduled event ids
|
|
|
|
* @type {Array}
|
2015-08-31 19:13:43 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2016-08-16 03:12:12 +00:00
|
|
|
this._scheduled = [];
|
2015-08-31 19:13:43 +00:00
|
|
|
|
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";
|
2016-05-14 21:46:21 +00:00
|
|
|
//mute initially
|
|
|
|
this.mute = options.mute;
|
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,
|
2016-05-14 21:46:21 +00:00
|
|
|
"mute" : false
|
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(){
|
2016-08-16 03:12:12 +00:00
|
|
|
if (this._synced){
|
|
|
|
if (Tone.Transport.state === Tone.State.Started){
|
2016-12-19 03:14:14 +00:00
|
|
|
return this._state.getValueAtTime(Tone.Transport.seconds);
|
2016-08-16 03:12:12 +00:00
|
|
|
} else {
|
|
|
|
return Tone.State.Stopped;
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-19 03:14:14 +00:00
|
|
|
return this._state.getValueAtTime(this.now());
|
2016-08-16 03:12:12 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-16 03:12:12 +00:00
|
|
|
//overwrite these functions
|
|
|
|
Tone.Source.prototype._start = Tone.noOp;
|
|
|
|
Tone.Source.prototype._stop = Tone.noOp;
|
|
|
|
|
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
|
|
|
*/
|
2016-08-16 03:12:12 +00:00
|
|
|
Tone.Source.prototype.start = function(time, offset, duration){
|
|
|
|
if (this.isUndef(time) && this._synced){
|
2016-10-01 20:26:37 +00:00
|
|
|
time = Tone.Transport.seconds;
|
2016-08-16 03:12:12 +00:00
|
|
|
} else {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
}
|
|
|
|
//if it's started, stop it and restart it
|
2016-12-19 03:14:14 +00:00
|
|
|
if (!this.retrigger && this._state.getValueAtTime(time) === Tone.State.Started){
|
2016-08-16 03:12:12 +00:00
|
|
|
this.stop(time);
|
|
|
|
}
|
|
|
|
this._state.setStateAtTime(Tone.State.Started, time);
|
|
|
|
if (this._synced){
|
|
|
|
// add the offset time to the event
|
2016-12-19 02:56:22 +00:00
|
|
|
var event = this._state.get(time);
|
2016-08-16 03:12:12 +00:00
|
|
|
event.offset = this.defaultArg(offset, 0);
|
2016-09-25 16:35:57 +00:00
|
|
|
event.duration = duration;
|
2016-08-16 03:12:12 +00:00
|
|
|
var sched = Tone.Transport.schedule(function(t){
|
|
|
|
this._start(t, offset, duration);
|
|
|
|
}.bind(this), time);
|
|
|
|
this._scheduled.push(sched);
|
|
|
|
} else {
|
|
|
|
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){
|
2016-08-16 03:12:12 +00:00
|
|
|
if (this.isUndef(time) && this._synced){
|
2016-10-01 20:26:37 +00:00
|
|
|
time = Tone.Transport.seconds;
|
2016-08-16 03:12:12 +00:00
|
|
|
} else {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
}
|
2016-03-03 18:02:38 +00:00
|
|
|
this._state.cancel(time);
|
|
|
|
this._state.setStateAtTime(Tone.State.Stopped, time);
|
2016-08-16 03:12:12 +00:00
|
|
|
if (!this._synced){
|
2016-03-03 18:02:38 +00:00
|
|
|
this._stop.apply(this, arguments);
|
2016-08-16 03:12:12 +00:00
|
|
|
} else {
|
|
|
|
var sched = Tone.Transport.schedule(this._stop.bind(this), time);
|
|
|
|
this._scheduled.push(sched);
|
|
|
|
}
|
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
|
|
|
/**
|
2016-08-16 03:12:12 +00:00
|
|
|
* Sync the source to the Transport so that all subsequent
|
|
|
|
* calls to `start` and `stop` are synced to the TransportTime
|
|
|
|
* instead of the AudioContext time.
|
2014-06-25 16:47:47 +00:00
|
|
|
*
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Source} this
|
2015-06-14 02:30:33 +00:00
|
|
|
* @example
|
2016-08-16 03:12:12 +00:00
|
|
|
* //sync the source so that it plays between 0 and 0.3 on the Transport's timeline
|
|
|
|
* source.sync().start(0).stop(0.3);
|
|
|
|
* //start the transport.
|
2015-06-14 02:30:33 +00:00
|
|
|
* Tone.Transport.start();
|
2016-08-16 03:12:12 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* //start the transport with an offset and the sync'ed sources
|
|
|
|
* //will start in the correct position
|
|
|
|
* source.sync().start(0.1);
|
|
|
|
* //the source will be invoked with an offset of 0.4
|
|
|
|
* Tone.Transport.start("+0.5", 0.5);
|
2014-06-19 05:40:16 +00:00
|
|
|
*/
|
2016-08-16 03:12:12 +00:00
|
|
|
Tone.Source.prototype.sync = function(){
|
|
|
|
this._synced = true;
|
2016-10-19 18:50:39 +00:00
|
|
|
Tone.Transport.on("start loopStart", function(time, offset){
|
2016-08-16 03:12:12 +00:00
|
|
|
if (offset > 0){
|
|
|
|
// get the playback state at that time
|
2016-12-19 02:56:22 +00:00
|
|
|
var stateEvent = this._state.get(offset);
|
2016-09-25 16:35:57 +00:00
|
|
|
// listen for start events which may occur in the middle of the sync'ed time
|
|
|
|
if (stateEvent && stateEvent.state === Tone.State.Started && stateEvent.time !== offset){
|
2016-08-16 03:12:12 +00:00
|
|
|
// get the offset
|
2016-09-25 16:35:57 +00:00
|
|
|
var startOffset = offset - this.toSeconds(stateEvent.time);
|
|
|
|
var duration;
|
|
|
|
if (stateEvent.duration){
|
|
|
|
duration = this.toSeconds(stateEvent.duration) - startOffset;
|
|
|
|
}
|
2016-10-01 22:31:24 +00:00
|
|
|
this._start(time, this.toSeconds(stateEvent.offset) + startOffset, duration);
|
2016-08-16 03:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2016-10-19 18:50:39 +00:00
|
|
|
Tone.Transport.on("stop pause loopEnd", function(time){
|
2016-12-19 03:14:14 +00:00
|
|
|
if (this._state.getValueAtTime(Tone.Transport.seconds) === Tone.State.Started){
|
2016-08-16 03:12:12 +00:00
|
|
|
this._stop(time);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
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(){
|
2016-08-16 03:12:12 +00:00
|
|
|
this._synced = false;
|
2016-10-19 18:50:39 +00:00
|
|
|
Tone.Transport.off("start stop pause loopEnd loopStart");
|
2016-08-16 03:12:12 +00:00
|
|
|
// clear all of the scheduled ids
|
|
|
|
for (var i = 0; i < this._scheduled.length; i++){
|
|
|
|
var id = this._scheduled[i];
|
|
|
|
Tone.Transport.clear(id);
|
|
|
|
}
|
|
|
|
this._scheduled = [];
|
|
|
|
this._state.cancel(0);
|
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-08-31 19:13:43 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
|
|
|
this.unsync();
|
2016-08-16 03:12:12 +00:00
|
|
|
this._scheduled = null;
|
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;
|
2014-08-24 20:24:16 +00:00
|
|
|
};
|
|
|
|
|
2014-06-16 01:00:22 +00:00
|
|
|
return Tone.Source;
|
|
|
|
});
|