-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathaudio-context-constructor.js
232 lines (174 loc) · 8.24 KB
/
audio-context-constructor.js
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
import { loadFixtureAsArrayBuffer } from '../../../helper/load-fixture';
describe('audioContextConstructor', () => {
let audioContext;
afterEach(() => audioContext.close());
beforeEach(() => {
audioContext = new AudioContext();
});
describe('destination', () => {
describe('numberOfOutputs', () => {
// bug #168
it('should be zero', () => {
expect(audioContext.destination.numberOfOutputs).to.equal(0);
});
});
});
describe('outputLatency', () => {
// bug #40
it('should not be implemented', () => {
expect(audioContext.outputLatency).to.be.undefined;
});
});
describe('close()', () => {
// bug #35
it('should not throw an error if it was closed before', () => {
return audioContext.close().then(() => audioContext.close());
});
});
describe('createBufferSource()', () => {
describe('stop()', () => {
// bug #44
it('should throw a DOMException when called with a negavtive value', () => {
const audioBufferSourceNode = audioContext.createBufferSource();
expect(() => audioBufferSourceNode.stop(-1))
.to.throw(DOMException)
.with.property('name', 'InvalidStateError');
});
});
});
describe('createConvolver()', () => {
let convolverNode;
beforeEach(() => {
convolverNode = audioContext.createConvolver();
});
describe('buffer', () => {
// bug #115
it('should not allow to assign the buffer to null', () => {
const audioBuffer = audioContext.createBuffer(2, 100, audioContext.sampleRate);
convolverNode.buffer = audioBuffer;
convolverNode.buffer = null;
expect(convolverNode.buffer).to.equal(audioBuffer);
});
});
});
describe('createMediaStreamSource()', () => {
// @todo There is currently no way to disable the autoplay policy on BrowserStack or Sauce Labs.
// eslint-disable-next-line no-undef
if (!process.env.CI) {
describe('with a mediaStream with two audio tracks', () => {
let audioBufferSourceNode;
let gainNodes;
let mediaStream;
let mediaStreamAudioDestinationNodes;
afterEach(() => {
audioBufferSourceNode.stop();
audioBufferSourceNode.disconnect(gainNodes[0]);
audioBufferSourceNode.disconnect(gainNodes[1]);
gainNodes[0].disconnect(mediaStreamAudioDestinationNodes[0]);
gainNodes[1].disconnect(mediaStreamAudioDestinationNodes[1]);
});
beforeEach(() => {
audioBufferSourceNode = audioContext.createBufferSource();
gainNodes = [audioContext.createGain(), audioContext.createGain()];
mediaStreamAudioDestinationNodes = [
audioContext.createMediaStreamDestination(),
audioContext.createMediaStreamDestination()
];
const audioBuffer = audioContext.createBuffer(1, 2, audioContext.sampleRate);
audioBuffer.getChannelData(0)[0] = 1;
audioBuffer.getChannelData(0)[1] = 1;
audioBufferSourceNode.buffer = audioBuffer;
audioBufferSourceNode.loop = true;
audioBufferSourceNode.connect(gainNodes[0]).connect(mediaStreamAudioDestinationNodes[0]);
audioBufferSourceNode.connect(gainNodes[1]).connect(mediaStreamAudioDestinationNodes[1]);
audioBufferSourceNode.start();
const audioStreamTracks = mediaStreamAudioDestinationNodes.map(({ stream }) => stream.getAudioTracks()[0]);
if (audioStreamTracks[0].id > audioStreamTracks[1].id) {
mediaStream = mediaStreamAudioDestinationNodes[0].stream;
gainNodes[0].gain.value = 0;
mediaStream.addTrack(audioStreamTracks[1]);
while (mediaStream.getAudioTracks()[0] !== audioStreamTracks[0]) {
mediaStream = new MediaStream([audioStreamTracks[0], audioStreamTracks[1]]);
}
} else {
mediaStream = mediaStreamAudioDestinationNodes[1].stream;
gainNodes[1].gain.value = 0;
mediaStream.addTrack(audioStreamTracks[0]);
while (mediaStream.getAudioTracks()[0] !== audioStreamTracks[1]) {
mediaStream = new MediaStream([audioStreamTracks[1], audioStreamTracks[0]]);
}
}
});
// bug #159
it('should pick the first track', (done) => {
const mediaStreamAudioSourceNode = audioContext.createMediaStreamSource(mediaStream);
const scriptProcessorNode = audioContext.createScriptProcessor(512);
let numberOfInvocations = 0;
scriptProcessorNode.onaudioprocess = ({ inputBuffer }) => {
numberOfInvocations += 1;
if (numberOfInvocations > 10) {
const channelData = inputBuffer.getChannelData(0);
for (let i = 0; i < 512; i += 1) {
if (channelData[i] !== 0) {
mediaStreamAudioSourceNode.disconnect(scriptProcessorNode);
scriptProcessorNode.disconnect(audioContext.destination);
done(new Error('The signal is expected to be zero at all time.'));
break;
}
}
}
};
mediaStreamAudioSourceNode.connect(scriptProcessorNode).connect(audioContext.destination);
setTimeout(() => {
mediaStreamAudioSourceNode.disconnect(scriptProcessorNode);
scriptProcessorNode.disconnect(audioContext.destination);
expect(numberOfInvocations).to.be.above(10);
done();
}, 1000);
});
});
}
});
describe('createMediaStreamTrackSource()', () => {
// bug #121
it('should not be implemented', () => {
expect(audioContext.createMediaStreamTrackSource).to.be.undefined;
});
});
describe('decodeAudioData()', () => {
// bug #4
it('should throw null when asked to decode an unsupported file', function (done) {
this.timeout(10000);
// PNG files are not supported by any browser :-)
loadFixtureAsArrayBuffer('one-pixel-of-transparency.png').then((arrayBuffer) => {
audioContext.decodeAudioData(
arrayBuffer,
() => {},
(err) => {
expect(err).to.be.null;
done();
}
);
});
});
// bug #43
it('should not throw a DataCloneError', function (done) {
this.timeout(10000);
loadFixtureAsArrayBuffer('1000-frames-of-noise-stereo.wav').then((arrayBuffer) => {
audioContext.decodeAudioData(arrayBuffer, () => {
audioContext.decodeAudioData(arrayBuffer, () => done());
});
});
});
// bug #133
it('should not neuter the arrayBuffer', function (done) {
this.timeout(10000);
loadFixtureAsArrayBuffer('1000-frames-of-noise-stereo.wav').then((arrayBuffer) => {
audioContext.decodeAudioData(arrayBuffer, () => {
expect(arrayBuffer.byteLength).to.not.equal(0);
done();
});
});
});
});
});