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 short st_alaw_to_short(
unsigned char alawbyte);
57 static unsigned char st_short_to_ulaw(
short sample);
58 static unsigned char st_short_to_alaw(
short sample);
67 static unsigned short a2s[] = {
68 5120,60160, 320,65200,20480,44032, 1280,64192,
69 2560,62848, 64,65456,10240,54784, 640,64864,
70 7168,58112, 448,65072,28672,35840, 1792,63680,
71 3584,61824, 192,65328,14336,50688, 896,64608,
72 4096,61184, 256,65264,16384,48128, 1024,64448,
73 2048,63360, 0,65520, 8192,56832, 512,64992,
74 6144,59136, 384,65136,24576,39936, 1536,63936,
75 3072,62336, 128,65392,12288,52736, 768,64736,
76 5632,59648, 352,65168,22528,41984, 1408,64064,
77 2816,62592, 96,65424,11264,53760, 704,64800,
78 7680,57600, 480,65040,30720,33792, 1920,63552,
79 3840,61568, 224,65296,15360,49664, 960,64544,
80 4608,60672, 288,65232,18432,46080, 1152,64320,
81 2304,63104, 32,65488, 9216,55808, 576,64928,
82 6656,58624, 416,65104,26624,37888, 1664,63808,
83 3328,62080, 160,65360,13312,51712, 832,64672,
84 5376,59904, 336,65184,21504,43008, 1344,64128,
85 2688,62720, 80,65440,10752,54272, 672,64832,
86 7424,57856, 464,65056,29696,34816, 1856,63616,
87 3712,61696, 208,65312,14848,50176, 928,64576,
88 4352,60928, 272,65248,17408,47104, 1088,64384,
89 2176,63232, 16,65504, 8704,56320, 544,64960,
90 6400,58880, 400,65120,25600,38912, 1600,63872,
91 3200,62208, 144,65376,12800,52224, 800,64704,
92 5888,59392, 368,65152,23552,40960, 1472,64000,
93 2944,62464, 112,65408,11776,53248, 736,64768,
94 7936,57344, 496,65024,31744,32768, 1984,63488,
95 3968,61440, 240,65280,15872,49152, 992,64512,
96 4864,60416, 304,65216,19456,45056, 1216,64256,
97 2432,62976, 48,65472, 9728,55296, 608,64896,
98 6912,58368, 432,65088,27648,36864, 1728,63744,
99 3456,61952, 176,65344,13824,51200, 864,64640
102 #define st_alaw_to_short(a) (a2s[(unsigned char)a])
104 void ulaw_to_short(
const unsigned char *ulaw,
short *data,
int length)
109 for (i=0; i<length; i++)
110 data[i] = st_ulaw_to_short(ulaw[i]);
114 void alaw_to_short(
const unsigned char *alaw,
short *data,
int length)
119 for (i=0; i<length; i++)
121 data[i] = st_alaw_to_short(alaw[i])-32768;
125 void shorten_to_short(
unsigned char *ulaw,
short *data,
int length)
134 void uchar_to_short(
const unsigned char *chars,
short *data,
int length)
139 for (i=0; i<length; i++)
141 data[i] = (((int)chars[i])-128)*256;
146 void schar_to_short(
const unsigned char *chars,
short *data,
int length)
151 for (i=0; i<length; i++)
152 data[i] = (((
unsigned char)chars[i]))*256;
156 void short_to_uchar(
const short *data,
unsigned char *chars,
int length)
161 for (i=0; i<length; i++)
162 chars[i] = (data[i]/256)+128;
166 void short_to_schar(
const short *data,
unsigned char *chars,
int length)
171 for (i=0; i<length; i++)
172 chars[i] = (data[i]/256);
177 void short_to_adpcm(
short *data,
signed char *chars,
int length)
179 struct adpcm_state state;
183 adpcm_coder(data,chars,length,&state);
187 void adpcm_to_short(
signed char *chars,
short *data,
int length)
189 struct adpcm_state state;
193 adpcm_decoder(chars,data,length/2,&state);
197 void short_to_ulaw(
const short *data,
unsigned char *ulaw,
int length)
202 for (i=0; i<length; i++)
203 ulaw[i] = st_short_to_ulaw(data[i]);
207 void short_to_alaw(
const short *data,
unsigned char *alaw,
int length)
212 for (i=0; i<length; i++)
213 alaw[i] = st_short_to_alaw(data[i]);
217 short *convert_raw_data(
unsigned char *file_data,
int data_length,
218 enum EST_sample_type_t sample_type,
int bo)
224 if (sample_type == st_short)
227 if (bo != EST_NATIVE_BO)
228 swap_bytes_short((
short *)file_data,data_length);
229 return (
short *)file_data;
231 else if (sample_type == st_mulaw)
233 d = walloc(
short,data_length);
234 ulaw_to_short(file_data,d,data_length);
238 else if (sample_type == st_alaw)
240 d = walloc(
short,data_length);
241 alaw_to_short(file_data,d,data_length);
245 else if (sample_type == st_alaw)
247 d = walloc(
short,data_length);
248 alaw_to_short(file_data,d,data_length);
253 else if (sample_type == st_adpcm)
255 d = walloc(
short,data_length);
256 adpcm_to_short((
signed char *)file_data,d,data_length);
261 else if (sample_type == st_schar)
263 d = walloc(
short,data_length);
264 schar_to_short((
unsigned char *)file_data,d,data_length);
268 else if (sample_type == st_uchar)
270 d = walloc(
short,data_length);
271 uchar_to_short((
unsigned char *)file_data,d,data_length);
276 EST_error(
"Convert raw data: unsupported sample type %s(%d)",
277 EST_sample_type_map.name(sample_type), sample_type);
283 enum EST_write_status save_raw_data(FILE *fp,
const short *data,
int offset,
284 int num_samples,
int num_channels,
285 enum EST_sample_type_t sample_type,
291 if (sample_type == st_mulaw)
293 unsigned char *ulaw = walloc(
unsigned char,num_samples*num_channels);
294 short_to_ulaw(data+(offset*num_channels),
295 ulaw,num_samples*num_channels);
296 n = fwrite(ulaw,1,num_channels * num_samples,fp);
298 if (n != (num_channels * num_samples))
299 return misc_write_error;
301 else if (sample_type == st_alaw)
303 unsigned char *alaw = walloc(
unsigned char,num_samples*num_channels);
304 short_to_alaw(data+(offset*num_channels),
305 alaw,num_samples*num_channels);
306 n = fwrite(alaw,1,num_channels * num_samples,fp);
308 if (n != (num_channels * num_samples))
309 return misc_write_error;
311 else if (sample_type == st_ascii)
313 for (i=offset*num_channels; i < num_samples*num_channels; i++)
314 fprintf(fp,
"%d\n",data[i]);
316 else if (sample_type == st_schar)
318 unsigned char *chars = walloc(
unsigned char,num_samples*num_channels);
319 short_to_schar(data+(offset*num_channels),
320 chars,num_samples*num_channels);
321 n = fwrite(chars,1,num_channels * num_samples,fp);
323 if (n != (num_channels * num_samples))
324 return misc_write_error;
326 else if (sample_type == st_uchar)
328 unsigned char *chars = walloc(
unsigned char,num_samples*num_channels);
329 short_to_uchar(data+(offset*num_channels),
330 chars,num_samples*num_channels);
331 n = fwrite(chars,1,num_channels * num_samples,fp);
333 if ( n != (num_channels * num_samples))
334 return misc_write_error;
337 else if (sample_type == st_adpcm)
339 signed char *chars = walloc(
signed char,num_samples*num_channels);
340 short_to_adpcm(data+(offset*num_channels),
341 chars,num_samples*num_channels);
342 n = fwrite(chars,1,num_channels * num_samples,fp);
344 if ( n != (num_channels * num_samples))
345 return misc_write_error;
348 else if (sample_type == st_short)
350 if (bo != EST_NATIVE_BO)
352 short *xdata = walloc(
short,num_channels*num_samples);
353 memmove(xdata,data+(offset*num_channels),
354 num_channels*num_samples*
sizeof(
short));
355 swap_bytes_short(xdata,num_channels*num_samples);
356 n = fwrite(xdata,
sizeof(
short),num_channels * num_samples, fp);
360 n = fwrite(&data[offset],
sizeof(
short),
361 num_channels * num_samples, fp);
362 if (n != (num_channels * num_samples))
363 return misc_write_error;
367 fprintf(stderr,
"save data file: unsupported sample type\n");
368 return misc_write_error;
373 int get_word_size(
enum EST_sample_type_t sample_type)
381 word_size = 2;
break;
384 word_size = 1;
break;
387 word_size = 1;
break;
390 word_size = 1;
break;
393 word_size = 2;
break;
396 word_size = 4;
break;
398 word_size = 4;
break;
400 word_size = 8;
break;
402 fprintf(stderr,
"Unknown encoding format error\n");
408 enum EST_sample_type_t str_to_sample_type(
const char *type)
412 if (streq(type,
"short"))
414 if (streq(type,
"shorten"))
416 else if ((streq(type,
"ulaw")) || (streq(type,
"mulaw")))
418 else if ((streq(type,
"char")) || (streq(type,
"byte")) ||
419 (streq(type,
"8bit")))
421 else if ((streq(type,
"unsignedchar")) || (streq(type,
"unsignedbyte")) ||
422 (streq(type,
"unsigned8bit")))
424 else if (streq(type,
"int"))
427 else if (streq(type,
"adpcm"))
430 else if ((streq(type,
"real")) || (streq(type,
"float")) ||
431 (streq(type,
"real4")))
433 else if ((streq(type,
"real8")) || (streq(type,
"double")))
435 else if (streq(type,
"alaw"))
437 else if (streq(type,
"ascii"))
441 fprintf(stderr,
"Unknown sample type: \"%s\"\n",type);
446 const char *sample_type_to_str(
enum EST_sample_type_t type)
450 case st_short:
return "short";
451 case st_shorten:
return "shorten";
452 case st_mulaw:
return "ulaw";
453 case st_alaw:
return "alaw";
454 case st_schar:
return "char";
455 case st_uchar:
return "unsignedchar";
456 case st_int:
return "int";
458 case st_adpcm:
return "adpcm";
460 case st_float:
return "float";
461 case st_double:
return "double";
462 case st_ascii:
return "ascii";
463 case st_unknown:
return "unknown";
465 fprintf(stderr,
"Unknown sample_type %d\n",type);
466 return "very_unknown";
495 static unsigned char st_short_to_ulaw(
short sample)
497 static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
498 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
499 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
500 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
501 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
502 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
503 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
504 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
505 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
506 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
507 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
508 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
509 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
510 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
511 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
512 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
513 int sign, exponent, mantissa;
514 unsigned char ulawbyte;
517 sign = (sample >> 8) & 0x80;
518 if ( sign != 0 ) sample = -sample;
519 if ( sample > CLIP ) sample = CLIP;
522 sample = sample + BIAS;
523 exponent = exp_lut[( sample >> 7 ) & 0xFF];
524 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
525 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
527 if ( ulawbyte == 0 ) ulawbyte = 0x02;
549 static short st_ulaw_to_short(
unsigned char ulawbyte )
551 static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
552 int sign, exponent, mantissa;
555 ulawbyte = ~ ulawbyte;
556 sign = ( ulawbyte & 0x80 );
557 exponent = ( ulawbyte >> 4 ) & 0x07;
558 mantissa = ulawbyte & 0x0F;
559 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
560 if ( sign != 0 ) sample = -sample;
574 static unsigned char _u2a[128] = {
575 1, 1, 2, 2, 3, 3, 4, 4,
576 5, 5, 6, 6, 7, 7, 8, 8,
577 9, 10, 11, 12, 13, 14, 15, 16,
578 17, 18, 19, 20, 21, 22, 23, 24,
579 25, 27, 29, 31, 33, 34, 35, 36,
580 37, 38, 39, 40, 41, 42, 43, 44,
581 46, 48, 49, 50, 51, 52, 53, 54,
582 55, 56, 57, 58, 59, 60, 61, 62,
583 64, 65, 66, 67, 68, 69, 70, 71,
584 72, 73, 74, 75, 76, 77, 78, 79,
585 80, 82, 83, 84, 85, 86, 87, 88,
586 89, 90, 91, 92, 93, 94, 95, 96,
587 97, 98, 99, 100, 101, 102, 103, 104,
588 105, 106, 107, 108, 109, 110, 111, 112,
589 113, 114, 115, 116, 117, 118, 119, 120,
590 121, 122, 123, 124, 125, 126, 127, 128};
592 static unsigned char _a2u[128] = {
593 1, 3, 5, 7, 9, 11, 13, 15,
594 16, 17, 18, 19, 20, 21, 22, 23,
595 24, 25, 26, 27, 28, 29, 30, 31,
596 32, 32, 33, 33, 34, 34, 35, 35,
597 36, 37, 38, 39, 40, 41, 42, 43,
598 44, 45, 46, 47, 48, 48, 49, 49,
599 50, 51, 52, 53, 54, 55, 56, 57,
600 58, 59, 60, 61, 62, 63, 64, 64,
601 65, 66, 67, 68, 69, 70, 71, 72,
602 73, 74, 75, 76, 77, 78, 79, 80,
603 80, 81, 82, 83, 84, 85, 86, 87,
604 88, 89, 90, 91, 92, 93, 94, 95,
605 96, 97, 98, 99, 100, 101, 102, 103,
606 104, 105, 106, 107, 108, 109, 110, 111,
607 112, 113, 114, 115, 116, 117, 118, 119,
608 120, 121, 122, 123, 124, 125, 126, 127};
611 static inline unsigned char st_alaw2ulaw(
615 return (
unsigned char) ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) :
616 (0x7F ^ _a2u[aval ^ 0x55]));
620 static inline unsigned char st_ulaw2alaw(
624 return (
unsigned char) ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) :
625 (
unsigned char) (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
630 static unsigned char st_short_to_alaw(
short sample)
632 return st_ulaw2alaw(st_short_to_ulaw(sample));
672 # define HUGE_VAL HUGE
675 # define FloatToUnsigned(f) ((unsigned long)(((long)(f - 2147483648.0)) + 2147483647L) + 1)
677 void ConvertToIeeeExtended(
double num,
unsigned char *bytes)
681 double fMant, fsMant;
682 unsigned long hiMant, loMant;
692 expon = 0; hiMant = 0; loMant = 0;
695 fMant = frexp(num, &expon);
696 if ((expon > 16384) || !(fMant < 1)) {
697 expon = sign|0x7FFF; hiMant = 0; loMant = 0;
702 fMant = ldexp(fMant, expon);
706 fMant = ldexp(fMant, 32);
707 fsMant = floor(fMant);
708 hiMant = FloatToUnsigned(fsMant);
709 fMant = ldexp(fMant - fsMant, 32);
710 fsMant = floor(fMant);
711 loMant = FloatToUnsigned(fsMant);
715 bytes[0] = expon >> 8;
717 bytes[2] = hiMant >> 24;
718 bytes[3] = hiMant >> 16;
719 bytes[4] = hiMant >> 8;
721 bytes[6] = loMant >> 24;
722 bytes[7] = loMant >> 16;
723 bytes[8] = loMant >> 8;
765 # define HUGE_VAL HUGE
768 # define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
774 double ConvertFromIeeeExtended(
unsigned char *bytes)
778 unsigned long hiMant, loMant;
780 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
781 hiMant = ((
unsigned long)(bytes[2] & 0xFF) << 24)
782 | ((
unsigned long)(bytes[3] & 0xFF) << 16)
783 | ((
unsigned long)(bytes[4] & 0xFF) << 8)
784 | ((
unsigned long)(bytes[5] & 0xFF));
785 loMant = ((
unsigned long)(bytes[6] & 0xFF) << 24)
786 | ((
unsigned long)(bytes[7] & 0xFF) << 16)
787 | ((
unsigned long)(bytes[8] & 0xFF) << 8)
788 | ((
unsigned long)(bytes[9] & 0xFF));
790 if (expon == 0 && hiMant == 0 && loMant == 0) {
794 if (expon == 0x7FFF) {
799 f = ldexp((
double)(hiMant), expon-=31);
800 f += ldexp((
double)(loMant), expon-=32);