Tone.js/Tone/signal/Switch.js

99 lines
2.4 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}
2015-06-20 19:50:57 +00:00
* @param {Boolean} [open=false] If the gate is initially open or closed.
2015-02-27 18:40:35 +00:00
* @example
2015-06-14 05:17:09 +00:00
* 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
*/
2015-06-20 19:50:57 +00:00
Tone.Switch = function(open){
open = this.defaultArg(open, false);
2014-07-23 19:47:46 +00:00
Tone.call(this);
/**
2015-06-20 19:50:57 +00:00
* The control signal for the switch.
* When this value is 0, the input signal will NOT pass through,
2014-07-23 19:47:46 +00:00
* when it is high (1), the input signal will pass through.
*
2015-06-13 23:50:39 +00:00
* @type {Number}
* @signal
2014-07-23 19:47:46 +00:00
*/
this.gate = new Tone.Signal(0);
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);
2015-06-20 19:50:57 +00:00
//initially open
if (open){
this.open();
}
2014-07-23 19:47:46 +00:00
};
Tone.extend(Tone.Switch, Tone.SignalBase);
2014-07-23 19:47:46 +00:00
/**
2015-06-19 04:52:04 +00:00
* Open the switch at a specific time.
2014-07-23 19:47:46 +00:00
*
2015-06-19 04:52:04 +00:00
* @param {Time} [time=now] The time when the switch will be open.
* @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
};
/**
2015-06-19 04:52:04 +00:00
* Close the switch at a specific time.
2014-07-23 19:47:46 +00:00
*
2015-06-19 04:52:04 +00:00
* @param {Time} [time=now] The time when the switch will be closed.
* @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
};
/**
2015-06-19 04:52:04 +00:00
* Clean up.
* @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);
this._writable("gate");
2014-07-23 19:47:46 +00:00
this.gate.dispose();
this.gate = null;
2014-07-23 19:47:46 +00:00
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;
});