Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|
hongchan
2017/01/04 00:38:12
If this file includes manual changes, please simpl
Raymond Toy
2017/01/04 19:05:02
Done.
| |
| 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> |
| 10 <div id="description"></div> | 11 <script> |
| 11 <div id="console"></div> | 12 let audit = Audit.createTaskRunner(); |
| 12 | 13 |
| 13 <script> | |
| 14 // These are global to make debugging a little easier. | 14 // These are global to make debugging a little easier. |
| 15 var context; | 15 let context; |
| 16 var buffer; | 16 let buffer; |
| 17 var source; | 17 let source; |
| 18 var renderedData; | 18 let renderedData; |
| 19 var trueData; | 19 let trueData; |
| 20 var signalEnergy; | 20 let signalEnergy; |
| 21 var noiseEnergy; | 21 let noiseEnergy; |
| 22 var maxError; | 22 let maxError; |
| 23 | 23 |
| 24 // Context sample rate. | 24 // Context sample rate. |
| 25 var sampleRate = 48000; | 25 let sampleRate = 48000; |
| 26 // The sample rate of the buffer. | 26 // The sample rate of the buffer. |
| 27 var bufferRate = 3000; | 27 let bufferRate = 3000; |
| 28 // The audio buffer is a sine wave of this frequency. | 28 // The audio buffer is a sine wave of this frequency. |
| 29 var toneFrequency = 440; | 29 let toneFrequency = 440; |
| 30 // How long test is | 30 // How long test is |
| 31 var lengthInSeconds = 0.5; | 31 let lengthInSeconds = 0.5; |
| 32 // The maximum allowed peak error between the actual and true output. This value was | 32 // The maximum allowed peak error between the actual and true output. This |
| 33 // experimentally determined for the given bufferRate. | 33 // value was experimentally determined for the given bufferRate. |
| 34 var peakThreshold = 0.11; | 34 let peakThreshold = 0.11; |
| 35 // The minimum SNR allowed between the actual and true output. | 35 // The minimum SNR allowed between the actual and true output. |
| 36 var snrThreshold = 22.35; | 36 let snrThreshold = 22.35; |
| 37 | 37 |
| 38 description("Test resampling of an AudioBuffer at " + bufferRate + " Hz"); | |
| 39 | 38 |
| 40 function log10(x) { | 39 // Generate a sine wave in an AudioBuffer using the given |freq|. The |
| 41 return Math.log(x)/Math.LN10; | 40 // AudioBuffer has the sample rate of |rate|. |
| 42 } | |
| 43 | |
| 44 // Generate a sine wave in an AudioBuffer using the given |freq|. The Audi oBuffer has the | |
| 45 // sample rate of |rate|. | |
| 46 function createSineBuffer(context, freq, rate) { | 41 function createSineBuffer(context, freq, rate) { |
| 47 var buf = context.createBuffer(1, lengthInSeconds * rate, rate); | 42 let buf = context.createBuffer(1, lengthInSeconds * rate, rate); |
| 48 var omega = 2 * Math.PI * freq / rate; | 43 let omega = 2 * Math.PI * freq / rate; |
| 49 var signal = buf.getChannelData(0); | 44 let signal = buf.getChannelData(0); |
| 50 var length = signal.length; | 45 let length = signal.length; |
| 51 for (var k = 0; k < length; ++k) | 46 for (let k = 0; k < length; ++k) |
| 52 signal[k] = Math.sin(omega * k); | 47 signal[k] = Math.sin(omega * k); |
| 53 | 48 |
| 54 return buf; | 49 return buf; |
| 55 } | 50 } |
| 56 | 51 |
| 57 // Check the output against the expected output. | 52 // Check the output against the expected output. |
| 58 function checkResult(event) { | 53 function checkResult(buffer, should) { |
| 59 renderedData = event.renderedBuffer.getChannelData(0); | 54 renderedData = buffer.getChannelData(0); |
| 60 var length = renderedData.length; | 55 let length = renderedData.length; |
| 61 // Generate a reference sine wave at the context rate | 56 // Generate a reference sine wave at the context rate |
| 62 var trueReference = createSineBuffer(context, toneFrequency, context.s ampleRate); | 57 let trueReference = createSineBuffer(context, toneFrequency, context.sam pleRate); |
| 63 trueData = trueReference.getChannelData(0); | 58 trueData = trueReference.getChannelData(0); |
| 64 | 59 |
| 65 // To compare the actual output against the reference, we compute the peak error and the | 60 // To compare the actual output against the reference, we compute the |
| 66 // SNR between the two. | 61 // peak error and the SNR between the two. |
| 67 signalEnergy = 0; | 62 signalEnergy = 0; |
| 68 noiseEnergy = 0; | 63 noiseEnergy = 0; |
| 69 maxError = -1; | 64 maxError = -1; |
| 70 | 65 |
| 71 var success = true; | 66 let success = true; |
| 72 | 67 |
| 73 // Compute the peak error and the SNR. | 68 // Compute the peak error and the SNR. |
| 74 for (var k = 0; k < length / 2; ++k) { | 69 for (let k = 0; k < length / 2; ++k) { |
| 75 var diff = renderedData[k] - trueData[k]; | 70 let diff = renderedData[k] - trueData[k]; |
| 76 noiseEnergy += diff * diff; | 71 noiseEnergy += diff * diff; |
| 77 signalEnergy += trueData[k] * trueData[k]; | 72 signalEnergy += trueData[k] * trueData[k]; |
| 78 if (Math.abs(diff) > maxError) | 73 if (Math.abs(diff) > maxError) |
| 79 maxError = Math.abs(diff); | 74 maxError = Math.abs(diff); |
| 80 } | 75 } |
| 81 | 76 |
| 82 var snr; | 77 let snr; |
| 83 | 78 |
| 84 if (noiseEnergy == 0) | 79 if (noiseEnergy == 0) |
| 85 snr = 1000; | 80 snr = 1000; |
| 86 else | 81 else |
| 87 snr = 10 * log10(signalEnergy / noiseEnergy); | 82 snr = 10 * Math.log10(signalEnergy / noiseEnergy); |
| 88 | |
| 89 if (maxError < peakThreshold) { | |
| 90 testPassed("Peak error between actual and reference data below thr eshold of " + | |
| 91 peakThreshold + "."); | |
| 92 } else { | |
| 93 testFailed("Peak error of " + maxError + " exceeds threshold of " + | |
| 94 peakThreshold + "."); | |
| 95 success = false; | |
| 96 } | |
| 97 | 83 |
| 98 if (snr > snrThreshold) { | 84 should(maxError, "Peak error between actual and reference data") |
| 99 testPassed("SNR exceeds threshold of " + snrThreshold + " dB."); | 85 .beLessThan(peakThreshold); |
|
hongchan
2017/01/04 00:38:12
It looks like the new audit pays off. Happy to hav
Raymond Toy
2017/01/04 19:05:02
To be fair, the old Audit would have simplified th
| |
| 100 } else { | |
| 101 testFailed("SNR of " + snr + " is below the threshold of " + snrTh reshold + "."); | |
| 102 success = false; | |
| 103 } | |
| 104 | |
| 105 if (success) | |
| 106 testPassed("AudioBuffer resampling is accurate for buffer rate of " + | |
| 107 bufferRate + " Hz."); | |
| 108 else | |
| 109 testFailed("AudioBuffer resampling is not accurate enough for buff er rate of " + | |
| 110 bufferRate + " Hz."); | |
| 111 | 86 |
| 112 finishJSTest(); | 87 should(snr, "SNR") |
| 88 .beGreaterThan(snrThreshold); | |
| 113 } | 89 } |
| 114 | 90 |
| 115 function runTest() { | 91 audit.define("Test resampling of an AudioBuffer", function (task, should) { |
|
hongchan
2017/01/04 00:38:12
Please wrap this line.
Raymond Toy
2017/01/04 19:05:03
Done.
| |
| 116 if (window.testRunner) { | 92 context = new OfflineAudioContext(1, lengthInSeconds * sampleRate, |
| 117 testRunner.dumpAsText(); | 93 sampleRate); |
| 118 testRunner.waitUntilDone(); | |
| 119 } | |
| 120 | 94 |
| 121 window.jsTestIsAsync = true; | 95 // Create a sine wave in a buffer that's different from the context rate to test |
|
hongchan
2017/01/04 00:38:12
ditto.
Raymond Toy
2017/01/04 19:05:02
Done.
| |
| 96 // resampling. | |
| 97 buffer = createSineBuffer(context, toneFrequency, bufferRate); | |
| 98 source = context.createBufferSource(); | |
| 99 source.buffer = buffer; | |
| 100 source.connect(context.destination); | |
| 101 source.start(); | |
| 122 | 102 |
| 123 context = new OfflineAudioContext(1, lengthInSeconds * sampleRate, sam pleRate); | 103 context.startRendering() |
| 104 .then(function (buffer) { | |
| 105 checkResult(buffer, should); | |
| 106 }) | |
| 107 .then(task.done.bind(task)); | |
| 108 }); | |
| 124 | 109 |
| 125 // Create a sine wave in a buffer that's different from the context ra te to test | 110 audit.run(); |
| 126 // resampling. | |
| 127 buffer = createSineBuffer(context, toneFrequency, bufferRate); | |
| 128 source = context.createBufferSource(); | |
| 129 source.buffer = buffer; | |
| 130 source.connect(context.destination); | |
| 131 source.start(); | |
| 132 | |
| 133 context.oncomplete = checkResult; | |
| 134 context.startRendering(); | |
| 135 } | |
| 136 runTest(); | |
| 137 successfullyParsed = true; | |
| 138 </script> | 111 </script> |
| 139 </body> | 112 </body> |
| 140 </html> | 113 </html> |
| OLD | NEW |