OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <script src="../../resources/testharness.js"></script> |
| 5 <script src="../../resources/testharnessreport.js"></script> |
| 6 <script src="../resources/audit-util.js"></script> |
| 7 <script src="../resources/audit.js"></script> |
| 8 <title>Handle Silent Inputs to AnalyserNode</title> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 let audit = Audit.createTaskRunner(); |
| 14 let sampleRate = 16000; |
| 15 let renderDuration = 1; |
| 16 let renderFrames = renderDuration * sampleRate; |
| 17 |
| 18 audit.define('connected', function(task, should) { |
| 19 task.describe('Test handling of silent inputs'); |
| 20 |
| 21 tester(should, false).then(task.done.bind(task)); |
| 22 }); |
| 23 |
| 24 audit.define('auto-pull', function(task, should) { |
| 25 task.describe('Test handling of silent inputs'); |
| 26 |
| 27 tester(should, true).then(task.done.bind(task)); |
| 28 }); |
| 29 |
| 30 audit.define('timing', function(task, should) { |
| 31 task.describe('Test shifting in of zeroes after source has stopped'); |
| 32 |
| 33 let renderQuantumFrames = 128; |
| 34 |
| 35 // sampleRate chosen to be a power of two so we don't have round-off |
| 36 // errors in computing the times for when to suspend the context. |
| 37 let context = new OfflineAudioContext(1, 16384, 16384); |
| 38 let source = new ConstantSourceNode(context); |
| 39 |
| 40 // The fftSize for the analyser is fairly arbitrary, except the code |
| 41 // assumes it is larger than 128. |
| 42 let analyser = new AnalyserNode(context, {fftSize: 2048}); |
| 43 |
| 44 source.connect(analyser).connect(context.destination); |
| 45 |
| 46 source.start(); |
| 47 |
| 48 // Stop the source after 1 fftSize frames. |
| 49 let time = analyser.fftSize / context.sampleRate; |
| 50 source.stop(time); |
| 51 |
| 52 // Verify that the time data at this point is constant. |
| 53 context.suspend(time) |
| 54 .then(() => { |
| 55 let data = new Float32Array(analyser.fftSize); |
| 56 analyser.getFloatTimeDomainData(data); |
| 57 should( |
| 58 data, 'At time ' + context.currentTime + |
| 59 ' Analyser frames [0, ' + analyser.fftSize + ')') |
| 60 .beConstantValueOf(1); |
| 61 }) |
| 62 .then(context.resume.bind(context)); |
| 63 |
| 64 // After each rendering quantum from the point at which the source |
| 65 // stopped, verify that zeroes are inserted into the time data one |
| 66 // rendering quantum at a time. |
| 67 |
| 68 let limit = analyser.fftSize / renderQuantumFrames; |
| 69 |
| 70 for (let k = 1; k <= limit; ++k) { |
| 71 let analyserTime = |
| 72 (analyser.fftSize + k * renderQuantumFrames) / context.sampleRate; |
| 73 context.suspend(analyserTime) |
| 74 .then(() => { |
| 75 let data = new Float32Array(analyser.fftSize); |
| 76 let indexNewest = analyser.fftSize - k * renderQuantumFrames; |
| 77 analyser.getFloatTimeDomainData(data); |
| 78 if (k < limit) { |
| 79 should( |
| 80 data.slice(0, indexNewest), 'At time ' + |
| 81 context.currentTime + ' Analyser frames [0, ' + |
| 82 indexNewest + ')') |
| 83 .beConstantValueOf(1); |
| 84 } |
| 85 should( |
| 86 data.slice(indexNewest), 'At time ' + context.currentTime + |
| 87 ' Analyser frames [' + indexNewest + ', ' + |
| 88 analyser.fftSize + ')') |
| 89 .beConstantValueOf(0); |
| 90 }) |
| 91 .then(context.resume.bind(context)); |
| 92 } |
| 93 |
| 94 // Start the test |
| 95 context.startRendering().then(() => task.done()); |
| 96 }); |
| 97 |
| 98 audit.run(); |
| 99 |
| 100 function tester(should, isAutoPullTest) { |
| 101 // Connect an oscillator to an analyser for testing the time data of the |
| 102 // analyser after the oscillator stops. |
| 103 let context = new OfflineAudioContext(1, renderFrames, sampleRate); |
| 104 let source = new OscillatorNode(context); |
| 105 let analyser = new AnalyserNode(context, {fftSize: 128}); |
| 106 let timeData = new Float32Array(analyser.fftSize); |
| 107 timeData.fill(NaN); |
| 108 |
| 109 source.connect(analyser); |
| 110 |
| 111 // For the automatic pull test, leave the analyser output disconnected. |
| 112 if (isAutoPullTest) { |
| 113 source.connect(context.destination); |
| 114 } else { |
| 115 analyser.connect(context.destination); |
| 116 } |
| 117 |
| 118 source.start(); |
| 119 |
| 120 // Stop the source well in advance of when we want to get the time data |
| 121 // from the analyser. |
| 122 let stopTime = 0.1; |
| 123 let dataTime = 0.5; |
| 124 |
| 125 source.stop(stopTime); |
| 126 context.suspend(dataTime) |
| 127 .then(() => { analyser.getFloatTimeDomainData(timeData); }) |
| 128 .then(context.resume.bind(context)); |
| 129 |
| 130 return context.startRendering().then(buffer => { |
| 131 should(timeData, 'Analyser time data at time ' + dataTime) |
| 132 .beConstantValueOf(0); |
| 133 }); |
| 134 } |
| 135 </script> |
| 136 </body> |
| 137 </html> |
OLD | NEW |