Tone.js/Tone/signal/Switch.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/signal/SignalBase", "Tone/signal/GreaterThan"], function(Tone){
2014-07-23 19:47:46 +00:00
"use strict";
2014-07-23 19:47:46 +00:00
/**
* @class When the gate is set to 0, the input signal does not pass through to the output.
* If the gate is set to 1, the input signal passes through.
* the gate is initially closed.
*
* @constructor
* @extends {Tone.SignalBase}
2014-07-23 19:47:46 +00:00
*/
Tone.Switch = function(){
Tone.call(this);
/**
* the control signal for the switch
* when this value is 0, the input signal will not pass through,
* when it is high (1), the input signal will pass through.
*
* @type {Tone.Signal}
*/
this.gate = new Tone.Signal(0);
/**
2014-10-03 17:07:53 +00:00
* thresh the control signal to either 0 or 1
* @type {Tone.GreaterThan}
2014-07-23 19:47:46 +00:00
* @private
*/
2014-10-03 17:07:53 +00:00
this._thresh = new Tone.GreaterThan(0.5);
2014-07-23 19:47:46 +00:00
this.input.connect(this.output);
2014-12-01 02:32:09 +00:00
this.gate.chain(this._thresh, this.output.gain);
2014-07-23 19:47:46 +00:00
};
Tone.extend(Tone.Switch, Tone.SignalBase);
2014-07-23 19:47:46 +00:00
/**
* open the switch at a specific time
*
* @param {Tone.Time} time the time when the switch will be open
2015-02-02 03:56:33 +00:00
* @returns {Tone.Switch} `this`
2014-07-23 19:47:46 +00:00
*/
Tone.Switch.prototype.open = function(time){
this.gate.setValueAtTime(1, this.toSeconds(time));
2015-02-02 03:56:33 +00:00
return this;
2014-07-23 19:47:46 +00:00
};
/**
* close the switch at a specific time
*
* @param {Tone.Time} time the time when the switch will be open
2015-02-02 03:56:33 +00:00
* @returns {Tone.Switch} `this`
2014-07-23 19:47:46 +00:00
*/
Tone.Switch.prototype.close = function(time){
this.gate.setValueAtTime(0, this.toSeconds(time));
2015-02-02 03:56:33 +00:00
return this;
2014-07-23 19:47:46 +00:00
};
/**
* clean up
2015-02-02 03:56:33 +00:00
* @returns {Tone.Switch} `this`
2014-07-23 19:47:46 +00:00
*/
Tone.Switch.prototype.dispose = function(){
2014-08-24 19:47:59 +00:00
Tone.prototype.dispose.call(this);
2014-07-23 19:47:46 +00:00
this.gate.dispose();
this._thresh.dispose();
this.gate = null;
2014-07-23 19:47:46 +00:00
this._thresh = null;
2015-02-02 03:56:33 +00:00
return this;
2014-07-23 19:47:46 +00:00
};
return Tone.Switch;
});