Tone.js/Tone/component/Follower.js
2014-07-23 15:28:06 -04:00

66 lines
No EOL
1.6 KiB
JavaScript

define(["Tone/core/Tone", "Tone/signal/Abs"], function(Tone){
/**
* @class Follow the envelope of the incoming signal
* Note: due to the nature of low-pass filters,
* there is some rippling which is proportional
* to the smoothTime
*
* @constructor
* @extends {Tone}
* @param {Tone.Time=} [smoothTime = 0.1]
*/
Tone.Follower = function(smoothTime){
Tone.call(this);
//default values
smoothTime = this.defaultArg(smoothTime, 0.1);
/**
* @type {Tone.Abs}
* @private
*/
this._abs = new Tone.Abs();
/**
* the lowpass filter which smooths the input
* @type {BiquadFilterNode}
* @private
*/
this._filter = this.context.createBiquadFilter();
this._filter.type = "lowpass";
this._filter.Q.value = -10;
//the connections
this.chain(this.input, this._abs, this._filter, this.output);
this.setSmoothTime(smoothTime);
};
Tone.extend(Tone.Follower);
/**
* set the amount of time it takes to reach the destination value
*
* @param {Tone.Time} smoothTime the amount of time it takes
* to reach the destination value
*/
Tone.Follower.prototype.setSmoothTime = function(smoothTime){
this._filter.frequency.value = this.secondsToFrequency(smoothTime * 2);
};
/**
* dispose
*/
Tone.Follower.prototype.dispose = function(){
this._filter.disconnect();
this.input.disconnect();
this.output.disconnect();
this._abs.dispose();
this._filter = null;
this.input = null;
this.output = null;
this._abs = null;
};
return Tone.Follower;
});