OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 | |
3 <!-- | |
4 Tests that AudioChannelSplitter works correctly. | |
5 --> | |
6 | |
7 <html> | |
8 <head> | |
9 <script src="../resources/js-test.js"></script> | |
10 <script src="resources/compatibility.js"></script> | |
11 <script src="resources/audit-util.js"></script> | |
12 <script src="resources/audio-testing.js"></script> | |
13 </head> | |
14 | |
15 <body> | |
16 | |
17 <div id="description"></div> | |
18 <div id="console"></div> | |
19 | |
20 <script> | |
21 description("Tests AudioChannelSplitter."); | |
22 | |
23 var sampleRate = 44100.0; | |
24 var lengthInSampleFrames = 512; | |
25 | |
26 var context = 0; | |
27 var sourceBuffer; | |
28 var sourceNode; | |
29 var channelSplitter; | |
30 var channelMerger; | |
31 | |
32 function createStereoBufferWithDCOffset(length, sampleRate, offset) { | |
33 var buffer = context.createBuffer(2, length, sampleRate); | |
34 var n = buffer.length; | |
35 var channelL = buffer.getChannelData(0); | |
36 var channelR = buffer.getChannelData(1); | |
37 | |
38 for (var i = 0; i < n; ++i) { | |
39 channelL[i] = offset; | |
40 channelR[i] = -1.0 * offset; | |
41 } | |
42 | |
43 return buffer; | |
44 } | |
45 | |
46 // checkResult() checks that the rendered buffer is stereo and that the left cha
nnel is all -1 and right channel all +1. | |
47 // In other words, we've reversed the order of the two channels. | |
48 function checkResult(event) { | |
49 var buffer = event.renderedBuffer; | |
50 | |
51 var success = true; | |
52 | |
53 if (buffer.numberOfChannels == 2) { | |
54 var bufferDataL = buffer.getChannelData(0); | |
55 var bufferDataR = buffer.getChannelData(1); | |
56 | |
57 // Go through every sample and make sure it's all -1 for the left-channe
l, and all +1 for the right-channel. | |
58 for (var i = 0; i < buffer.length; ++i) { | |
59 if (bufferDataL[i] != -1 || bufferDataR[i] != 1) { | |
60 success = false; | |
61 break; | |
62 } | |
63 } | |
64 } else { | |
65 success = false; | |
66 } | |
67 | |
68 if (success) { | |
69 testPassed("Correctly exchanged left and right channels."); | |
70 } else { | |
71 testFailed("Error on exchanging left and right channels."); | |
72 } | |
73 | |
74 finishJSTest(); | |
75 } | |
76 | |
77 function runTest() { | |
78 if (window.testRunner) { | |
79 testRunner.dumpAsText(); | |
80 testRunner.waitUntilDone(); | |
81 } | |
82 | |
83 window.jsTestIsAsync = true; | |
84 | |
85 // Create stereo offline audio context. | |
86 context = new OfflineAudioContext(2, lengthInSampleFrames, sampleRate); | |
87 | |
88 try { | |
89 var splitternode = context.createChannelSplitter(0); | |
90 testFailed("Exception should be thrown for numberOfOutputs <= 0."); | |
91 } catch(e) { | |
92 testPassed("Exception been thrown for numberOfOutputs <= 0."); | |
93 } | |
94 | |
95 try { | |
96 var splitternode = context.createChannelSplitter(33); | |
97 testFailed("Exception should be thrown for numerOfOutputs >= 32."); | |
98 } catch(e) { | |
99 testPassed("Exception been thrown for numberOfOutputs >= 32."); | |
100 } | |
101 | |
102 try { | |
103 var splitternode = context.createChannelSplitter(32); | |
104 testPassed("AudioChannelSplitter created successfully with numberOfOutpu
ts = 32."); | |
105 if (splitternode.numberOfOutputs === 32) | |
106 testPassed("AudioChannelSplitter has 32 outputs when it is created w
ith numberOfOutputs = 32."); | |
107 else | |
108 testFailed("AudioChannelSplitter should have 32 outputs when it is c
reated with numberOfOutputs = 32."); | |
109 | |
110 if (splitternode.numberOfInputs === 1) | |
111 testPassed("AudioChannelSplitter has one input."); | |
112 else | |
113 testFailed("AudioChannelSplitter should have one input."); | |
114 } catch(e) { | |
115 testFailed("Failed to create AudioChannelSplitter with numberOfInputs =
32."); | |
116 } | |
117 | |
118 try { | |
119 var splitternode = context.createChannelSplitter(); | |
120 testPassed("AudioChannelSplitter created successfully with empty paramet
er."); | |
121 if (splitternode.numberOfOutputs === 6) | |
122 testPassed("AudioChannelSplitter has 6 outputs when it is created wi
th empty parameter."); | |
123 else | |
124 testFailed("AudioChannelSplitter should have 6 outputs when it is cr
eated with empty parameter."); | |
125 | |
126 if (splitternode.toString().indexOf("ChannelSplitterNode") > -1) | |
127 testPassed("ChannelSplitterNode Object is available."); | |
128 else | |
129 testFailed("ChannelSplitterNode Object is not available."); | |
130 } catch(e) { | |
131 testFailed("Failed to create AudioChannelSplitter with empty parameter."
); | |
132 } | |
133 | |
134 // Create a stereo buffer, with all +1 values in left channel, all -1 in rig
ht channel. | |
135 sourceBuffer = createStereoBufferWithDCOffset(lengthInSampleFrames, sampleRa
te, 1); | |
136 | |
137 sourceNode = context.createBufferSource(); | |
138 sourceNode.buffer = sourceBuffer; | |
139 | |
140 // Create a channel splitter and connect it so that it split the stereo stre
am into two mono streams. | |
141 channelSplitter = context.createChannelSplitter(2); | |
142 sourceNode.connect(channelSplitter); | |
143 | |
144 // Create a channel merger to merge the output of channel splitter. | |
145 channelMerger = context.createChannelMerger(); | |
146 channelMerger.connect(context.destination); | |
147 | |
148 // When merging, exchange channel layout: left->right, right->left | |
149 channelSplitter.connect(channelMerger, 0, 1); | |
150 channelSplitter.connect(channelMerger, 1, 0); | |
151 | |
152 sourceNode.start(0); | |
153 | |
154 context.oncomplete = checkResult; | |
155 context.startRendering(); | |
156 } | |
157 | |
158 runTest(); | |
159 | |
160 </script> | |
161 | |
162 </body> | |
163 </html> | |
OLD | NEW |