Tone.js/Tone/component/LFO.js

203 lines
4.3 KiB
JavaScript
Raw Normal View History

2014-11-30 02:36:32 +00:00
define(["Tone/core/Tone", "Tone/source/Oscillator", "Tone/signal/Scale", "Tone/signal/Signal", "Tone/signal/AudioToGain"],
2014-08-23 17:49:50 +00:00
function(Tone){
2014-04-05 00:24:19 +00:00
"use strict";
2014-06-17 17:01:06 +00:00
/**
2014-08-23 17:49:50 +00:00
* @class The Low Frequency Oscillator produces an output signal
* which can be attached to an AudioParam or Tone.Signal
* for constant control over that parameter. the LFO can
* also be synced to the transport to start/stop/pause
* and change when the tempo changes.
2014-06-17 17:01:06 +00:00
*
* @constructor
2015-02-02 17:49:13 +00:00
* @extends {Tone.Oscillator}
2014-12-08 16:02:47 +00:00
* @param {Tone.Time} [frequency="4n"]
2014-12-02 06:42:08 +00:00
* @param {number} [outputMin=0]
* @param {number} [outputMax=1]
2014-06-17 17:01:06 +00:00
*/
2014-11-30 02:54:29 +00:00
Tone.LFO = function(){
2014-12-08 16:02:47 +00:00
var options = this.optionsObject(arguments, ["frequency", "min", "max"], Tone.LFO.defaults);
2014-08-20 20:50:27 +00:00
/**
* the oscillator
* @type {Tone.Oscillator}
*/
2015-02-02 17:49:13 +00:00
this.oscillator = new Tone.Oscillator({
"frequency" : options.frequency,
"type" : options.type,
"phase" : options.phase
});
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
2014-08-25 13:57:36 +00:00
* @type {Tone.Signal}
*/
this.frequency = this.oscillator.frequency;
/**
2014-11-30 02:36:32 +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
/**
* @type {Tone.Scale}
* @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
2014-04-06 00:47:59 +00:00
//connect it up
2014-12-01 02:32:09 +00:00
this.oscillator.chain(this._a2g, this._scaler);
2014-06-17 17:01:06 +00:00
};
2014-04-05 00:24:19 +00:00
2015-02-02 17:49:13 +00:00
Tone.extend(Tone.LFO, Tone.Oscillator);
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",
};
2014-06-17 17:01:06 +00:00
/**
* start the LFO
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [time=now] the time the LFO will start
2015-02-02 17:49:13 +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){
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
/**
* stop the LFO
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [time=now] the time the LFO will stop
2015-02-02 17:49:13 +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){
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
/**
* Sync the start/stop/pause to the transport
* and the frequency to the bpm of the transport
2014-08-20 20:50:27 +00:00
*
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [delay=0] the time to delay the start of the
2014-08-20 20:50:27 +00:00
* LFO from the start of the transport
2015-02-02 17:49:13 +00:00
* @returns {Tone.LFO} `this`
2014-06-17 17:01:06 +00:00
*/
2014-08-20 20:50:27 +00:00
Tone.LFO.prototype.sync = function(delay){
2014-12-19 21:40:06 +00:00
this.oscillator.sync(delay);
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-02-02 17:49:13 +00:00
* @returns {Tone.LFO} `this`
2014-06-17 17:01:06 +00:00
*/
Tone.LFO.prototype.unsync = function(){
2014-12-19 21:40:06 +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
/**
* the miniumum output of the scale
* @memberOf Tone.LFO#
* @type {number}
* @name min
2015-02-02 17:49:13 +00:00
*/
Object.defineProperty(Tone.LFO.prototype, "min", {
get : function(){
return this._scaler.min;
},
set : function(min){
this._scaler.min = min;
}
});
2014-04-06 00:47:59 +00:00
2014-06-17 17:01:06 +00:00
/**
* the maximum output of the scale
* @memberOf Tone.LFO#
* @type {number}
* @name max
2014-06-17 17:01:06 +00:00
*/
Object.defineProperty(Tone.LFO.prototype, "max", {
get : function(){
return this._scaler.max;
},
set : function(max){
this._scaler.max = max;
}
});
2014-04-06 00:47:59 +00:00
2014-08-25 13:57:36 +00:00
/**
* the type of the oscillator
* @memberOf Tone.LFO#
* @type {string}
* @name type
*/
Object.defineProperty(Tone.LFO.prototype, "type", {
get : function(){
return this.oscillator.type;
},
set : function(type){
this.oscillator.type = type;
}
});
/**
* the phase of the LFO
* @memberOf Tone.LFO#
* @type {string}
* @name phase
2014-08-25 13:57:36 +00:00
*/
Object.defineProperty(Tone.LFO.prototype, "phase", {
get : function(){
return this.oscillator.phase;
},
set : function(phase){
this.oscillator.phase = phase;
}
});
2014-08-25 13:57:36 +00:00
2014-06-17 17:01:06 +00:00
/**
2014-08-23 17:49:50 +00:00
* Override the connect method so that it 0's out the value
* if attached to an AudioParam or Tone.Signal.
*
* Borrowed from {@link Tone.Signal}
2014-06-17 17:01:06 +00:00
*
2014-08-23 17:49:50 +00:00
* @function
2014-06-17 17:01:06 +00:00
*/
2014-06-21 19:53:27 +00:00
Tone.LFO.prototype.connect = Tone.Signal.prototype.connect;
2014-06-20 05:23:35 +00:00
/**
* disconnect and dispose
2015-02-02 17:49:13 +00:00
* @returns {Tone.LFO} `this`
2014-06-20 05:23:35 +00:00
*/
Tone.LFO.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
2014-06-20 05:23:35 +00:00
this.oscillator.dispose();
2014-11-30 02:36:32 +00:00
this.oscillator = null;
2014-06-21 19:53:27 +00:00
this._scaler.dispose();
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-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;
});