| OLD | NEW |
| (Empty) | |
| 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 <html> |
| 3 <head> |
| 4 <script src="resources/compatibility.js"></script> |
| 5 <script src="resources/audio-testing.js"></script> |
| 6 <script src="../resources/js-test.js"></script> |
| 7 </head> |
| 8 |
| 9 <body> |
| 10 <div id="description"></div> |
| 11 <div id="console"></div> |
| 12 |
| 13 <script> |
| 14 // These are global to make debugging a little easier. |
| 15 var context; |
| 16 var buffer; |
| 17 var source; |
| 18 var renderedData; |
| 19 var trueData; |
| 20 var signalEnergy; |
| 21 var noiseEnergy; |
| 22 var maxError; |
| 23 |
| 24 // Context sample rate. |
| 25 var sampleRate = 48000; |
| 26 // The sample rate of the buffer. |
| 27 var bufferRate = 3000; |
| 28 // The audio buffer is a sine wave of this frequency. |
| 29 var toneFrequency = 440; |
| 30 // How long test is |
| 31 var lengthInSeconds = 0.5; |
| 32 // The maximum allowed peak error between the actual and true output. This
value was |
| 33 // experimentally determined for the given bufferRate. |
| 34 var peakThreshold = 0.11; |
| 35 // The minimum SNR allowed between the actual and true output. |
| 36 var snrThreshold = 22.35; |
| 37 |
| 38 description("Test resampling of an AudioBuffer at " + bufferRate + " Hz"); |
| 39 |
| 40 function log10(x) { |
| 41 return Math.log(x)/Math.LN10; |
| 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) { |
| 47 var buf = context.createBuffer(1, lengthInSeconds * rate, rate); |
| 48 var omega = 2 * Math.PI * freq / rate; |
| 49 var signal = buf.getChannelData(0); |
| 50 var length = signal.length; |
| 51 for (var k = 0; k < length; ++k) |
| 52 signal[k] = Math.sin(omega * k); |
| 53 |
| 54 return buf; |
| 55 } |
| 56 |
| 57 // Check the output against the expected output. |
| 58 function checkResult(event) { |
| 59 renderedData = event.renderedBuffer.getChannelData(0); |
| 60 var length = renderedData.length; |
| 61 // Generate a reference sine wave at the context rate |
| 62 var trueReference = createSineBuffer(context, toneFrequency, context.s
ampleRate); |
| 63 trueData = trueReference.getChannelData(0); |
| 64 |
| 65 // To compare the actual output against the reference, we compute the
peak error and the |
| 66 // SNR between the two. |
| 67 signalEnergy = 0; |
| 68 noiseEnergy = 0; |
| 69 maxError = -1; |
| 70 |
| 71 var success = true; |
| 72 |
| 73 // Compute the peak error and the SNR. |
| 74 for (var k = 0; k < length / 2; ++k) { |
| 75 var diff = renderedData[k] - trueData[k]; |
| 76 noiseEnergy += diff * diff; |
| 77 signalEnergy += trueData[k] * trueData[k]; |
| 78 if (Math.abs(diff) > maxError) |
| 79 maxError = Math.abs(diff); |
| 80 } |
| 81 |
| 82 var snr; |
| 83 |
| 84 if (noiseEnergy == 0) |
| 85 snr = 1000; |
| 86 else |
| 87 snr = 10 * 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 |
| 98 if (snr > snrThreshold) { |
| 99 testPassed("SNR exceeds threshold of " + snrThreshold + " dB."); |
| 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 |
| 112 finishJSTest(); |
| 113 } |
| 114 |
| 115 function runTest() { |
| 116 if (window.testRunner) { |
| 117 testRunner.dumpAsText(); |
| 118 testRunner.waitUntilDone(); |
| 119 } |
| 120 |
| 121 window.jsTestIsAsync = true; |
| 122 |
| 123 context = new OfflineAudioContext(1, lengthInSeconds * sampleRate, sam
pleRate); |
| 124 |
| 125 // Create a sine wave in a buffer that's different from the context ra
te to test |
| 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> |
| 139 </body> |
| 140 </html> |
| OLD | NEW |