5 <script src=
"../resources/js-test.js"></script>
6 <script src=
"resources/compatibility.js"></script>
7 <script src=
"resources/audio-testing.js"></script>
12 description("Test if ChannelMergerNode runs correctly in a cycle.");
13 window
.jsTestIsAsync
= true;
15 // This specific sample rate is chosen to avoid the round/truncation error
16 // in delay time. See: crbug.com/448801
17 var sampleRate
= 32768;
19 // Web Audio API's rendering quantum.
20 var renderingQuantum
= 128;
22 // 4x of rendering quantum. This is to make the rendered result long enough
23 // so that we can observe the delayed output.
24 var renderLength
= renderingQuantum
* 4;
26 // 1x rendering quantum of delay.
27 var delayTime
= renderingQuantum
/ sampleRate
;
29 // Use 2 channels as a test case.
30 var numberOfChannels
= 2;
32 var audit
= Audit
.createTaskRunner();
34 audit
.defineTask('merger-cyclic-graph', function (done
) {
36 var context
= new OfflineAudioContext(
37 numberOfChannels
, renderLength
, sampleRate
39 var merger
= context
.createChannelMerger(2);
40 var delay
= context
.createDelay();
41 var source
= context
.createBufferSource();
43 // Create a mono source buffer filled with '1'.
44 source
.buffer
= createTestingAudioBuffer(context
, 1, renderLength
);
46 delay
.delayTime
.value
= delayTime
;
48 // Connect the source to input 0 of the merger. Connect the output of
49 // the merger to a delay node whose output is then connected to input 1
50 // of the merger. See: crbug.com/442925
51 source
.connect(merger
, 0, 0);
52 delay
.connect(merger
, 0, 1);
53 merger
.connect(delay
);
54 merger
.connect(context
.destination
);
57 context
.startRendering().then(function (buffer
) {
58 // Expected output values: the output of delay node will be a stereo
59 // signal of [1, 0]. When it feeds back to the 2nd input of merger node,
60 // the stereo channel will be summed to mono resulting in 0.5.
61 var expected_left
= [];
62 var expected_right
= [];
64 for (var i
= 0; i
< renderLength
; i
++) {
65 // Note that the delayed channel will be zero for the first 128 samples
66 // due to the cyclic audio graph, the second 128 sample will be also
67 // zero because of 128 samples delay.
68 expected_left
[i
] = 1.0;
69 expected_right
[i
] = (i
< renderingQuantum
* 2) ? 0.0 : 0.5;
72 for (i
= 0; i
< buffer
.numberOfChannels
; i
++) {
73 var actual_left
= buffer
.getChannelData(0);
74 var actual_right
= buffer
.getChannelData(1);
75 for (var j
= 0; j
< renderLength
; j
++) {
76 if (expected_left
[j
] !== actual_left
[j
]) {
77 testFailed('The value ' + actual_left
[j
] +
78 'in the left channel did not match the expected value ' +
79 expected_left
[j
] + ' at the index ' + j
+ '.');
83 if (expected_right
[j
] !== actual_right
[j
]) {
84 testFailed('The value ' + actual_left
[j
] +
85 'in the right channel did not match the expected value ' +
86 expected_left
[j
] + ' at the index ' + j
+ '.');
93 testPassed("ChannerMergerNode passed cyclic audio graph test.");
99 audit
.defineTask('finish', function (done
) {
105 'merger-cyclic-graph',
109 successfullyParsed
= true;