Bug 805254. Part 6: Replace some #ifdefs with if statements. r=kinetik
authorRobert O'Callahan <robert@ocallahan.org>
Thu, 25 Oct 2012 23:09:39 +1300
changeset 111622 da7b1a16814662dcb2a4182e6693089571e6bec1
parent 111621 870743d33a56ba6b6a25ddccd79d509f5cceebd3
child 111623 569b5cf142851ddc79c169a205c21a2dc84f3acd
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerskinetik
bugs805254
milestone19.0a1
Bug 805254. Part 6: Replace some #ifdefs with if statements. r=kinetik This is largely obsoleted by part 8. Sorry.
content/html/content/src/nsHTMLAudioElement.cpp
content/media/nsAudioStream.cpp
--- a/content/html/content/src/nsHTMLAudioElement.cpp
+++ b/content/html/content/src/nsHTMLAudioElement.cpp
@@ -174,38 +174,38 @@ nsHTMLAudioElement::MozWriteAudio(const 
   if (dataLength % mChannels != 0) {
     return NS_ERROR_DOM_INDEX_SIZE_ERR;
   }
 
   // Don't write more than can be written without blocking.
   uint32_t writeLen = NS_MIN(mAudioStream->Available(), dataLength / mChannels);
 
   float* frames = JS_GetFloat32ArrayData(tsrc, aCx);
-#ifdef MOZ_SAMPLE_TYPE_S16
-  // Convert the samples back to integers as we are using fixed point audio in
-  // the nsAudioStream.
-  nsAutoArrayPtr<short> shortsArray(new short[writeLen * mChannels]);
-  // Hard clip the samples.
-  for (uint32_t i = 0; i <  writeLen * mChannels; ++i) {
-    float scaled_value = floorf(0.5 + 32768 * frames[i]);
-    if (frames[i] < 0.0) {
-      shortsArray[i] = (scaled_value < -32768.0) ?
-        -32768 :
-        short(scaled_value);
-    } else {
-      shortsArray[i] = (scaled_value > 32767.0) ?
-        32767 :
-        short(scaled_value);
+  nsresult rv;
+  if (nsAudioStream::Format() == FORMAT_S16) {
+    // Convert the samples back to integers as we are using fixed point audio in
+    // the nsAudioStream.
+    nsAutoArrayPtr<short> shortsArray(new short[writeLen * mChannels]);
+    // Hard clip the samples.
+    for (uint32_t i = 0; i <  writeLen * mChannels; ++i) {
+      float scaled_value = floorf(0.5 + 32768 * frames[i]);
+      if (frames[i] < 0.0) {
+        shortsArray[i] = (scaled_value < -32768.0) ?
+          -32768 :
+          short(scaled_value);
+      } else {
+        shortsArray[i] = (scaled_value > 32767.0) ?
+          32767 :
+          short(scaled_value);
+      }
     }
+    rv = mAudioStream->Write(shortsArray, writeLen);
+  } else {
+    rv = mAudioStream->Write(frames, writeLen);
   }
-  nsresult rv = mAudioStream->Write(shortsArray, writeLen);
-#else
-  nsresult rv = mAudioStream->Write(frames, writeLen);
-#endif
-
 
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   // Return the actual amount written.
   *aRetVal = writeLen * mChannels;
   return rv;
--- a/content/media/nsAudioStream.cpp
+++ b/content/media/nsAudioStream.cpp
@@ -471,37 +471,37 @@ nsresult nsNativeAudioStream::Write(cons
   if (mInError)
     return NS_ERROR_FAILURE;
 
   uint32_t samples = aFrames * mChannels;
   nsAutoArrayPtr<short> s_data(new short[samples]);
 
   if (s_data) {
     double scaled_volume = GetVolumeScale() * mVolume;
-#ifdef MOZ_SAMPLE_TYPE_S16
-    const short* buf = static_cast<const short*>(aBuf);
-    int32_t volume = int32_t((1 << 16) * scaled_volume);
-    for (uint32_t i = 0; i < samples; ++i) {
-      s_data[i] = short((int32_t(buf[i]) * volume) >> 16);
-    }
-#else /* MOZ_SAMPLE_TYPE_FLOAT32 */
-    const float* buf = static_cast<const float*>(aBuf);
-    for (uint32_t i = 0; i <  samples; ++i) {
-      float scaled_value = floorf(0.5 + 32768 * buf[i] * scaled_volume);
-      if (buf[i] < 0.0) {
-        s_data[i] = (scaled_value < -32768.0) ?
-          -32768 :
-          short(scaled_value);
-      } else {
-        s_data[i] = (scaled_value > 32767.0) ?
-          32767 :
-          short(scaled_value);
+    if (Format() == FORMAT_S16) {
+      const short* buf = static_cast<const short*>(aBuf);
+      int32_t volume = int32_t((1 << 16) * scaled_volume);
+      for (uint32_t i = 0; i < samples; ++i) {
+        s_data[i] = short((int32_t(buf[i]) * volume) >> 16);
+      }
+    } else {
+      const float* buf = static_cast<const float*>(aBuf);
+      for (uint32_t i = 0; i <  samples; ++i) {
+        float scaled_value = floorf(0.5 + 32768 * buf[i] * scaled_volume);
+        if (buf[i] < 0.0) {
+          s_data[i] = (scaled_value < -32768.0) ?
+            -32768 :
+            short(scaled_value);
+        } else {
+          s_data[i] = (scaled_value > 32767.0) ?
+            32767 :
+            short(scaled_value);
+        }
       }
     }
-#endif
   }
 
   if (sa_stream_write(static_cast<sa_stream_t*>(mAudioHandle),
                       s_data.get(),
                       samples * sizeof(short)) != SA_SUCCESS)
   {
     PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsNativeAudioStream: sa_stream_write error"));
     mInError = true;
@@ -942,23 +942,23 @@ nsBufferedAudioStream::Init(int32_t aNum
   }
 
   mRate = aRate;
   mChannels = aNumChannels;
 
   cubeb_stream_params params;
   params.rate = aRate;
   params.channels = aNumChannels;
-#ifdef MOZ_SAMPLE_TYPE_S16
-  params.format =  CUBEB_SAMPLE_S16NE;
-  mBytesPerFrame = sizeof(int16_t) * aNumChannels;
-#else /* MOZ_SAMPLE_TYPE_FLOAT32 */
-  params.format = CUBEB_SAMPLE_FLOAT32NE;
-  mBytesPerFrame = sizeof(float) * aNumChannels;
-#endif
+  if (Format() == FORMAT_S16) {
+    params.format =  CUBEB_SAMPLE_S16NE;
+    mBytesPerFrame = sizeof(int16_t) * aNumChannels;
+  } else {
+    params.format = CUBEB_SAMPLE_FLOAT32NE;
+    mBytesPerFrame = sizeof(float) * aNumChannels;
+  }
 
   {
     cubeb_stream* stream;
     if (cubeb_stream_init(cubebContext, &stream, "nsBufferedAudioStream", params,
                           GetCubebLatency(), DataCallback_S, StateCallback_S, this) == CUBEB_OK) {
       mCubebStream.own(stream);
     }
   }
@@ -1178,33 +1178,32 @@ nsBufferedAudioStream::DataCallback(void
     mBuffer.PopElements(available, &input[0], &input_size[0], &input[1], &input_size[1]);
 
     uint8_t* output = reinterpret_cast<uint8_t*>(aBuffer);
     for (int i = 0; i < 2; ++i) {
       // Fast path for unity volume case.
       if (scaled_volume == 1.0) {
         memcpy(output, input[i], input_size[i]);
         output += input_size[i];
-      } else {
+      } else if (Format() == FORMAT_S16) {
         // Adjust volume as each sample is copied out.
-#ifdef MOZ_SAMPLE_TYPE_S16
         int32_t volume = int32_t(1 << 16) * scaled_volume;
 
         const short* src = static_cast<const short*>(input[i]);
         short* dst = reinterpret_cast<short*>(output);
         for (uint32_t j = 0; j < input_size[i] / (mBytesPerFrame / mChannels); ++j) {
           dst[j] = short((int32_t(src[j]) * volume) >> 16);
         }
-#else /* MOZ_SAMPLE_TYPE_FLOAT32 */
+        output += input_size[i];
+      } else {
         const float* src = static_cast<const float*>(input[i]);
         float* dst = reinterpret_cast<float*>(output);
         for (uint32_t j = 0; j < input_size[i] / (mBytesPerFrame / mChannels); ++j) {
           dst[j] = src[j] * scaled_volume;
         }
-#endif
         output += input_size[i];
       }
     }
 
     NS_ABORT_IF_FALSE(mBuffer.Length() % mBytesPerFrame == 0, "Must copy complete frames");
 
     // Notify any blocked Write() call that more space is available in mBuffer.
     mon.NotifyAll();