Tone.js/Tone/component/LFO.js

180 lines
4.1 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
* @extends {Tone}
* @param {number} rate
* @param {number=} outputMin
* @param {number=} outputMax
*/
2014-11-30 02:54:29 +00:00
Tone.LFO = function(){
var options = this.optionsObject(arguments, ["rate", "min", "max"], Tone.LFO.defaults);
2014-08-20 20:50:27 +00:00
/**
* the oscillator
* @type {Tone.Oscillator}
*/
2014-11-30 02:54:29 +00:00
this.oscillator = new Tone.Oscillator(options.rate, options.type);
2014-08-20 20:50:27 +00:00
2014-08-25 13:57:36 +00:00
/**
* pointer to the oscillator's frequency
* @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-11-30 02:36:32 +00:00
this.chain(this.oscillator, this._a2g, this._scaler);
2014-06-17 17:01:06 +00:00
};
2014-04-05 00:24:19 +00:00
2014-06-15 21:38:07 +00:00
Tone.extend(Tone.LFO);
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,
"frequency" : "4n",
};
2014-06-17 17:01:06 +00:00
/**
* start the LFO
2014-08-20 20:50:27 +00:00
* @param {Tone.Time=} [time=now] the time the LFO will start
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);
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-08-20 20:50:27 +00:00
* @param {Tone.Time=} [time=now] the time the LFO will stop
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);
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
*
* @param {Tone.Time=} [delay=0] the time to delay the start of the
* LFO from the start of the transport
2014-06-17 17:01:06 +00:00
*/
2014-08-20 20:50:27 +00:00
Tone.LFO.prototype.sync = function(delay){
Tone.Transport.syncSource(this.oscillator, delay);
Tone.Transport.syncSignal(this.oscillator.frequency);
2014-06-17 17:01:06 +00:00
};
/**
* unsync the LFO from transport control
*/
Tone.LFO.prototype.unsync = function(){
2014-08-20 20:50:27 +00:00
Tone.Transport.unsyncSource(this.oscillator);
Tone.Transport.unsyncSignal(this.oscillator.frequency);
2014-06-17 17:01:06 +00:00
};
2014-06-15 21:38:07 +00:00
2014-04-05 00:24:19 +00:00
2014-06-17 17:01:06 +00:00
/**
* set the frequency
* @param {number} rate
*/
2014-04-07 00:12:40 +00:00
Tone.LFO.prototype.setFrequency = function(rate){
2014-04-06 00:47:59 +00:00
this.oscillator.setFrequency(rate);
2014-06-17 17:01:06 +00:00
};
2014-04-05 00:24:19 +00:00
2014-09-01 16:52:32 +00:00
/**
* set the phase
* @param {number} phase
*/
Tone.LFO.prototype.setPhase = function(phase){
this.oscillator.setPhase(phase);
};
2014-06-17 17:01:06 +00:00
/**
* set the minimum output of the LFO
* @param {number} min
*/
2014-04-06 00:47:59 +00:00
Tone.LFO.prototype.setMin = function(min){
2014-11-30 02:36:32 +00:00
this._scaler.setMin(min);
2014-06-17 17:01:06 +00:00
};
2014-04-05 00:24:19 +00:00
2014-06-17 17:01:06 +00:00
/**
2014-08-23 17:49:50 +00:00
* Set the maximum output of the LFO
2014-06-17 17:01:06 +00:00
* @param {number} min
*/
2014-04-06 00:47:59 +00:00
Tone.LFO.prototype.setMax = function(max){
2014-11-30 02:36:32 +00:00
this._scaler.setMax(max);
2014-06-17 17:01:06 +00:00
};
2014-04-06 00:47:59 +00:00
2014-06-17 17:01:06 +00:00
/**
2014-08-23 17:49:50 +00:00
* Set the waveform of the LFO
2014-06-17 17:01:06 +00:00
* @param {string} type
*/
2014-04-06 00:47:59 +00:00
Tone.LFO.prototype.setType = function(type){
this.oscillator.setType(type);
2014-06-17 17:01:06 +00:00
};
2014-04-06 00:47:59 +00:00
2014-08-25 13:57:36 +00:00
/**
* set all of the parameters with an object
* @param {Object} params
*/
Tone.LFO.prototype.set = function(params){
if (!this.isUndef(params.frequency)) this.setFrequency(params.frequency);
if (!this.isUndef(params.type)) this.setType(params.type);
if (!this.isUndef(params.min)) this.setMin(params.min);
if (!this.isUndef(params.max)) this.setMax(params.max);
};
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
*/
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;
2014-06-20 05:23:35 +00:00
};
2014-04-06 00:47:59 +00:00
return Tone.LFO;
});