Bug 921695 - Part 1: Add convert function. r=karlt
authorJW Wang <jwwang@mozilla.com>
Tue, 01 Oct 2013 14:22:54 +0800
changeset 149958 0744a7b07dea4751007fb432072af43492da5d7e
parent 149957 b62de18b545fee20d093b0228c759dde5c51500d
child 149959 04e5314e0de65533d578ff2b9eaf5d4db1ba3891
push idunknown
push userunknown
push dateunknown
reviewerskarlt
bugs921695
milestone27.0a1
Bug 921695 - Part 1: Add convert function. r=karlt
content/media/webaudio/WebAudioUtils.cpp
content/media/webaudio/WebAudioUtils.h
--- a/content/media/webaudio/WebAudioUtils.cpp
+++ b/content/media/webaudio/WebAudioUtils.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "WebAudioUtils.h"
 #include "AudioNodeStream.h"
 #include "AudioParamTimeline.h"
 #include "blink/HRTFDatabaseLoader.h"
+#include "speex/speex_resampler.h"
 
 namespace mozilla {
 
 namespace dom {
 
 struct ConvertTimeToTickHelper
 {
   AudioNodeStream* mSourceStream;
@@ -64,10 +65,50 @@ WebAudioUtils::ConvertAudioParamToTicks(
 }
 
 void
 WebAudioUtils::Shutdown()
 {
   WebCore::HRTFDatabaseLoader::shutdown();
 }
 
+int
+WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
+                                     uint32_t aChannel,
+                                     const float* aIn, uint32_t* aInLen,
+                                     float* aOut, uint32_t* aOutLen)
+{
+#ifdef MOZ_SAMPLE_TYPE_S16
+  nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1;
+  nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2;
+  tmp1.SetLength(*aInLen);
+  tmp2.SetLength(*aOutLen);
+  ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen);
+  int result = speex_resampler_process_int(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen);
+  ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen);
+  return result;
+#else
+  return speex_resampler_process_float(aResampler, aChannel, aIn, aInLen, aOut, aOutLen);
+#endif
+}
+
+int
+WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
+                                     uint32_t aChannel,
+                                     const int16_t* aIn, uint32_t* aInLen,
+                                     float* aOut, uint32_t* aOutLen)
+{
+  nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp;
+#ifdef MOZ_SAMPLE_TYPE_S16
+  tmp.SetLength(*aOutLen);
+  int result = speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, tmp.Elements(), aOutLen);
+  ConvertAudioSamples(tmp.Elements(), aOut, *aOutLen);
+  return result;
+#else
+  tmp.SetLength(*aInLen);
+  ConvertAudioSamples(aIn, tmp.Elements(), *aInLen);
+  int result = speex_resampler_process_float(aResampler, aChannel, tmp.Elements(), aInLen, aOut, aOutLen);
+  return result;
+#endif
+}
+
 }
 }
--- a/content/media/webaudio/WebAudioUtils.h
+++ b/content/media/webaudio/WebAudioUtils.h
@@ -8,16 +8,19 @@
 #define WebAudioUtils_h_
 
 #include <cmath>
 #include <limits>
 #include "mozilla/TypeTraits.h"
 #include "mozilla/FloatingPoint.h"
 #include "MediaSegment.h"
 
+// Forward declaration
+typedef struct SpeexResamplerState_ SpeexResamplerState;
+
 namespace mozilla {
 
 class AudioNodeStream;
 class MediaStream;
 
 namespace dom {
 
 class AudioParamTimeline;
@@ -209,15 +212,27 @@ struct WebAudioUtils {
       return numeric_limits<IntType>::min();
     }
 
     // Otherwise, this conversion must be well defined.
     return IntType(f);
   }
 
   static void Shutdown();
+
+  static int
+  SpeexResamplerProcess(SpeexResamplerState* aResampler,
+                        uint32_t aChannel,
+                        const float* aIn, uint32_t* aInLen,
+                        float* aOut, uint32_t* aOutLen);
+
+  static int
+  SpeexResamplerProcess(SpeexResamplerState* aResampler,
+                        uint32_t aChannel,
+                        const int16_t* aIn, uint32_t* aInLen,
+                        float* aOut, uint32_t* aOutLen);
 };
 
 }
 }
 
 #endif