Skip to content

Commit 703f27a

Browse files
committed
feat: adding send/receive to Channel
Removing it from all ToneAudioNode's. Now it's just on the Channel Interface
1 parent 28c078d commit 703f27a

File tree

3 files changed

+61
-67
lines changed

3 files changed

+61
-67
lines changed

Tone/component/channel/Bus.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

Tone/component/channel/Channel.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,17 @@ describe("Channel", () => {
6161
channelA.dispose();
6262
channelB.dispose();
6363
});
64+
65+
describe("bus", () => {
66+
it("can connect two channels together by name", () => {
67+
return PassAudio(input => {
68+
const sendChannel = new Channel();
69+
input.connect(sendChannel);
70+
sendChannel.send("test");
71+
const recvChannel = new Channel().toDestination();
72+
recvChannel.receive("test");
73+
});
74+
});
75+
});
6476
});
6577
});

Tone/component/channel/Channel.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Solo } from "./Solo";
55
import { PanVol } from "./PanVol";
66
import { Param } from "../../core/context/Param";
77
import { readOnly } from "../../core/util/Interface";
8+
import { Gain } from "../../core/context/Gain";
89

910
export interface ChannelOptions extends ToneAudioNodeOptions {
1011
pan: AudioRange;
@@ -112,6 +113,54 @@ export class Channel extends ToneAudioNode<ChannelOptions> {
112113
this._panVol.mute = mute;
113114
}
114115

116+
/**
117+
* Store the send/receive channels by name.
118+
*/
119+
private static buses: Map<string, Gain> = new Map();
120+
121+
/**
122+
* Get the gain node belonging to the bus name. Create it if
123+
* it doesn't exist
124+
* @param name The bus name
125+
*/
126+
private _getBus(name: string): Gain {
127+
if (!Channel.buses.has(name)) {
128+
Channel.buses.set(name, new Gain({ context: this.context }));
129+
}
130+
return Channel.buses.get(name) as Gain;
131+
}
132+
133+
/**
134+
* Send audio to another channel using a string. `send` is a lot like
135+
* [[connect]], except it uses a string instead of an object. This can
136+
* be useful in large applications to decouple sections since [[send]]
137+
* and [[receive]] can be invoked separately in order to connect an object
138+
* @param name The channel name to send the audio
139+
* @param volume The amount of the signal to send.
140+
* Defaults to 0db, i.e. send the entire signal
141+
* @returns Returns the gain node of this connection.
142+
*/
143+
send(name: string, volume: Decibels = 0): Gain<"decibels"> {
144+
const bus = this._getBus(name);
145+
const sendKnob = new Gain({
146+
context: this.context,
147+
units: "decibels",
148+
gain: volume,
149+
});
150+
this.connect(sendKnob);
151+
sendKnob.connect(bus);
152+
return sendKnob;
153+
}
154+
155+
/**
156+
* Receive audio from a channel which was connected with [[send]].
157+
* @param name The channel name to receive audio from.
158+
*/
159+
receive(name: string) {
160+
const bus = this._getBus(name);
161+
bus.connect(this);
162+
}
163+
115164
dispose(): this {
116165
super.dispose();
117166
this._panVol.dispose();

0 commit comments

Comments
 (0)