4 Tests that AudioChannelSplitter works correctly.
9 <script src=
"../resources/js-test.js"></script>
10 <script src=
"resources/compatibility.js"></script>
11 <script type=
"text/javascript" src=
"resources/audio-testing.js"></script>
16 <div id=
"description"></div>
17 <div id=
"console"></div>
20 description("Tests AudioChannelSplitter.");
22 var sampleRate
= 44100.0;
23 var lengthInSampleFrames
= 512;
31 function createStereoBufferWithDCOffset(length
, sampleRate
, offset
) {
32 var buffer
= context
.createBuffer(2, length
, sampleRate
);
33 var n
= buffer
.length
;
34 var channelL
= buffer
.getChannelData(0);
35 var channelR
= buffer
.getChannelData(1);
37 for (var i
= 0; i
< n
; ++i
) {
39 channelR
[i
] = -1.0 * offset
;
45 // checkResult() checks that the rendered buffer is stereo and that the left channel is all -1 and right channel all +1.
46 // In other words, we've reversed the order of the two channels.
47 function checkResult(event
) {
48 var buffer
= event
.renderedBuffer
;
52 if (buffer
.numberOfChannels
== 2) {
53 var bufferDataL
= buffer
.getChannelData(0);
54 var bufferDataR
= buffer
.getChannelData(1);
56 // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
57 for (var i
= 0; i
< buffer
.length
; ++i
) {
58 if (bufferDataL
[i
] != -1 || bufferDataR
[i
] != 1) {
68 testPassed("Correctly exchanged left and right channels.");
70 testFailed("Error on exchanging left and right channels.");
77 if (window
.testRunner
) {
78 testRunner
.dumpAsText();
79 testRunner
.waitUntilDone();
82 window
.jsTestIsAsync
= true;
84 // Create stereo offline audio context.
85 context
= new OfflineAudioContext(2, lengthInSampleFrames
, sampleRate
);
88 var splitternode
= context
.createChannelSplitter(0);
89 testFailed("Exception should be thrown for numberOfOutputs <= 0.");
91 testPassed("Exception been thrown for numberOfOutputs <= 0.");
95 var splitternode
= context
.createChannelSplitter(33);
96 testFailed("Exception should be thrown for numerOfOutputs >= 32.");
98 testPassed("Exception been thrown for numberOfOutputs >= 32.");
102 var splitternode
= context
.createChannelSplitter(32);
103 testPassed("AudioChannelSplitter created successfully with numberOfOutputs = 32.");
104 if (splitternode
.numberOfOutputs
=== 32)
105 testPassed("AudioChannelSplitter has 32 outputs when it is created with numberOfOutputs = 32.");
107 testFailed("AudioChannelSplitter should have 32 outputs when it is created with numberOfOutputs = 32.");
109 if (splitternode
.numberOfInputs
=== 1)
110 testPassed("AudioChannelSplitter has one input.");
112 testFailed("AudioChannelSplitter should have one input.");
114 testFailed("Failed to create AudioChannelSplitter with numberOfInputs = 32.");
118 var splitternode
= context
.createChannelSplitter();
119 testPassed("AudioChannelSplitter created successfully with empty parameter.");
120 if (splitternode
.numberOfOutputs
=== 6)
121 testPassed("AudioChannelSplitter has 6 outputs when it is created with empty parameter.");
123 testFailed("AudioChannelSplitter should have 6 outputs when it is created with empty parameter.");
125 if (splitternode
.toString().indexOf("ChannelSplitterNode") > -1)
126 testPassed("ChannelSplitterNode Object is available.");
128 testFailed("ChannelSplitterNode Object is not available.");
130 testFailed("Failed to create AudioChannelSplitter with empty parameter.");
133 // Create a stereo buffer, with all +1 values in left channel, all -1 in right channel.
134 sourceBuffer
= createStereoBufferWithDCOffset(lengthInSampleFrames
, sampleRate
, 1);
136 sourceNode
= context
.createBufferSource();
137 sourceNode
.buffer
= sourceBuffer
;
139 // Create a channel splitter and connect it so that it split the stereo stream into two mono streams.
140 channelSplitter
= context
.createChannelSplitter(2);
141 sourceNode
.connect(channelSplitter
);
143 // Create a channel merger to merge the output of channel splitter.
144 channelMerger
= context
.createChannelMerger();
145 channelMerger
.connect(context
.destination
);
147 // When merging, exchange channel layout: left->right, right->left
148 channelSplitter
.connect(channelMerger
, 0, 1);
149 channelSplitter
.connect(channelMerger
, 1, 0);
153 context
.oncomplete
= checkResult
;
154 context
.startRendering();