@@ -5,6 +5,7 @@ import { Solo } from "./Solo";
5
5
import { PanVol } from "./PanVol" ;
6
6
import { Param } from "../../core/context/Param" ;
7
7
import { readOnly } from "../../core/util/Interface" ;
8
+ import { Gain } from "../../core/context/Gain" ;
8
9
9
10
export interface ChannelOptions extends ToneAudioNodeOptions {
10
11
pan : AudioRange ;
@@ -112,6 +113,54 @@ export class Channel extends ToneAudioNode<ChannelOptions> {
112
113
this . _panVol . mute = mute ;
113
114
}
114
115
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
+
115
164
dispose ( ) : this {
116
165
super . dispose ( ) ;
117
166
this . _panVol . dispose ( ) ;
0 commit comments