Bug 1456271 - Implement new behaviour of PeriodicWave ctor: not passing in any array makes it into a sine wave. r=baku
☠☠ backed out by 2613d4453b1f ☠ ☠
authorPaul Adenot <paul@paul.cx>
Wed, 30 May 2018 18:42:29 +0200
changeset 818993 b37a0606d6a0fa4c397e9779c96f8ed879136a15
parent 818992 633b1aae6b2ebb4af909cfa85cd51a02c66242dd
child 818994 b99bd412118184d8b8663bdac561b5679dba1b5f
push id116413
push userbgrinstead@mozilla.com
push dateMon, 16 Jul 2018 22:40:17 +0000
reviewersbaku
bugs1456271
milestone63.0a1
Bug 1456271 - Implement new behaviour of PeriodicWave ctor: not passing in any array makes it into a sine wave. r=baku MozReview-Commit-ID: AbI62hSxZyS
dom/media/webaudio/PeriodicWave.cpp
dom/webidl/PeriodicWave.webidl
--- a/dom/media/webaudio/PeriodicWave.cpp
+++ b/dom/media/webaudio/PeriodicWave.cpp
@@ -21,17 +21,17 @@ PeriodicWave::PeriodicWave(AudioContext*
                            const float* aImagData,
                            const uint32_t aLength,
                            const bool aDisableNormalization,
                            ErrorResult& aRv)
   : mContext(aContext)
   , mDisableNormalization(aDisableNormalization)
 {
   MOZ_ASSERT(aContext);
-  MOZ_ASSERT(aRealData || aImagData);
+  MOZ_ASSERT((aRealData || aImagData) || aLength == 2);
 
   // Caller should have checked this and thrown.
   MOZ_ASSERT(aLength > 0);
   mCoefficients.mDuration = aLength;
 
   // Copy coefficient data.
   // The SharedBuffer and two arrays share a single allocation.
   RefPtr<SharedBuffer> buffer =
@@ -39,54 +39,67 @@ PeriodicWave::PeriodicWave(AudioContext*
   if (!buffer) {
     aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
     return;
   }
 
   auto data = static_cast<float*>(buffer->Data());
   mCoefficients.mBuffer = std::move(buffer);
 
-  if (aRealData) {
-    PodCopy(data, aRealData, aLength);
+  if (!aRealData && !aImagData) {
+    PodZero(data, aLength);
+    mCoefficients.mChannelData.AppendElement(data);
+    data += aLength;
+    data[0] = 0.0f;
+    data[1] = 1.0f;
+    mCoefficients.mChannelData.AppendElement(data);
   } else {
-    PodZero(data, aLength);
-  }
-  mCoefficients.mChannelData.AppendElement(data);
+    if (aRealData) {
+      PodCopy(data, aRealData, aLength);
+    } else {
+      PodZero(data, aLength);
+    }
+    mCoefficients.mChannelData.AppendElement(data);
 
-  data += aLength;
-  if (aImagData) {
-    PodCopy(data, aImagData, aLength);
-  } else {
-    PodZero(data, aLength);
+    data += aLength;
+    if (aImagData) {
+      PodCopy(data, aImagData, aLength);
+    } else {
+      PodZero(data, aLength);
+    }
+    mCoefficients.mChannelData.AppendElement(data);
   }
-  mCoefficients.mChannelData.AppendElement(data);
-
   mCoefficients.mVolume = 1.0f;
   mCoefficients.mBufferFormat = AUDIO_FORMAT_FLOAT32;
 }
 
 /* static */ already_AddRefed<PeriodicWave>
 PeriodicWave::Constructor(const GlobalObject& aGlobal,
                           AudioContext& aAudioContext,
                           const PeriodicWaveOptions& aOptions,
                           ErrorResult& aRv)
 {
-  if (!aOptions.mReal.WasPassed() && !aOptions.mImag.WasPassed()) {
-    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
-    return nullptr;
-  }
 
   if (aOptions.mReal.WasPassed() && aOptions.mImag.WasPassed() &&
       aOptions.mReal.Value().Length() != aOptions.mImag.Value().Length()) {
     aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
     return nullptr;
   }
 
-  uint32_t length =
-    aOptions.mReal.WasPassed() ? aOptions.mReal.Value().Length() : aOptions.mImag.Value().Length();
+  uint32_t length = 0;
+  if (aOptions.mReal.WasPassed()) {
+    length = aOptions.mReal.Value().Length();
+  } else if (aOptions.mImag.WasPassed()) {
+    length = aOptions.mImag.Value().Length();
+  } else {
+    // If nothing has been passed, this PeriodicWave will be a sine wave: 2
+    // elements for each array, the second imaginary component set to 1.0.
+    length = 2;
+  }
+
   if (length == 0) {
     aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
     return nullptr;
   }
 
   const float* realData =
     aOptions.mReal.WasPassed() ? aOptions.mReal.Value().Elements() : nullptr;
   const float* imagData =
--- a/dom/webidl/PeriodicWave.webidl
+++ b/dom/webidl/PeriodicWave.webidl
@@ -15,14 +15,11 @@ dictionary PeriodicWaveConstraints {
 };
 
 dictionary PeriodicWaveOptions : PeriodicWaveConstraints {
              sequence<float> real;
              sequence<float> imag;
 };
 
 [Pref="dom.webaudio.enabled",
- // XXXbz The second arg is not optional in the spec, but that looks
- // like a spec bug to me.  See
- // <https://github.com/WebAudio/web-audio-api/issues/1116>.
  Constructor(BaseAudioContext context, optional PeriodicWaveOptions options)]
 interface PeriodicWave {
 };