Tone.js/Tone/component/Follower.js

119 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-06-06 02:14:46 +00:00
define(["Tone/core/Tone", "Tone/signal/Abs", "Tone/signal/Subtract",
"Tone/signal/Signal", "Tone/type/Type", "Tone/core/Delay", "Tone/core/AudioNode"], function(Tone){
"use strict";
/**
* @class Tone.Follower is a crude envelope follower which will follow
2018-06-06 02:14:46 +00:00
* the amplitude of an incoming signal. Read more about envelope followers (also known
2015-07-04 19:25:37 +00:00
* as envelope detectors) on [Wikipedia](https://en.wikipedia.org/wiki/Envelope_detector).
*
* @constructor
* @extends {Tone.AudioNode}
2018-06-06 02:14:46 +00:00
* @param {Time} [smoothing=0.05] The rate of change of the follower.
2015-02-27 21:53:10 +00:00
* @example
2018-06-06 02:14:46 +00:00
* var follower = new Tone.Follower(0.3);
*/
Tone.Follower = function(){
2018-06-06 02:14:46 +00:00
var options = Tone.defaults(arguments, ["smoothing"], Tone.Follower);
Tone.AudioNode.call(this);
2016-09-20 03:02:42 +00:00
this.createInsOuts(1, 1);
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;
2018-06-06 02:14:46 +00:00
this._filter.Q.value = 0;
/**
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();
/**
2018-06-06 02:14:46 +00:00
* delay node to compare change over time
* @type {Tone.Delay}
* @private
*/
this._delay = new Tone.Delay(this.blockTime);
/**
2018-06-06 02:14:46 +00:00
* the smoothing value
* @private
2018-06-06 02:14:46 +00:00
* @type {Number}
*/
2018-06-06 02:14:46 +00:00
this._smoothing = options.smoothing;
2018-06-06 02:14:46 +00:00
this.input.connect(this._delay, this._sub);
this.input.connect(this._sub, 0, 1);
this._sub.chain(this._abs, this._filter, this.output);
2014-08-25 13:57:36 +00:00
2018-06-06 02:14:46 +00:00
//set the smoothing initially
this.smoothing = options.smoothing;
};
Tone.extend(Tone.Follower, Tone.AudioNode);
2014-07-02 22:20:34 +00:00
/**
* @static
* @type {Object}
*/
Tone.Follower.defaults = {
2018-06-06 02:14:46 +00:00
"smoothing" : 0.05,
2014-07-23 19:25:46 +00:00
};
2014-08-25 13:57:36 +00:00
/**
* The attack time.
* @memberOf Tone.Follower#
2015-06-14 00:20:36 +00:00
* @type {Time}
2018-06-06 02:14:46 +00:00
* @name smoothing
2014-08-25 13:57:36 +00:00
*/
2018-06-06 02:14:46 +00:00
Object.defineProperty(Tone.Follower.prototype, "smoothing", {
get : function(){
2018-06-06 02:14:46 +00:00
return this._smoothing;
},
2018-06-06 02:14:46 +00:00
set : function(smoothing){
this._smoothing = smoothing;
this._filter.frequency.value = Tone.Time(smoothing).toFrequency() * 0.5;
}
});
2014-08-25 13:57:36 +00:00
/**
2015-07-02 00:19:58 +00:00
* Borrows the connect method from Signal so that the output can be used
* as a Tone.Signal control signal.
2015-02-27 21:53:10 +00:00
* @function
*/
Tone.Follower.prototype.connect = Tone.SignalBase.prototype.connect;
2014-07-02 22:20:34 +00:00
/**
* dispose
* @returns {Tone.Follower} this
2014-07-02 22:20:34 +00:00
*/
Tone.Follower.prototype.dispose = function(){
Tone.AudioNode.prototype.dispose.call(this);
this._filter.disconnect();
2014-10-30 23:44:05 +00:00
this._filter = null;
this._delay.dispose();
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;
2015-02-02 17:49:13 +00:00
return this;
2014-07-02 22:20:34 +00:00
};
return Tone.Follower;
});