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
24 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
25 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
29 mulaw_encode(gint16* in, guint8* out, gint numsamples)
31 static gint16 exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
32 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
33 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
34 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
35 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
36 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
37 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
38 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
39 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
40 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
41 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
42 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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 gint16 sign, exponent, mantissa,i;
51 for(i=0;i<numsamples;i++) {
53 /** get the sample into sign-magnitude **/
54 sign = (sample >> 8) & 0x80; /* set aside the sign */
55 if (sign != 0) sample = -sample; /* get magnitude */
56 if (sample > CLIP) sample = CLIP; /* clip the magnitude */
57 /** convert from 16 bit linear to ulaw **/
58 sample = sample + BIAS;
59 exponent = exp_lut[(sample>>7) & 0xFF];
60 mantissa = (sample >> (exponent+3)) & 0x0F;
61 ulawbyte = ~(sign | (exponent << 4) | mantissa);
63 if (ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */
70 * This routine converts from ulaw to 16 bit linear
73 * Craig Reese: IDA/Supercomputing Research Center
76 * 1) CCITT Recommendation G.711 (very difficult to follow)
77 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
78 * for Analog-to_Digital Conversion Techniques,"
81 * Input: 8 bit ulaw sample
82 * Output: signed 16 bit linear sample
86 mulaw_decode(guint8* in,gint16* out,gint numsamples)
88 static gint16 exp_lut[8]={0,132,396,924,1980,4092,8316,16764};
89 gint16 sign, exponent, mantissa;
92 for(i=0;i<numsamples;i++) {
95 sign = (ulawbyte & 0x80);
96 exponent = (ulawbyte >> 4) & 0x07;
97 mantissa = ulawbyte & 0x0F;
98 linear = exp_lut[exponent] + (mantissa << (exponent+3));
99 if (sign != 0) linear = -linear;