-
Notifications
You must be signed in to change notification settings - Fork 36
/
audio-context.js
1043 lines (767 loc) · 37 KB
/
audio-context.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'core-js/es7/reflect';
import {
UNPATCHED_AUDIO_CONTEXT_CONSTRUCTOR_PROVIDER,
unpatchedAudioContextConstructor as nptchdDCntxtCnstrctr
} from '../../../src/providers/unpatched-audio-context-constructor';
import { AudioContext } from '../../../src/audio-contexts/audio-context';
import { ReflectiveInjector } from '@angular/core';
import { WINDOW_PROVIDER } from '../../../src/providers/window';
import { createScriptProcessor } from '../../helper/create-script-processor';
import { loadFixture } from '../../helper/load-fixture';
import { spy } from 'sinon';
describe('AudioContext', () => {
let audioContext;
afterEach(() => {
return audioContext.close();
});
describe('without a constructed AudioContext', () => {
it('should allow to set the latencyHint to balanced', () => {
audioContext = new AudioContext({ latencyHint: 'balanced' });
});
it('should allow to set the latencyHint to interactive', () => {
audioContext = new AudioContext({ latencyHint: 'interactive' });
});
it('should allow to set the latencyHint to playback', () => {
audioContext = new AudioContext({ latencyHint: 'playback' });
});
it('should allow to set the latencyHint to a number', () => {
audioContext = new AudioContext({ latencyHint: 0.5 });
});
it('should not allow to set the latencyHint to an unsupported value', () => {
expect(() => {
audioContext = new AudioContext({ latencyHint: 'negative' });
}).to.throw(TypeError, "The provided value 'negative' is not a valid enum value of type AudioContextLatencyCategory.");
// Create a new AudioContext to ensure the afterEach hooks keeps working.
audioContext = new AudioContext();
});
});
describe('with a constructed AudioContext', () => {
beforeEach(() => {
audioContext = new AudioContext();
});
describe('audioWorklet', () => {
it('should be an instance of the AudioWorklet interface', () => {
const audioWorklet = audioContext.audioWorklet;
expect(audioWorklet.addModule).to.be.a('function');
});
});
describe('currentTime', () => {
it('should be a number', () => {
expect(audioContext.currentTime).to.be.a('number');
});
it('should be readonly', () => {
expect(() => {
audioContext.currentTime = 0;
}).to.throw(TypeError);
});
it('should advance over time', (done) => {
const now = audioContext.currentTime;
audioContext.onstatechange = () => {
// Prevent consecutive calls.
audioContext.onstatechange = null;
setTimeout(() => {
expect(audioContext.currentTime).to.above(now);
done();
}, 1000);
};
// Kick off the audioContext.
audioContext.createGain();
});
});
describe('destination', () => {
it('should be an instance of the AudioDestinationNode interface', () => {
const destination = audioContext.destination;
expect(destination.channelCount).to.equal(2);
expect(destination.channelCountMode).to.equal('explicit');
expect(destination.channelInterpretation).to.equal('speakers');
expect(destination.maxChannelCount).to.be.a('number');
expect(destination.numberOfInputs).to.equal(1);
expect(destination.numberOfOutputs).to.equal(0);
});
it('should be readonly', () => {
expect(() => {
audioContext.destination = 'a fake AudioDestinationNode';
}).to.throw(TypeError);
});
it('should have maxChannelCount which is at least the channelCount', () => {
const destination = audioContext.destination;
expect(destination.maxChannelCount).to.be.at.least(destination.channelCount);
});
it('should not allow to change the value of the channelCount property to a value above the maxChannelCount', (done) => {
const destination = audioContext.destination;
try {
destination.channelCount = destination.maxChannelCount + 1;
} catch (err) {
expect(err.code).to.equal(1);
expect(err.name).to.equal('IndexSizeError');
done();
}
});
});
describe('onstatechange', () => {
it('should be null', () => {
expect(audioContext.onstatechange).to.be.null;
});
it('should be assignable to a function', () => {
const fn = () => {};
const onstatechange = audioContext.onstatechange = fn; // eslint-disable-line no-multi-assign
expect(onstatechange).to.equal(fn);
expect(audioContext.onstatechange).to.equal(fn);
});
it('should be assignable to null', () => {
const onstatechange = audioContext.onstatechange = null; // eslint-disable-line no-multi-assign
expect(onstatechange).to.be.null;
expect(audioContext.onstatechange).to.be.null;
});
it('should not be assignable to something else', () => {
const string = 'no function or null value';
audioContext.onstatechange = () => {};
const onstatechange = audioContext.onstatechange = string; // eslint-disable-line no-multi-assign
expect(onstatechange).to.equal(string);
expect(audioContext.onstatechange).to.be.null;
});
});
describe('sampleRate', () => {
it('should be a number', () => {
expect(audioContext.sampleRate).to.be.a('number');
});
it('should be readonly', () => {
expect(() => {
audioContext.sampleRate = 22050;
}).to.throw(TypeError);
});
});
describe('state', () => {
it('should be suspended at the beginning', () => {
expect(audioContext.state).to.equal('suspended');
});
it('should be readonly', () => {
expect(() => {
audioContext.state = 'closed';
}).to.throw(TypeError);
});
it('should be transitioned to running', (done) => {
audioContext.onstatechange = () => {
expect(audioContext.state).to.equal('running');
// Prevent consecutive calls.
audioContext.onstatechange = null;
done();
};
// Kick off the audioContext.
audioContext.createGain();
});
// closed is tested below
});
describe('close()', () => {
afterEach(() => {
// Create a closeable AudioContext to align the behaviour with other tests.
audioContext = new AudioContext();
});
it('should return a promise', () => {
expect(audioContext.close()).to.be.an.instanceOf(Promise);
});
it('should set the state to closed', (done) => {
audioContext
.close()
.then(() => {
// According to the spec the context state is changed to 'closed' after the promise gets resolved.
setTimeout(() => {
expect(audioContext.state).to.equal('closed');
done();
});
})
.catch(done);
});
describe('with a closed AudioContext', () => {
beforeEach(() => audioContext.close());
it('should throw an error', (done) => {
audioContext
.close()
.catch((err) => {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
});
});
});
});
describe('createAnalyser()', () => {
it('should be a function', () => {
expect(audioContext.createAnalyser).to.be.a('function');
});
});
describe('createBiquadFilter()', () => {
it('should be a function', () => {
expect(audioContext.createBiquadFilter).to.be.a('function');
});
});
describe('createBuffer()', () => {
it('should return an instance of the AudioBuffer interface', () => {
const audioBuffer = audioContext.createBuffer(2, 10, 44100);
expect(audioBuffer.duration).to.be.closeTo(10 / 44100, 0.001);
expect(audioBuffer.length).to.equal(10);
expect(audioBuffer.numberOfChannels).to.equal(2);
expect(audioBuffer.sampleRate).to.equal(44100);
expect(audioBuffer.getChannelData).to.be.a('function');
expect(audioBuffer.copyFromChannel).to.be.a('function');
expect(audioBuffer.copyToChannel).to.be.a('function');
});
it('should return an AudioBuffer which can be used with an unpatched AudioContext', () => {
const audioBuffer = audioContext.createBuffer(2, 10, 44100);
const injector = ReflectiveInjector.resolveAndCreate([
UNPATCHED_AUDIO_CONTEXT_CONSTRUCTOR_PROVIDER,
WINDOW_PROVIDER
]);
const UnpatchedAudioContext = injector.get(nptchdDCntxtCnstrctr);
const unpatchedAudioContext = new UnpatchedAudioContext();
const unpatchedAudioBufferSourceNode = unpatchedAudioContext.createBufferSource();
unpatchedAudioBufferSourceNode.buffer = audioBuffer;
});
describe('copyFromChannel()', () => {
let audioBuffer;
let destination;
beforeEach(() => {
audioBuffer = audioContext.createBuffer(2, 10, 44100);
destination = new Float32Array(10);
});
it('should not allow to copy a channel with a number greater or equal than the number of channels', (done) => {
try {
audioBuffer.copyFromChannel(destination, 2);
} catch (err) {
expect(err.code).to.equal(1);
expect(err.name).to.equal('IndexSizeError');
done();
}
});
it('should not allow to copy values with an offset greater than the length', (done) => {
try {
audioBuffer.copyFromChannel(destination, 0, 10);
} catch (err) {
expect(err.code).to.equal(1);
expect(err.name).to.equal('IndexSizeError');
done();
}
});
});
describe('copyToChannel()', () => {
let audioBuffer;
let source;
beforeEach(() => {
audioBuffer = audioContext.createBuffer(2, 10, 44100);
source = new Float32Array(10);
});
it('should not allow to copy a channel with a number greater or equal than the number of channels', (done) => {
try {
audioBuffer.copyToChannel(source, 2);
} catch (err) {
expect(err.code).to.equal(1);
expect(err.name).to.equal('IndexSizeError');
done();
}
});
it('should not allow to copy values with an offset greater than the length', (done) => {
try {
audioBuffer.copyToChannel(source, 0, 10);
} catch (err) {
expect(err.code).to.equal(1);
expect(err.name).to.equal('IndexSizeError');
done();
}
});
});
describe('copyFromChannel()/copyToChannel()', () => {
let audioBuffer;
let destination;
let source;
beforeEach(() => {
audioBuffer = audioContext.createBuffer(2, 100, 44100);
destination = new Float32Array(10);
source = new Float32Array(10);
for (let i = 0; i < 10; i += 1) {
destination[i] = Math.random();
source[i] = Math.random();
}
});
it('should copy values with an offset of 0', () => {
audioBuffer.copyToChannel(source, 0);
audioBuffer.copyFromChannel(destination, 0);
for (let i = 0; i < 10; i += 1) {
expect(destination[i]).to.equal(source[i]);
}
});
it('should copy values with an offset of 50', () => {
audioBuffer.copyToChannel(source, 0, 50);
audioBuffer.copyFromChannel(destination, 0, 50);
for (let i = 0; i < 10; i += 1) {
expect(destination[i]).to.equal(source[i]);
}
});
it('should copy values with an offset large enough to leave a part of the destination untouched', () => {
const destinationCopy = Array.from(destination);
audioBuffer.copyToChannel(source, 0, 95);
audioBuffer.copyFromChannel(destination, 0, 95);
for (let i = 0; i < 5; i += 1) {
expect(destination[i]).to.equal(source[i]);
}
for (let i = 5; i < 10; i += 1) {
expect(destination[i]).to.equal(destinationCopy[i]);
}
});
it('should copy values with an offset low enough to leave a part of the buffer untouched', () => {
audioBuffer.copyToChannel(source, 0, 35);
audioBuffer.copyToChannel(source, 0, 25);
audioBuffer.copyFromChannel(destination, 0, 35);
for (let i = 0; i < 10; i += 1) {
expect(destination[i]).to.equal(source[i]);
}
});
});
});
describe('createBufferSource()', () => {
it('should be a function', () => {
expect(audioContext.createBufferSource).to.be.a('function');
});
});
describe('createChannelMerger()', () => {
it('should return an instance of the ChannelMergerNode interface', () => {
const channelMergerNode = audioContext.createChannelMerger();
expect(channelMergerNode.channelCount).to.equal(1);
expect(channelMergerNode.channelCountMode).to.equal('explicit');
expect(channelMergerNode.channelInterpretation).to.equal('speakers');
expect(channelMergerNode.numberOfInputs).to.equal(6);
expect(channelMergerNode.numberOfOutputs).to.equal(1);
});
it('should return a channelMergerNode with the given parameters', () => {
const channelMergerNode = audioContext.createChannelMerger(2);
expect(channelMergerNode.numberOfInputs).to.equal(2);
});
it('should not allow to change the value of the channelCount property', (done) => {
const channelMergerNode = audioContext.createChannelMerger(2);
try {
channelMergerNode.channelCount = 2;
} catch (err) {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
}
});
it('should not allow to change the value of the channelCountMode property', (done) => {
const channelMergerNode = audioContext.createChannelMerger(2);
try {
channelMergerNode.channelCountMode = 'max';
} catch (err) {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
}
});
it('should throw an error if the AudioContext is closed', (done) => {
audioContext
.close()
.then(() => {
audioContext.createChannelMerger();
})
.catch((err) => {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
audioContext = new AudioContext();
done();
});
});
it('should be connectable', () => {
const channelMergerNode = audioContext.createChannelMerger();
const gainNode = audioContext.createGain();
channelMergerNode.connect(gainNode);
});
it('should be chainable', () => {
const channelMergerNode = audioContext.createChannelMerger();
const gainNode = audioContext.createGain();
expect(channelMergerNode.connect(gainNode)).to.equal(gainNode);
});
it('should not be connectable to a node of another AudioContext', (done) => {
const anotherAudioContext = new AudioContext();
const channelMergerNode = audioContext.createChannelMerger();
try {
channelMergerNode.connect(anotherAudioContext.destination);
} catch (err) {
expect(err.code).to.equal(15);
expect(err.name).to.equal('InvalidAccessError');
done();
} finally {
anotherAudioContext.close();
}
});
it('should handle unconnected channels as silence', (done) => {
const audioBufferSourceNode = audioContext.createBufferSource();
const channelMergerNode = audioContext.createChannelMerger(2);
const sampleRate = audioContext.sampleRate;
const audioBuffer = audioContext.createBuffer(1, 2, sampleRate);
const scriptProcessorNode = createScriptProcessor(audioContext, 256, 2, 2);
// @todo Safari does not play/loop 1 sample buffers. This should be patched.
audioBuffer.copyToChannel(new Float32Array([ 1, 1 ]), 0);
audioBufferSourceNode.buffer = audioBuffer;
audioBufferSourceNode.loop = true;
const startTime = audioContext.currentTime;
scriptProcessorNode.onaudioprocess = (event) => {
const channelData = event.inputBuffer.getChannelData(1);
for (let i = 0, length = channelData.length; i < length; i += 1) {
if (channelData[i] === 1) {
done(new Error('This channel should be silent.'));
return;
}
}
if (startTime + (1 / sampleRate) < event.playbackTime) {
done();
}
};
audioBufferSourceNode
.connect(channelMergerNode, 0, 0)
.connect(scriptProcessorNode)
.connect(audioContext.destination);
audioBufferSourceNode.start(startTime);
});
});
describe('createChannelSplitter()', () => {
it('should return an instance of the ChannelSplitterNode interface', () => {
const channelSplitterNode = audioContext.createChannelSplitter();
expect(channelSplitterNode.channelCount).to.equal(6);
expect(channelSplitterNode.channelCountMode).to.equal('explicit');
expect(channelSplitterNode.channelInterpretation).to.equal('discrete');
expect(channelSplitterNode.numberOfInputs).to.equal(1);
expect(channelSplitterNode.numberOfOutputs).to.equal(6);
});
it('should return a channelSplitterNode with the given parameters', () => {
const channelSplitterNode = audioContext.createChannelSplitter(2);
expect(channelSplitterNode.numberOfOutputs).to.equal(2);
});
it('should not allow to change the value of the channelCount property', (done) => {
const channelSplitterNode = audioContext.createChannelSplitter(2);
try {
channelSplitterNode.channelCount = 4;
} catch (err) {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
}
});
it('should not allow to change the value of the channelCountMode property', (done) => {
const channelSplitterNode = audioContext.createChannelSplitter(2);
try {
channelSplitterNode.channelCountMode = 'max';
} catch (err) {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
}
});
it('should not allow to change the value of the channelInterpretation property', (done) => {
const channelSplitterNode = audioContext.createChannelSplitter(2);
try {
channelSplitterNode.channelInterpretation = 'speakers';
} catch (err) {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
}
});
it('should throw an error if the AudioContext is closed', (done) => {
audioContext
.close()
.then(() => {
audioContext.createChannelSplitter();
})
.catch((err) => {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
audioContext = new AudioContext();
done();
});
});
it('should be chainable', () => {
const channelSplitterNode = audioContext.createChannelSplitter();
const gainNode = audioContext.createGain();
expect(channelSplitterNode.connect(gainNode)).to.equal(gainNode);
});
it('should not be connectable to a node of another AudioContext', (done) => {
const anotherAudioContext = new AudioContext();
const channelSplitterNode = audioContext.createChannelSplitter();
try {
channelSplitterNode.connect(anotherAudioContext.destination);
} catch (err) {
expect(err.code).to.equal(15);
expect(err.name).to.equal('InvalidAccessError');
done();
} finally {
anotherAudioContext.close();
}
});
});
describe('createConstantSource()', () => {
it('should be a function', () => {
expect(audioContext.createConstantSource).to.be.a('function');
});
});
describe('createGain()', () => {
it('should be a function', () => {
expect(audioContext.createGain).to.be.a('function');
});
});
describe('createIIRFilter()', () => {
it('should be a function', () => {
expect(audioContext.createIIRFilter).to.be.a('function');
});
});
describe('createMediaElementSource()', () => {
it('should be a function', () => {
expect(audioContext.createMediaElementSource).to.be.a('function');
});
});
describe('createMediaStreamSource()', () => {
it('should be a function', () => {
expect(audioContext.createMediaStreamSource).to.be.a('function');
});
});
describe('createOscillator()', () => {
it('should be a function', () => {
expect(audioContext.createOscillator).to.be.a('function');
});
});
describe('decodeAudioData()', () => {
it('should return a promise', () => {
const promise = audioContext.decodeAudioData();
promise.catch(() => { /* Ignore the error. */ });
expect(promise).to.be.an.instanceOf(Promise);
});
describe('without a valid arrayBuffer', () => {
it('should throw an error', (done) => {
audioContext
.decodeAudioData(null)
.catch((err) => {
expect(err).to.be.an.instanceOf(TypeError);
done();
});
});
it('should call the errorCallback with a TypeError', (done) => {
audioContext
.decodeAudioData(null, () => {}, (err) => {
expect(err).to.be.an.instanceOf(TypeError);
done();
})
.catch(() => { /* Ignore the error. */ });
});
// The promise is rejected before but the errorCallback gets called synchronously.
it('should call the errorCallback before the promise gets rejected', (done) => {
const errorCallback = spy();
audioContext
.decodeAudioData(null, () => {}, errorCallback)
.catch(() => {
expect(errorCallback).to.have.been.calledOnce;
done();
});
});
});
describe('with an arrayBuffer of an unsupported file', () => {
let arrayBuffer;
beforeEach(function (done) {
this.timeout(5000);
// PNG files are not supported by any browser :-)
loadFixture('one-pixel-of-transparency.png', (err, rrBffr) => {
expect(err).to.be.null;
arrayBuffer = rrBffr;
done();
});
});
it('should throw an error', (done) => {
audioContext
.decodeAudioData(arrayBuffer)
.catch((err) => {
expect(err.code).to.equal(0);
expect(err.name).to.equal('EncodingError');
done();
});
});
it('should call the errorCallback with an error', (done) => {
audioContext
.decodeAudioData(arrayBuffer, () => {}, (err) => {
expect(err.code).to.equal(0);
expect(err.name).to.equal('EncodingError');
done();
})
.catch(() => { /* Ignore the error. */ });
});
// The promise is rejected before but the errorCallback gets called synchronously.
it('should call the errorCallback before the promise gets rejected', (done) => {
const errorCallback = spy();
audioContext
.decodeAudioData(arrayBuffer, () => {}, errorCallback)
.catch(() => {
expect(errorCallback).to.have.been.calledOnce;
done();
});
});
});
describe('with an arrayBuffer of a supported file', function () {
let arrayBuffer;
beforeEach((done) => {
this.timeout(5000);
loadFixture('1000-frames-of-noise.wav', (err, rrBffr) => {
expect(err).to.be.null;
arrayBuffer = rrBffr;
done();
});
});
it('should resolve to an instance of the AudioBuffer interface', () => {
return audioContext
.decodeAudioData(arrayBuffer)
.then((audioBuffer) => {
expect(audioBuffer.duration).to.be.closeTo(1000 / 44100, 0.001);
expect(audioBuffer.length).to.equal(1000);
expect(audioBuffer.numberOfChannels).to.equal(2);
expect(audioBuffer.sampleRate).to.equal(44100);
expect(audioBuffer.getChannelData).to.be.a('function');
expect(audioBuffer.copyFromChannel).to.be.a('function');
expect(audioBuffer.copyToChannel).to.be.a('function');
});
});
it('should call the successCallback with an instance of the AudioBuffer interface', (done) => {
audioContext.decodeAudioData(arrayBuffer, (audioBuffer) => {
expect(audioBuffer.duration).to.be.closeTo(1000 / 44100, 0.001);
expect(audioBuffer.length).to.equal(1000);
expect(audioBuffer.numberOfChannels).to.equal(2);
expect(audioBuffer.sampleRate).to.equal(44100);
expect(audioBuffer.getChannelData).to.be.a('function');
expect(audioBuffer.copyFromChannel).to.be.a('function');
expect(audioBuffer.copyToChannel).to.be.a('function');
done();
});
});
// The promise is resolved before but the successCallback gets called synchronously.
it('should call the successCallback before the promise gets resolved', () => {
const successCallback = spy();
return audioContext
.decodeAudioData(arrayBuffer, successCallback)
.then(() => {
expect(successCallback).to.have.been.calledOnce;
});
});
it('should throw a DataCloneError', (done) => {
audioContext
.decodeAudioData(arrayBuffer)
.then(() => audioContext.decodeAudioData(arrayBuffer))
.catch((err) => {
expect(err.code).to.equal(25);
expect(err.name).to.equal('DataCloneError');
done();
});
});
it('should neuter the arrayBuffer', (done) => {
audioContext.decodeAudioData(arrayBuffer);
setTimeout(() => {
expect(() => {
// Firefox will throw an error when using a neutered ArrayBuffer.
const uint8Array = new Uint8Array(arrayBuffer);
// Chrome, Opera and Safari will throw an error when trying to convert a typed array with a neutered ArrayBuffer.
Array.from(uint8Array);
}).to.throw(Error);
done();
});
});
it('should resolve with a assignable AudioBuffer', () => {
return audioContext
.decodeAudioData(arrayBuffer)
.then((audioBuffer) => {
const audioBufferSourceNode = audioContext.createBufferSource();
audioBufferSourceNode.buffer = audioBuffer;
});
});
it('should allow to encode in parallel', function () {
this.timeout(10000);
const arrayBufferCopies = [];
for (let i = 1; i < 100; i += 1) {
arrayBufferCopies.push(arrayBuffer.slice(0));
}
return Promise
.all(arrayBufferCopies.map((rrBffr) => audioContext.decodeAudioData(rrBffr)));
});
});
});
describe('resume()', () => {
it('should return a promise', () => {
expect(audioContext.resume()).to.be.an.instanceOf(Promise);
});
it('should set the state to running', (done) => {
audioContext
.resume()
.then(() => {
// According to the spec the context state is changed to 'running' after the promise gets resolved.
setTimeout(() => {
expect(audioContext.state).to.equal('running');
done();
});
})
.catch(done);
});
describe('with a closed AudioContext', () => {
afterEach(() => {
// Create a closeable AudioContext to align the behaviour with other tests.
audioContext = new AudioContext();
});
beforeEach(() => audioContext.close());
it('should throw an error', (done) => {
audioContext
.resume()
.catch((err) => {
expect(err.code).to.equal(11);
expect(err.name).to.equal('InvalidStateError');
done();
});
});
});
describe('with a running AudioContext', () => {
beforeEach(() => audioContext.resume());
it('should ignore consecutive calls', () => {
return audioContext.resume();
});
});
});
describe('suspend()', () => {
it('should return a promise', () => {
expect(audioContext.suspend()).to.be.an.instanceOf(Promise);
});
it('should set the state to suspended', (done) => {
audioContext
.suspend()
.then(() => {
// According to the spec the context state is changed to 'suspended' after the promise gets resolved.
setTimeout(() => {
expect(audioContext.state).to.equal('suspended');