-
Notifications
You must be signed in to change notification settings - Fork 36
/
audio-node.ts
284 lines (220 loc) · 10.1 KB
/
audio-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { Injector } from '@angular/core';
import { EventTarget } from '../event-target';
import { INDEX_SIZE_ERROR_FACTORY_PROVIDER } from '../factories/index-size-error';
import { INVALID_ACCES_ERROR_FACTORY_PROVIDER, InvalidAccessErrorFactory } from '../factories/invalid-access-error';
import {
AUDIO_NODE_RENDERER_DESTINATIONS_STORE,
AUDIO_NODE_RENDERER_STORE,
AUDIO_NODE_STORE,
AUDIO_PARAM_RENDERER_STORE,
AUDIO_PARAM_STORE,
CONTEXT_STORE
} from '../globals';
import { isAudioNode } from '../guards/audio-node';
import { cacheTestResult } from '../helpers/cache-test-result';
import { getNativeContext } from '../helpers/get-native-context';
import { isOfflineAudioContext } from '../helpers/is-offline-audio-context';
import { IAudioNode, IAudioParam, IMinimalBaseAudioContext, INativeAudioNodeFaker } from '../interfaces';
import { DISCONNECTING_SUPPORT_TESTER_PROVIDER, DisconnectingSupportTester } from '../support-testers/disconnecting';
import { TNativeAudioNode, TNativeAudioParam, TUnpatchedAudioContext } from '../types';
import { AUDIO_NODE_DISCONNECT_METHOD_WRAPPER_PROVIDER, AudioNodeDisconnectMethodWrapper } from '../wrappers/audio-node-disconnect-method';
const injector = Injector.create({
providers: [
AUDIO_NODE_DISCONNECT_METHOD_WRAPPER_PROVIDER,
DISCONNECTING_SUPPORT_TESTER_PROVIDER,
INDEX_SIZE_ERROR_FACTORY_PROVIDER,
INVALID_ACCES_ERROR_FACTORY_PROVIDER
]
});
const audioNodeDisconnectMethodWrapper = injector.get(AudioNodeDisconnectMethodWrapper);
const disconnectingSupportTester = injector.get(DisconnectingSupportTester);
const invalidAccessErrorFactory = injector.get(InvalidAccessErrorFactory);
export class AudioNode<T extends INativeAudioNodeFaker | TNativeAudioNode> extends EventTarget implements IAudioNode {
protected _nativeNode: T;
private _channelCount: number;
private _context: IMinimalBaseAudioContext;
constructor (
context: IMinimalBaseAudioContext,
nativeNode: T,
channelCount: number,
// @todo The parentNode property is only needed as long as the source gets transpiled to ES5.
parentNode?: IAudioNode
) {
super();
this._channelCount = channelCount;
this._context = context;
this._nativeNode = nativeNode;
const nativeContext = getNativeContext(context);
// Bug #12: Firefox and Safari do not support to disconnect a specific destination.
// @todo Make sure this is not used with an OfflineAudioContext.
if (!isOfflineAudioContext(nativeContext) && true !== cacheTestResult(DisconnectingSupportTester, () => {
return disconnectingSupportTester.test(<TUnpatchedAudioContext> nativeContext);
})) {
audioNodeDisconnectMethodWrapper.wrap(nativeNode);
}
AUDIO_NODE_STORE.set((parentNode === undefined) ? this : parentNode, nativeNode);
}
public get channelCount () {
// Bug #47: The AudioDestinationNode in Edge and Safari do not initialize the maxChannelCount property correctly.
return ((<TNativeAudioNode> this._nativeNode) === this._nativeNode.context.destination) ?
this._channelCount :
this._nativeNode.channelCount;
}
public set channelCount (value) {
// Bug #47: The AudioDestinationNode in Edge and Safari do not initialize the maxChannelCount property correctly.
if ((<TNativeAudioNode> this._nativeNode) === this._nativeNode.context.destination) {
this._channelCount = value;
} else {
this._nativeNode.channelCount = value;
}
}
public get channelCountMode () {
return this._nativeNode.channelCountMode;
}
public set channelCountMode (value) {
this._nativeNode.channelCountMode = value;
}
public get channelInterpretation () {
return this._nativeNode.channelInterpretation;
}
public set channelInterpretation (value) {
this._nativeNode.channelInterpretation = value;
}
public get context () {
return this._context;
}
public get numberOfInputs () {
return this._nativeNode.numberOfInputs;
}
public get numberOfOutputs () {
return this._nativeNode.numberOfOutputs;
}
public addEventListener (
type: string,
listener: EventListenerOrEventListenerObject | null = null,
options?: boolean | AddEventListenerOptions
): void {
return this._nativeNode.addEventListener(type, listener, options);
}
public connect (destinationNode: IAudioNode, output?: number, input?: number): IAudioNode;
public connect (destinationParam: IAudioParam, output?: number): void;
public connect (destination: IAudioNode | IAudioParam, output = 0, input = 0): void | IAudioNode {
const nativeContext = CONTEXT_STORE.get(this._context);
if (isAudioNode(destination)) {
// Bug #41: Only Chrome, Firefox and Opera throw the correct exception by now.
if (this._context !== destination.context) {
throw invalidAccessErrorFactory.create();
}
if (nativeContext === undefined) {
throw new Error('The native (Offline)AudioContext is missing.');
}
if (isOfflineAudioContext(nativeContext)) {
const renderer = AUDIO_NODE_RENDERER_STORE.get(destination);
if (renderer === undefined) {
throw invalidAccessErrorFactory.create();
}
const source = AUDIO_NODE_RENDERER_STORE.get(this);
if (source === undefined) {
throw new Error('The associated renderer is missing.');
}
renderer.wire(source, output, input);
return destination;
}
const nativeDestinationNode = AUDIO_NODE_STORE.get(destination);
if (nativeDestinationNode === undefined) {
throw new Error('The associated nativeNode is missing.');
}
if ((<INativeAudioNodeFaker> nativeDestinationNode).inputs !== undefined) {
const nativeInputDestinationNode = (<TNativeAudioNode[]> (<INativeAudioNodeFaker> nativeDestinationNode).inputs)[input];
this._nativeNode.connect(nativeInputDestinationNode, output, input);
} else {
this._nativeNode.connect(nativeDestinationNode, output, input);
}
// Bug #11: Safari does not support chaining yet. This can be tested with the ChainingSupportTester.
return destination;
}
if (nativeContext === undefined) {
throw new Error('The native (Offline)AudioContext is missing.');
}
if (isOfflineAudioContext(nativeContext)) {
const renderer = AUDIO_PARAM_RENDERER_STORE.get(destination);
if (renderer === undefined) {
throw invalidAccessErrorFactory.create();
}
const source = AUDIO_NODE_RENDERER_STORE.get(this);
if (source === undefined) {
throw new Error('The associated renderer is missing.');
}
const nativeAudioParam = AUDIO_PARAM_STORE.get(destination);
if (nativeAudioParam === undefined) {
throw new Error('The associated nativeAudioParam is missing.');
}
try {
// @todo This is only needed to throw possible errors.
this._nativeNode.connect(nativeAudioParam, output);
} catch (err) {
// Bug #58: Only Firefox does throw an InvalidStateError yet.
if (err.code === 12) {
throw invalidAccessErrorFactory.create();
}
throw err;
}
renderer.wire(source, output);
} else {
try {
this._nativeNode.connect(<TNativeAudioParam> (<any> destination), output);
} catch (err) {
// Bug #58: Only Firefox does throw an InvalidStateError yet.
if (err.code === 12) {
throw invalidAccessErrorFactory.create();
}
throw err;
}
}
}
public disconnect (destination?: IAudioNode): void {
const nativeContext = CONTEXT_STORE.get(this._context);
if (nativeContext === undefined) {
throw new Error('The native (Offline)AudioContext is missing.');
}
if (isOfflineAudioContext(nativeContext)) {
const source = AUDIO_NODE_RENDERER_STORE.get(this);
if (source === undefined) {
throw new Error('The associated renderer is missing.');
}
if (destination === undefined) {
const renderers = AUDIO_NODE_RENDERER_DESTINATIONS_STORE.get(source);
if (renderers === undefined) {
return;
}
return renderers.forEach((rndrr) => rndrr.unwire(source));
}
const renderer = AUDIO_NODE_RENDERER_STORE.get(destination);
if (renderer === undefined) {
throw new Error('The associated renderer is missing.');
}
return renderer.unwire(source);
}
if (destination === undefined) {
return this._nativeNode.disconnect();
}
const nativeDestinationNode = AUDIO_NODE_STORE.get(destination);
if (nativeDestinationNode === undefined) {
throw new Error('The associated nativeNode is missing.');
}
if ((<INativeAudioNodeFaker> nativeDestinationNode).inputs !== undefined) {
for (const input of (<TNativeAudioNode[]> (<INativeAudioNodeFaker> nativeDestinationNode).inputs)) {
this._nativeNode.disconnect(input);
}
return;
}
return this._nativeNode.disconnect(nativeDestinationNode);
}
public removeEventListener (
type: string,
listener: EventListenerOrEventListenerObject | null = null,
options?: EventListenerOptions | boolean
): void {
return this._nativeNode.removeEventListener(type, listener, options);
}
}