2014-07-23 19:47:00 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/Equal", "Tone/signal/Signal"], function(Tone){
|
2014-07-20 22:18:43 +00:00
|
|
|
|
2014-09-04 04:41:40 +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}
|
2014-07-22 16:47:28 +00:00
|
|
|
* @param {number=} [sourceCount=2] the number of inputs the switch accepts
|
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
|
|
|
|
2014-11-04 06:21:42 +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
|
|
|
|
* @type {Tone.Signal}
|
2014-07-20 22:18:43 +00:00
|
|
|
*/
|
|
|
|
this.gate = new Tone.Signal(0);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2014-09-08 01:42:31 +00:00
|
|
|
Tone.extend(Tone.Select);
|
2014-07-20 22:18:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* open one of the inputs and close the other
|
|
|
|
* @param {number=} [which=0] open one of the gates (closes the other)
|
|
|
|
* @param {Tone.Time} time the time when the switch will open
|
|
|
|
*/
|
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));
|
|
|
|
};
|
|
|
|
|
2014-08-24 19:47:59 +00:00
|
|
|
/**
|
|
|
|
* borrows the method from {@link Tone.Signal}
|
|
|
|
*
|
|
|
|
* @function
|
|
|
|
*/
|
2014-09-08 01:42:31 +00:00
|
|
|
Tone.Select.prototype.connect = Tone.Signal.prototype.connect;
|
2014-08-24 19:47:59 +00:00
|
|
|
|
2014-07-20 22:18:43 +00:00
|
|
|
/**
|
|
|
|
* dispose method
|
|
|
|
*/
|
2014-09-08 01:42:31 +00:00
|
|
|
Tone.Select.prototype.dispose = function(){
|
2014-07-20 22:18:43 +00:00
|
|
|
this.gate.dispose();
|
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;
|
|
|
|
}
|
2014-09-06 22:55:11 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2014-07-20 22:18:43 +00:00
|
|
|
this.gate = null;
|
|
|
|
};
|
|
|
|
|
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}
|
2014-09-08 01:42:31 +00:00
|
|
|
* @internal only used by Tone.Select
|
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(){
|
2014-09-06 22:55:11 +00:00
|
|
|
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
|
|
|
});
|