From 20d8ea1c2d4472ba878fec217f01da875e0ee0f5 Mon Sep 17 00:00:00 2001 From: Hannes Date: Fri, 13 Jan 2012 11:52:52 +0100 Subject: [PATCH] Added a ulawdecoder. --- headeredit.cpp | 17 ++++++++++++++--- ulaw_decode.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 ulaw_decode.h diff --git a/headeredit.cpp b/headeredit.cpp index 5618a08..59db65a 100644 --- a/headeredit.cpp +++ b/headeredit.cpp @@ -4,11 +4,11 @@ #include #include - +signed short decode(unsigned char t); main() { std::vector data; - FILE* f = fopen("outputfileulaw.wav", "rb"); + FILE* f = fopen("outputfileulaw.raw", "rb"); rewind(f); while(!feof(f)) { @@ -16,7 +16,7 @@ main() fread(&s, sizeof s, 1, f); data.push_back(s); } - FILE* o = fopen("outputfileulaw.raw", "wb"); + FILE* o = fopen("outputfileulaw.dec", "wb"); int headersize = 4; int old_header_size = 58; unsigned int samples = data.size() - old_header_size; @@ -37,4 +37,15 @@ main() //memcpy(&new_data[0], &samples, sizeof(int)); memcpy(&new_data[4], &data[old_header_size], samples_old); fwrite(&new_data[0], sizeof(unsigned char), samples_old+headersize, o); + +} + +signed short decode(unsigned char t) +{ + int exp = 0x8 - (0x7 & (t >> 4)); + int base = (0x1 << (exp+ 5)) - 34; + int step = 0x1 << exp; + signed short ret = base - step * (0xf & t); + ret = -ret - 1 + (2 * ret + 1) * (t >> 7); + return ret; } \ No newline at end of file diff --git a/ulaw_decode.h b/ulaw_decode.h new file mode 100644 index 0000000..99d86ac --- /dev/null +++ b/ulaw_decode.h @@ -0,0 +1,9 @@ + signed short decode(unsigned char t) +{ + int exp = 0x8 - (0x7 & (t >> 4)); + int base = (0x1 << (exp+ 5)) - 34; + int step = 0x1 << exp; + signed short ret = base - step * (0xf & t); + ret = -ret - 1 + (2 * ret + 1) * (t >> 7); + return ret; +} \ No newline at end of file