bug 1024182 test normalization of convolution buffers via response concatenation r?dminor draft
authorKarl Tomlinson <karlt+@karlt.net>
Fri, 11 Aug 2017 18:07:54 +1200
changeset 645163 76bfa8df319f61851f4755bd48648476bc83803b
parent 645162 bd79599d0d103db038d2487274db47226b88067b
child 645164 52b98a4c3b3266905c8cb4aa222769cd3ecce7fd
push id73684
push userktomlinson@mozilla.com
push dateFri, 11 Aug 2017 22:29:12 +0000
reviewersdminor
bugs1024182
milestone57.0a1
bug 1024182 test normalization of convolution buffers via response concatenation r?dminor
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/webaudio/the-audio-api/the-convolvernode-interface/convolver-normalization.html
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -376024,16 +376024,22 @@
     ]
    ],
    "webaudio/the-audio-api/the-constantsourcenode-interface/test-constantsourcenode.html": [
     [
      "/webaudio/the-audio-api/the-constantsourcenode-interface/test-constantsourcenode.html",
      {}
     ]
    ],
+   "webaudio/the-audio-api/the-convolvernode-interface/convolver-normalization.html": [
+    [
+     "/webaudio/the-audio-api/the-convolvernode-interface/convolver-normalization.html",
+     {}
+    ]
+   ],
    "webaudio/the-audio-api/the-delaynode-interface/idl-test.html": [
     [
      "/webaudio/the-audio-api/the-delaynode-interface/idl-test.html",
      {}
     ]
    ],
    "webaudio/the-audio-api/the-gainnode-interface/idl-test.html": [
     [
@@ -624783,16 +624789,20 @@
   "webaudio/the-audio-api/the-constantsourcenode-interface/test-constantsourcenode.html": [
    "711b3f183d847e437a4c332f33054cc5a648fd22",
    "testharness"
   ],
   "webaudio/the-audio-api/the-convolvernode-interface/.gitkeep": [
    "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "support"
   ],
+  "webaudio/the-audio-api/the-convolvernode-interface/convolver-normalization.html": [
+   "a7a2bf2055391daa44080da287cb9f3651079776",
+   "testharness"
+  ],
   "webaudio/the-audio-api/the-delaynode-interface/.gitkeep": [
    "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "support"
   ],
   "webaudio/the-audio-api/the-delaynode-interface/idl-test.html": [
    "55a67c98908e18856e55c2e9437b63e8dc6feb94",
    "testharness"
   ],
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/webaudio/the-audio-api/the-convolvernode-interface/convolver-normalization.html
@@ -0,0 +1,82 @@
+<!DOCTYPE html>
+<title>Test normalization of convolution buffers</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+const GainCalibration = 0.00125;
+const GainCalibrationSampleRate = 44100;
+const sampleRate = GainCalibrationSampleRate;
+const LENGTH = 12800;
+// tolerate 16-bit math.
+const EPSILON = 1.0 / Math.pow(2, 15);
+
+function test_normalization_via_response_concat(delayIndex) {
+
+  var context = new OfflineAudioContext(1, LENGTH, sampleRate);
+
+  var impulse = context.createBuffer(1, 1, sampleRate);
+  impulse.getChannelData(0)[0] = 1.0;
+  var source = context.createBufferSource();
+  source.buffer = impulse;
+  source.start(0);
+
+  // Construct a set of adjacent responses in such a way that, when each is
+  // convolved with the impulse, they can be merged to produce a constant.
+
+  // The 5/4 ratio provides a range of lengths with different offsets within
+  // blocks.
+  var i = 0;
+  for (var responseEnd = 1;
+       i < LENGTH;
+       responseEnd = Math.floor((5 * responseEnd) / 4) + 1) {
+    var responseBuffer = context.createBuffer(1, responseEnd, sampleRate);
+    var response = responseBuffer.getChannelData(0);
+    var responseStart = i;
+    // The values in the response should be normalized, and so the output
+    // should not be dependent on the value.  Pick a changing value to test
+    // this.
+    var value = responseStart + 1;
+    for (; i < responseEnd; ++i) {
+      response[i] = value;
+    }
+    var convolver = context.createConvolver();
+    convolver.normalize = true;
+    convolver.buffer = responseBuffer;
+    convolver.connect(context.destination);
+    // Undo the normalization calibration by scaling the impulse so as to
+    // expect unit pulse output from the convolver.
+    var gain = context.createGain();
+    gain.gain.value =
+      Math.sqrt((responseEnd - responseStart) / responseEnd) / GainCalibration;
+    gain.connect(convolver);
+    source.connect(gain);
+  }
+
+  return context.startRendering().
+    then((buffer) => {
+      var output = buffer.getChannelData(0);
+      var max = output[0];
+      var maxIndex = 0;
+      var min = max;
+      var minIndex = 0;
+      for (var i = 1; i < buffer.length; ++i) {
+        if (output[i] > max) {
+          max = output[i];
+          maxIndex = i;
+        } else if (output[i] < min) {
+          min = output[i];
+          minIndex = i;
+        }
+      }
+      // Blink and Gecko use a slighty different GainCalibration to that
+      // specified, and so, instead of expecting unit output, check that the
+      // output is constant.
+      assert_approx_equals(output[maxIndex], output[minIndex], EPSILON,
+                           "output at " + maxIndex +
+                           " differs from output at " + minIndex);
+    });
+}
+
+promise_test(test_normalization_via_response_concat,
+             "via response concatenation");
+</script>