Tone.js/Tone/core/Bus.js

67 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
*/
/**
* All of the routes
*
* @type {Object}
2014-07-30 17:55:36 +00:00
* @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
/**
2015-06-14 00:52:51 +00:00
* Send this signal to the channel name.
2015-06-14 05:24:12 +00:00
* @param {string} channelName A named channel to send the signal to.
2015-06-14 00:52:51 +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.
* 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();
}
amount = this.defaultArg(amount, 0);
var sendKnob = new Tone.Gain(amount, Tone.Type.Decibels);
2014-12-01 02:32:09 +00:00
this.output.chain(sendKnob, Buses[channelName]);
2014-04-16 04:23:58 +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
*
2015-06-14 05:24:12 +00:00
* @param {string} channelName A named channel to send the signal to.
2015-06-14 00:52:51 +00:00
* @param {AudioNode} [input] If no input is selected, the
2014-09-02 16:09:16 +00:00
* input of the current node is
* chosen.
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
*/
2014-09-02 16:09:16 +00:00
Tone.prototype.receive = function(channelName, input){
2014-04-16 04:23:58 +00:00
if (!Buses.hasOwnProperty(channelName)){
Buses[channelName] = this.context.createGain();
}
2014-12-08 05:44:40 +00:00
if (this.isUndef(input)){
input = this.input;
}
2014-09-02 16:09:16 +00:00
Buses[channelName].connect(input);
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
Tone._initAudioContext(function(){
Buses = {};
});
return Tone;
2014-04-16 04:23:58 +00:00
});