OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <script src="../resources/js-test.js"></script> | |
5 <script src="resources/compatibility.js"></script> | |
6 <script src="resources/audit-util.js"></script> | |
7 <script src="resources/audio-testing.js"></script> | |
8 </head> | |
9 | |
10 <body> | |
11 | |
12 <div id="description"></div> | |
13 <div id="console"></div> | |
14 | |
15 <script> | |
16 description("Tests ScriptProcessorNode."); | |
17 | |
18 var sampleRate = 44100.0; | |
19 var outputChannels = 6; | |
20 var playbackTime = 0.0; | |
21 | |
22 // For the current implementation of ScriptProcessorNode, when it works with Off
lineAudioContext (which runs much faster | |
23 // than real-time) the event.inputBuffer might be overwrite again before onaudio
process ever get chance to be called. | |
24 // We carefully arrange the renderLengthInFrames and bufferSize to have exactly
the same value to avoid this issue. | |
25 var renderLengthInFrames = 512; | |
26 var bufferSize = 512; | |
27 | |
28 var context; | |
29 | |
30 function createBuffer(context, length) { | |
31 var audioBuffer = context.createBuffer(2, length, sampleRate); | |
32 var n = audioBuffer.length; | |
33 var dataL = audioBuffer.getChannelData(0); | |
34 var dataR = audioBuffer.getChannelData(1); | |
35 | |
36 for (var i = 0; i < n; ++i) { | |
37 dataL[i] = -1; | |
38 dataR[i] = 1; | |
39 } | |
40 | |
41 return audioBuffer; | |
42 } | |
43 | |
44 function processAudioData(event) { | |
45 playbackTime = event.playbackTime; | |
46 var expectedTime = context.currentTime + (bufferSize / context.sampleRate); | |
47 var allowedTimeGap = 0.0000001; | |
48 | |
49 // There may be a little time gap which is from different thread operation | |
50 // between currentTime when main thread fires onaudioprocess() and currentTi
me when read in JS | |
51 // since currentTime is continuously increasing on audio thread. | |
52 | |
53 shouldBeCloseTo("playbackTime", expectedTime, allowedTimeGap, true); | |
54 | |
55 buffer = event.outputBuffer; | |
56 if (buffer.numberOfChannels != outputChannels) | |
57 testFailed("numberOfOutputChannels doesn't match!"); | |
58 | |
59 if (buffer.length != bufferSize) | |
60 testFailed("numberOfOutputChannels doesn't match!"); | |
61 | |
62 buffer = event.inputBuffer; | |
63 var bufferDataL = buffer.getChannelData(0); | |
64 var bufferDataR = buffer.getChannelData(1); | |
65 | |
66 var success = true; | |
67 // Go through every sample and make sure it's all -1 for the left-channel, a
nd all +1 for the right-channel. | |
68 for (var i = 0; i < buffer.length; ++i) { | |
69 if (bufferDataL[i] != -1 || bufferDataR[i] != 1) { | |
70 success = false; | |
71 break; | |
72 } | |
73 } | |
74 | |
75 if (success) { | |
76 testPassed("onaudioprocess was called with correct data."); | |
77 } else { | |
78 testFailed("onaudioprocess was called with wrong data."); | |
79 } | |
80 } | |
81 | |
82 function doBufferSizeTest(size) { | |
83 try { | |
84 var jsnode = context.createScriptProcessor(size, 1, 1); | |
85 testPassed("Successfully created ScriptProcessorNode with bufferSize = "
+ size + "."); | |
86 } catch(e) { | |
87 testFailed("Failed to create ScriptProcessorNode with bufferSize = " + s
ize + "."); | |
88 } | |
89 } | |
90 | |
91 function runTest() { | |
92 if (window.testRunner) { | |
93 testRunner.dumpAsText(); | |
94 testRunner.waitUntilDone(); | |
95 } | |
96 | |
97 window.jsTestIsAsync = true; | |
98 | |
99 // Create offline audio context. | |
100 context = new OfflineAudioContext(2, renderLengthInFrames, sampleRate); | |
101 | |
102 try { | |
103 var jsnode = context.createScriptProcessor(512, 0, 0); | |
104 testFailed("Exception should be thrown when both numberOfInputChannels a
nd numberOfOutputChannels are zero."); | |
105 } catch(e) { | |
106 testPassed("Exception was thrown when both numberOfInputChannels and num
berOfOutputChannels are zero."); | |
107 } | |
108 | |
109 try { | |
110 var jsnode = context.createScriptProcessor(512, 1, 0); | |
111 testPassed("Successfully created ScriptProcessorNode with numberOfInputC
hannels = 1 and numberOfOutputChannels = 0."); | |
112 } catch(e) { | |
113 testFailed("Exception should not be thrown when numberOfInputChannels =
1 and numberOfOutputChannels = 0."); | |
114 } | |
115 | |
116 try { | |
117 var jsnode = context.createScriptProcessor(512, 2, 0); | |
118 testPassed("Successfully created ScriptProcessorNode with numberOfInputC
hannels = 2 and numberOfOutputChannels = 0."); | |
119 } catch(e) { | |
120 testFailed("Exception should not be thrown when numberOfInputChannels =
2 and numberOfOutputChannels = 0."); | |
121 } | |
122 | |
123 try { | |
124 var jsnode = context.createScriptProcessor(512, 0, 1); | |
125 testPassed("Successfully created ScriptProcessorNode with numberOfInputC
hannels = 0 and numberOfOutputChannels = 1."); | |
126 } catch(e) { | |
127 testFailed("Exception should not be thrown when numberOfInputChannels =
0 and numberOfOutputChannels = 1."); | |
128 } | |
129 | |
130 try { | |
131 var jsnode = context.createScriptProcessor(512, 0, 2); | |
132 testPassed("Successfully created ScriptProcessorNode with numberOfInputC
hannels = 0 and numberOfOutputChannels = 2."); | |
133 } catch(e) { | |
134 testFailed("Exception should not be thrown when numberOfInputChannels =
0 and numberOfOutputChannels = 2."); | |
135 } | |
136 | |
137 try { | |
138 var jsnode = context.createScriptProcessor(511, 1, 1); | |
139 testFailed("Exception should be thrown for illegal bufferSize."); | |
140 } catch(e) { | |
141 testPassed("Exception was thrown for illegal bufferSize."); | |
142 } | |
143 | |
144 doBufferSizeTest(256); | |
145 doBufferSizeTest(512); | |
146 doBufferSizeTest(1024); | |
147 doBufferSizeTest(2048); | |
148 doBufferSizeTest(4096); | |
149 doBufferSizeTest(8192); | |
150 doBufferSizeTest(16384); | |
151 | |
152 var sourceBuffer = createBuffer(context, renderLengthInFrames); | |
153 | |
154 var bufferSource = context.createBufferSource(); | |
155 bufferSource.buffer = sourceBuffer; | |
156 | |
157 var jsnode = context.createScriptProcessor(bufferSize, 2, outputChannels); | |
158 | |
159 bufferSource.connect(jsnode); | |
160 jsnode.connect(context.destination); | |
161 jsnode.onaudioprocess = processAudioData; | |
162 | |
163 bufferSource.start(0); | |
164 context.oncomplete = finishJSTest; | |
165 context.startRendering(); | |
166 } | |
167 | |
168 runTest(); | |
169 | |
170 </script> | |
171 | |
172 </body> | |
173 </html> | |
OLD | NEW |