55 static short st_ulaw_to_short(
unsigned char ulawbyte);
    56 static unsigned char st_short_to_ulaw(
short sample);
    57 static unsigned char st_short_to_alaw(
short sample);
    66 static unsigned short a2s[] = {
    67          5120,60160,  320,65200,20480,44032, 1280,64192,
    68          2560,62848,   64,65456,10240,54784,  640,64864,
    69          7168,58112,  448,65072,28672,35840, 1792,63680,
    70          3584,61824,  192,65328,14336,50688,  896,64608,
    71          4096,61184,  256,65264,16384,48128, 1024,64448,
    72          2048,63360,    0,65520, 8192,56832,  512,64992,
    73          6144,59136,  384,65136,24576,39936, 1536,63936,
    74          3072,62336,  128,65392,12288,52736,  768,64736,
    75          5632,59648,  352,65168,22528,41984, 1408,64064,
    76          2816,62592,   96,65424,11264,53760,  704,64800,
    77          7680,57600,  480,65040,30720,33792, 1920,63552,
    78          3840,61568,  224,65296,15360,49664,  960,64544,
    79          4608,60672,  288,65232,18432,46080, 1152,64320,
    80          2304,63104,   32,65488, 9216,55808,  576,64928,
    81          6656,58624,  416,65104,26624,37888, 1664,63808,
    82          3328,62080,  160,65360,13312,51712,  832,64672,
    83          5376,59904,  336,65184,21504,43008, 1344,64128,
    84          2688,62720,   80,65440,10752,54272,  672,64832,
    85          7424,57856,  464,65056,29696,34816, 1856,63616,
    86          3712,61696,  208,65312,14848,50176,  928,64576,
    87          4352,60928,  272,65248,17408,47104, 1088,64384,
    88          2176,63232,   16,65504, 8704,56320,  544,64960,
    89          6400,58880,  400,65120,25600,38912, 1600,63872,
    90          3200,62208,  144,65376,12800,52224,  800,64704,
    91          5888,59392,  368,65152,23552,40960, 1472,64000,
    92          2944,62464,  112,65408,11776,53248,  736,64768,
    93          7936,57344,  496,65024,31744,32768, 1984,63488,
    94          3968,61440,  240,65280,15872,49152,  992,64512,
    95          4864,60416,  304,65216,19456,45056, 1216,64256,
    96          2432,62976,   48,65472, 9728,55296,  608,64896,
    97          6912,58368,  432,65088,27648,36864, 1728,63744,
    98          3456,61952,  176,65344,13824,51200,  864,64640
   101 #define st_alaw_to_short(a) (a2s[(unsigned char)a])   108     for (i=0; i<length; i++)
   109     data[i] = st_ulaw_to_short(ulaw[i]);  
   118     for (i=0; i<length; i++)
   138     for (i=0; i<length; i++)
   140       data[i] = (((
int)chars[i])-128)*256;
   150     for (i=0; i<length; i++)
   151     data[i] = (((
unsigned char)chars[i]))*256;
   160     for (i=0; i<length; i++)
   161     chars[i] = (data[i]/256)+128;
   170     for (i=0; i<length; i++)
   171     chars[i] = (data[i]/256);
   176 void short_to_adpcm(
short *data,
signed char *chars,
int length)
   178     struct adpcm_state state;
   182     adpcm_coder(data,chars,length,&state);
   186 void adpcm_to_short(
signed char *chars, 
short *data,
int length)
   188     struct adpcm_state state;
   192     adpcm_decoder(chars,data,length/2,&state);
   201     for (i=0; i<length; i++)
   202     ulaw[i] = st_short_to_ulaw(data[i]);  
   211     for (i=0; i<length; i++)
   212     alaw[i] = st_short_to_alaw(data[i]);  
   228     return (
short *)file_data;
   232     d = 
walloc(
short,data_length);
   237     else if (sample_type == 
st_alaw)
   239     d = 
walloc(
short,data_length);
   247     d = 
walloc(
short,data_length);
   248     adpcm_to_short((
signed char *)file_data,d,data_length);
   255     d = 
walloc(
short,data_length);
   262     d = 
walloc(
short,data_length);
   268       EST_error(
"Convert raw data: unsupported sample type %s(%d)",
   276                     int num_samples, 
int num_channels, 
   285     unsigned char *ulaw = 
walloc(
unsigned char,num_samples*num_channels);
   287               ulaw,num_samples*num_channels);
   288     n = fwrite(ulaw,1,num_channels * num_samples,fp);   
   290     if (n != (num_channels * num_samples))
   293     else if (sample_type == 
st_alaw)
   295     unsigned char *alaw = 
walloc(
unsigned char,num_samples*num_channels);
   297               alaw,num_samples*num_channels);
   298     n = fwrite(alaw,1,num_channels * num_samples,fp);   
   300     if (n != (num_channels * num_samples))
   305     for (i=offset*num_channels; i < num_samples*num_channels; i++)
   306         fprintf(fp,
"%d\n",data[i]);
   310     unsigned char *chars = 
walloc(
unsigned char,num_samples*num_channels);
   312                chars,num_samples*num_channels);
   313     n = fwrite(chars,1,num_channels * num_samples,fp);
   315     if (n != (num_channels * num_samples))
   320     unsigned char *chars = 
walloc(
unsigned char,num_samples*num_channels);
   322                chars,num_samples*num_channels);
   323     n = fwrite(chars,1,num_channels * num_samples,fp);
   325     if ( n != (num_channels * num_samples))
   331     signed char *chars = 
walloc(
signed char,num_samples*num_channels);
   332     short_to_adpcm(data+(offset*num_channels),
   333                chars,num_samples*num_channels);
   334     n = fwrite(chars,1,num_channels * num_samples,fp);
   336     if ( n != (num_channels * num_samples))
   344         short *xdata = 
walloc(
short,num_channels*num_samples);
   345         memmove(xdata,data+(offset*num_channels),
   346             num_channels*num_samples*
sizeof(
short));
   348         n = fwrite(xdata, 
sizeof(
short),num_channels * num_samples, fp);
   352         n = fwrite(&data[offset], 
sizeof(
short), 
   353                num_channels * num_samples, fp);
   354     if (n != (num_channels * num_samples))
   359     fprintf(stderr,
"save data file: unsupported sample type\n");
   373     word_size = 2;  
break;
   376     word_size = 1;  
break;
   379     word_size = 1;  
break;
   382     word_size = 1;  
break;
   385     word_size = 2;  
break;
   388     word_size = 4;  
break;
   390     word_size = 4;  
break;
   392     word_size = 8;  
break;
   394     fprintf(stderr,
"Unknown encoding format error\n");
   404     if (
streq(type,
"short"))
   406     if (
streq(type,
"shorten"))
   408     else if ((
streq(type,
"ulaw")) || (
streq(type,
"mulaw")))
   410     else if ((
streq(type,
"char")) || (
streq(type,
"byte")) || 
   411          (
streq(type,
"8bit")))
   413     else if ((
streq(type,
"unsignedchar")) || (
streq(type,
"unsignedbyte")) || 
   414          (
streq(type,
"unsigned8bit")))
   416     else if (
streq(type,
"int"))
   419     else if (
streq(type,
"adpcm"))
   422     else if ((
streq(type,
"real")) || (
streq(type,
"float")) || 
   423          (
streq(type,
"real4")))
   425     else if ((
streq(type,
"real8")) || (
streq(type,
"double")))
   427     else if (
streq(type,
"alaw"))
   429     else if (
streq(type,
"ascii"))
   433     fprintf(stderr,
"Unknown sample type: \"%s\"\n",type);
   447       case st_uchar:    
return "unsignedchar";
   448       case st_int:     
return "int";
   457     fprintf(stderr,
"Unknown sample_type %d\n",type);
   458     return "very_unknown";
   487 static unsigned char st_short_to_ulaw(
short sample)
   489     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
   490                    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
   491                    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
   492                    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
   493                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
   494                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
   495                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
   496                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
   497                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   498                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   499                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   500                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   501                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   502                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   503                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
   504                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
   505     int sign, exponent, mantissa;
   506     unsigned char ulawbyte;
   509     sign = (sample >> 8) & 0x80; 
   510     if ( sign != 0 ) sample = -sample; 
   514     sample = sample + 
BIAS;
   515     exponent = exp_lut[( sample >> 7 ) & 0xFF];
   516     mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
   517     ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
   519     if ( ulawbyte == 0 ) ulawbyte = 0x02; 
   541 static short st_ulaw_to_short( 
unsigned char ulawbyte )
   543     static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
   544     int sign, exponent, mantissa;
   547     ulawbyte = ~ ulawbyte;
   548     sign = ( ulawbyte & 0x80 );
   549     exponent = ( ulawbyte >> 4 ) & 0x07;
   550     mantissa = ulawbyte & 0x0F;
   551     sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
   552     if ( sign != 0 ) sample = -sample;
   566 static unsigned char _u2a[128] = {  
   567     1,  1,  2,  2,  3,  3,  4,  4,
   568     5,  5,  6,  6,  7,  7,  8,  8,
   569     9,  10, 11, 12, 13, 14, 15, 16,
   570     17, 18, 19, 20, 21, 22, 23, 24,
   571     25, 27, 29, 31, 33, 34, 35, 36,
   572     37, 38, 39, 40, 41, 42, 43, 44,
   573     46, 48, 49, 50, 51, 52, 53, 54,
   574     55, 56, 57, 58, 59, 60, 61, 62,
   575     64, 65, 66, 67, 68, 69, 70, 71,
   576     72, 73, 74, 75, 76, 77, 78, 79,
   577     80, 82, 83, 84, 85, 86, 87, 88,
   578     89, 90, 91, 92, 93, 94, 95, 96,
   579     97, 98, 99, 100,    101,    102,    103,    104,
   580     105,    106,    107,    108,    109,    110,    111,    112,
   581     113,    114,    115,    116,    117,    118,    119,    120,
   582     121,    122,    123,    124,    125,    126,    127,    128};
   584 static unsigned char _a2u[128] = {      
   585     1,  3,  5,  7,  9,  11, 13, 15,
   586     16, 17, 18, 19, 20, 21, 22, 23,
   587     24, 25, 26, 27, 28, 29, 30, 31,
   588     32, 32, 33, 33, 34, 34, 35, 35,
   589     36, 37, 38, 39, 40, 41, 42, 43,
   590     44, 45, 46, 47, 48, 48, 49, 49,
   591     50, 51, 52, 53, 54, 55, 56, 57,
   592     58, 59, 60, 61, 62, 63, 64, 64,
   593     65, 66, 67, 68, 69, 70, 71, 72,
   594     73, 74, 75, 76, 77, 78, 79, 80,
   595     80, 81, 82, 83, 84, 85, 86, 87,
   596     88, 89, 90, 91, 92, 93, 94, 95,
   597     96, 97, 98, 99, 100,    101,    102,    103,
   598     104,    105,    106,    107,    108,    109,    110,    111,
   599     112,    113,    114,    115,    116,    117,    118,    119,
   600     120,    121,    122,    123,    124,    125,    126,    127};
   603 static inline unsigned char st_alaw2ulaw(
   607     return (
unsigned char) ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) :
   608         (0x7F ^ _a2u[aval ^ 0x55]));
   612 static inline unsigned char st_ulaw2alaw(
   616     return (
unsigned char) ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) :
   617         (
unsigned char) (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
   622 static unsigned char st_short_to_alaw(
short sample)
   624     return st_ulaw2alaw(st_short_to_ulaw(sample));
   664 # define HUGE_VAL HUGE   667 # define FloatToUnsigned(f)      ((unsigned long)(((long)(f - 2147483648.0)) + 2147483647L) + 1)   673     double fMant, fsMant;
   674     unsigned long hiMant, loMant;
   684         expon = 0; hiMant = 0; loMant = 0;
   687         fMant = frexp(num, &expon);
   688         if ((expon > 16384) || !(fMant < 1)) {    
   689             expon = sign|0x7FFF; hiMant = 0; loMant = 0; 
   694                 fMant = ldexp(fMant, expon);
   698             fMant = ldexp(fMant, 32);          
   699             fsMant = floor(fMant); 
   701             fMant = ldexp(fMant - fsMant, 32); 
   702             fsMant = floor(fMant); 
   707     bytes[0] = expon >> 8;
   709     bytes[2] = hiMant >> 24;
   710     bytes[3] = hiMant >> 16;
   711     bytes[4] = hiMant >> 8;
   713     bytes[6] = loMant >> 24;
   714     bytes[7] = loMant >> 16;
   715     bytes[8] = loMant >> 8;
   757 # define HUGE_VAL HUGE   760 # define UnsignedToFloat(u)         (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)   770     unsigned long hiMant, loMant;
   772     expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
   773     hiMant    =    ((
unsigned long)(bytes[2] & 0xFF) << 24)
   774             |    ((
unsigned long)(bytes[3] & 0xFF) << 16)
   775             |    ((
unsigned long)(bytes[4] & 0xFF) << 8)
   776             |    ((
unsigned long)(bytes[5] & 0xFF));
   777     loMant    =    ((
unsigned long)(bytes[6] & 0xFF) << 24)
   778             |    ((
unsigned long)(bytes[7] & 0xFF) << 16)
   779             |    ((
unsigned long)(bytes[8] & 0xFF) << 8)
   780             |    ((
unsigned long)(bytes[9] & 0xFF));
   782     if (expon == 0 && hiMant == 0 && loMant == 0) {
   786         if (expon == 0x7FFF) {    
   791             f  = ldexp((
double)(hiMant), expon-=31);
   792             f += ldexp((
double)(loMant), expon-=32);
 enum EST_sample_type_t str_to_sample_type(const char *type)
#define walloc(TYPE, SIZE)
void alaw_to_short(const unsigned char *alaw, short *data, int length)
enum EST_write_status save_raw_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, enum EST_sample_type_t sample_type, int bo)
void ulaw_to_short(const unsigned char *ulaw, short *data, int length)
#define st_alaw_to_short(a)
EST_TNamedEnum< EST_sample_type_t > EST_sample_type_map
void uchar_to_short(const unsigned char *chars, short *data, int length)
const char * sample_type_to_str(enum EST_sample_type_t type)
short * convert_raw_data(unsigned char *file_data, int data_length, enum EST_sample_type_t sample_type, int bo)
void schar_to_short(const unsigned char *chars, short *data, int length)
void short_to_alaw(const short *data, unsigned char *alaw, int length)
void short_to_ulaw(const short *data, unsigned char *ulaw, int length)
double ConvertFromIeeeExtended(unsigned char *bytes)
The file was written successfully. 
int get_word_size(enum EST_sample_type_t sample_type)
void swap_bytes_short(short *data, int length)
void short_to_schar(const short *data, unsigned char *chars, int length)
void short_to_uchar(const short *data, unsigned char *chars, int length)
void ConvertToIeeeExtended(double num, unsigned char *bytes)
void shorten_to_short(unsigned char *ulaw, short *data, int length)
#define FloatToUnsigned(f)