Tone.js/Tone/component/LFO.js

163 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-08-23 17:49:50 +00:00
define(["Tone/core/Tone", "Tone/source/Oscillator", "Tone/signal/Scale", "Tone/signal/Signal"],
function(Tone){
2014-04-05 00:24:19 +00:00
"use strict";
2014-06-17 17:01:06 +00:00
/**
* Low Frequency Oscillator
*
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-04-06 00:47:59 +00:00
Tone.LFO = function(rate, outputMin, outputMax){
2014-08-20 20:50:27 +00:00
/**
* the oscillator
* @type {Tone.Oscillator}
*/
this.oscillator = new Tone.Oscillator(this.defaultArg(rate, 1), "sine");
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-08-20 20:50:27 +00:00
* @type {Tone.Scale}
* @private
*/
2014-06-21 19:53:27 +00:00
this._scaler = new Tone.Scale(this.defaultArg(outputMin, 0), this.defaultArg(outputMax, 1));
2014-08-20 20:50:27 +00:00
/**
* alias for the output
* @type {Tone.Scale}
*/
2014-06-21 19:53:27 +00:00
this.output = this._scaler;
2014-04-05 00:24:19 +00:00
2014-04-06 00:47:59 +00:00
//connect it up
2014-06-21 19:53:27 +00:00
this.chain(this.oscillator, this.output);
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-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-06-21 19:53:27 +00:00
this._scaler.setOutputMin(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-08-23 19:18:51 +00:00
this._scaler.setOutputMax(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(){
this.oscillator.dispose();
2014-06-21 19:53:27 +00:00
this._scaler.dispose();
2014-06-20 05:23:35 +00:00
this.oscillator = null;
2014-08-25 13:57:36 +00:00
this.frequency = null;
2014-06-20 05:23:35 +00:00
this.output = null;
};
2014-04-06 00:47:59 +00:00
return Tone.LFO;
});