Tone.js/Tone/core/Bus.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/core/Gain"], function(Tone){
2014-04-16 04:23:58 +00:00
"use strict";
2014-06-15 22:32:49 +00:00
/**
* buses are another way of routing audio
*
* augments Tone.prototype to include send and recieve
*/
2017-10-21 23:02:46 +00:00
/**
* All of the routes
*
* @type {Object}
* @static
* @private
*/
2014-06-15 22:32:49 +00:00
var Buses = {};
2014-04-16 04:23:58 +00:00
2014-06-15 22:32:49 +00:00
/**
2017-10-21 23:02:46 +00:00
* Send this signal to the channel name.
* @param {String} channelName A named channel to send the signal to.
2017-10-21 23:02:46 +00:00
* @param {Decibels} amount The amount of the source to send to the bus.
* @return {GainNode} The gain node which connects this node to the desired channel.
2015-06-14 00:52:51 +00:00
* Can be used to adjust the levels of the send.
2015-06-14 05:24:12 +00:00
* @example
* source.send("reverb", -12);
2014-06-15 22:32:49 +00:00
*/
2014-04-16 04:23:58 +00:00
Tone.prototype.send = function(channelName, amount){
if (!Buses.hasOwnProperty(channelName)){
Buses[channelName] = this.context.createGain();
}
2017-04-30 19:03:49 +00:00
amount = Tone.defaultArg(amount, 0);
var sendKnob = new Tone.Gain(amount, Tone.Type.Decibels);
this.connect(sendKnob);
sendKnob.connect(Buses[channelName]);
2017-10-21 23:02:46 +00:00
return sendKnob;
2014-06-15 22:32:49 +00:00
};
2014-04-16 04:23:58 +00:00
2014-06-15 22:32:49 +00:00
/**
2015-06-14 00:52:51 +00:00
* Recieve the input from the desired channelName to the input
2014-06-19 17:38:21 +00:00
*
* @param {String} channelName A named channel to send the signal to.
* @param {Number=} channelNumber The channel to connect to
2015-06-14 03:56:32 +00:00
* @returns {Tone} this
2015-06-14 05:24:12 +00:00
* @example
* reverbEffect.receive("reverb");
2014-06-15 22:32:49 +00:00
*/
Tone.prototype.receive = function(channelName, inputNum){
2014-04-16 04:23:58 +00:00
if (!Buses.hasOwnProperty(channelName)){
2017-10-21 23:02:46 +00:00
Buses[channelName] = this.context.createGain();
2014-04-16 04:23:58 +00:00
}
Buses[channelName].connect(this, 0, inputNum);
2015-01-06 04:33:05 +00:00
return this;
2014-06-15 22:32:49 +00:00
};
//remove all the send/receives when a new audio context is passed in
2017-02-19 16:50:53 +00:00
Tone.Context.on("init", function(context){
if (context.Buses){
Buses = context.Buses;
} else {
Buses = {};
context.Buses = Buses;
}
});
return Tone;
2017-10-21 23:02:46 +00:00
});