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