51 #include "EST_wave_utils.h"
52 #include "EST_wave_aux.h"
53 #include "EST_error.h"
55 static short st_ulaw_to_short(
unsigned char ulawbyte);
56 static unsigned char st_short_to_ulaw(
short sample);
65 static unsigned short a2s[] = {
66 5120,60160, 320,65200,20480,44032, 1280,64192,
67 2560,62848, 64,65456,10240,54784, 640,64864,
68 7168,58112, 448,65072,28672,35840, 1792,63680,
69 3584,61824, 192,65328,14336,50688, 896,64608,
70 4096,61184, 256,65264,16384,48128, 1024,64448,
71 2048,63360, 0,65520, 8192,56832, 512,64992,
72 6144,59136, 384,65136,24576,39936, 1536,63936,
73 3072,62336, 128,65392,12288,52736, 768,64736,
74 5632,59648, 352,65168,22528,41984, 1408,64064,
75 2816,62592, 96,65424,11264,53760, 704,64800,
76 7680,57600, 480,65040,30720,33792, 1920,63552,
77 3840,61568, 224,65296,15360,49664, 960,64544,
78 4608,60672, 288,65232,18432,46080, 1152,64320,
79 2304,63104, 32,65488, 9216,55808, 576,64928,
80 6656,58624, 416,65104,26624,37888, 1664,63808,
81 3328,62080, 160,65360,13312,51712, 832,64672,
82 5376,59904, 336,65184,21504,43008, 1344,64128,
83 2688,62720, 80,65440,10752,54272, 672,64832,
84 7424,57856, 464,65056,29696,34816, 1856,63616,
85 3712,61696, 208,65312,14848,50176, 928,64576,
86 4352,60928, 272,65248,17408,47104, 1088,64384,
87 2176,63232, 16,65504, 8704,56320, 544,64960,
88 6400,58880, 400,65120,25600,38912, 1600,63872,
89 3200,62208, 144,65376,12800,52224, 800,64704,
90 5888,59392, 368,65152,23552,40960, 1472,64000,
91 2944,62464, 112,65408,11776,53248, 736,64768,
92 7936,57344, 496,65024,31744,32768, 1984,63488,
93 3968,61440, 240,65280,15872,49152, 992,64512,
94 4864,60416, 304,65216,19456,45056, 1216,64256,
95 2432,62976, 48,65472, 9728,55296, 608,64896,
96 6912,58368, 432,65088,27648,36864, 1728,63744,
97 3456,61952, 176,65344,13824,51200, 864,64640
100 #define st_alaw_to_short(a) (a2s[(unsigned char)a])
102 void ulaw_to_short(
const unsigned char *ulaw,
short *data,
int length)
107 for (i=0; i<length; i++)
108 data[i] = st_ulaw_to_short(ulaw[i]);
112 void alaw_to_short(
const unsigned char *alaw,
short *data,
int length)
117 for (i=0; i<length; i++)
119 data[i] = st_alaw_to_short(alaw[i])-32768;
123 void shorten_to_short(
unsigned char *ulaw,
short *data,
int length)
132 void uchar_to_short(
const unsigned char *chars,
short *data,
int length)
137 for (i=0; i<length; i++)
139 data[i] = (((int)chars[i])-128)*256;
144 void schar_to_short(
const unsigned char *chars,
short *data,
int length)
149 for (i=0; i<length; i++)
150 data[i] = (((
unsigned char)chars[i]))*256;
154 void short_to_uchar(
const short *data,
unsigned char *chars,
int length)
159 for (i=0; i<length; i++)
160 chars[i] = (data[i]/256)+128;
164 void short_to_schar(
const short *data,
unsigned char *chars,
int length)
169 for (i=0; i<length; i++)
170 chars[i] = (data[i]/256);
175 void short_to_adpcm(
short *data,
signed char *chars,
int length)
177 struct adpcm_state state;
181 adpcm_coder(data,chars,length,&state);
185 void adpcm_to_short(
signed char *chars,
short *data,
int length)
187 struct adpcm_state state;
191 adpcm_decoder(chars,data,length/2,&state);
195 void short_to_ulaw(
const short *data,
unsigned char *ulaw,
int length)
200 for (i=0; i<length; i++)
201 ulaw[i] = st_short_to_ulaw(data[i]);
205 short *convert_raw_data(
unsigned char *file_data,
int data_length,
206 enum EST_sample_type_t sample_type,
int bo)
212 if (sample_type == st_short)
215 if (bo != EST_NATIVE_BO)
216 swap_bytes_short((
short *)file_data,data_length);
217 return (
short *)file_data;
219 else if (sample_type == st_mulaw)
221 d = walloc(
short,data_length);
222 ulaw_to_short(file_data,d,data_length);
226 else if (sample_type == st_alaw)
228 d = walloc(
short,data_length);
229 alaw_to_short(file_data,d,data_length);
234 else if (sample_type == st_adpcm)
236 d = walloc(
short,data_length);
237 adpcm_to_short((
signed char *)file_data,d,data_length);
242 else if (sample_type == st_schar)
244 d = walloc(
short,data_length);
245 schar_to_short((
unsigned char *)file_data,d,data_length);
249 else if (sample_type == st_uchar)
251 d = walloc(
short,data_length);
252 uchar_to_short((
unsigned char *)file_data,d,data_length);
257 EST_error(
"Convert raw data: unsupported sample type %s(%d)",
258 EST_sample_type_map.name(sample_type), sample_type);
264 enum EST_write_status save_raw_data(FILE *fp,
const short *data,
int offset,
265 int num_samples,
int num_channels,
266 enum EST_sample_type_t sample_type,
272 if (sample_type == st_mulaw)
274 unsigned char *ulaw = walloc(
unsigned char,num_samples*num_channels);
275 short_to_ulaw(data+(offset*num_channels),
276 ulaw,num_samples*num_channels);
277 n = fwrite(ulaw,1,num_channels * num_samples,fp);
279 if (n != (num_channels * num_samples))
280 return misc_write_error;
282 else if (sample_type == st_ascii)
284 for (i=offset*num_channels; i < num_samples*num_channels; i++)
285 fprintf(fp,
"%d\n",data[i]);
287 else if (sample_type == st_schar)
289 unsigned char *chars = walloc(
unsigned char,num_samples*num_channels);
290 short_to_schar(data+(offset*num_channels),
291 chars,num_samples*num_channels);
292 n = fwrite(chars,1,num_channels * num_samples,fp);
294 if (n != (num_channels * num_samples))
295 return misc_write_error;
297 else if (sample_type == st_uchar)
299 unsigned char *chars = walloc(
unsigned char,num_samples*num_channels);
300 short_to_uchar(data+(offset*num_channels),
301 chars,num_samples*num_channels);
302 n = fwrite(chars,1,num_channels * num_samples,fp);
304 if ( n != (num_channels * num_samples))
305 return misc_write_error;
308 else if (sample_type == st_adpcm)
310 signed char *chars = walloc(
signed char,num_samples*num_channels);
311 short_to_adpcm(data+(offset*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))
316 return misc_write_error;
319 else if (sample_type == st_short)
321 if (bo != EST_NATIVE_BO)
323 short *xdata = walloc(
short,num_channels*num_samples);
324 memmove(xdata,data+(offset*num_channels),
325 num_channels*num_samples*
sizeof(
short));
326 swap_bytes_short(xdata,num_channels*num_samples);
327 n = fwrite(xdata,
sizeof(
short),num_channels * num_samples, fp);
331 n = fwrite(&data[offset],
sizeof(
short),
332 num_channels * num_samples, fp);
333 if (n != (num_channels * num_samples))
334 return misc_write_error;
338 fprintf(stderr,
"save data file: unsupported sample type\n");
339 return misc_write_error;
344 int get_word_size(
enum EST_sample_type_t sample_type)
352 word_size = 2;
break;
355 word_size = 1;
break;
357 word_size = 1;
break;
360 word_size = 1;
break;
363 word_size = 2;
break;
366 word_size = 4;
break;
368 word_size = 4;
break;
370 word_size = 8;
break;
372 fprintf(stderr,
"Unknown encoding format error\n");
378 enum EST_sample_type_t str_to_sample_type(
const char *type)
382 if (streq(type,
"short"))
384 if (streq(type,
"shorten"))
386 else if ((streq(type,
"ulaw")) || (streq(type,
"mulaw")))
388 else if ((streq(type,
"char")) || (streq(type,
"byte")) ||
389 (streq(type,
"8bit")))
391 else if ((streq(type,
"unsignedchar")) || (streq(type,
"unsignedbyte")) ||
392 (streq(type,
"unsigned8bit")))
394 else if (streq(type,
"int"))
397 else if (streq(type,
"adpcm"))
400 else if ((streq(type,
"real")) || (streq(type,
"float")) ||
401 (streq(type,
"real4")))
403 else if ((streq(type,
"real8")) || (streq(type,
"double")))
405 else if (streq(type,
"alaw"))
407 else if (streq(type,
"ascii"))
411 fprintf(stderr,
"Unknown sample type: \"%s\"\n",type);
416 const char *sample_type_to_str(
enum EST_sample_type_t type)
420 case st_short:
return "short";
421 case st_shorten:
return "shorten";
422 case st_mulaw:
return "ulaw";
423 case st_schar:
return "char";
424 case st_uchar:
return "unsignedchar";
425 case st_int:
return "int";
427 case st_adpcm:
return "adpcm";
429 case st_float:
return "float";
430 case st_double:
return "double";
431 case st_ascii:
return "ascii";
432 case st_unknown:
return "unknown";
434 fprintf(stderr,
"Unknown sample_type %d\n",type);
435 return "very_unknown";
464 static unsigned char st_short_to_ulaw(
short sample)
466 static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
467 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
468 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
469 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
470 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
471 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
472 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
473 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
474 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
475 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
476 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
477 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
478 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
479 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
480 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
481 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
482 int sign, exponent, mantissa;
483 unsigned char ulawbyte;
486 sign = (sample >> 8) & 0x80;
487 if ( sign != 0 ) sample = -sample;
488 if ( sample > CLIP ) sample = CLIP;
491 sample = sample + BIAS;
492 exponent = exp_lut[( sample >> 7 ) & 0xFF];
493 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
494 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
496 if ( ulawbyte == 0 ) ulawbyte = 0x02;
518 static short st_ulaw_to_short(
unsigned char ulawbyte )
520 static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
521 int sign, exponent, mantissa;
524 ulawbyte = ~ ulawbyte;
525 sign = ( ulawbyte & 0x80 );
526 exponent = ( ulawbyte >> 4 ) & 0x07;
527 mantissa = ulawbyte & 0x0F;
528 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
529 if ( sign != 0 ) sample = -sample;
570 # define HUGE_VAL HUGE
573 # define FloatToUnsigned(f) ((unsigned long)(((long)(f - 2147483648.0)) + 2147483647L) + 1)
575 void ConvertToIeeeExtended(
double num,
unsigned char *bytes)
579 double fMant, fsMant;
580 unsigned long hiMant, loMant;
590 expon = 0; hiMant = 0; loMant = 0;
593 fMant = frexp(num, &expon);
594 if ((expon > 16384) || !(fMant < 1)) {
595 expon = sign|0x7FFF; hiMant = 0; loMant = 0;
600 fMant = ldexp(fMant, expon);
604 fMant = ldexp(fMant, 32);
605 fsMant = floor(fMant);
606 hiMant = FloatToUnsigned(fsMant);
607 fMant = ldexp(fMant - fsMant, 32);
608 fsMant = floor(fMant);
609 loMant = FloatToUnsigned(fsMant);
613 bytes[0] = expon >> 8;
615 bytes[2] = hiMant >> 24;
616 bytes[3] = hiMant >> 16;
617 bytes[4] = hiMant >> 8;
619 bytes[6] = loMant >> 24;
620 bytes[7] = loMant >> 16;
621 bytes[8] = loMant >> 8;
663 # define HUGE_VAL HUGE
666 # define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
672 double ConvertFromIeeeExtended(
unsigned char *bytes)
676 unsigned long hiMant, loMant;
678 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
679 hiMant = ((
unsigned long)(bytes[2] & 0xFF) << 24)
680 | ((
unsigned long)(bytes[3] & 0xFF) << 16)
681 | ((
unsigned long)(bytes[4] & 0xFF) << 8)
682 | ((
unsigned long)(bytes[5] & 0xFF));
683 loMant = ((
unsigned long)(bytes[6] & 0xFF) << 24)
684 | ((
unsigned long)(bytes[7] & 0xFF) << 16)
685 | ((
unsigned long)(bytes[8] & 0xFF) << 8)
686 | ((
unsigned long)(bytes[9] & 0xFF));
688 if (expon == 0 && hiMant == 0 && loMant == 0) {
692 if (expon == 0x7FFF) {
697 f = ldexp((
double)(hiMant), expon-=31);
698 f += ldexp((
double)(loMant), expon-=32);