comparison dev/s2u-regen.c @ 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
comparison
equal deleted inserted replaced
226:84d22eb72196 227:a5200ad12d58
1 /* 1 /*
2 * This program regenerates a G.711 mu-law encoding table equivalent to the 2 * This program generates a G.711 mu-law encoding table of the same form
3 * s2u[] table in the toast_ulaw.c module in libgsm/toast; the intent is 3 * as the s2u[] table in the toast_ulaw.c module in libgsm/toast, i.e.,
4 * to check that table for correctness. The "engine" function that does 4 * an encoding table that takes only the upper 13 bits of the linear PCM
5 * the computation is based on ulaw_compress() from ITU-T G.191 STL. 5 * sample to be encoded, even though canonical mu-law encoding requires
6 * 14-bit input. The 13-bit table constructed by this program is computed
7 * as if the lsb of the "proper" 14-bit input is always zero, like it is
8 * expected to be when the linear PCM samples came from the output of a
9 * GSM speech decoder.
10 *
11 * The "engine" function that does the computation is based on ulaw_compress()
12 * from ITU-T G.191 STL.
6 */ 13 */
7 14
8 #include <stdio.h> 15 #include <stdio.h>
9 #include <stdlib.h> 16 #include <stdlib.h>
10 17
18 short low_nibble; /* low nibble of log companded sample */ 25 short low_nibble; /* low nibble of log companded sample */
19 short high_nibble; /* high nibble of log companded sample */ 26 short high_nibble; /* high nibble of log companded sample */
20 unsigned output; 27 unsigned output;
21 28
22 /* -------------------------------------------------------------------- */ 29 /* -------------------------------------------------------------------- */
23 /* Change from 14 bit left justified to 14 bit right justified */ 30 /* Input is 14-bit right-justified in this version */
24 /* Compute absolute value; adjust for easy processing */ 31 /* Compute absolute value; adjust for easy processing */
25 /* -------------------------------------------------------------------- */ 32 /* -------------------------------------------------------------------- */
26 absno = input >= 4096 /* compute 1's complement in case of */ 33 absno = input >= 0x2000 /* compute 1's complement in case of */
27 ? ((~input & 4095) << 1) + 33 /* negative samples */ 34 ? (~input & 0x1FFF) + 33 /* negative samples */
28 : (input << 1) + 33; /* NB: 33 is the difference value */ 35 : input + 33; /* NB: 33 is the difference value */
29 /* between the thresholds for */ 36 /* between the thresholds for */
30 /* A-law and u-law. */ 37 /* A-law and u-law. */
31 if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */ 38 if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */
32 absno = (0x1FFF); 39 absno = (0x1FFF);
33 40
49 56
50 /* Joining the high-nibble and the low-nibble of the log PCM sample */ 57 /* Joining the high-nibble and the low-nibble of the log PCM sample */
51 output = (high_nibble << 4) | low_nibble; 58 output = (high_nibble << 4) | low_nibble;
52 59
53 /* Add sign bit */ 60 /* Add sign bit */
54 if (input < 4096) 61 if (input < 0x2000)
55 output = output | (0x0080); 62 output = output | (0x0080);
56 63
57 return output; 64 return output;
58 } 65 }
59 66
61 char **argv; 68 char **argv;
62 { 69 {
63 unsigned input, output; 70 unsigned input, output;
64 71
65 for (input = 0; input < 8192; input++) { 72 for (input = 0; input < 8192; input++) {
66 output = ulaw_compress(input); 73 output = ulaw_compress(input << 1);
67 printf("%04o,", output); 74 printf("%04o,", output);
68 if ((input % 15) == 14 || input == 8191) 75 if ((input % 15) == 14 || input == 8191)
69 putchar('\n'); 76 putchar('\n');
70 } 77 }
71 exit(0); 78 exit(0);