2014-12-01 04:26:06 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/SignalBase", "Tone/signal/GreaterThan"], function(Tone){
|
2014-07-23 19:47:46 +00:00
|
|
|
|
2014-09-04 04:41:40 +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
|
2014-12-01 04:26:06 +00:00
|
|
|
* @extends {Tone.SignalBase}
|
2015-02-27 18:40:35 +00:00
|
|
|
* @example
|
|
|
|
* var sigSwitch = new Tone.Switch();
|
|
|
|
* var signal = new Tone.Signal(2).connect(sigSwitch);
|
|
|
|
* //initially no output from sigSwitch
|
|
|
|
* sigSwitch.gate.value = 1;
|
|
|
|
* //open the switch and allow the signal through
|
|
|
|
* //the output of sigSwitch is now 2.
|
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);
|
2015-04-05 18:25:01 +00:00
|
|
|
this._readOnly("gate");
|
2014-07-23 19:47:46 +00:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
};
|
|
|
|
|
2014-12-01 04:26:06 +00:00
|
|
|
Tone.extend(Tone.Switch, Tone.SignalBase);
|
2014-07-23 19:47:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* open the switch at a specific time
|
|
|
|
*
|
2015-02-27 18:40:35 +00:00
|
|
|
* @param {Tone.Time=} time the time when the switch will be open
|
2015-02-02 03:56:33 +00:00
|
|
|
* @returns {Tone.Switch} `this`
|
2015-02-27 18:40:35 +00:00
|
|
|
* @example
|
|
|
|
* //open the switch to let the signal through
|
|
|
|
* sigSwitch.open();
|
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`
|
2015-02-27 18:40:35 +00:00
|
|
|
* @example
|
|
|
|
* //close the switch a half second from now
|
|
|
|
* sigSwitch.close("+0.5");
|
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._thresh = null;
|
2015-02-02 03:56:33 +00:00
|
|
|
return this;
|
2014-07-23 19:47:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Switch;
|
|
|
|
});
|