Connections
source.connect(sink);connect is used to specify how audio data should flow from one node to the next. It’s analogous to “connecting a cable” from the output of one thing to the input of another. You will see the method used all over Tone.js and the Web Audio API.
Connections are directional
Section titled “Connections are directional”Signal flows from the output of the connect-er node (aka “source”) to the input of the connect-ee (“sink”). For example, this code snippet: source.connect(sink) could be visualized like this:
One output can feed many inputs
Section titled “One output can feed many inputs”Connecting a node more than once doesn’t replace its earlier connections, it adds to them. Each destination gets its own copy of the signal:
a.connect(b);a.connect(c);Many outputs can feed one input
Section titled “Many outputs can feed one input”When several nodes connect to the same input, their signals are added together. This is how sounds are mixed:
a.connect(c);b.connect(c);Connections are not queryable
Section titled “Connections are not queryable”The connection is made at a low level within the underlying API. Neither Tone.js nor the Web Audio API explicitly retain connection data so there is no query mechanism enabling you to determine something like “Is A connected to B?”.
You can connect different inputs and outputs
Section titled “You can connect different inputs and outputs”For nodes that have multiple inputs or outputs, connect can accept two additional arguments: the output number of the connect-er and the input number of the connect-ee.
To connect the left (0th) output of a splitter node to the right (1st) input of a merger node: split.connect(merge, 0, 1).
Convenience methods
Section titled “Convenience methods”Tone.js gives you a few convenience methods for connecting up nodes.
Use chain to connect a group of nodes in series.
source.chain(filter, pan, volume, Tone.getDestination());fan connects the output of a node to the inputs of all of the arguments:
source.fan(reverb, chorus);toDestination
Section titled “toDestination”toDestination is shorthand for connecting a node to the Destination (the speakers). It returns the node so it can be chained onto the constructor:
const synth = new Tone.Synth().toDestination();Disconnecting
Section titled “Disconnecting”disconnect removes connections. With no arguments it removes all of a node’s outgoing connections, or pass a node to remove just that one:
source.disconnect(reverb);