works. sort of.

This commit is contained in:
Yotam Mann 2014-07-22 19:17:45 -04:00
parent 86c3b6e261
commit 73c10a90c4

View file

@ -1,7 +1,8 @@
define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/Threshold"], function(Tone){
define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/Multiply"], function(Tone){
/**
* Follow the envelope of the incoming signal
* @class Follow the envelope of the incoming signal
*
* @constructor
* @extends {Tone}
* @param {Tone.Time=} [attackTime = 0.01]
@ -29,6 +30,7 @@ define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/
this._filter = this.context.createBiquadFilter();
this._filter.type = "lowpass";
this._filter.frequency.value = 0;
this._filter.Q.value = -10;
/**
* @type {WaveShaperNode}
@ -42,6 +44,13 @@ define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/
*/
this._negate = new Tone.Negate();
/**
* @type {Tone.Negate}
* @private
*/
this._delay = this.context.createDelay();
this._delay.delayTime.value = 0.002;//2ms 'lookahead'
/**
* @type {GainNode}
* @private
@ -49,19 +58,19 @@ define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/
this._difference = this.context.createGain();
/**
* @type {GainNode}
* this keeps it far from 0, even for very small differences
* @type {Tone.Multiply}
* @private
*/
this._thresh = new Tone.Threshold(0);
this._mult = new Tone.Multiply(100000);
//the smoothed signal
this.chain(this.input, this._abs, this._filter, this.output);
//subtract the smoothed signal from the input signal
this.input.connect(this._negate);
this.output.connect(this._difference);
this._negate.connect(this._difference);
this.chain(this.input, this._abs, this._filter, this._delay, this.output);
//the difference path
this.chain(this._abs, this._negate, this._difference);
this._delay.connect(this._difference);
//threshold the difference and use the thresh to set the frequency
this.chain(this._difference, this._thresh, this._frequencyValues, this._filter.frequency);
this.chain(this._difference, this._mult, this._frequencyValues, this._filter.frequency);
//set the attack and release values in the table
this._setAttackRelease(this.secondsToFrequency(attackTime), this.secondsToFrequency(releaseTime));
};
@ -76,6 +85,8 @@ define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/
*/
Tone.Follower.prototype._setAttackRelease = function(attack, release){
var curveLength = 1024;
attack /= 2;
release /= 2;
var curve = new Float32Array(curveLength);
for (var i = 0; i < curveLength; i++){
var normalized = (i / (curveLength - 1)) * 2 - 1;
@ -99,17 +110,19 @@ define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Negate", "Tone/signal/
this._frequencyValues.disconnect();
this.output.disconnect();
this._abs.dispose();
this._delay.dispose();
this._negate.dispose();
this._difference.dispose();
this._thresh.dispose();
this._mult.dispose();
this._filter = null;
this.input = null;
this._delay = null;
this._frequencyValues = null;
this.output = null;
this._abs = null;
this._negate = null;
this._difference = null;
this._thresh = null;
this._mult = null;
};
return Tone.Follower;