Tone.js/Tone/component/Gate.js

119 lines
2.8 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/component/Follower", "Tone/signal/GreaterThan", "Tone/core/AudioNode"], function(Tone){
2014-07-23 19:50:45 +00:00
"use strict";
2014-07-23 19:50:45 +00:00
/**
* @class Tone.Gate only passes a signal through when the incoming
* signal exceeds a specified threshold. To do this, Gate uses
* a Tone.Follower to follow the amplitude of the incoming signal.
2015-07-04 19:25:37 +00:00
* A common implementation of this class is a [Noise Gate](https://en.wikipedia.org/wiki/Noise_gate).
*
2014-07-23 19:50:45 +00:00
* @constructor
* @extends {Tone.AudioNode}
* @param {Decibels|Object} [threshold] The threshold above which the gate will open.
2015-06-20 23:25:49 +00:00
* @param {Time=} attack The follower's attack time
* @param {Time=} release The follower's release time
2015-02-27 21:53:10 +00:00
* @example
2015-06-20 23:25:49 +00:00
* var gate = new Tone.Gate(-30, 0.2, 0.3).toMaster();
* var mic = new Tone.UserMedia().connect(gate);
* //the gate will only pass through the incoming
2015-06-20 23:25:49 +00:00
* //signal when it's louder than -30db
2014-07-23 19:50:45 +00:00
*/
2015-02-02 17:49:13 +00:00
Tone.Gate = function(){
var options = Tone.defaults(arguments, ["threshold", "attack", "release"], Tone.Gate);
Tone.AudioNode.call(this);
2016-09-20 03:02:42 +00:00
this.createInsOuts(1, 1);
2014-07-23 19:50:45 +00:00
/**
* @type {Tone.Follower}
* @private
*/
2015-02-02 17:49:13 +00:00
this._follower = new Tone.Follower(options.attack, options.release);
2014-07-23 19:50:45 +00:00
/**
* @type {Tone.GreaterThan}
* @private
*/
this._gt = new Tone.GreaterThan(Tone.dbToGain(options.threshold));
2014-07-23 19:50:45 +00:00
//the connections
2014-12-01 02:32:09 +00:00
this.input.connect(this.output);
2014-07-23 19:50:45 +00:00
//the control signal
2014-12-01 02:32:09 +00:00
this.input.chain(this._gt, this._follower, this.output.gain);
2014-07-23 19:50:45 +00:00
};
Tone.extend(Tone.Gate, Tone.AudioNode);
2014-07-23 19:50:45 +00:00
2015-02-02 17:49:13 +00:00
/**
* @const
* @static
* @type {Object}
*/
Tone.Gate.defaults = {
"attack" : 0.1,
2015-02-02 17:49:13 +00:00
"release" : 0.1,
"threshold" : -40
};
2014-07-23 19:50:45 +00:00
/**
* The threshold of the gate in decibels
* @memberOf Tone.Gate#
2015-06-14 00:20:36 +00:00
* @type {Decibels}
* @name threshold
2015-02-02 17:49:13 +00:00
*/
Object.defineProperty(Tone.Gate.prototype, "threshold", {
get : function(){
return Tone.gainToDb(this._gt.value);
},
set : function(thresh){
this._gt.value = Tone.dbToGain(thresh);
}
});
2014-09-06 22:10:42 +00:00
/**
* The attack speed of the gate
* @memberOf Tone.Gate#
2015-06-14 00:20:36 +00:00
* @type {Time}
* @name attack
2014-09-06 22:10:42 +00:00
*/
Object.defineProperty(Tone.Gate.prototype, "attack", {
get : function(){
return this._follower.attack;
},
set : function(attackTime){
this._follower.attack = attackTime;
}
});
2015-02-02 17:49:13 +00:00
/**
* The release speed of the gate
* @memberOf Tone.Gate#
2015-06-14 00:20:36 +00:00
* @type {Time}
* @name release
2015-02-02 17:49:13 +00:00
*/
Object.defineProperty(Tone.Gate.prototype, "release", {
get : function(){
return this._follower.release;
},
set : function(releaseTime){
this._follower.release = releaseTime;
}
});
2014-07-23 19:50:45 +00:00
/**
* Clean up.
* @returns {Tone.Gate} this
2014-07-23 19:50:45 +00:00
*/
Tone.Gate.prototype.dispose = function(){
Tone.AudioNode.prototype.dispose.call(this);
2014-07-23 19:50:45 +00:00
this._follower.dispose();
this._gt.dispose();
this._follower = null;
this._gt = null;
2015-02-02 17:49:13 +00:00
return this;
2014-07-23 19:50:45 +00:00
};
return Tone.Gate;
});