pcm - u-law encode/decode

Dependents:   PhonePlatform

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Thu Nov 11 16:13:47 2010 +0000
Child:
1:546b99dee7bb
Commit message:

Changed in this revision

ulaw.cpp Show annotated file Show diff for this revision Revisions of this file
ulaw.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ulaw.cpp	Thu Nov 11 16:13:47 2010 +0000
@@ -0,0 +1,45 @@
+/*
+ * mbed library for ulaw
+ * Copyright (c) 2010 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "ulaw.h"
+
+// unsigned 16(14)bit to signed 8bit
+char pcm2ulaw (int dat) {
+    int num, seg;
+    char ret;
+
+    dat = dat - 32768;
+    if (dat >= 0) {
+        num = dat;
+    } else {
+        num = - dat;
+    }
+    num = (num >> 2) + 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;
+}
+
+
+// signed 8bit to unsigned 16(14)bit
+int ulaw2pcm (char dat) {
+    int ret, seg;
+
+    seg = (dat >> 4) & 0x07;
+    ret = (0x21 | ((dat & 0x0f) << 1)) << seg;
+    ret = (ret - 0x21) << 2;
+
+    if (dat & 0x80) ret = - ret;
+    ret = ret + 32768;
+    return ret;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ulaw.h	Thu Nov 11 16:13:47 2010 +0000
@@ -0,0 +1,15 @@
+/*
+ * mbed library for ulaw
+ * Copyright (c) 2010 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#ifndef ulaw_H
+#define ulaw_H
+
+#include "mbed.h"
+
+char pcm2ulaw (int);
+int ulaw2pcm (char);
+
+#endif