2014-07-23 19:47:00 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/signal/Equal", "Tone/signal/Signal"], function(Tone){
|
2014-07-22 17:39:53 +00:00
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-07-22 17:39:53 +00:00
|
|
|
/**
|
|
|
|
* @class Route a single input to the specified output
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
|
|
|
* @param {number=} [outputCount=2] the number of inputs the switch accepts
|
|
|
|
*/
|
|
|
|
Tone.Route = function(outputCount){
|
|
|
|
|
|
|
|
outputCount = this.defaultArg(outputCount, 2);
|
2014-11-04 06:21:42 +00:00
|
|
|
Tone.call(this, 1, outputCount);
|
2014-07-22 17:39:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the control signal
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
|
|
|
this.gate = new Tone.Signal(0);
|
|
|
|
|
|
|
|
//make all the inputs and connect them
|
|
|
|
for (var i = 0; i < outputCount; i++){
|
|
|
|
var routeGate = new RouteGate(i);
|
|
|
|
this.output[i] = routeGate;
|
|
|
|
this.gate.connect(routeGate.selecter);
|
|
|
|
this.input.connect(routeGate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Route);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* routes the signal to one of the outputs and close the others
|
|
|
|
* @param {number=} [which=0] open one of the gates (closes the other)
|
|
|
|
* @param {Tone.Time} time the time when the switch will open
|
|
|
|
*/
|
|
|
|
Tone.Route.prototype.select = function(which, time){
|
|
|
|
//make sure it's an integer
|
|
|
|
which = Math.floor(which);
|
|
|
|
this.gate.setValueAtTime(which, this.toSeconds(time));
|
|
|
|
};
|
|
|
|
|
2014-08-24 19:47:59 +00:00
|
|
|
/**
|
|
|
|
* borrows the method from {@link Tone.Signal}
|
|
|
|
*
|
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
Tone.Route.prototype.connect = Tone.Signal.prototype.connect;
|
|
|
|
|
2014-07-22 17:39:53 +00:00
|
|
|
/**
|
|
|
|
* dispose method
|
|
|
|
*/
|
|
|
|
Tone.Route.prototype.dispose = function(){
|
|
|
|
this.gate.dispose();
|
|
|
|
for (var i = 0; i < this.output.length; i++){
|
|
|
|
this.output[i].dispose();
|
|
|
|
this.output[i] = null;
|
|
|
|
}
|
2014-09-06 22:55:11 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2014-07-22 17:39:53 +00:00
|
|
|
this.gate = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////START HELPER////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* helper class for Tone.Route representing a single gate
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
|
|
|
* @internal only used by Tone.Route
|
|
|
|
*/
|
|
|
|
var RouteGate = function(num){
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(RouteGate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
RouteGate.prototype.dispose = function(){
|
2014-09-06 22:55:11 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2014-07-22 17:39:53 +00:00
|
|
|
this.selecter.dispose();
|
|
|
|
this.gate.disconnect();
|
|
|
|
this.selecter = null;
|
|
|
|
this.gate = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////END HELPER////////////
|
|
|
|
|
|
|
|
//return Tone.Route
|
|
|
|
return Tone.Route;
|
|
|
|
});
|