Tone.js/Tone/component/Follower.js

185 lines
4.5 KiB
JavaScript
Raw Normal View History

2014-10-30 23:44:05 +00:00
define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Subtract", "Tone/signal/Multiply", "Tone/signal/Signal"],
function(Tone){
"use strict";
/**
* @class Follow the envelope of the incoming signal.
* Careful with small (< 0.02) attack or decay values.
* The follower has some ripple which gets exaggerated
* by small values.
2014-07-22 23:17:45 +00:00
*
* @constructor
* @extends {Tone}
* @param {Tone.Time=} [attack = 0.05]
* @param {Tone.Time=} [release = 0.5]
*/
Tone.Follower = function(){
2014-07-02 22:20:34 +00:00
Tone.call(this);
var options = this.optionsObject(arguments, ["attack", "release"], Tone.Follower.defaults);
2014-07-02 22:20:34 +00:00
/**
2014-07-02 22:20:34 +00:00
* @type {Tone.Abs}
* @private
*/
2014-07-02 22:20:34 +00:00
this._abs = new Tone.Abs();
/**
2014-07-23 19:22:46 +00:00
* the lowpass filter which smooths the input
* @type {BiquadFilterNode}
* @private
*/
this._filter = this.context.createBiquadFilter();
this._filter.type = "lowpass";
this._filter.frequency.value = 0;
this._filter.Q.value = -100;
/**
* @type {WaveShaperNode}
* @private
*/
this._frequencyValues = this.context.createWaveShaper();
/**
2014-10-30 23:44:05 +00:00
* @type {Tone.Subtract}
* @private
*/
2014-10-30 23:44:05 +00:00
this._sub = new Tone.Subtract();
/**
* @type {DelayNode}
* @private
*/
this._delay = this.context.createDelay();
2014-10-20 02:07:18 +00:00
this._delay.delayTime.value = this.bufferTime;
/**
* this keeps it far from 0, even for very small differences
* @type {Tone.Multiply}
* @private
*/
2014-10-30 23:44:05 +00:00
this._mult = new Tone.Multiply(10000);
2014-08-25 13:57:36 +00:00
/**
* @private
* @type {number}
*/
this._attack = this.secondsToFrequency(options.attack);
/**
* @private
* @type {number}
*/
this._release = this.secondsToFrequency(options.release);
/**
* the curve that the waveshaper uses
* @type {Float32Array}
* @private
*/
this._curve = new Float32Array(1024);
//the smoothed signal to get the values
2014-07-23 19:22:46 +00:00
this.chain(this.input, this._abs, this._filter, this.output);
//the difference path
2014-10-30 23:44:05 +00:00
this._abs.connect(this._sub, 0, 1);
this.chain(this._filter, this._delay, this._sub);
//threshold the difference and use the thresh to set the frequency
2014-10-30 23:44:05 +00:00
this.chain(this._sub, this._mult, this._frequencyValues, this._filter.frequency);
//set the attack and release values in the table
2014-08-25 13:57:36 +00:00
this._setAttackRelease(this._attack, this._release);
};
Tone.extend(Tone.Follower);
2014-07-02 22:20:34 +00:00
/**
* @static
* @type {Object}
*/
Tone.Follower.defaults = {
"attack" : 0.05,
"release" : 0.5
};
2014-07-23 19:25:46 +00:00
/**
* sets the attack and release times in the wave shaper
* @param {number} attack
* @param {number} release
* @private
2014-07-23 19:25:46 +00:00
*/
Tone.Follower.prototype._setAttackRelease = function(attack, release){
var curveLength = this._curve.length;
2014-10-13 23:21:34 +00:00
//the minimum value for attack/release is the bufferSize / sampleRate
2014-10-20 02:07:18 +00:00
var minTime = this.bufferTime;
2014-10-13 23:21:34 +00:00
attack = Math.max(attack, minTime);
release = Math.max(release, minTime);
for (var i = 0; i < curveLength; i++){
var normalized = (i / (curveLength - 1)) * 2 - 1;
var val;
if (normalized <= 0){
val = attack;
} else {
val = release;
}
this._curve[i] = val;
}
this._frequencyValues.curve = this._curve;
2014-07-23 19:25:46 +00:00
};
2014-08-25 13:57:36 +00:00
/**
* set the attack time
* @param {Tone.Time} attack
*/
Tone.Follower.prototype.setAttack = function(attack){
this._attack = this.secondsToFrequency(attack);
this._setAttackRelease(this._attack, this._release);
};
/**
* set the release time
* @param {Tone.Time} release
*/
Tone.Follower.prototype.setRelease = function(release){
this._release = this.secondsToFrequency(release);
this._setAttackRelease(this._attack, this._release);
};
/**
* setter in bulk
* @param {Object} params
*/
Tone.Follower.prototype.set = function(params){
if (!this.isUndef(params.attack)) this.setAttack(params.attack);
if (!this.isUndef(params.release)) this.setRelease(params.release);
Tone.Effect.prototype.set.call(this, params);
};
/**
* borrows the connect method from Signal so that the output can be used
* as a control signal {@link Tone.Signal}
*/
Tone.Follower.prototype.connect = Tone.Signal.prototype.connect;
2014-07-02 22:20:34 +00:00
/**
* dispose
*/
Tone.Follower.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._filter.disconnect();
2014-10-30 23:44:05 +00:00
this._filter = null;
this._frequencyValues.disconnect();
2014-10-30 23:44:05 +00:00
this._frequencyValues = null;
this._delay.disconnect();
this._delay = null;
2014-10-30 23:44:05 +00:00
this._sub.disconnect();
this._sub = null;
this._abs.dispose();
2014-07-02 22:20:34 +00:00
this._abs = null;
2014-10-30 23:44:05 +00:00
this._mult.dispose();
this._mult = null;
this._curve = null;
2014-07-02 22:20:34 +00:00
};
return Tone.Follower;
});