2017-08-27 21:50:31 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/component/Follower", "Tone/signal/GreaterThan", "Tone/core/AudioNode"], function(Tone){
|
2014-07-23 19:50:45 +00:00
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-07-23 19:50:45 +00:00
|
|
|
/**
|
2017-08-27 21:50:31 +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).
|
2017-08-27 21:50:31 +00:00
|
|
|
*
|
2014-07-23 19:50:45 +00:00
|
|
|
* @constructor
|
2017-08-27 21:50:31 +00:00
|
|
|
* @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();
|
2017-01-08 20:58:58 +00:00
|
|
|
* var mic = new Tone.UserMedia().connect(gate);
|
2017-08-27 21:50:31 +00:00
|
|
|
* //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(){
|
2017-08-27 21:50:31 +00:00
|
|
|
|
2017-04-26 03:08:23 +00:00
|
|
|
var options = Tone.defaults(arguments, ["threshold", "attack", "release"], Tone.Gate);
|
2017-08-27 21:50:31 +00:00
|
|
|
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
|
|
|
|
*/
|
2017-04-27 03:21:26 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-08-27 21:50:31 +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 = {
|
2017-08-27 21:50:31 +00:00
|
|
|
"attack" : 0.1,
|
2015-02-02 17:49:13 +00:00
|
|
|
"release" : 0.1,
|
|
|
|
"threshold" : -40
|
|
|
|
};
|
|
|
|
|
2014-07-23 19:50:45 +00:00
|
|
|
/**
|
2015-02-06 22:49:04 +00:00
|
|
|
* The threshold of the gate in decibels
|
|
|
|
* @memberOf Tone.Gate#
|
2015-06-14 00:20:36 +00:00
|
|
|
* @type {Decibels}
|
2015-02-06 22:49:04 +00:00
|
|
|
* @name threshold
|
2015-02-02 17:49:13 +00:00
|
|
|
*/
|
2015-02-06 22:49:04 +00:00
|
|
|
Object.defineProperty(Tone.Gate.prototype, "threshold", {
|
|
|
|
get : function(){
|
2017-04-27 03:21:26 +00:00
|
|
|
return Tone.gainToDb(this._gt.value);
|
2017-08-27 21:50:31 +00:00
|
|
|
},
|
2015-02-06 22:49:04 +00:00
|
|
|
set : function(thresh){
|
2017-04-27 03:21:26 +00:00
|
|
|
this._gt.value = Tone.dbToGain(thresh);
|
2015-02-06 22:49:04 +00:00
|
|
|
}
|
|
|
|
});
|
2014-09-06 22:10:42 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-06 22:49:04 +00:00
|
|
|
* The attack speed of the gate
|
|
|
|
* @memberOf Tone.Gate#
|
2015-06-14 00:20:36 +00:00
|
|
|
* @type {Time}
|
2015-02-06 22:49:04 +00:00
|
|
|
* @name attack
|
2014-09-06 22:10:42 +00:00
|
|
|
*/
|
2015-02-06 22:49:04 +00:00
|
|
|
Object.defineProperty(Tone.Gate.prototype, "attack", {
|
|
|
|
get : function(){
|
|
|
|
return this._follower.attack;
|
2017-08-27 21:50:31 +00:00
|
|
|
},
|
2015-02-06 22:49:04 +00:00
|
|
|
set : function(attackTime){
|
|
|
|
this._follower.attack = attackTime;
|
|
|
|
}
|
|
|
|
});
|
2015-02-02 17:49:13 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-06 22:49:04 +00:00
|
|
|
* The release speed of the gate
|
|
|
|
* @memberOf Tone.Gate#
|
2015-06-14 00:20:36 +00:00
|
|
|
* @type {Time}
|
2015-02-06 22:49:04 +00:00
|
|
|
* @name release
|
2015-02-02 17:49:13 +00:00
|
|
|
*/
|
2015-02-06 22:49:04 +00:00
|
|
|
Object.defineProperty(Tone.Gate.prototype, "release", {
|
|
|
|
get : function(){
|
|
|
|
return this._follower.release;
|
2017-08-27 21:50:31 +00:00
|
|
|
},
|
2015-02-06 22:49:04 +00:00
|
|
|
set : function(releaseTime){
|
|
|
|
this._follower.release = releaseTime;
|
|
|
|
}
|
|
|
|
});
|
2014-07-23 19:50:45 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-27 21:50:31 +00:00
|
|
|
* Clean up.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Gate} this
|
2014-07-23 19:50:45 +00:00
|
|
|
*/
|
|
|
|
Tone.Gate.prototype.dispose = function(){
|
2017-08-27 21:50:31 +00:00
|
|
|
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;
|
2017-08-27 21:50:31 +00:00
|
|
|
});
|