Tone.js/Tone/signal/Select.js

122 lines
2.8 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/signal/Equal", "Tone/signal/Signal"], function(Tone){
2014-07-20 22:18:43 +00:00
"use strict";
2014-07-20 22:18:43 +00:00
/**
2014-09-08 01:42:31 +00:00
* @class Select between any number of inputs, sending the one
2014-07-22 16:47:28 +00:00
* selected by the gate signal to the output
2014-07-20 22:18:43 +00:00
*
* @constructor
* @extends {Tone.SignalBase}
2014-12-03 22:20:23 +00:00
* @param {number} [sourceCount=2] the number of inputs the switch accepts
2015-02-27 18:40:35 +00:00
* @example
* var sel = new Tone.Select(2);
* var sigA = new Tone.Signal(10).connect(sel, 0, 0);
* var sigB = new Tone.Signal(20).connect(sel, 0, 1);
* sel.gate.value = 0;
* //sel outputs 10 (the value of sigA);
* sel.gate.value = 1;
* //sel outputs 20 (the value of sigB);
2014-07-20 22:18:43 +00:00
*/
2014-09-08 01:42:31 +00:00
Tone.Select = function(sourceCount){
2014-07-22 16:47:28 +00:00
sourceCount = this.defaultArg(sourceCount, 2);
2014-07-20 22:18:43 +00:00
Tone.call(this, sourceCount, 1);
2014-07-20 22:18:43 +00:00
/**
2014-07-22 16:47:28 +00:00
* the control signal
2015-06-13 23:50:39 +00:00
* @type {Number}
* @signal
2014-07-20 22:18:43 +00:00
*/
this.gate = new Tone.Signal(0);
this._readOnly("gate");
2014-07-20 22:18:43 +00:00
2014-07-22 16:47:28 +00:00
//make all the inputs and connect them
for (var i = 0; i < sourceCount; i++){
2014-09-08 01:42:31 +00:00
var switchGate = new SelectGate(i);
2014-07-22 16:47:28 +00:00
this.input[i] = switchGate;
this.gate.connect(switchGate.selecter);
switchGate.connect(this.output);
}
2014-07-20 22:18:43 +00:00
};
Tone.extend(Tone.Select, Tone.SignalBase);
2014-07-20 22:18:43 +00:00
/**
* open one of the inputs and close the other
2015-02-27 18:40:35 +00:00
* @param {number} which open one of the gates (closes the other)
2015-06-14 00:20:36 +00:00
* @param {Time=} time the time when the switch will open
* @returns {Tone.Select} this
2015-02-27 18:40:35 +00:00
* @example
* //open input 1 in a half second from now
* sel.select(1, "+0.5");
2014-07-20 22:18:43 +00:00
*/
2014-09-08 01:42:31 +00:00
Tone.Select.prototype.select = function(which, time){
2014-07-22 16:47:28 +00:00
//make sure it's an integer
which = Math.floor(which);
2014-07-20 22:18:43 +00:00
this.gate.setValueAtTime(which, this.toSeconds(time));
2015-02-02 03:56:33 +00:00
return this;
2014-07-20 22:18:43 +00:00
};
/**
* dispose method
* @returns {Tone.Select} this
2014-07-20 22:18:43 +00:00
*/
2014-09-08 01:42:31 +00:00
Tone.Select.prototype.dispose = function(){
this._writable("gate");
2014-07-20 22:18:43 +00:00
this.gate.dispose();
this.gate = null;
2014-07-22 16:47:28 +00:00
for (var i = 0; i < this.input.length; i++){
this.input[i].dispose();
this.input[i] = null;
}
Tone.prototype.dispose.call(this);
2015-02-02 03:56:33 +00:00
return this;
2014-07-20 22:18:43 +00:00
};
2014-07-22 16:47:28 +00:00
////////////START HELPER////////////
/**
2014-09-08 01:42:31 +00:00
* helper class for Tone.Select representing a single gate
2014-07-22 16:47:28 +00:00
* @constructor
* @extends {Tone}
* @private
2014-07-22 16:47:28 +00:00
*/
2014-09-08 01:42:31 +00:00
var SelectGate = function(num){
2014-07-22 16:47:28 +00:00
/**
* the selector
* @type {Tone.Equal}
*/
this.selecter = new Tone.Equal(num);
/**
* the gate
* @type {GainNode}
*/
this.gate = this.input = this.output = this.context.createGain();
//connect the selecter to the gate gain
this.selecter.connect(this.gate.gain);
};
2014-09-08 01:42:31 +00:00
Tone.extend(SelectGate);
2014-07-22 16:47:28 +00:00
/**
* clean up
* @private
*/
2014-09-08 01:42:31 +00:00
SelectGate.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
2014-07-22 16:47:28 +00:00
this.selecter.dispose();
this.gate.disconnect();
this.selecter = null;
this.gate = null;
};
////////////END HELPER////////////
2014-09-08 01:42:31 +00:00
//return Tone.Select
return Tone.Select;
2014-07-20 22:18:43 +00:00
});