4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "byteorder.h"
28 // ======================================= ulaw codecs
30 float FileBase::ulawtofloat(char ulaw)
32 //printf("%f\n", ulawtofloat_ptr[ulaw]);
33 return ulawtofloat_ptr[(unsigned char)ulaw];
36 char FileBase::floattoulaw(float value)
38 return floattoulaw_ptr[(int)(value * 32767)];
41 // turn off the trap as per the MIL-STD
43 // define the add-in bias for 16 bit samples
47 int FileBase::generate_ulaw_tables()
49 if( !ulawtofloat_table )
51 static int exp_lut[8] = {
52 0, 132, 396, 924, 1980, 4092, 8316, 16764
54 int sign, exponent, mantissa, sample;
55 unsigned char ulawbyte;
57 ulawtofloat_table = new float[256];
58 ulawtofloat_ptr = ulawtofloat_table;
59 for(int i = 0; i < 256; i++)
61 ulawbyte = (unsigned char)i;
62 ulawbyte = ~ ulawbyte;
63 sign = ( ulawbyte & 0x80 );
64 exponent = ( ulawbyte >> 4 ) & 0x07;
65 mantissa = ulawbyte & 0x0F;
66 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
67 if ( sign != 0 ) sample = -sample;
68 ulawtofloat_ptr[(int)i] = (float)sample / 32768;
72 if( !floattoulaw_table )
74 int sign, exponent, mantissa;
75 unsigned char ulawbyte;
78 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
79 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
80 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
81 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
82 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
83 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
84 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
85 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
86 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
87 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
88 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
89 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
90 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
91 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
92 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
93 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
96 floattoulaw_table = new unsigned char[65536];
97 floattoulaw_ptr = floattoulaw_table + 32768;
99 for(int i = -32768; i < 32768; i++)
102 // Get the sample into sign-magnitude.
103 sign = (sample >> 8) & 0x80; // set aside the sign
104 if ( sign != 0 ) sample = -sample; // get magnitude
105 if ( sample > uCLIP ) sample = uCLIP; // clip the magnitude
106 // Convert from 16 bit linear to ulaw.
107 sample = sample + uBIAS;
108 exponent = exp_lut[( sample >> 7 ) & 0xFF];
109 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
110 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
112 if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */
114 floattoulaw_ptr[i] = ulawbyte;
120 int FileBase::delete_ulaw_tables()
122 if(floattoulaw_table) delete [] floattoulaw_table;
123 if(ulawtofloat_table) delete [] ulawtofloat_table;
124 floattoulaw_table = 0;
125 ulawtofloat_table = 0;