pcm - u-law encode/decode

Dependents:   PhonePlatform

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Thu Jan 06 16:07:42 2011 +0000
Parent:
2:2587e6e46668
Commit message:

Changed in this revision

ulaw.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ulaw.cpp	Wed Jan 05 14:14:01 2011 +0000
+++ b/ulaw.cpp	Thu Jan 06 16:07:42 2011 +0000
@@ -10,23 +10,24 @@
 char pcm2ulaw (int dat) {
     int num, ret, seg;
 
-    dat = dat - 32768;
-    if (dat >= 0) {
-        num = dat;
-    } else {
+    dat = dat - 0x7fff;
+    if (dat & 0x8000) {
         num = - dat;
+    } else {
+        num = dat;
     }
-    num = (num >> 2) + 0x21;
+    num = num >> 2;
+    
+    num = num + 0x21;
     if (num >= 0x2000) num = 0x1fff;
-
     for (seg = 7; seg >= 0; seg --) {
         if (num & 0x1000) break;
         num = num << 1;
     }
     ret = ((num >> 8) & 0x0f) | (seg << 4);
 
-    if (dat < 0) ret = - ret;
-    return ret;
+    if (dat & 0x8000) ret = ret | 0x80;
+    return ~ret;
 }
 
 
@@ -34,16 +35,14 @@
 int ulaw2pcm (char dat) {
     int num, ret, seg;
 
-    if (dat & 0x80) {
-        num = - dat;
-    } else {
-        num = dat;
-    }
+    num = ~dat;
+
     seg = (num >> 4) & 0x07;
     ret = (0x21 | ((num & 0x0f) << 1)) << seg;
-    ret = (ret - 0x21) << 2;
+    ret = ret - 0x21;
 
+    ret = ret << 2;
     if (dat & 0x80) ret = - ret;
-    ret = ret + 32768;
+    ret = ret + 0x8000;
     return ret;
 }