Bug 1220037 - test convolution. r=padenot, a=sylvestre
authorKarl Tomlinson <karlt+@karlt.net>
Fri, 30 Oct 2015 15:32:16 +1300
changeset 296666 d3b021968c63
parent 296665 ab27c2db7e68
child 296667 ce3f2c696251
push id5278
push usercbook@mozilla.com
push date2015-11-09 10:38 +0000
treeherdermozilla-beta@d3b021968c63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot, sylvestre
bugs1220037
milestone43.0
Bug 1220037 - test convolution. r=padenot, a=sylvestre This is in the mochitest suite so that Android and B2G tests can run it, but designed so that it can be moved to web-platform-tests when they run on all platforms.
dom/media/webaudio/test/mochitest.ini
dom/media/webaudio/test/test_convolverNodeDelay.html
--- a/dom/media/webaudio/test/mochitest.ini
+++ b/dom/media/webaudio/test/mochitest.ini
@@ -90,16 +90,17 @@ skip-if = toolkit == 'android' # bug 105
 [test_channelMergerNode.html]
 [test_channelMergerNodeWithVolume.html]
 [test_channelSplitterNode.html]
 [test_channelSplitterNodeWithVolume.html]
 skip-if = (android_version == '18' && debug) # bug 1158417
 [test_convolverNode.html]
 [test_convolverNode_mono_mono.html]
 [test_convolverNodeChannelCount.html]
+[test_convolverNodeDelay.html]
 [test_convolverNodePassThrough.html]
 [test_convolverNodeWithGain.html]
 [test_currentTime.html]
 [test_decodeMultichannel.html]
 [test_decodeAudioDataPromise.html]
 [test_delayNode.html]
 [test_delayNodeAtMax.html]
 [test_delayNodeChannelChanges.html]
new file mode 100644
--- /dev/null
+++ b/dom/media/webaudio/test/test_convolverNodeDelay.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<title>Test convolution to delay a triangle pulse</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script>
+const sampleRate = 48000;
+const LENGTH = 12800;
+// tolerate 16-bit math.
+const EPSILON = 1.0 / Math.pow(2, 15);
+
+// Triangle pulse
+var sourceBuffer = new OfflineAudioContext(1, 1, sampleRate).
+  createBuffer(1, 2 * 128, sampleRate);
+var channelData = sourceBuffer.getChannelData(0);
+for (var i = 0; i < 128; ++i) {
+  channelData[i] = i/128;
+  channelData[128 + i] = 1.0 - i/128;
+}
+
+function test_delay_index(delayIndex) {
+
+  var context = new OfflineAudioContext(2, LENGTH, sampleRate);
+
+  var merger = context.createChannelMerger(2);
+  merger.connect(context.destination);
+
+  var impulse = context.createBuffer(1, delayIndex + 1, sampleRate);
+  impulse.getChannelData(0)[delayIndex] = 1.0;
+  var convolver = context.createConvolver();
+  convolver.normalize = false;
+  convolver.buffer = impulse;
+  convolver.connect(merger, 0, 0);
+
+  var delayTime = delayIndex/sampleRate;
+  var delay = context.createDelay(delayTime || 1/sampleRate);
+  delay.delayTime.value = delayTime;
+  delay.connect(merger, 0, 1);
+
+  var source = context.createBufferSource();
+  source.buffer = sourceBuffer;
+  source.connect(convolver);
+  source.connect(delay);
+  source.start(0);
+
+  return context.startRendering().
+    then((buffer) => {
+      var convolverOutput = buffer.getChannelData(0);
+      var delayOutput = buffer.getChannelData(1);
+      var maxDiff = 0.0;
+      var maxIndex = 0;
+      for (var i = 0; i < buffer.length; ++i) {
+        var diff = Math.abs(convolverOutput[i] - delayOutput[i]);
+        if (diff > maxDiff) {
+          maxDiff = diff;
+          maxIndex = i;
+        }
+      }
+      // The convolver should produce similar output to the delay.
+      assert_approx_equals(convolverOutput[maxIndex], delayOutput[maxIndex],
+                           EPSILON, "output at " + maxIndex);
+    });
+}
+
+// The 5/4 ratio provides sampling across a range of delays and offsets within
+// blocks.
+for (var delayIndex = 0;
+     delayIndex < LENGTH;
+     delayIndex = Math.floor((5 * (delayIndex + 1)) / 4)) {
+  promise_test(test_delay_index.bind(null, delayIndex),
+               "Delay " + delayIndex);
+}
+</script>