diff --git a/content/media/webaudio/WebAudioUtils.cpp b/content/media/webaudio/WebAudioUtils.cpp index f9ae8c26775..9a21f9a5966 100644 --- a/content/media/webaudio/WebAudioUtils.cpp +++ b/content/media/webaudio/WebAudioUtils.cpp @@ -8,6 +8,7 @@ #include "AudioNodeStream.h" #include "AudioParamTimeline.h" #include "blink/HRTFDatabaseLoader.h" +#include "speex/speex_resampler.h" namespace mozilla { @@ -69,5 +70,45 @@ 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 tmp1; + nsAutoTArray 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 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 +} + } } diff --git a/content/media/webaudio/WebAudioUtils.h b/content/media/webaudio/WebAudioUtils.h index 777ce4135af..c525a2c781a 100644 --- a/content/media/webaudio/WebAudioUtils.h +++ b/content/media/webaudio/WebAudioUtils.h @@ -13,6 +13,9 @@ #include "mozilla/FloatingPoint.h" #include "MediaSegment.h" +// Forward declaration +typedef struct SpeexResamplerState_ SpeexResamplerState; + namespace mozilla { class AudioNodeStream; @@ -214,6 +217,18 @@ struct WebAudioUtils { } 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); }; }