changeset 227:a5200ad12d58

dev/s2u-regen.c: new approach
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Apr 2023 16:53:32 +0000
parents 84d22eb72196
children 67d60915fbbe
files dev/s2u-regen.c
diffstat 1 files changed, 17 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/dev/s2u-regen.c	Sun Apr 23 05:17:06 2023 +0000
+++ b/dev/s2u-regen.c	Sun Apr 23 16:53:32 2023 +0000
@@ -1,8 +1,15 @@
 /*
- * This program regenerates a G.711 mu-law encoding table equivalent to the
- * s2u[] table in the toast_ulaw.c module in libgsm/toast; the intent is
- * to check that table for correctness.  The "engine" function that does
- * the computation is based on ulaw_compress() from ITU-T G.191 STL.
+ * This program generates a G.711 mu-law encoding table of the same form
+ * as the s2u[] table in the toast_ulaw.c module in libgsm/toast, i.e.,
+ * an encoding table that takes only the upper 13 bits of the linear PCM
+ * sample to be encoded, even though canonical mu-law encoding requires
+ * 14-bit input.  The 13-bit table constructed by this program is computed
+ * as if the lsb of the "proper" 14-bit input is always zero, like it is
+ * expected to be when the linear PCM samples came from the output of a
+ * GSM speech decoder.
+ *
+ * The "engine" function that does the computation is based on ulaw_compress()
+ * from ITU-T G.191 STL.
  */
 
 #include <stdio.h>
@@ -20,12 +27,12 @@
     unsigned output;
 
     /* -------------------------------------------------------------------- */
-    /* Change from 14 bit left justified to 14 bit right justified */
+    /* Input is 14-bit right-justified in this version */
     /* Compute absolute value; adjust for easy processing */
     /* -------------------------------------------------------------------- */
-    absno = input >= 4096       /* compute 1's complement in case of */
-      ? ((~input & 4095) << 1) + 33     /* negative samples */
-      : (input << 1) + 33;              /* NB: 33 is the difference value */
+    absno = input >= 0x2000     /* compute 1's complement in case of */
+      ? (~input & 0x1FFF) + 33          /* negative samples */
+      : input + 33;                     /* NB: 33 is the difference value */
     /* between the thresholds for */
     /* A-law and u-law. */
     if (absno > (0x1FFF))       /* limitation to "absno" < 8192 */
@@ -51,7 +58,7 @@
     output = (high_nibble << 4) | low_nibble;
 
     /* Add sign bit */
-    if (input < 4096)
+    if (input < 0x2000)
       output = output | (0x0080);
 
     return output;
@@ -63,7 +70,7 @@
 	unsigned input, output;
 
 	for (input = 0; input < 8192; input++) {
-		output = ulaw_compress(input);
+		output = ulaw_compress(input << 1);
 		printf("%04o,", output);
 		if ((input % 15) == 14 || input == 8191)
 			putchar('\n');