5 <script src=
"../resources/js-test.js"></script>
6 <script src=
"resources/compatibility.js"></script>
7 <script src=
"resources/audio-testing.js"></script>
8 <script src=
"resources/merger-testing.js"></script>
13 description('Test input handling behavior of ChannelMergerNode.');
14 window
.jsTestIsAsync
= true;
16 var audit
= Audit
.createTaskRunner();
18 // Task: Check if an inactive input renders a silent mono channel in the
20 audit
.defineTask('silent-channel', function (done
) {
24 // Create a mono source buffer filled with '1'.
25 testBufferChannelCount
: 1,
27 // Connect the output of source into the 4th input of merger.
30 // All channels should contain 0, except channel 4 which should be 1.
31 expected
: [0, 0, 0, 1, 0, 0],
36 // Task: Check if a stereo input is being down-mixed to mono channel
37 // correctly based on the mixing rule.
38 audit
.defineTask('stereo-down-mixing', function (done
) {
42 // Create a stereo buffer filled with '1' and '2' for left and right
43 // channels respectively.
44 testBufferChannelCount
: 2,
46 // Connect the output of source into the 1st input of merger.
47 mergerInputIndex
: undefined,
49 // The result of summed and down-mixed stereo audio should be 1.5.
50 // (= 1 * 0.5 + 2 * 0.5)
51 expected
: [1.5, 0, 0, 0, 0, 0],
56 // Task: Check if 3-channel input gets processed by the 'discrete' mixing
58 audit
.defineTask('undefined-channel-layout', function (done
) {
62 // Create a 3-channel buffer filled with '1', '2', and '3' respectively.
63 testBufferChannelCount
: 3,
65 // Connect the output of source into the 1st input of merger.
66 mergerInputIndex
: undefined,
68 // The result of summed stereo audio should be 1 because 3-channel is
69 // not a canonical layout, so the input channel 2 and 3 should be
70 // dropped by 'discrete' mixing rule.
71 expected
: [1, 0, 0, 0, 0, 0],
76 // Task: Merging two inputs into a single stereo stream.
77 audit
.defineTask('merging-to-stereo', function (done
) {
79 // For this test, the number of channel should be 2.
80 var context
= new OfflineAudioContext(2, 128, 44100);
81 var merger
= context
.createChannelMerger();
82 var source1
= context
.createBufferSource();
83 var source2
= context
.createBufferSource();
85 // Create a DC offset buffer (mono) filled with 1 and assign it to BS
87 var positiveDCOffset
= createConstantBuffer(context
, 128, 1);
88 var negativeDCOffset
= createConstantBuffer(context
, 128, -1);
89 source1
.buffer
= positiveDCOffset
;
90 source2
.buffer
= negativeDCOffset
;
92 // Connect: BS#1 => merger_input#0, BS#2 => Inverter => merger_input#1
93 source1
.connect(merger
, 0, 0);
94 source2
.connect(merger
, 0, 1);
95 merger
.connect(context
.destination
);
99 context
.startRendering().then(function (buffer
) {
101 // Channel#0 = 1, Channel#1 = -1
102 Should('Channel #0', buffer
.getChannelData(0)).beConstantValueOf(1);
103 Should('Channel #1', buffer
.getChannelData(1)).beConstantValueOf(-1);
110 audit
.defineTask('finish', function (done
) {
117 'stereo-down-mixing',
118 'undefined-channel-layout',
123 successfullyParsed
= true;