From 089ff9d73b882c021b64c4fbb8dc49870249b4c2 Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Tue, 1 Sep 2015 14:25:48 +0200 Subject: [PATCH] Bug 901633 - Part 12 - Add a function to deinterleave and convert an audio buffer. r=jesup --HG-- extra : rebase_source : 60200bdecfb896d17cf42f205aba2f08afe2e4ac --- dom/media/AudioSampleFormat.h | 45 +++++++++++++++++++++++++++++++++++ dom/media/AudioSegment.h | 26 ++++++++++++++++---- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/dom/media/AudioSampleFormat.h b/dom/media/AudioSampleFormat.h index 2ea986518b46c..5ce98d3ad0233 100644 --- a/dom/media/AudioSampleFormat.h +++ b/dom/media/AudioSampleFormat.h @@ -96,6 +96,51 @@ FloatToAudioSample(float aValue) return int16_t(clamped); } +template T IntegerToAudioSample(int16_t aValue); + +template <> inline float +IntegerToAudioSample(int16_t aValue) +{ + return aValue / 32768.0f; +} +template <> inline int16_t +IntegerToAudioSample(int16_t aValue) +{ + return aValue; +} + +template +inline void +ConvertAudioSample(SrcT aIn, DstT& aOut); + +template<> +inline void +ConvertAudioSample(int16_t aIn, int16_t & aOut) +{ + aOut = aIn; +} + +template<> +inline void +ConvertAudioSample(int16_t aIn, float& aOut) +{ + aOut = AudioSampleToFloat(aIn); +} + +template<> +inline void +ConvertAudioSample(float aIn, float& aOut) +{ + aOut = aIn; +} + +template<> +inline void +ConvertAudioSample(float aIn, int16_t& aOut) +{ + aOut = FloatToAudioSample(aIn); +} + // Sample buffer conversion template inline void diff --git a/dom/media/AudioSegment.h b/dom/media/AudioSegment.h index 90b2750527fc1..9bfb4f1185cde 100644 --- a/dom/media/AudioSegment.h +++ b/dom/media/AudioSegment.h @@ -57,16 +57,16 @@ const int GUESS_AUDIO_CHANNELS = 2; const uint32_t WEBAUDIO_BLOCK_SIZE_BITS = 7; const uint32_t WEBAUDIO_BLOCK_SIZE = 1 << WEBAUDIO_BLOCK_SIZE_BITS; -template +template static void InterleaveAndConvertBuffer(const SrcT* const* aSourceChannels, - int32_t aLength, float aVolume, - int32_t aChannels, + uint32_t aLength, float aVolume, + uint32_t aChannels, DestT* aOutput) { DestT* output = aOutput; - for (int32_t i = 0; i < aLength; ++i) { - for (int32_t channel = 0; channel < aChannels; ++channel) { + for (size_t i = 0; i < aLength; ++i) { + for (size_t channel = 0; channel < aChannels; ++channel) { float v = AudioSampleToFloat(aSourceChannels[channel][i])*aVolume; *output = FloatToAudioSample(v); ++output; @@ -74,6 +74,22 @@ InterleaveAndConvertBuffer(const SrcT* const* aSourceChannels, } } +template +static void +DeinterleaveAndConvertBuffer(const SrcT* aSourceBuffer, + uint32_t aFrames, uint32_t aChannels, + DestT** aOutput) +{ + for (size_t i = 0; i < aChannels; i++) { + size_t interleavedIndex = i; + for (size_t j = 0; j < aFrames; j++) { + ConvertAudioSample(aSourceBuffer[interleavedIndex], + aOutput[i][j]); + interleavedIndex += aChannels; + } + } +} + class SilentChannel { public: