2 * This routine converts from linear to ulaw
5 * Craig Reese: IDA/Supercomputing Research Center
6 * Joe Campbell: Department of Defense
9 * 1) CCITT Recommendation G.711 (very difficult to follow)
10 * 2) "A New Digital Technique for Implementation of Any
11 * Continuous PCM Companding Law," Villeret, Michel,
12 * et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
13 * 1973, pg. 11.12-11.17
14 * 3) MIL-STD-188-113,"Interoperability and Performance Standards
15 * for Analog-to_Digital Conversion Techniques,"
18 * Input: Signed 16 bit linear sample
19 * Output: 8 bit ulaw sample
28 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
29 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
33 mulaw_encode(gint16* in, guint8* out, gint numsamples)
35 static gint16 exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
36 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
37 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
38 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
39 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
40 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
41 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
42 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
43 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
44 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
45 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
46 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
47 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
48 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
49 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
50 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
51 gint16 sign, exponent, mantissa,i;
55 for(i=0;i<numsamples;i++) {
57 /** get the sample into sign-magnitude **/
58 sign = (sample >> 8) & 0x80; /* set aside the sign */
59 if (sign != 0) sample = -sample; /* get magnitude */
60 if (sample > CLIP) sample = CLIP; /* clip the magnitude */
61 /** convert from 16 bit linear to ulaw **/
62 sample = sample + BIAS;
63 exponent = exp_lut[(sample>>7) & 0xFF];
64 mantissa = (sample >> (exponent+3)) & 0x0F;
65 ulawbyte = ~(sign | (exponent << 4) | mantissa);
67 if (ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */
74 * This routine converts from ulaw to 16 bit linear
77 * Craig Reese: IDA/Supercomputing Research Center
80 * 1) CCITT Recommendation G.711 (very difficult to follow)
81 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
82 * for Analog-to_Digital Conversion Techniques,"
85 * Input: 8 bit ulaw sample
86 * Output: signed 16 bit linear sample
90 mulaw_decode(guint8* in,gint16* out,gint numsamples)
92 static gint16 exp_lut[8]={0,132,396,924,1980,4092,8316,16764};
93 gint16 sign, exponent, mantissa;
96 for(i=0;i<numsamples;i++) {
99 sign = (ulawbyte & 0x80);
100 exponent = (ulawbyte >> 4) & 0x07;
101 mantissa = ulawbyte & 0x0F;
102 linear = exp_lut[exponent] + (mantissa << (exponent+3));
103 if (sign != 0) linear = -linear;