7 // ======================================= ulaw codecs
9 float FileBase::ulawtofloat(char ulaw)
11 //printf("%f\n", ulawtofloat_ptr[ulaw]);
12 return ulawtofloat_ptr[(unsigned char)ulaw];
15 char FileBase::floattoulaw(float value)
17 return floattoulaw_ptr[(int)(value * 32767)];
20 // turn off the trap as per the MIL-STD
22 // define the add-in bias for 16 bit samples
26 int FileBase::generate_ulaw_tables()
31 if(!ulawtofloat_table)
33 static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
34 int sign, exponent, mantissa, sample;
35 unsigned char ulawbyte;
37 ulawtofloat_table = new float[256];
38 ulawtofloat_ptr = ulawtofloat_table;
39 for(i = 0; i < 256; i++)
41 ulawbyte = (unsigned char)i;
42 ulawbyte = ~ ulawbyte;
43 sign = ( ulawbyte & 0x80 );
44 exponent = ( ulawbyte >> 4 ) & 0x07;
45 mantissa = ulawbyte & 0x0F;
46 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
47 if ( sign != 0 ) sample = -sample;
49 ulawtofloat_ptr[(int)i] = (float)sample / 32768;
53 if(!floattoulaw_table)
55 int sign, exponent, mantissa;
56 unsigned char ulawbyte;
58 int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
59 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
60 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
61 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
62 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
63 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
64 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
65 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
66 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
67 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
68 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
69 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
70 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
71 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
72 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
73 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
75 floattoulaw_table = new unsigned char[65536];
76 floattoulaw_ptr = floattoulaw_table + 32768;
78 for(i = -32768; i < 32768; i++)
81 // Get the sample into sign-magnitude.
82 sign = (sample >> 8) & 0x80; // set aside the sign
83 if ( sign != 0 ) sample = -sample; // get magnitude
84 if ( sample > uCLIP ) sample = uCLIP; // clip the magnitude
86 // Convert from 16 bit linear to ulaw.
87 sample = sample + uBIAS;
88 exponent = exp_lut[( sample >> 7 ) & 0xFF];
89 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
90 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
92 if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */
95 floattoulaw_ptr[i] = ulawbyte;
101 int FileBase::delete_ulaw_tables()
103 if(floattoulaw_table) delete [] floattoulaw_table;
104 if(ulawtofloat_table) delete [] ulawtofloat_table;
105 floattoulaw_table = 0;
106 ulawtofloat_table = 0;