2017-08-27 21:50:31 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Oscillator", "Tone/signal/Scale", "Tone/core/AudioNode",
|
2017-10-21 23:02:46 +00:00
|
|
|
"Tone/signal/Signal", "Tone/signal/AudioToGain", "Tone/type/Type", "Tone/signal/Zero"], function(Tone){
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-06-17 17:01:06 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* @class LFO stands for low frequency oscillator. Tone.LFO produces an output signal
|
|
|
|
* which can be attached to an AudioParam or Tone.Signal
|
|
|
|
* in order to modulate that parameter with an oscillator. The LFO can
|
2015-06-20 23:25:49 +00:00
|
|
|
* also be synced to the transport to start/stop and change when the tempo changes.
|
2014-06-17 17:01:06 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2017-08-27 21:50:31 +00:00
|
|
|
* @extends {Tone.AudioNode}
|
2015-06-20 23:25:49 +00:00
|
|
|
* @param {Frequency|Object} [frequency] The frequency of the oscillation. Typically, LFOs will be
|
2017-08-27 21:50:31 +00:00
|
|
|
* in the frequency range of 0.1 to 10 hertz.
|
|
|
|
* @param {number=} min The minimum output value of the LFO.
|
|
|
|
* @param {number=} max The maximum value of the LFO.
|
2015-02-27 21:53:10 +00:00
|
|
|
* @example
|
2015-06-20 23:25:49 +00:00
|
|
|
* var lfo = new Tone.LFO("4n", 400, 4000);
|
|
|
|
* lfo.connect(filter.frequency);
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
2014-11-30 02:54:29 +00:00
|
|
|
Tone.LFO = function(){
|
|
|
|
|
2017-04-26 03:08:23 +00:00
|
|
|
var options = Tone.defaults(arguments, ["frequency", "min", "max"], Tone.LFO);
|
2017-08-27 21:50:31 +00:00
|
|
|
Tone.AudioNode.call(this);
|
2014-08-20 20:50:27 +00:00
|
|
|
|
2017-08-27 21:50:31 +00:00
|
|
|
/**
|
|
|
|
* The oscillator.
|
2014-08-20 20:50:27 +00:00
|
|
|
* @type {Tone.Oscillator}
|
2015-07-04 16:32:18 +00:00
|
|
|
* @private
|
2014-08-20 20:50:27 +00:00
|
|
|
*/
|
2015-08-28 03:03:42 +00:00
|
|
|
this._oscillator = new Tone.Oscillator({
|
2017-08-27 21:50:31 +00:00
|
|
|
"frequency" : options.frequency,
|
|
|
|
"type" : options.type,
|
2015-02-02 17:49:13 +00:00
|
|
|
});
|
2014-08-20 20:50:27 +00:00
|
|
|
|
2014-08-25 13:57:36 +00:00
|
|
|
/**
|
2015-02-02 17:49:13 +00:00
|
|
|
* the lfo's frequency
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Frequency}
|
|
|
|
* @signal
|
2014-08-25 13:57:36 +00:00
|
|
|
*/
|
2015-08-28 03:03:42 +00:00
|
|
|
this.frequency = this._oscillator.frequency;
|
2014-08-25 13:57:36 +00:00
|
|
|
|
2015-02-11 19:37:48 +00:00
|
|
|
/**
|
|
|
|
* The amplitude of the LFO, which controls the output range between
|
2017-08-27 21:50:31 +00:00
|
|
|
* the min and max output. For example if the min is -10 and the max
|
2015-02-11 19:37:48 +00:00
|
|
|
* is 10, setting the amplitude to 0.5 would make the LFO modulate
|
2017-08-27 21:50:31 +00:00
|
|
|
* between -5 and 5.
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Number}
|
|
|
|
* @signal
|
2015-02-11 19:37:48 +00:00
|
|
|
*/
|
2015-08-28 03:03:42 +00:00
|
|
|
this.amplitude = this._oscillator.volume;
|
2015-05-24 13:45:15 +00:00
|
|
|
this.amplitude.units = Tone.Type.NormalRange;
|
2015-02-11 19:37:48 +00:00
|
|
|
this.amplitude.value = options.amplitude;
|
|
|
|
|
2015-08-28 03:03:42 +00:00
|
|
|
/**
|
|
|
|
* The signal which is output when the LFO is stopped
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._stoppedSignal = new Tone.Signal(0, Tone.Type.AudioRange);
|
|
|
|
|
2016-03-03 06:35:29 +00:00
|
|
|
/**
|
|
|
|
* Just outputs zeros.
|
|
|
|
* @type {Tone.Zero}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._zeros = new Tone.Zero();
|
|
|
|
|
2015-08-28 03:03:42 +00:00
|
|
|
/**
|
|
|
|
* The value that the LFO outputs when it's stopped
|
|
|
|
* @type {AudioRange}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._stoppedValue = 0;
|
|
|
|
|
2014-06-22 02:56:51 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* @type {Tone.AudioToGain}
|
2014-08-20 20:50:27 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-11-30 02:36:32 +00:00
|
|
|
this._a2g = new Tone.AudioToGain();
|
2014-08-20 20:50:27 +00:00
|
|
|
|
2014-11-30 02:36:32 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* @type {Tone.Scale}
|
2014-11-30 02:36:32 +00:00
|
|
|
* @private
|
2014-08-20 20:50:27 +00:00
|
|
|
*/
|
2014-11-30 02:54:29 +00:00
|
|
|
this._scaler = this.output = new Tone.Scale(options.min, options.max);
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2015-05-23 22:15:19 +00:00
|
|
|
/**
|
|
|
|
* the units of the LFO (used for converting)
|
2017-08-27 21:50:31 +00:00
|
|
|
* @type {Tone.Type}
|
2015-05-23 22:15:19 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2015-05-24 13:45:15 +00:00
|
|
|
this._units = Tone.Type.Default;
|
2015-10-21 16:12:35 +00:00
|
|
|
this.units = options.units;
|
2015-05-23 22:15:19 +00:00
|
|
|
|
2014-04-06 00:47:59 +00:00
|
|
|
//connect it up
|
2015-08-28 03:03:42 +00:00
|
|
|
this._oscillator.chain(this._a2g, this._scaler);
|
2016-03-03 06:35:29 +00:00
|
|
|
this._zeros.connect(this._a2g);
|
2015-08-28 03:03:42 +00:00
|
|
|
this._stoppedSignal.connect(this._a2g);
|
|
|
|
this._readOnly(["amplitude", "frequency"]);
|
|
|
|
this.phase = options.phase;
|
2014-06-17 17:01:06 +00:00
|
|
|
};
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2017-08-27 21:50:31 +00:00
|
|
|
Tone.extend(Tone.LFO, Tone.AudioNode);
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2014-11-30 02:54:29 +00:00
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.LFO.defaults = {
|
|
|
|
"type" : "sine",
|
|
|
|
"min" : 0,
|
|
|
|
"max" : 1,
|
2015-02-02 17:49:13 +00:00
|
|
|
"phase" : 0,
|
2014-11-30 02:54:29 +00:00
|
|
|
"frequency" : "4n",
|
2015-10-21 16:12:35 +00:00
|
|
|
"amplitude" : 1,
|
|
|
|
"units" : Tone.Type.Default
|
2014-11-30 02:54:29 +00:00
|
|
|
};
|
|
|
|
|
2014-06-17 17:01:06 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Start the LFO.
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [time=now] the time the LFO will start
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.LFO} this
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
2014-04-06 00:47:59 +00:00
|
|
|
Tone.LFO.prototype.start = function(time){
|
2015-08-28 03:03:42 +00:00
|
|
|
time = this.toSeconds(time);
|
|
|
|
this._stoppedSignal.setValueAtTime(0, time);
|
|
|
|
this._oscillator.start(time);
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-17 17:01:06 +00:00
|
|
|
};
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2014-06-17 17:01:06 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Stop the LFO.
|
2015-06-14 00:20:36 +00:00
|
|
|
* @param {Time} [time=now] the time the LFO will stop
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.LFO} this
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
2014-04-06 00:47:59 +00:00
|
|
|
Tone.LFO.prototype.stop = function(time){
|
2015-08-28 03:03:42 +00:00
|
|
|
time = this.toSeconds(time);
|
|
|
|
this._stoppedSignal.setValueAtTime(this._stoppedValue, time);
|
|
|
|
this._oscillator.stop(time);
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-17 17:01:06 +00:00
|
|
|
};
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2014-06-17 17:01:06 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Sync the start/stop/pause to the transport
|
2014-06-17 17:01:06 +00:00
|
|
|
* and the frequency to the bpm of the transport
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.LFO} this
|
2015-02-27 21:53:10 +00:00
|
|
|
* @example
|
|
|
|
* lfo.frequency.value = "8n";
|
2016-09-18 23:37:31 +00:00
|
|
|
* lfo.sync().start(0)
|
2017-08-27 21:50:31 +00:00
|
|
|
* //the rate of the LFO will always be an eighth note,
|
2015-06-20 23:25:49 +00:00
|
|
|
* //even as the tempo changes
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
2016-09-18 23:37:31 +00:00
|
|
|
Tone.LFO.prototype.sync = function(){
|
|
|
|
this._oscillator.sync();
|
2015-08-28 03:03:42 +00:00
|
|
|
this._oscillator.syncFrequency();
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-17 17:01:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* unsync the LFO from transport control
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.LFO} this
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
|
|
|
Tone.LFO.prototype.unsync = function(){
|
2015-08-28 03:03:42 +00:00
|
|
|
this._oscillator.unsync();
|
|
|
|
this._oscillator.unsyncFrequency();
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-17 17:01:06 +00:00
|
|
|
};
|
2014-04-05 00:24:19 +00:00
|
|
|
|
2014-09-01 16:52:32 +00:00
|
|
|
/**
|
2015-02-27 21:53:10 +00:00
|
|
|
* The miniumum output of the LFO.
|
2015-02-06 22:49:04 +00:00
|
|
|
* @memberOf Tone.LFO#
|
|
|
|
* @type {number}
|
|
|
|
* @name min
|
2015-02-02 17:49:13 +00:00
|
|
|
*/
|
2015-02-06 22:49:04 +00:00
|
|
|
Object.defineProperty(Tone.LFO.prototype, "min", {
|
|
|
|
get : function(){
|
2015-05-23 22:15:19 +00:00
|
|
|
return this._toUnits(this._scaler.min);
|
2015-02-06 22:49:04 +00:00
|
|
|
},
|
|
|
|
set : function(min){
|
2015-05-23 22:15:19 +00:00
|
|
|
min = this._fromUnits(min);
|
2015-02-06 22:49:04 +00:00
|
|
|
this._scaler.min = min;
|
|
|
|
}
|
|
|
|
});
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-06-17 17:01:06 +00:00
|
|
|
/**
|
2015-02-27 21:53:10 +00:00
|
|
|
* The maximum output of the LFO.
|
2015-02-06 22:49:04 +00:00
|
|
|
* @memberOf Tone.LFO#
|
|
|
|
* @type {number}
|
|
|
|
* @name max
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
2015-02-06 22:49:04 +00:00
|
|
|
Object.defineProperty(Tone.LFO.prototype, "max", {
|
|
|
|
get : function(){
|
2015-05-23 22:15:19 +00:00
|
|
|
return this._toUnits(this._scaler.max);
|
2015-02-06 22:49:04 +00:00
|
|
|
},
|
|
|
|
set : function(max){
|
2015-05-23 22:15:19 +00:00
|
|
|
max = this._fromUnits(max);
|
2015-02-06 22:49:04 +00:00
|
|
|
this._scaler.max = max;
|
|
|
|
}
|
|
|
|
});
|
2014-04-06 00:47:59 +00:00
|
|
|
|
2014-08-25 13:57:36 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* The type of the oscillator: sine, square, sawtooth, triangle.
|
2015-02-06 22:49:04 +00:00
|
|
|
* @memberOf Tone.LFO#
|
|
|
|
* @type {string}
|
|
|
|
* @name type
|
|
|
|
*/
|
2017-10-21 23:02:46 +00:00
|
|
|
Object.defineProperty(Tone.LFO.prototype, "type", {
|
2015-02-06 22:49:04 +00:00
|
|
|
get : function(){
|
2015-08-28 03:03:42 +00:00
|
|
|
return this._oscillator.type;
|
2015-02-06 22:49:04 +00:00
|
|
|
},
|
|
|
|
set : function(type){
|
2015-08-28 03:03:42 +00:00
|
|
|
this._oscillator.type = type;
|
|
|
|
this._stoppedValue = this._oscillator._getInitialValue();
|
|
|
|
this._stoppedSignal.value = this._stoppedValue;
|
2015-02-06 22:49:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-27 21:53:10 +00:00
|
|
|
/**
|
2015-06-20 23:25:49 +00:00
|
|
|
* The phase of the LFO.
|
2015-02-06 22:49:04 +00:00
|
|
|
* @memberOf Tone.LFO#
|
2015-03-07 19:17:16 +00:00
|
|
|
* @type {number}
|
2015-02-06 22:49:04 +00:00
|
|
|
* @name phase
|
2014-08-25 13:57:36 +00:00
|
|
|
*/
|
2017-10-21 23:02:46 +00:00
|
|
|
Object.defineProperty(Tone.LFO.prototype, "phase", {
|
2015-02-06 22:49:04 +00:00
|
|
|
get : function(){
|
2015-08-28 03:03:42 +00:00
|
|
|
return this._oscillator.phase;
|
2015-02-06 22:49:04 +00:00
|
|
|
},
|
|
|
|
set : function(phase){
|
2015-08-28 03:03:42 +00:00
|
|
|
this._oscillator.phase = phase;
|
|
|
|
this._stoppedValue = this._oscillator._getInitialValue();
|
|
|
|
this._stoppedSignal.value = this._stoppedValue;
|
2015-05-23 22:15:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2015-06-20 23:25:49 +00:00
|
|
|
* The output units of the LFO.
|
2015-05-23 22:15:19 +00:00
|
|
|
* @memberOf Tone.LFO#
|
2015-06-07 16:09:08 +00:00
|
|
|
* @type {Tone.Type}
|
2015-05-23 22:15:19 +00:00
|
|
|
* @name units
|
|
|
|
*/
|
2017-10-21 23:02:46 +00:00
|
|
|
Object.defineProperty(Tone.LFO.prototype, "units", {
|
2015-05-23 22:15:19 +00:00
|
|
|
get : function(){
|
|
|
|
return this._units;
|
|
|
|
},
|
|
|
|
set : function(val){
|
|
|
|
var currentMin = this.min;
|
|
|
|
var currentMax = this.max;
|
|
|
|
//convert the min and the max
|
|
|
|
this._units = val;
|
|
|
|
this.min = currentMin;
|
|
|
|
this.max = currentMax;
|
2015-02-06 22:49:04 +00:00
|
|
|
}
|
|
|
|
});
|
2014-08-25 13:57:36 +00:00
|
|
|
|
2016-05-14 22:07:53 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Mute the output.
|
2016-05-14 22:07:53 +00:00
|
|
|
* @memberOf Tone.LFO#
|
|
|
|
* @type {Boolean}
|
|
|
|
* @name mute
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Tone.LFO.prototype, "mute", {
|
|
|
|
get : function(){
|
|
|
|
return this._oscillator.mute;
|
2017-08-27 21:50:31 +00:00
|
|
|
},
|
2016-05-14 22:07:53 +00:00
|
|
|
set : function(mute){
|
|
|
|
this._oscillator.mute = mute;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-20 21:30:20 +00:00
|
|
|
/**
|
|
|
|
* Returns the playback state of the source, either "started" or "stopped".
|
|
|
|
* @type {Tone.State}
|
|
|
|
* @readOnly
|
|
|
|
* @memberOf Tone.LFO#
|
|
|
|
* @name state
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Tone.LFO.prototype, "state", {
|
|
|
|
get : function(){
|
|
|
|
return this._oscillator.state;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-06-17 17:01:06 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Connect the output of the LFO to an AudioParam, AudioNode, or Tone Node.
|
|
|
|
* Tone.LFO will automatically convert to the destination units of the
|
2015-05-23 22:15:19 +00:00
|
|
|
* will get the units from the connected node.
|
2017-08-27 21:50:31 +00:00
|
|
|
* @param {Tone | AudioParam | AudioNode} node
|
2015-05-23 22:15:19 +00:00
|
|
|
* @param {number} [outputNum=0] optionally which output to connect from
|
|
|
|
* @param {number} [inputNum=0] optionally which input to connect to
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.LFO} this
|
2015-06-20 23:25:49 +00:00
|
|
|
* @private
|
2015-05-23 22:15:19 +00:00
|
|
|
*/
|
|
|
|
Tone.LFO.prototype.connect = function(node){
|
2015-10-21 16:12:35 +00:00
|
|
|
if (node.constructor === Tone.Signal || node.constructor === Tone.Param || node.constructor === Tone.TimelineSignal){
|
2015-05-23 22:15:19 +00:00
|
|
|
this.convert = node.convert;
|
|
|
|
this.units = node.units;
|
|
|
|
}
|
|
|
|
Tone.Signal.prototype.connect.apply(this, arguments);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* private method borrowed from Param converts
|
2015-05-23 22:15:19 +00:00
|
|
|
* units from their destination value
|
|
|
|
* @function
|
|
|
|
* @private
|
|
|
|
*/
|
2015-10-21 14:34:37 +00:00
|
|
|
Tone.LFO.prototype._fromUnits = Tone.Param.prototype._fromUnits;
|
2015-05-23 22:15:19 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* private method borrowed from Param converts
|
2015-05-23 22:15:19 +00:00
|
|
|
* units to their destination value
|
2014-08-23 17:49:50 +00:00
|
|
|
* @function
|
2015-05-23 22:15:19 +00:00
|
|
|
* @private
|
2014-06-17 17:01:06 +00:00
|
|
|
*/
|
2015-10-21 14:34:37 +00:00
|
|
|
Tone.LFO.prototype._toUnits = Tone.Param.prototype._toUnits;
|
2014-04-11 23:17:01 +00:00
|
|
|
|
2014-06-20 05:23:35 +00:00
|
|
|
/**
|
|
|
|
* disconnect and dispose
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.LFO} this
|
2014-06-20 05:23:35 +00:00
|
|
|
*/
|
|
|
|
Tone.LFO.prototype.dispose = function(){
|
2017-08-27 21:50:31 +00:00
|
|
|
Tone.AudioNode.prototype.dispose.call(this);
|
2015-08-28 03:03:42 +00:00
|
|
|
this._writable(["amplitude", "frequency"]);
|
|
|
|
this._oscillator.dispose();
|
|
|
|
this._oscillator = null;
|
|
|
|
this._stoppedSignal.dispose();
|
|
|
|
this._stoppedSignal = null;
|
2016-03-03 06:35:29 +00:00
|
|
|
this._zeros.dispose();
|
|
|
|
this._zeros = null;
|
2014-06-21 19:53:27 +00:00
|
|
|
this._scaler.dispose();
|
2014-09-06 22:55:11 +00:00
|
|
|
this._scaler = null;
|
2014-11-30 02:36:32 +00:00
|
|
|
this._a2g.dispose();
|
|
|
|
this._a2g = null;
|
2014-08-25 13:57:36 +00:00
|
|
|
this.frequency = null;
|
2015-02-12 04:08:53 +00:00
|
|
|
this.amplitude = null;
|
2015-02-02 17:49:13 +00:00
|
|
|
return this;
|
2014-06-20 05:23:35 +00:00
|
|
|
};
|
|
|
|
|
2014-04-06 00:47:59 +00:00
|
|
|
return Tone.LFO;
|
2017-08-27 21:50:31 +00:00
|
|
|
});
|