Skip to content

Commit

Permalink
Bug 901633 - Part 12 - Add a function to deinterleave and convert an …
Browse files Browse the repository at this point in the history
…audio buffer. r=jesup

--HG--
extra : rebase_source : 60200bdecfb896d17cf42f205aba2f08afe2e4ac
  • Loading branch information
padenot committed Sep 1, 2015
1 parent c0fd8df commit 089ff9d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
45 changes: 45 additions & 0 deletions dom/media/AudioSampleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ FloatToAudioSample<int16_t>(float aValue)
return int16_t(clamped);
}

template <typename T> T IntegerToAudioSample(int16_t aValue);

template <> inline float
IntegerToAudioSample<float>(int16_t aValue)
{
return aValue / 32768.0f;
}
template <> inline int16_t
IntegerToAudioSample<int16_t>(int16_t aValue)
{
return aValue;
}

template<typename SrcT, typename DstT>
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<int16_t>(aIn);
}

// Sample buffer conversion

template <typename From, typename To> inline void
Expand Down
26 changes: 21 additions & 5 deletions dom/media/AudioSegment.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,39 @@ 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 <class SrcT, class DestT>
template <typename SrcT, typename DestT>
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<DestT>(v);
++output;
}
}
}

template <typename SrcT, typename DestT>
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:
Expand Down

0 comments on commit 089ff9d

Please sign in to comment.