Bug 921695 - Part 2: Modify callers. r=karlt
authorJW Wang <jwwang@mozilla.com>
Tue, 01 Oct 2013 14:22:57 +0800
changeset 149959 04e5314e0de65533d578ff2b9eaf5d4db1ba3891
parent 149958 0744a7b07dea4751007fb432072af43492da5d7e
child 149960 51aea0658e35acfe8c281faef4b68227a3e42423
push idunknown
push userunknown
push dateunknown
reviewerskarlt
bugs921695
milestone27.0a1
Bug 921695 - Part 2: Modify callers. r=karlt
content/media/AudioNodeExternalInputStream.cpp
content/media/webaudio/AudioBufferSourceNode.cpp
content/media/webaudio/MediaBufferDecoder.cpp
content/media/webaudio/WaveShaperNode.cpp
--- a/content/media/AudioNodeExternalInputStream.cpp
+++ b/content/media/AudioNodeExternalInputStream.cpp
@@ -74,49 +74,16 @@ AudioNodeExternalInputStream::GetTrackMa
   map->mResampler = resampler;
   map->mResamplerChannelCount = channelCount;
   map->mTrackID = aTrack.GetID();
   return mTrackMap.Length() - 1;
 }
 
 static const uint32_t SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT = 1000;
 
-template <typename T> static int
-SpeexResamplerProcess(SpeexResamplerState* aResampler,
-                      uint32_t aChannel,
-                      const T* aInput, uint32_t* aIn,
-                      float* aOutput, uint32_t* aOut);
-
-template <> int
-SpeexResamplerProcess<float>(SpeexResamplerState* aResampler,
-                             uint32_t aChannel,
-                             const float* aInput, uint32_t* aIn,
-                             float* aOutput, uint32_t* aOut)
-{
-  NS_ASSERTION(*aOut <= SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT, "Bad aOut");
-  return speex_resampler_process_float(aResampler, aChannel, aInput, aIn, aOutput, aOut);
-}
-
-template <> int
-SpeexResamplerProcess<int16_t>(SpeexResamplerState* aResampler,
-                               uint32_t aChannel,
-                               const int16_t* aInput, uint32_t* aIn,
-                               float* aOutput, uint32_t* aOut)
-{
-  NS_ASSERTION(*aOut <= SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT, "Bad aOut");
-  int16_t tmp[SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT];
-  int result = speex_resampler_process_int(aResampler, aChannel, aInput, aIn, tmp, aOut);
-  if (result == RESAMPLER_ERR_SUCCESS) {
-    for (uint32_t i = 0; i < *aOut; ++i) {
-      aOutput[i] = AudioSampleToFloat(tmp[i]);
-    }
-  }
-  return result;
-}
-
 template <typename T> static void
 ResampleChannelBuffer(SpeexResamplerState* aResampler, uint32_t aChannel,
                       const T* aInput, uint32_t aInputDuration,
                       nsTArray<float>* aOutput)
 {
   if (!aResampler) {
     float* out = aOutput->AppendElements(aInputDuration);
     for (uint32_t i = 0; i < aInputDuration; ++i) {
@@ -126,19 +93,19 @@ ResampleChannelBuffer(SpeexResamplerStat
   }
 
   uint32_t processed = 0;
   while (processed < aInputDuration) {
     uint32_t prevLength = aOutput->Length();
     float* output = aOutput->AppendElements(SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT);
     uint32_t in = aInputDuration - processed;
     uint32_t out = aOutput->Length() - prevLength;
-    SpeexResamplerProcess(aResampler, aChannel,
-                          aInput + processed, &in,
-                          output, &out);
+    WebAudioUtils::SpeexResamplerProcess(aResampler, aChannel,
+                                         aInput + processed, &in,
+                                         output, &out);
     processed += in;
     aOutput->SetLength(prevLength + out);
   }
 }
 
 class SharedChannelArrayBuffer : public ThreadSharedObject {
 public:
   SharedChannelArrayBuffer(nsTArray<nsTArray<float> >* aBuffers)
--- a/content/media/webaudio/AudioBufferSourceNode.cpp
+++ b/content/media/webaudio/AudioBufferSourceNode.cpp
@@ -218,19 +218,19 @@ public:
       uint32_t inSamples = inputSamples;
       uint32_t outSamples = outputSamples;
 
       const float* inputData = mBuffer->GetData(i) + aSourceOffset;
       float* outputData =
         static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
         aBufferOffset;
 
-      speex_resampler_process_float(resampler, i,
-                                    inputData, &inSamples,
-                                    outputData, &outSamples);
+      WebAudioUtils::SpeexResamplerProcess(resampler, i,
+                                           inputData, &inSamples,
+                                           outputData, &outSamples);
 
       aFramesRead = inSamples;
       aFramesWritten = outSamples;
     }
   }
 
   /**
    * Fill aOutput with as many zero frames as we can, and advance
--- a/content/media/webaudio/MediaBufferDecoder.cpp
+++ b/content/media/webaudio/MediaBufferDecoder.cpp
@@ -14,16 +14,17 @@
 #include "BufferMediaResource.h"
 #include "DecoderTraits.h"
 #include "AudioContext.h"
 #include "AudioBuffer.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIScriptError.h"
 #include "nsMimeTypes.h"
 #include "nsCxPusher.h"
+#include "WebAudioUtils.h"
 
 namespace mozilla {
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(WebAudioDecodeJob)
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(WebAudioDecodeJob)
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mContext)
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutput)
@@ -345,47 +346,31 @@ MediaDecodeTask::Decode()
       (audioData->mAudioBuffer->Data());
 
     if (sampleRate != destSampleRate) {
       const uint32_t expectedOutSamples = static_cast<uint32_t>(
           static_cast<uint64_t>(destSampleRate) *
           static_cast<uint64_t>(audioData->mFrames) /
           static_cast<uint64_t>(sampleRate)
         );
-#ifdef MOZ_SAMPLE_TYPE_S16
-      AudioDataValue* resampledBuffer = new(fallible) AudioDataValue[channelCount * expectedOutSamples];
-#endif
 
       for (uint32_t i = 0; i < audioData->mChannels; ++i) {
         uint32_t inSamples = audioData->mFrames;
         uint32_t outSamples = expectedOutSamples;
 
-#ifdef MOZ_SAMPLE_TYPE_S16
-        speex_resampler_process_int(resampler, i, &bufferData[i * audioData->mFrames], &inSamples,
-                                    &resampledBuffer[i * expectedOutSamples],
-                                    &outSamples);
-
-        ConvertAudioSamples(&resampledBuffer[i * expectedOutSamples],
-                            mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
-                            outSamples);
-#else
-        speex_resampler_process_float(resampler, i, &bufferData[i * audioData->mFrames], &inSamples,
-                                      mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
-                                      &outSamples);
-#endif
+        WebAudioUtils::SpeexResamplerProcess(
+            resampler, i, &bufferData[i * audioData->mFrames], &inSamples,
+            mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
+            &outSamples);
 
         if (i == audioData->mChannels - 1) {
           mDecodeJob.mWriteIndex += outSamples;
           MOZ_ASSERT(mDecodeJob.mWriteIndex <= resampledFrames);
         }
       }
-
-#ifdef MOZ_SAMPLE_TYPE_S16
-      delete[] resampledBuffer;
-#endif
     } else {
       for (uint32_t i = 0; i < audioData->mChannels; ++i) {
         ConvertAudioSamples(&bufferData[i * audioData->mFrames],
                             mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
                             audioData->mFrames);
 
         if (i == audioData->mChannels - 1) {
           mDecodeJob.mWriteIndex += audioData->mFrames;
@@ -394,56 +379,38 @@ MediaDecodeTask::Decode()
     }
   }
 
   if (sampleRate != destSampleRate) {
     int inputLatency = speex_resampler_get_input_latency(resampler);
     int outputLatency = speex_resampler_get_output_latency(resampler);
     AudioDataValue* zero = (AudioDataValue*)calloc(inputLatency, sizeof(AudioDataValue));
 
-#ifdef MOZ_SAMPLE_TYPE_S16
-    AudioDataValue* resampledBuffer = new(fallible) AudioDataValue[channelCount * outputLatency];
-    if (!resampledBuffer || !zero) {
-#else
     if (!zero) {
-#endif
       // Out of memory!
       ReportFailureOnMainThread(WebAudioDecodeJob::UnknownError);
       return;
     }
 
     for (uint32_t i = 0; i < channelCount; ++i) {
       uint32_t inSamples = inputLatency;
       uint32_t outSamples = outputLatency;
 
-#ifdef MOZ_SAMPLE_TYPE_S16
-      speex_resampler_process_int(resampler, i, zero, &inSamples,
-                                  &resampledBuffer[i * outputLatency],
-                                  &outSamples);
-
-      ConvertAudioSamples(&resampledBuffer[i * outputLatency],
-                          mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
-                          outSamples);
-#else
-      speex_resampler_process_float(resampler, i, zero, &inSamples,
-                                    mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
-                                    &outSamples);
-#endif
+      WebAudioUtils::SpeexResamplerProcess(
+          resampler, i, zero, &inSamples,
+          mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
+          &outSamples);
 
       if (i == channelCount - 1) {
         mDecodeJob.mWriteIndex += outSamples;
         MOZ_ASSERT(mDecodeJob.mWriteIndex <= resampledFrames);
       }
     }
 
     free(zero);
-
-#ifdef MOZ_SAMPLE_TYPE_S16
-    delete[] resampledBuffer;
-#endif
   }
 
   mPhase = PhaseEnum::AllocateBuffer;
   RunNextPhase();
 }
 
 void
 MediaDecodeTask::AllocateBuffer()
--- a/content/media/webaudio/WaveShaperNode.cpp
+++ b/content/media/webaudio/WaveShaperNode.cpp
@@ -101,36 +101,36 @@ public:
   float* UpSample(uint32_t aChannel, const float* aInputData, uint32_t aBlocks)
   {
     uint32_t inSamples = WEBAUDIO_BLOCK_SIZE;
     uint32_t outSamples = WEBAUDIO_BLOCK_SIZE*aBlocks;
     float* outputData = mBuffer.Elements();
 
     MOZ_ASSERT(mBuffer.Length() == outSamples);
 
-    speex_resampler_process_float(mUpSampler, aChannel,
-                                  aInputData, &inSamples,
-                                  outputData, &outSamples);
+    WebAudioUtils::SpeexResamplerProcess(mUpSampler, aChannel,
+                                         aInputData, &inSamples,
+                                         outputData, &outSamples);
 
     MOZ_ASSERT(inSamples == WEBAUDIO_BLOCK_SIZE && outSamples == WEBAUDIO_BLOCK_SIZE*aBlocks);
 
     return outputData;
   }
 
   void DownSample(uint32_t aChannel, float* aOutputData, uint32_t aBlocks)
   {
     uint32_t inSamples = WEBAUDIO_BLOCK_SIZE*aBlocks;
     uint32_t outSamples = WEBAUDIO_BLOCK_SIZE;
     const float* inputData = mBuffer.Elements();
 
     MOZ_ASSERT(mBuffer.Length() == inSamples);
 
-    speex_resampler_process_float(mDownSampler, aChannel,
-                                  inputData, &inSamples,
-                                  aOutputData, &outSamples);
+    WebAudioUtils::SpeexResamplerProcess(mDownSampler, aChannel,
+                                         inputData, &inSamples,
+                                         aOutputData, &outSamples);
 
     MOZ_ASSERT(inSamples == WEBAUDIO_BLOCK_SIZE*aBlocks && outSamples == WEBAUDIO_BLOCK_SIZE);
   }
 
 private:
   void Destroy()
   {
     if (mUpSampler) {