Skip to content

Commit

Permalink
feat: adding send/receive to Channel
Browse files Browse the repository at this point in the history
Removing it from all ToneAudioNode's. Now it's just on the Channel Interface
  • Loading branch information
tambien committed Oct 29, 2019
1 parent 28c078d commit 703f27a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 67 deletions.
67 changes: 0 additions & 67 deletions Tone/component/channel/Bus.js

This file was deleted.

12 changes: 12 additions & 0 deletions Tone/component/channel/Channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,17 @@ describe("Channel", () => {
channelA.dispose();
channelB.dispose();
});

describe("bus", () => {
it("can connect two channels together by name", () => {
return PassAudio(input => {
const sendChannel = new Channel();
input.connect(sendChannel);
sendChannel.send("test");
const recvChannel = new Channel().toDestination();
recvChannel.receive("test");
});
});
});
});
});
49 changes: 49 additions & 0 deletions Tone/component/channel/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Solo } from "./Solo";
import { PanVol } from "./PanVol";
import { Param } from "../../core/context/Param";
import { readOnly } from "../../core/util/Interface";
import { Gain } from "../../core/context/Gain";

export interface ChannelOptions extends ToneAudioNodeOptions {
pan: AudioRange;
Expand Down Expand Up @@ -112,6 +113,54 @@ export class Channel extends ToneAudioNode<ChannelOptions> {
this._panVol.mute = mute;
}

/**
* Store the send/receive channels by name.
*/
private static buses: Map<string, Gain> = new Map();

/**
* Get the gain node belonging to the bus name. Create it if
* it doesn't exist
* @param name The bus name
*/
private _getBus(name: string): Gain {
if (!Channel.buses.has(name)) {
Channel.buses.set(name, new Gain({ context: this.context }));
}
return Channel.buses.get(name) as Gain;
}

/**
* Send audio to another channel using a string. `send` is a lot like
* [[connect]], except it uses a string instead of an object. This can
* be useful in large applications to decouple sections since [[send]]
* and [[receive]] can be invoked separately in order to connect an object
* @param name The channel name to send the audio
* @param volume The amount of the signal to send.
* Defaults to 0db, i.e. send the entire signal
* @returns Returns the gain node of this connection.
*/
send(name: string, volume: Decibels = 0): Gain<"decibels"> {
const bus = this._getBus(name);
const sendKnob = new Gain({
context: this.context,
units: "decibels",
gain: volume,
});
this.connect(sendKnob);
sendKnob.connect(bus);
return sendKnob;
}

/**
* Receive audio from a channel which was connected with [[send]].
* @param name The channel name to receive audio from.
*/
receive(name: string) {
const bus = this._getBus(name);
bus.connect(this);
}

dispose(): this {
super.dispose();
this._panVol.dispose();
Expand Down

0 comments on commit 703f27a

Please sign in to comment.