4 Tests that AudioChannelSplitter works correctly.
9 <script src="../fast/js/resources/js-test-pre.js"></script>
10 <script type="text/javascript" src="resources/audio-testing.js"></script>
15 <div id="description"></div>
16 <div id="console"></div>
19 description("Tests AudioChannelSplitter.");
21 var sampleRate = 44100.0;
22 var lengthInSampleFrames = 512;
30 function createStereoBufferWithDCOffset(length, sampleRate, offset) {
31 var buffer = context.createBuffer(2, length, sampleRate);
32 var n = buffer.length;
33 var channelL = buffer.getChannelData(0);
34 var channelR = buffer.getChannelData(1);
36 for (var i = 0; i < n; ++i) {
38 channelR[i] = -1.0 * offset;
44 // checkResult() checks that the rendered buffer is stereo and that the left channel is all -1 and right channel all +1.
45 // In other words, we've reversed the order of the two channels.
46 function checkResult(event) {
47 var buffer = event.renderedBuffer;
51 if (buffer.numberOfChannels == 2) {
52 var bufferDataL = buffer.getChannelData(0);
53 var bufferDataR = buffer.getChannelData(1);
55 // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
56 for (var i = 0; i < buffer.length; ++i) {
57 if (bufferDataL[i] != -1 || bufferDataR[i] != 1) {
67 testPassed("Correctly exchanged left and right channels.");
69 testFailed("Error on exchanging left and right channels.");
76 if (window.testRunner) {
77 testRunner.dumpAsText();
78 testRunner.waitUntilDone();
81 window.jsTestIsAsync = true;
83 // Create stereo offline audio context.
84 context = new webkitOfflineAudioContext(2, lengthInSampleFrames, sampleRate);
87 var splitternode = context.createChannelSplitter(0);
88 testFailed("Exception should be thrown for numberOfOutputs <= 0.");
90 testPassed("Exception been thrown for numberOfOutputs <= 0.");
94 var splitternode = context.createChannelSplitter(33);
95 testFailed("Exception should be thrown for numerOfOutputs >= 32.");
97 testPassed("Exception been thrown for numberOfOutputs >= 32.");
101 var splitternode = context.createChannelSplitter(32);
102 testPassed("AudioChannelSplitter created successfully with numberOfOutputs = 32.");
103 if (splitternode.numberOfOutputs === 32)
104 testPassed("AudioChannelSplitter has 32 outputs when it is created with numberOfOutputs = 32.");
106 testFailed("AudioChannelSplitter should have 32 outputs when it is created with numberOfOutputs = 32.");
108 if (splitternode.numberOfInputs === 1)
109 testPassed("AudioChannelSplitter has one input.");
111 testFailed("AudioChannelSplitter should have one input.");
113 testFailed("Failed to create AudioChannelSplitter with numberOfInputs = 32.");
117 var splitternode = context.createChannelSplitter();
118 testPassed("AudioChannelSplitter created successfully with empty parameter.");
119 if (splitternode.numberOfOutputs === 6)
120 testPassed("AudioChannelSplitter has 6 outputs when it is created with empty parameter.");
122 testFailed("AudioChannelSplitter should have 6 outputs when it is created with empty parameter.");
124 if (splitternode.toString().indexOf("ChannelSplitterNode") > -1)
125 testPassed("ChannelSplitterNode Object is available.");
127 testFailed("ChannelSplitterNode Object is not available.");
129 testFailed("Failed to create AudioChannelSplitter with empty parameter.");
132 // Create a stereo buffer, with all +1 values in left channel, all -1 in right channel.
133 sourceBuffer = createStereoBufferWithDCOffset(lengthInSampleFrames, sampleRate, 1);
135 sourceNode = context.createBufferSource();
136 sourceNode.buffer = sourceBuffer;
138 // Create a channel splitter and connect it so that it split the stereo stream into two mono streams.
139 channelSplitter = context.createChannelSplitter(2);
140 sourceNode.connect(channelSplitter);
142 // Create a channel merger to merge the output of channel splitter.
143 channelMerger = context.createChannelMerger();
144 channelMerger.connect(context.destination);
146 // When merging, exchange channel layout: left->right, right->left
147 channelSplitter.connect(channelMerger, 0, 1);
148 channelSplitter.connect(channelMerger, 1, 0);
150 sourceNode.noteOn(0);
152 context.oncomplete = checkResult;
153 context.startRendering();
160 <script src="../fast/js/resources/js-test-post.js"></script>