Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!doctype html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <title>Test Oscillator Node: sine</title> | |
| 5 <script src="resources/compatibility.js"></script> | |
| 6 <script src="resources/buffer-loader.js"></script> | |
| 7 <script src="../resources/js-test.js"></script> | |
| 8 <script src="resources/oscillator-testing.js"></script> | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 <script> | |
| 13 // See oscillator-sine.html for more info on the actual wave shape | |
|
hongchan
2014/11/11 19:03:14
TYPO: Missing period?
Raymond Toy
2014/11/11 19:48:02
Done.
| |
| 14 // | |
| 15 // This test is a partial duplicate of oscillator-sine but is designed t o be less sensitive | |
| 16 // to the actual output versus the reference. Instead of requiring an ex act match, we check | |
| 17 // that for several criteria to pass. The SNR between the actual and exp ected signals be | |
|
hongchan
2014/11/11 19:03:14
TYPO: "should" or "must" be?
Raymond Toy
2014/11/11 19:48:02
Done.
| |
| 18 // large enough. The maximum difference must be below a threshold and t he actual number of | |
| 19 // points that are different must be below a threshold. | |
| 20 | |
| 21 var sampleRate = 44100.0; | |
| 22 var nyquist = 0.5 * sampleRate; | |
| 23 var lengthInSeconds = 4; | |
| 24 var lowFrequency = 10; | |
| 25 var highFrequency = nyquist + 2000; // go slightly higher than nyquist t o make sure we generate silence there | |
| 26 var context = 0; | |
| 27 var reference = 0; | |
| 28 var renderedData = 0; | |
| 29 var signalPower = 0; | |
| 30 var noisePower = 0; | |
| 31 | |
| 32 // SNR must be greater than this to pass the test. | |
| 33 // Q: Why is the SNR threshold not infinity? | |
| 34 // A: The reference result is a 16-bit WAV file, so it won't compare exa ctly with the | |
| 35 // floating point result. | |
| 36 var thresholdSNR = 86.5; | |
| 37 // Max diff must be less than this to pass the test | |
| 38 var thresholdDiff = 1.5; | |
| 39 // The number of times the diff exceeds the threshold should be less tha n this to pass. | |
| 40 var thresholdDiffCount = 8000; | |
| 41 | |
| 42 function db(sPower, nPower) | |
| 43 { | |
| 44 if (nPower == 0 && sPower > 0) { | |
| 45 return 1000; | |
| 46 } | |
| 47 return 10 * Math.log10(sPower / nPower); | |
| 48 } | |
| 49 | |
| 50 function checkResult (event) { | |
| 51 renderedData = event.renderedBuffer.getChannelData(0); | |
| 52 // Compute signal to noise ratio between the result and the referenc e. Also keep track | |
| 53 // of the max difference (and position). | |
| 54 | |
| 55 var maxError = -1; | |
| 56 var errorPosition = -1; | |
| 57 var diffCount = 0; | |
| 58 | |
| 59 for (var k = 0; k < renderedData.length; ++k) { | |
| 60 var diff = renderedData[k] - reference[k]; | |
| 61 noisePower += diff * diff; | |
| 62 signalPower += reference[k] * reference[k]; | |
| 63 if (Math.abs(diff) > maxError) { | |
| 64 maxError = Math.abs(diff); | |
| 65 errorPosition = k; | |
| 66 } | |
| 67 // The reference file is a 16-bit WAV file, so we will never get an exact match | |
| 68 // between it and the actual floating-point result. | |
| 69 if (diff > 1/32768) { | |
| 70 diffCount++; | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 var snr = db(signalPower, noisePower); | |
| 75 if (snr < thresholdSNR) { | |
| 76 testFailed("Expected SNR of " + thresholdSNR + " dB, but actual SNR is " + snr + " dB"); | |
| 77 } else { | |
| 78 testPassed("Exceeded SNR threshold of " + thresholdSNR + " dB"); | |
| 79 } | |
| 80 if (maxError > thresholdDiff) { | |
| 81 testFailed("Maximum difference of " + maxError + " at " + errorP osition + " exceeded threshold of " + thresholdDiff); | |
| 82 } else { | |
| 83 testPassed("Maximum difference below threshold of " + thresholdD iff); | |
| 84 } | |
| 85 if (diffCount > thresholdDiffCount) { | |
| 86 testFailed(diffCount + " differences found but expected no more than " + thresholdDiffCount); | |
| 87 } else { | |
| 88 testPassed("Number of differences between actual and expected re sult is less than " + thresholdDiffCount); | |
| 89 } | |
| 90 | |
| 91 finishJSTest(); | |
| 92 } | |
| 93 | |
| 94 function finishedLoading(bufferList) { | |
| 95 reference = bufferList[0].getChannelData(0); | |
| 96 generateExponentialOscillatorSweep(context, "sine"); | |
| 97 context.oncomplete = checkResult; | |
| 98 context.startRendering(); | |
| 99 } | |
| 100 | |
| 101 function runTest () { | |
| 102 window.jsTestIsAsync = true; | |
| 103 | |
| 104 // Create offline audio context. | |
| 105 context = new OfflineAudioContext(1, sampleRate * lengthInSeconds, s ampleRate); | |
| 106 | |
| 107 bufferLoader = new BufferLoader( | |
| 108 context, | |
| 109 [ "oscillator-sine-expected.wav" ], | |
| 110 finishedLoading); | |
| 111 | |
| 112 bufferLoader.load(); | |
| 113 } | |
| 114 | |
| 115 runTest(); | |
| 116 successfullyParsed = true; | |
| 117 </script> | |
| 118 </body> | |
| 119 </html> | |
| OLD | NEW |