Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

filebaseulaw.C

Go to the documentation of this file.
00001 #include "assets.h"
00002 #include "byteorder.h"
00003 #include "file.h"
00004 #include "filebase.h"
00005 #include "sizes.h"
00006 
00007 // ======================================= ulaw codecs
00008 
00009 float FileBase::ulawtofloat(char ulaw)
00010 {
00011 //printf("%f\n", ulawtofloat_ptr[ulaw]);
00012         return ulawtofloat_ptr[(unsigned char)ulaw];
00013 }
00014 
00015 char FileBase::floattoulaw(float value)
00016 {
00017         return floattoulaw_ptr[(int)(value * 32767)];
00018 }
00019 
00020 // turn off the trap as per the MIL-STD
00021 #undef ZEROTRAP
00022 // define the add-in bias for 16 bit samples
00023 #define uBIAS 0x84
00024 #define uCLIP 32635
00025 
00026 int FileBase::generate_ulaw_tables()
00027 {
00028         int i;
00029         float value;
00030 
00031         if(!ulawtofloat_table)
00032         {
00033         static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
00034         int sign, exponent, mantissa, sample;
00035                 unsigned char ulawbyte;
00036 
00037                 ulawtofloat_table = new float[256];
00038                 ulawtofloat_ptr = ulawtofloat_table;
00039                 for(i = 0; i < 256; i++)
00040                 {
00041                         ulawbyte = (unsigned char)i;
00042                 ulawbyte = ~ ulawbyte;
00043                 sign = ( ulawbyte & 0x80 );
00044                 exponent = ( ulawbyte >> 4 ) & 0x07;
00045                 mantissa = ulawbyte & 0x0F;
00046                 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
00047                 if ( sign != 0 ) sample = -sample;
00048 
00049                         ulawtofloat_ptr[(int)i] = (float)sample / 32768;
00050                 }
00051         }
00052 
00053         if(!floattoulaw_table)
00054         {
00055         int sign, exponent, mantissa;
00056         unsigned char ulawbyte;
00057                 int sample;
00058         int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
00059                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
00060                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00061                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00062                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00063                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00064                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00065                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00066                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00067                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00068                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00069                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00070                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00071                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00072                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00073                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
00074 
00075                 floattoulaw_table = new unsigned char[65536];
00076                 floattoulaw_ptr = floattoulaw_table + 32768;
00077 
00078                 for(i = -32768; i < 32768; i++)
00079                 {
00080                         sample = i;
00081 // Get the sample into sign-magnitude.
00082                 sign = (sample >> 8) & 0x80;            // set aside the sign
00083                 if ( sign != 0 ) sample = -sample;              // get magnitude
00084                 if ( sample > uCLIP ) sample = uCLIP;           // clip the magnitude
00085 
00086 // Convert from 16 bit linear to ulaw.
00087                 sample = sample + uBIAS;
00088                     exponent = exp_lut[( sample >> 7 ) & 0xFF];
00089                     mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
00090                     ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
00091 #ifdef ZEROTRAP
00092                     if ( ulawbyte == 0 ) ulawbyte = 0x02;       /* optional CCITT trap */
00093 #endif
00094 
00095                     floattoulaw_ptr[i] = ulawbyte;
00096                 }
00097         }
00098         return 0;
00099 }
00100 
00101 int FileBase::delete_ulaw_tables()
00102 {
00103         if(floattoulaw_table) delete [] floattoulaw_table;
00104         if(ulawtofloat_table) delete [] ulawtofloat_table;
00105         floattoulaw_table = 0;
00106         ulawtofloat_table = 0;
00107         return 0;
00108 }

Generated on Sun Jan 8 13:38:54 2006 for Cinelerra-svn by  doxygen 1.4.4