Skip to content
Snippets Groups Projects
Commit 88832a4d authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #9979: Alaw: cleanup

parent f621f843
No related branches found
No related tags found
No related merge requests found
......@@ -37,8 +37,7 @@ class Alaw : public sfl::AudioCodec {
public:
// 8 PCMA A 8000 1 [RFC3551]
Alaw(int payload=8)
: sfl::AudioCodec(payload, "PCMA") {
Alaw(int payload = 8) : sfl::AudioCodec(payload, "PCMA") {
clockRate_ = 8000;
frameSize_ = 160; // samples, 20 ms at 8kHz
channel_ = 1;
......@@ -46,34 +45,30 @@ class Alaw : public sfl::AudioCodec {
hasDynamicPayload_ = false;
}
virtual ~Alaw() {}
virtual int decode(short *dst, unsigned char *src, size_t buf_size) {
private:
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++ = ALawDecode(*src++);
for (unsigned char* end = src + buf_size; src < end; ++src, ++dst)
*dst = ALawDecode(*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++ = ALawEncode(*src++);
for (unsigned char *end = dst + frameSize_; dst < end; ++src, ++dst)
*dst = ALawEncode(*src);
return frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat);
}
int ALawDecode(uint8 alaw) {
int ALawDecode(uint8 alaw)
{
alaw ^= 0x55; // A-law has alternate bits inverted for transmission
uint sign = alaw&0x80;
int linear = alaw&0x1f;
uint sign = alaw & 0x80;
int linear = alaw & 0x1f;
linear <<= 4;
linear += 8; // Add a 'half' bit (0x08) to place PCM value in middle of range
......@@ -91,8 +86,8 @@ class Alaw : public sfl::AudioCodec {
return linear;
}
uint8 ALawEncode(int16 pcm16) {
uint8 ALawEncode(SFLDataFormat pcm16)
{
int p = pcm16;
uint a; // u-law value we are forming
......@@ -107,18 +102,18 @@ class Alaw : public sfl::AudioCodec {
//calculate segment and interval numbers
p >>= 4;
if (p>=0x20) {
if (p>=0x100) {
if (p >= 0x20) {
if (p >= 0x100) {
p >>= 4;
a += 0x40;
}
if (p>=0x40) {
if (p >= 0x40) {
p >>= 2;
a += 0x20;
}
if (p>=0x20) {
if (p >= 0x20) {
p >>= 1;
a += 0x10;
}
......@@ -127,7 +122,8 @@ class Alaw : public sfl::AudioCodec {
// a&0x70 now holds segment value and 'p' the interval number
a += p; // a now equal to encoded A-law value
return a^0x55; // A-law has alternate bits inverted for transmission
// A-law has alternate bits inverted for transmission
return a ^ 0x55;
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment