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
|
|
|
/**
|
2015-06-19 04:52:04 +00:00
|
|
|
* @class Route a single input to the specified output.
|
2014-07-22 17:39:53 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-12-01 04:26:06 +00:00
|
|
|
* @extends {Tone.SignalBase}
|
2014-12-02 06:42:08 +00:00
|
|
|
* @param {number} [outputCount=2] the number of inputs the switch accepts
|
2015-02-27 18:40:35 +00:00
|
|
|
* @example
|
2015-06-14 05:17:09 +00:00
|
|
|
* var route = new Tone.Route(4);
|
|
|
|
* var signal = new Tone.Signal(3).connect(route);
|
2015-06-19 04:52:04 +00:00
|
|
|
* route.select(0);
|
2015-06-14 05:17:09 +00:00
|
|
|
* //signal is routed through output 0
|
2015-06-19 04:52:04 +00:00
|
|
|
* route.select(3);
|
2015-06-14 05:17:09 +00:00
|
|
|
* //signal is now routed through output 3
|
2014-07-22 17:39:53 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2015-06-19 04:52:04 +00:00
|
|
|
* The control signal.
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Number}
|
|
|
|
* @signal
|
2014-07-22 17:39:53 +00:00
|
|
|
*/
|
|
|
|
this.gate = new Tone.Signal(0);
|
2015-04-05 18:25:01 +00:00
|
|
|
this._readOnly("gate");
|
2014-07-22 17:39:53 +00:00
|
|
|
|
|
|
|
//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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-01 04:26:06 +00:00
|
|
|
Tone.extend(Tone.Route, Tone.SignalBase);
|
2014-07-22 17:39:53 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-19 04:52:04 +00:00
|
|
|
* 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 {Time} [time=now] The time when the switch will open.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Route} this
|
2014-07-22 17:39:53 +00:00
|
|
|
*/
|
|
|
|
Tone.Route.prototype.select = function(which, time){
|
|
|
|
//make sure it's an integer
|
|
|
|
which = Math.floor(which);
|
|
|
|
this.gate.setValueAtTime(which, this.toSeconds(time));
|
2015-02-02 03:56:33 +00:00
|
|
|
return this;
|
2014-07-22 17:39:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-19 04:52:04 +00:00
|
|
|
* Clean up.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @returns {Tone.Route} this
|
2014-07-22 17:39:53 +00:00
|
|
|
*/
|
|
|
|
Tone.Route.prototype.dispose = function(){
|
2015-04-05 18:35:32 +00:00
|
|
|
this._writable("gate");
|
2014-07-22 17:39:53 +00:00
|
|
|
this.gate.dispose();
|
2015-04-05 18:35:32 +00:00
|
|
|
this.gate = null;
|
2014-07-22 17:39:53 +00:00
|
|
|
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);
|
2015-02-02 03:56:33 +00:00
|
|
|
return this;
|
2014-07-22 17:39:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////START HELPER////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* helper class for Tone.Route representing a single gate
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
2015-02-25 21:19:43 +00:00
|
|
|
* @private
|
2014-07-22 17:39:53 +00:00
|
|
|
*/
|
|
|
|
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.selecter = null;
|
2015-04-05 18:25:01 +00:00
|
|
|
this.gate.disconnect();
|
2014-07-22 17:39:53 +00:00
|
|
|
this.gate = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////END HELPER////////////
|
|
|
|
|
|
|
|
//return Tone.Route
|
|
|
|
return Tone.Route;
|
|
|
|
});
|