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