From 41d62ad1ff2176693a9aa43e294158d639bc7557 Mon Sep 17 00:00:00 2001 From: Tristan Matthews <tristan.matthews@savoirfairelinux.com> Date: Thu, 26 Apr 2012 18:10:50 -0400 Subject: [PATCH] * #9979: ulaw: normalize types --- daemon/src/audio/codecs/ulaw.cpp | 42 +++++++++++++++----------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/daemon/src/audio/codecs/ulaw.cpp b/daemon/src/audio/codecs/ulaw.cpp index 7274b286ea..2c2ea5a7ae 100644 --- a/daemon/src/audio/codecs/ulaw.cpp +++ b/daemon/src/audio/codecs/ulaw.cpp @@ -44,73 +44,71 @@ class Ulaw : public sfl::AudioCodec { hasDynamicPayload_ = false; } - virtual int decode(short *dst, unsigned char *src, size_t buf_size) { + virtual int decode(SFLDataFormat *dst, unsigned char *src, size_t buf_size) { assert(buf_size == frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat)); - unsigned char* end = src+buf_size; - - while (src<end) - *dst++ = ULawDecode(*src++); + for (unsigned char* end = src + buf_size; src < end; ++src, ++dst) + *dst = ULawDecode(*src); return frameSize_; } - virtual int encode(unsigned char *dst, short *src, size_t buf_size) { + virtual int encode(unsigned char *dst, SFLDataFormat *src, size_t buf_size) { assert(buf_size >= frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat)); - uint8* end = dst + frameSize_; - - while (dst<end) - *dst++ = ULawEncode(*src++); + for (uint8* end = dst + frameSize_; dst < end; ++src, ++dst) + *dst = ULawEncode(*src); return frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat);; } - int ULawDecode(uint8 ulaw) { + SFLDataFormat ULawDecode(uint8 ulaw) + { ulaw ^= 0xff; // u-law has all bits inverted for transmission - int linear = ulaw&0x0f; + int linear = ulaw & 0x0f; linear <<= 3; linear |= 0x84; // Set MSB (0x80) and a 'half' bit (0x04) to place PCM value in middle of range - uint shift = ulaw>>4; + uint shift = ulaw >> 4; shift &= 7; linear <<= shift; linear -= 0x84; // Subract uLaw bias - if (ulaw&0x80) + if (ulaw & 0x80) return -linear; else return linear; } - uint8 ULawEncode(int16 pcm16) { + uint8 ULawEncode(SFLDataFormat pcm16) + { int p = pcm16; uint u; // u-law value we are forming - if (p<0) { + if (p < 0) { p = ~p; - u = 0x80^0x10^0xff; // Sign bit = 1 (^0x10 because this will get inverted later) ^0xff ^0xff to invert final u-Law code + u = 0x80 ^ 0x10 ^ 0xff; // Sign bit = 1 (^0x10 because this will get inverted later) ^0xff ^0xff to invert final u-Law code } else { - u = 0x00^0x10^0xff; // Sign bit = 0 (-0x10 because this amount extra will get added later) ^0xff to invert final u-Law code + u = 0x00 ^ 0x10 ^ 0xff; // Sign bit = 0 (-0x10 because this amount extra will get added later) ^0xff to invert final u-Law code } p += 0x84; // Add uLaw bias - if (p>0x7f00) + if (p > 0x7f00) p = 0x7f00; // Clip to 15 bits // Calculate segment and interval numbers p >>= 3; // Shift down to 13bit - if (p>=0x100) { + if (p >= 0x100) { p >>= 4; u ^= 0x40; } - if (p>=0x40) { + if (p >= 0x40) { p >>= 2; u ^= 0x20; } - if (p>=0x20) { + if (p >= 0x20) { p >>= 1; u ^= 0x10; } -- GitLab