comparison dev/s2u-regen-plus4.c @ 228:67d60915fbbe

dev: new program s2u-regen-plus4
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Apr 2023 17:07:51 +0000
parents dev/s2u-regen.c@a5200ad12d58
children
comparison
equal deleted inserted replaced
227:a5200ad12d58 228:67d60915fbbe
1 /*
2 * This program is a companion to s2u-regen.c: it computes a mu-law encoding
3 * table with 13-bit input just like s2u-regen, but computes it as if the
4 * lsb following the 13-bit part is always 1 rather than always 0. The
5 * purpose of this program is to illustrate the effect of that lsb on the
6 * mu-law output.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 static unsigned
13 ulaw_compress(input)
14 unsigned input;
15 {
16 short i; /* aux.var. */
17 short absno; /* absolute value of linear (input) sample */
18 short segno; /* segment (Table 2/G711, column 1) */
19 short low_nibble; /* low nibble of log companded sample */
20 short high_nibble; /* high nibble of log companded sample */
21 unsigned output;
22
23 /* -------------------------------------------------------------------- */
24 /* Input is 14-bit right-justified in this version */
25 /* Compute absolute value; adjust for easy processing */
26 /* -------------------------------------------------------------------- */
27 absno = input >= 0x2000 /* compute 1's complement in case of */
28 ? (~input & 0x1FFF) + 33 /* negative samples */
29 : input + 33; /* NB: 33 is the difference value */
30 /* between the thresholds for */
31 /* A-law and u-law. */
32 if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */
33 absno = (0x1FFF);
34
35 /* Determination of sample's segment */
36 i = absno >> 6;
37 segno = 1;
38 while (i != 0) {
39 segno++;
40 i >>= 1;
41 }
42
43 /* Mounting the high-nibble of the log-PCM sample */
44 high_nibble = (0x0008) - segno;
45
46 /* Mounting the low-nibble of the log PCM sample */
47 low_nibble = (absno >> segno) /* right shift of mantissa and */
48 &(0x000F); /* masking away leading '1' */
49 low_nibble = (0x000F) - low_nibble;
50
51 /* Joining the high-nibble and the low-nibble of the log PCM sample */
52 output = (high_nibble << 4) | low_nibble;
53
54 /* Add sign bit */
55 if (input < 0x2000)
56 output = output | (0x0080);
57
58 return output;
59 }
60
61 main(argc, argv)
62 char **argv;
63 {
64 unsigned input, output;
65
66 for (input = 0; input < 8192; input++) {
67 output = ulaw_compress((input << 1) + 1);
68 printf("%04o,", output);
69 if ((input % 15) == 14 || input == 8191)
70 putchar('\n');
71 }
72 exit(0);
73 }