4 <script src="../resources/js-test.js"></script>
5 <script type="text/javascript" src="resources/audio-testing.js"></script>
10 <div id="description"></div>
11 <div id="console"></div>
14 description("Tests ScriptProcessorNode.");
16 var sampleRate = 44100.0;
17 var outputChannels = 6;
19 // For the current implementation of ScriptProcessorNode, when it works with OfflineAudioContext (which runs much faster
20 // than real-time) the event.inputBuffer might be overwrite again before onaudioprocess ever get chance to be called.
21 // We carefully arrange the renderLengthInFrames and bufferSize to have exactly the same value to avoid this issue.
22 var renderLengthInFrames = 512;
27 function createBuffer(context, length) {
28 var audioBuffer = context.createBuffer(2, length, sampleRate);
29 var n = audioBuffer.length;
30 var dataL = audioBuffer.getChannelData(0);
31 var dataR = audioBuffer.getChannelData(1);
33 for (var i = 0; i < n; ++i) {
41 function processAudioData(event) {
42 buffer = event.outputBuffer;
43 if (buffer.numberOfChannels != outputChannels)
44 testFailed("numberOfOutputChannels doesn't match!");
46 if (buffer.length != bufferSize)
47 testFailed("numberOfOutputChannels doesn't match!");
49 buffer = event.inputBuffer;
50 var bufferDataL = buffer.getChannelData(0);
51 var bufferDataR = buffer.getChannelData(1);
54 // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
55 for (var i = 0; i < buffer.length; ++i) {
56 if (bufferDataL[i] != -1 || bufferDataR[i] != 1) {
63 testPassed("onaudioprocess was called with correct data.");
65 testFailed("onaudioprocess was called with wrong data.");
69 function doBufferSizeTest(size) {
71 var jsnode = context.createScriptProcessor(size, 1, 1);
72 testPassed("Successfully created ScriptProcessorNode with bufferSize = " + size + ".");
74 testFailed("Failed to create ScriptProcessorNode with bufferSize = " + size + ".");
79 if (window.testRunner) {
80 testRunner.dumpAsText();
81 testRunner.waitUntilDone();
84 window.jsTestIsAsync = true;
86 // Create offline audio context.
87 context = new webkitOfflineAudioContext(2, renderLengthInFrames, sampleRate);
90 var jsnode = context.createScriptProcessor(512, 0, 0);
91 testFailed("Exception should be thrown when both numberOfInputChannels and numberOfOutputChannels are zero.");
93 testPassed("Exception was thrown when both numberOfInputChannels and numberOfOutputChannels are zero.");
97 var jsnode = context.createScriptProcessor(512, 1, 0);
98 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 1 and numberOfOutputChannels = 0.");
100 testFailed("Exception should not be thrown when numberOfInputChannels = 1 and numberOfOutputChannels = 0.");
104 var jsnode = context.createScriptProcessor(512, 2, 0);
105 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 2 and numberOfOutputChannels = 0.");
107 testFailed("Exception should not be thrown when numberOfInputChannels = 2 and numberOfOutputChannels = 0.");
111 var jsnode = context.createScriptProcessor(512, 0, 1);
112 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 1.");
114 testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 1.");
118 var jsnode = context.createScriptProcessor(512, 0, 2);
119 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 2.");
121 testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 2.");
125 var jsnode = context.createScriptProcessor(511, 1, 1);
126 testFailed("Exception should be thrown for illegal bufferSize.");
128 testPassed("Exception was thrown for illegal bufferSize.");
131 doBufferSizeTest(256);
132 doBufferSizeTest(512);
133 doBufferSizeTest(1024);
134 doBufferSizeTest(2048);
135 doBufferSizeTest(4096);
136 doBufferSizeTest(8192);
137 doBufferSizeTest(16384);
139 var sourceBuffer = createBuffer(context, renderLengthInFrames);
141 var bufferSource = context.createBufferSource();
142 bufferSource.buffer = sourceBuffer;
144 var jsnode = context.createScriptProcessor(bufferSize, 2, outputChannels);
146 bufferSource.connect(jsnode);
147 jsnode.connect(context.destination);
148 jsnode.onaudioprocess = processAudioData;
150 bufferSource.start(0);
151 context.oncomplete = finishJSTest;
152 context.startRendering();