Bug 1424906 - Fix PeriodicWave disableNormalization false behaviour; r?padenot draft
authorDan Minor <dminor@mozilla.com>
Thu, 14 Dec 2017 09:43:21 -0600
changeset 711790 8742bd9bfeff8cfc9f2a9c22b3a466bcf395bcb5
parent 711413 22d2831cc1f41e1b3e1ebac9be5a7aff33684843
child 743871 f4fe64c3d3831c42d0ff38ca4fde37f2db53fcb8
push id93141
push userbmo:dminor@mozilla.com
push dateThu, 14 Dec 2017 17:10:38 +0000
reviewerspadenot
bugs1424906
milestone59.0a1
Bug 1424906 - Fix PeriodicWave disableNormalization false behaviour; r?padenot This adds a scaling factor of 0.5 even when normalization is disabled, which is required for correct results. MozReview-Commit-ID: J0VbMcaacGc
dom/media/webaudio/blink/PeriodicWave.cpp
dom/media/webaudio/test/test_periodicWaveDisableNormalization.html
--- a/dom/media/webaudio/blink/PeriodicWave.cpp
+++ b/dom/media/webaudio/blink/PeriodicWave.cpp
@@ -269,28 +269,30 @@ void PeriodicWave::createBandLimitedTabl
     m_bandLimitedTables[rangeIndex] = table;
 
     // Apply an inverse FFT to generate the time-domain table data.
     float* data = m_bandLimitedTables[rangeIndex]->Elements();
     frame.GetInverseWithoutScaling(data);
 
     // For the first range (which has the highest power), calculate
     // its peak value then compute normalization scale.
-    if (!m_disableNormalization && !rangeIndex) {
+    if (m_disableNormalization) {
+      // See Bug 1424906, results need to be scaled by 0.5 even
+      // when normalization is disabled.
+      m_normalizationScale = 0.5;
+    } else if (!rangeIndex) {
         float maxValue;
         maxValue = AudioBufferPeakValue(data, m_periodicWaveSize);
 
         if (maxValue)
             m_normalizationScale = 1.0f / maxValue;
     }
 
     // Apply normalization scale.
-    if (!m_disableNormalization) {
-      AudioBufferInPlaceScale(data, m_normalizationScale, m_periodicWaveSize);
-    }
+    AudioBufferInPlaceScale(data, m_normalizationScale, m_periodicWaveSize);
 }
 
 void PeriodicWave::generateBasicWaveform(OscillatorType shape)
 {
     const float piFloat = float(M_PI);
     unsigned fftSize = periodicWaveSize();
     unsigned halfSize = fftSize / 2;
 
--- a/dom/media/webaudio/test/test_periodicWaveDisableNormalization.html
+++ b/dom/media/webaudio/test/test_periodicWaveDisableNormalization.html
@@ -76,19 +76,17 @@ var gTest = {
       buffer.getChannelData(0)[i] = 1.0 / realPeak *
         (real[1] * Math.cos(2 * Math.PI * realFundamental * i /
                             context.sampleRate) +
          real[realMax] * Math.cos(2 * Math.PI * realMax * realFundamental * i /
                             context.sampleRate));
 
       buffer.getChannelData(1)[i] = buffer.getChannelData(0)[i];
 
-      // TODO: We need to scale by a factor of two to make the results work
-      //       out here. This seems suspicious, see Bug 1266737.
-      buffer.getChannelData(2)[i] = 2.0 *
+      buffer.getChannelData(2)[i] =
         (real[1] * Math.cos(2 * Math.PI * realFundamental * i /
                             context.sampleRate) +
          real[realMax] * Math.cos(2 * Math.PI * realMax * realFundamental * i /
                             context.sampleRate));
     }
     return buffer;
   },
   'numberOfChannels': 3,