| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="../resources/js-test-pre.js"></script> |
| <script type="text/javascript" src="resources/audio-testing.js"></script> |
| </head> |
| |
| <body> |
| |
| <div id="description"></div> |
| <div id="console"></div> |
| |
| <script> |
| description("Tests ScriptProcessorNode."); |
| |
| var sampleRate = 44100.0; |
| var outputChannels = 6; |
| |
| // For the current implementation of ScriptProcessorNode, when it works with OfflineAudioContext (which runs much faster |
| // than real-time) the event.inputBuffer might be overwrite again before onaudioprocess ever get chance to be called. |
| // We carefully arrange the renderLengthInFrames and bufferSize to have exactly the same value to avoid this issue. |
| var renderLengthInFrames = 512; |
| var bufferSize = 512; |
| |
| var context; |
| |
| function createBuffer(context, length) { |
| var audioBuffer = context.createBuffer(2, length, sampleRate); |
| var n = audioBuffer.length; |
| var dataL = audioBuffer.getChannelData(0); |
| var dataR = audioBuffer.getChannelData(1); |
| |
| for (var i = 0; i < n; ++i) { |
| dataL[i] = -1; |
| dataR[i] = 1; |
| } |
| |
| return audioBuffer; |
| } |
| |
| function processAudioData(event) { |
| buffer = event.outputBuffer; |
| if (buffer.numberOfChannels != outputChannels) |
| testFailed("numberOfOutputChannels doesn't match!"); |
| |
| if (buffer.length != bufferSize) |
| testFailed("numberOfOutputChannels doesn't match!"); |
| |
| buffer = event.inputBuffer; |
| var bufferDataL = buffer.getChannelData(0); |
| var bufferDataR = buffer.getChannelData(1); |
| |
| var success = true; |
| // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel. |
| for (var i = 0; i < buffer.length; ++i) { |
| if (bufferDataL[i] != -1 || bufferDataR[i] != 1) { |
| success = false; |
| break; |
| } |
| } |
| |
| if (success) { |
| testPassed("onaudioprocess was called with correct data."); |
| } else { |
| testFailed("onaudioprocess was called with wrong data."); |
| } |
| } |
| |
| function doBufferSizeTest(size) { |
| try { |
| var jsnode = context.createScriptProcessor(size, 1, 1); |
| testPassed("Successfully created ScriptProcessorNode with bufferSize = " + size + "."); |
| } catch(e) { |
| testFailed("Failed to create ScriptProcessorNode with bufferSize = " + size + "."); |
| } |
| } |
| |
| function runTest() { |
| if (window.testRunner) { |
| testRunner.dumpAsText(); |
| testRunner.waitUntilDone(); |
| } |
| |
| window.jsTestIsAsync = true; |
| |
| // Create offline audio context. |
| context = new webkitOfflineAudioContext(2, renderLengthInFrames, sampleRate); |
| |
| try { |
| var jsnode = context.createScriptProcessor(512, 0, 0); |
| testFailed("Exception should be thrown when both numberOfInputChannels and numberOfOutputChannels are zero."); |
| } catch(e) { |
| testPassed("Exception was thrown when both numberOfInputChannels and numberOfOutputChannels are zero."); |
| } |
| |
| try { |
| var jsnode = context.createScriptProcessor(512, 1, 0); |
| testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 1 and numberOfOutputChannels = 0."); |
| } catch(e) { |
| testFailed("Exception should not be thrown when numberOfInputChannels = 1 and numberOfOutputChannels = 0."); |
| } |
| |
| try { |
| var jsnode = context.createScriptProcessor(512, 2, 0); |
| testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 2 and numberOfOutputChannels = 0."); |
| } catch(e) { |
| testFailed("Exception should not be thrown when numberOfInputChannels = 2 and numberOfOutputChannels = 0."); |
| } |
| |
| try { |
| var jsnode = context.createScriptProcessor(512, 0, 1); |
| testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 1."); |
| } catch(e) { |
| testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 1."); |
| } |
| |
| try { |
| var jsnode = context.createScriptProcessor(512, 0, 2); |
| testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 2."); |
| } catch(e) { |
| testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 2."); |
| } |
| |
| try { |
| var jsnode = context.createScriptProcessor(511, 1, 1); |
| testFailed("Exception should be thrown for illegal bufferSize."); |
| } catch(e) { |
| testPassed("Exception was thrown for illegal bufferSize."); |
| } |
| |
| doBufferSizeTest(256); |
| doBufferSizeTest(512); |
| doBufferSizeTest(1024); |
| doBufferSizeTest(2048); |
| doBufferSizeTest(4096); |
| doBufferSizeTest(8192); |
| doBufferSizeTest(16384); |
| |
| var sourceBuffer = createBuffer(context, renderLengthInFrames); |
| |
| var bufferSource = context.createBufferSource(); |
| bufferSource.buffer = sourceBuffer; |
| |
| var jsnode = context.createScriptProcessor(bufferSize, 2, outputChannels); |
| |
| bufferSource.connect(jsnode); |
| jsnode.connect(context.destination); |
| jsnode.onaudioprocess = processAudioData; |
| |
| bufferSource.noteOn(0); |
| context.oncomplete = finishJSTest; |
| context.startRendering(); |
| } |
| |
| runTest(); |
| |
| </script> |
| |
| <script src="../resources/js-test-post.js"></script> |
| </body> |
| </html> |