swap template function

This commit is contained in:
badaix 2016-05-05 10:50:17 +02:00
parent e06cdc9024
commit f9c517e99d
3 changed files with 34 additions and 28 deletions

View file

@ -60,19 +60,7 @@ PcmDecoder::PcmDecoder() : Decoder()
bool PcmDecoder::decode(msg::PcmChunk* chunk) bool PcmDecoder::decode(msg::PcmChunk* chunk)
{ {
/* int16_t* bufferT = (int16_t*)chunk->payload; return true;
for (size_t n=0; n<chunk->getSampleCount(); ++n)
{
bufferT[n] = SWAP_16(bufferT[n]);
}
if (sampleFormat.bits == 8)
adjustVolume<int8_t>(buffer, frames*sampleFormat.channels, volume);
else if (sampleFormat.bits == 16)
adjustVolume<int16_t>(buffer, frames*sampleFormat.channels, volume);
else if (sampleFormat.bits == 32)
adjustVolume<int32_t>(buffer, frames*sampleFormat.channels, volume);
*/ return true;
} }

View file

@ -52,21 +52,8 @@ protected:
void adjustVolume(char *buffer, size_t count, double volume) void adjustVolume(char *buffer, size_t count, double volume)
{ {
T* bufferT = (T*)buffer; T* bufferT = (T*)buffer;
if (sizeof(T) == 1) for (size_t n=0; n<count; ++n)
{ bufferT[n] = endian::swap<T>(endian::swap<T>(bufferT[n]) * volume);
for (size_t n=0; n<count; ++n)
bufferT[n] = bufferT[n] * volume;
}
else if (sizeof(T) == 2)
{
for (size_t n=0; n<count; ++n)
bufferT[n] = (T)(SWAP_16(((T)SWAP_16(bufferT[n])) * volume));
}
else if (sizeof(T) == 4)
{
for (size_t n=0; n<count; ++n)
bufferT[n] = (T)(SWAP_32(((T)SWAP_32(bufferT[n])) * volume));
}
} }
void adjustVolume(char* buffer, size_t frames); void adjustVolume(char* buffer, size_t frames);

View file

@ -11,6 +11,37 @@
# define SWAP_64(x) x # define SWAP_64(x) x
#endif #endif
namespace endian
{
template <typename T>
T swap(const T&);
template <>
inline int8_t swap(const int8_t& val)
{
return val;
}
template <>
inline int16_t swap(const int16_t& val)
{
return SWAP_16(val);
}
template <>
inline int32_t swap(const int32_t& val)
{
return SWAP_32(val);
}
template <>
inline int64_t swap(const int64_t& val)
{
return SWAP_64(val);
}
}
#endif #endif