Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Unified Diff: LayoutTests/webaudio/resources/oscillator-testing.js

Issue 720293002: Replace oscillator tests with more robust tests (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: LayoutTests/webaudio/resources/oscillator-testing.js
diff --git a/LayoutTests/webaudio/resources/oscillator-testing.js b/LayoutTests/webaudio/resources/oscillator-testing.js
index 68e6b9fe14c809f9601f6ba9e7fdad25e8452de0..67f67877a3b404734c74fe10c5fff6367b01d016 100644
--- a/LayoutTests/webaudio/resources/oscillator-testing.js
+++ b/LayoutTests/webaudio/resources/oscillator-testing.js
@@ -9,6 +9,40 @@
//
// QUESTION: Why does the very end of the generated signal appear to get slightly weaker?
// ANSWER: This is an artifact of the algorithm to avoid aliasing.
+//
+// QUESTION: Since the tests compare the actual result with an expected reference file, how are the
+// reference files created?
+// ANSWER: Create an html with the following contents in the webaudio directory. Then run a layout
+// test on this file. A new file names "<file>-actual.wav" is created that contains the new result
+// that can be used as the new expected reference file. Replace the "sine" below with the
+// oscillator type that you want to use.
+//
+// <!DOCTYPE html>
+// <html>
+// <head>
+// <script type="text/javascript" src="resources/audio-testing.js"></script>
+// <script type="text/javascript" src="resources/oscillator-testing.js"></script>
+// </head>
+// <body>
+// <script>
+// window.onload = init;
+//
+// function init() {
+// if (!window.testRunner)
+// return;
+//
+// context = new OfflineAudioContext(1, sampleRate * lengthInSeconds, sampleRate);
+// generateExponentialOscillatorSweep(context, "sine");
+//
+// context.oncomplete = finishAudioTest;
+// context.startRendering();
+//
+// testRunner.waitUntilDone();
+// }
+//
+// </script>
+// </body>
+// </html>
var sampleRate = 44100.0;
var nyquist = 0.5 * sampleRate;
@@ -17,13 +51,27 @@ var lowFrequency = 10;
var highFrequency = nyquist + 2000; // go slightly higher than nyquist to make sure we generate silence there
var context = 0;
+// Mostly for debugging
+
+// An AudioBuffer for the reference (expected) result.
+var reference = 0;
+
+// The actual rendered data produced by the test.
+var renderedData = 0;
+
+// Signal power of the reference
+var signalPower = 0;
+
+// Noise power of the difference between the reference and actual result.
+var noisePower = 0;
+
function generateExponentialOscillatorSweep(context, oscillatorType) {
var osc = context.createOscillator();
if (oscillatorType == "custom") {
// Create a simple waveform with three Fourier coefficients.
// Note the first values are expected to be zero (DC for coeffA and Nyquist for coeffB).
var coeffA = new Float32Array([0, 1, 0.5]);
- var coeffB = new Float32Array([0, 0, 0]);
+ var coeffB = new Float32Array([0, 0, 0]);
var wave = context.createPeriodicWave(coeffA, coeffB);
osc.setPeriodicWave(wave);
} else {
@@ -42,3 +90,75 @@ function generateExponentialOscillatorSweep(context, oscillatorType) {
osc.frequency.setValueAtTime(10, 0);
osc.frequency.exponentialRampToValueAtTime(highFrequency, lengthInSeconds);
}
+
+function calculateSNR(sPower, nPower)
+{
+ if (nPower == 0 && sPower > 0) {
+ return 1000;
+ }
+ return 10 * Math.log10(sPower / nPower);
+}
+
+function loadReferenceAndRunTest(oscType) {
+ var bufferLoader = new BufferLoader(
+ context,
+ [ "oscillator-" + oscType + "-expected.wav" ],
+ function (bufferList) {
+ reference = bufferList[0].getChannelData(0);
+ generateExponentialOscillatorSweep(context, oscType);
+ context.oncomplete = checkResult;
+ context.startRendering();
+ });
+
+ bufferLoader.load();
+}
+
+function checkResult (event) {
+ renderedData = event.renderedBuffer.getChannelData(0);
+ // Compute signal to noise ratio between the result and the reference. Also keep track
+ // of the max difference (and position).
+
+ var maxError = -1;
+ var errorPosition = -1;
+ var diffCount = 0;
+
+ for (var k = 0; k < renderedData.length; ++k) {
+ var diff = renderedData[k] - reference[k];
+ noisePower += diff * diff;
+ signalPower += reference[k] * reference[k];
+ if (Math.abs(diff) > maxError) {
+ maxError = Math.abs(diff);
+ errorPosition = k;
+ }
+ // The reference file is a 16-bit WAV file, so we will never get an exact match
+ // between it and the actual floating-point result.
+ if (diff > 1/waveScaleFactor) {
+ diffCount++;
+ }
+ }
+
+ var snr = calculateSNR(signalPower, noisePower);
+ if (snr < thresholdSNR) {
+ testFailed("Expected SNR of " + thresholdSNR + " dB, but actual SNR is " + snr + " dB");
+ } else {
+ testPassed("Exceeded SNR threshold of " + thresholdSNR + " dB");
+ }
+
+ if (maxError > thresholdDiff) {
+ testFailed("Maximum difference of " + (maxError * waveScaleFactor) + " at "
+ + errorPosition + " exceeded threshold of " + (thresholdDiff * waveScaleFactor)
+ + " ulp (16-bits)");
+ } else {
+ testPassed("Maximum difference below threshold of "
+ + (thresholdDiff * waveScaleFactor) + " ulp (16-bits)");
+ }
+ if (diffCount > thresholdDiffCount) {
+ testFailed(diffCount + " differences found but expected no more than " + thresholdDiffCount);
+ } else {
+ testPassed("Number of differences between actual and expected result is less than " + thresholdDiffCount);
+ }
+
+ finishJSTest();
+}
+
+

Powered by Google App Engine
This is Rietveld 408576698