1 /* 2000-08-10T04:29:10Z
3 * generate ulaw<->linear conversion tables to be included
4 * directly in emerald mine source
10 #if defined(TARGET_X11)
12 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
16 int calc_ulaw_to_linear(unsigned char);
17 unsigned char calc_linear_to_ulaw(int);
21 /* convert from 8 bit ulaw to signed 16 bit linear */
22 short ulaw_to_linear[256];
24 /* convert from signed 16 bit linear to 8 bit ulaw */
25 unsigned char linear_to_ulaw[65536];
31 for(i = 0; i < 256; i++)
32 ulaw_to_linear[i] = calc_ulaw_to_linear(i);
34 for(i = -32768; i < 32768; i++)
35 linear_to_ulaw[i + 32768] = calc_linear_to_ulaw(i);
39 ** This routine converts from ulaw to 16 bit linear.
41 ** Craig Reese: IDA/Supercomputing Research Center
45 ** 1) CCITT Recommendation G.711 (very difficult to follow)
46 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
47 ** for Analog-to_Digital Conversion Techniques,"
50 ** Input: 8 bit ulaw sample
51 ** Output: signed 16 bit linear sample
54 int calc_ulaw_to_linear(unsigned char ulawbyte)
56 static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
57 int sign, exponent, mantissa, sample;
59 ulawbyte = ~ ulawbyte;
60 sign = ( ulawbyte & 0x80 );
61 exponent = ( ulawbyte >> 4 ) & 0x07;
62 mantissa = ulawbyte & 0x0F;
63 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
71 ** This routine converts from linear to ulaw.
73 ** Craig Reese: IDA/Supercomputing Research Center
74 ** Joe Campbell: Department of Defense
78 ** 1) CCITT Recommendation G.711 (very difficult to follow)
79 ** 2) "A New Digital Technique for Implementation of Any
80 ** Continuous PCM Companding Law," Villeret, Michel,
81 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
82 ** 1973, pg. 11.12-11.17
83 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
84 ** for Analog-to_Digital Conversion Techniques,"
87 ** Input: Signed 16 bit linear sample
88 ** Output: 8 bit ulaw sample
91 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
92 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
95 unsigned char calc_linear_to_ulaw(int sample)
97 static int exp_lut[256] =
99 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
100 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
101 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
102 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
103 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
104 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
105 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
106 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
107 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
108 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
109 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
110 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
111 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
112 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
113 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
114 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
117 int sign, exponent, mantissa;
118 unsigned char ulawbyte;
120 /* Get the sample into sign-magnitude. */
121 sign = (sample >> 8) & 0x80; /* set aside the sign */
123 sample = -sample; /* get magnitude */
125 sample = CLIP; /* clip the magnitude */
127 /* Convert from 16 bit linear to ulaw. */
128 sample = sample + BIAS;
129 exponent = exp_lut[( sample >> 7 ) & 0xFF];
130 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
131 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
134 ulawbyte = 0x02; /* optional CCITT trap */
140 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */