diff --git a/Tone/core/context/ToneAudioNode.test.ts b/Tone/core/context/ToneAudioNode.test.ts index 02880753..95cb6610 100644 --- a/Tone/core/context/ToneAudioNode.test.ts +++ b/Tone/core/context/ToneAudioNode.test.ts @@ -3,7 +3,7 @@ import { Merge } from "Tone/component"; import { Split } from "Tone/component/channel/Split"; import { Oscillator } from "Tone/source"; import { Gain } from "./Gain"; -import { connect, disconnect } from "./ToneAudioNode"; +import { connect, disconnect, fanIn } from "./ToneAudioNode"; import { PassAudio } from "test/helper/PassAudio"; import { Offline } from "test/helper/Offline"; @@ -218,6 +218,16 @@ describe("ToneAudioNode", () => { disconnect(input); }, false); }); + + it("can fan in multiple nodes to a destination", () => { + return PassAudio(input => { + const context = input.context; + const gain0 = context.createGain(); + const gain1 = context.createGain(); + const output = context.destination; + fanIn(gain0, gain1, input, output); + }); + }); it("can connect one channel to another", () => { return PassAudio(input => { @@ -320,6 +330,16 @@ describe("ToneAudioNode", () => { disconnect(gain, output); }); }); + + it("can fan in multiple nodes to a destination", async () => { + await Offline(() => { + const output = new Gain(); + const input0 = new Gain(); + const input1 = new Gain(); + const input2 = new Gain(); + fanIn(input0, input1, input2, output); + }); + }); }); }); diff --git a/Tone/core/context/ToneAudioNode.ts b/Tone/core/context/ToneAudioNode.ts index da7a1727..f5fa069b 100644 --- a/Tone/core/context/ToneAudioNode.ts +++ b/Tone/core/context/ToneAudioNode.ts @@ -376,3 +376,21 @@ export function disconnect( srcNode.disconnect(); } } + +/** + * Connect the output of one or more source nodes to a single destination node + * @param nodes One or more source nodes followed by one destination node + * @example + * const player = new Tone.Player("https://tonejs.github.io/audio/drum-samples/conga-rhythm.mp3"); + * const player1 = new Tone.Player("https://tonejs.github.io/audio/drum-samples/conga-rhythm.mp3"); + * const filter = new Tone.Filter("G5").toDestination(); + * // connect nodes to a common destination + * fanIn(player, player1, filter); + */ +export function fanIn(...nodes: OutputNode[]): void { + const dstNode = nodes.pop(); + + if (isDefined(dstNode)) { + nodes.forEach(node => connect(node, dstNode)); + } +}