changeset 228:67d60915fbbe

dev: new program s2u-regen-plus4
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Apr 2023 17:07:51 +0000
parents a5200ad12d58
children f00bf3687286
files .hgignore dev/Makefile dev/s2u-regen-plus4.c
diffstat 3 files changed, 83 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Apr 23 16:53:32 2023 +0000
+++ b/.hgignore	Sun Apr 23 17:07:51 2023 +0000
@@ -23,6 +23,8 @@
 ^dev/s2a-regen\.out$
 ^dev/s2u-regen$
 ^dev/s2u-regen\.out$
+^dev/s2u-regen-plus4$
+^dev/s2u-regen-plus4\.out$
 
 ^efrtest/gsmefr-cod-parse$
 ^efrtest/gsmefr-dec-parse$
--- a/dev/Makefile	Sun Apr 23 16:53:32 2023 +0000
+++ b/dev/Makefile	Sun Apr 23 17:07:51 2023 +0000
@@ -1,9 +1,9 @@
 CC=	gcc
 CFLAGS=	-O2
 PROGS=	a2s-regen efr-bit-packing efr-sid-insert efr-tidsp gsm0611-silence-fr \
-	s2a-regen s2u-regen
+	s2a-regen s2u-regen s2u-regen-plus4
 DATAOUT=a2s-regen.out efr-bit-packing.out efr-sid-insert.out efr-tidsp.out \
-	gsm0611-silence-fr.out s2a-regen.out s2u-regen.out
+	gsm0611-silence-fr.out s2a-regen.out s2u-regen.out s2u-regen-plus4.out
 
 all:	${PROGS} ${DATAOUT}
 
@@ -49,5 +49,11 @@
 s2u-regen.out:	s2u-regen
 	./s2u-regen > $@
 
+s2u-regen-plus4:	s2u-regen-plus4.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
+s2u-regen-plus4.out:	s2u-regen-plus4
+	./s2u-regen-plus4 > $@
+
 clean:
 	rm -f *.o *.out ${PROGS}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dev/s2u-regen-plus4.c	Sun Apr 23 17:07:51 2023 +0000
@@ -0,0 +1,73 @@
+/*
+ * This program is a companion to s2u-regen.c: it computes a mu-law encoding
+ * table with 13-bit input just like s2u-regen, but computes it as if the
+ * lsb following the 13-bit part is always 1 rather than always 0.  The
+ * purpose of this program is to illustrate the effect of that lsb on the
+ * mu-law output.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static unsigned
+ulaw_compress(input)
+    unsigned input;
+{
+    short i;                    /* aux.var. */
+    short absno;                /* absolute value of linear (input) sample */
+    short segno;                /* segment (Table 2/G711, column 1) */
+    short low_nibble;           /* low nibble of log companded sample */
+    short high_nibble;          /* high nibble of log companded sample */
+    unsigned output;
+
+    /* -------------------------------------------------------------------- */
+    /* Input is 14-bit right-justified in this version */
+    /* Compute absolute value; adjust for easy processing */
+    /* -------------------------------------------------------------------- */
+    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 */
+      absno = (0x1FFF);
+
+    /* Determination of sample's segment */
+    i = absno >> 6;
+    segno = 1;
+    while (i != 0) {
+      segno++;
+      i >>= 1;
+    }
+
+    /* Mounting the high-nibble of the log-PCM sample */
+    high_nibble = (0x0008) - segno;
+
+    /* Mounting the low-nibble of the log PCM sample */
+    low_nibble = (absno >> segno)       /* right shift of mantissa and */
+      &(0x000F);                /* masking away leading '1' */
+    low_nibble = (0x000F) - low_nibble;
+
+    /* Joining the high-nibble and the low-nibble of the log PCM sample */
+    output = (high_nibble << 4) | low_nibble;
+
+    /* Add sign bit */
+    if (input < 0x2000)
+      output = output | (0x0080);
+
+    return output;
+}
+
+main(argc, argv)
+	char **argv;
+{
+	unsigned input, output;
+
+	for (input = 0; input < 8192; input++) {
+		output = ulaw_compress((input << 1) + 1);
+		printf("%04o,", output);
+		if ((input % 15) == 14 || input == 8191)
+			putchar('\n');
+	}
+	exit(0);
+}