47 #include "EST_wave_aux.h" 48 #include "EST_wave_utils.h" 49 #include "EST_strcasecmp.h" 51 #include "EST_FileType.h" 53 static int def_load_sample_rate = 16000;
65 static const char *NIST_SIG =
"NIST_1A\n 1024\n";
66 static const char *NIST_END_SIG =
"end_head\n";
67 #define NIST_HDR_SIZE 1024 69 int nist_get_param_int(
const char *hdr,
const char *field,
int def_val)
74 if (((p=strstr(hdr,field)) != NULL) &&
75 (strncmp(
" -i ",p+strlen(field),4) == 0))
77 sscanf(p+strlen(field)+4,
"%d",&val);
85 char *nist_get_param_str(
const char *hdr,
const char *field,
const char *def_val)
91 if (((p=strstr(hdr,field)) != NULL) &&
92 (strncmp(
" -s",p+strlen(field),3) == 0))
94 sscanf(p+strlen(field)+3,
"%d",&size);
95 val = walloc(
char,size+1);
97 sscanf(p+strlen(field)+3,
"%d %s",&size,val);
101 return wstrdup(def_val);
106 const char *sample_type_to_nist(
enum EST_sample_type_t sample_type)
109 switch (sample_type) {
127 fprintf(stderr,
"Unknown sample type for nist");
133 enum EST_sample_type_t nist_to_sample_type(
char *type)
135 if ((streq(type,
"pcm")) ||
136 (streq(type,
"PCM")) ||
137 (streq(type,
"pcm-2")))
139 if (strcmp(type,
"pcm,embedded-shorten-v1.1") == 0)
141 else if ((EST_strcasecmp(type,
"ULAW",NULL) == 0) ||
142 (EST_strcasecmp(type,
"U-LAW",NULL) == 0) ||
143 (EST_strcasecmp(type,
"mu-law",NULL) == 0) ||
144 (EST_strcasecmp(type,
"mulaw",NULL) == 0))
146 else if ((EST_strcasecmp(type,
"ALAW",NULL) == 0) ||
147 (EST_strcasecmp(type,
"A-LAW",NULL) == 0))
149 else if (strcmp(type,
"alaw") == 0)
151 else if (strcmp(type,
"PCM-1") == 0)
153 else if (strcmp(type,
"PCM-4") == 0)
155 else if (strcmp(type,
"REAL") == 0)
160 fprintf(stderr,
"NIST: unknown sample type: %s\n",type);
165 enum EST_read_status load_wave_nist(
EST_TokenStream &ts,
short **data,
int 166 *num_samples,
int *num_channels,
int 167 *word_size,
int *sample_rate,
enum 168 EST_sample_type_t *sample_type,
int *bo ,
int 172 char header[NIST_HDR_SIZE];
173 int samps,sample_width,data_length,actual_bo;
174 unsigned char *file_data;
175 enum EST_sample_type_t actual_sample_type;
176 char *byte_order, *sample_coding;
180 current_pos = ts.
tell();
181 if (ts.
fread(header,NIST_HDR_SIZE,1) != 1)
184 if (strncmp(header,NIST_SIG,strlen(NIST_SIG)) != 0)
187 samps = nist_get_param_int(header,
"sample_count",-1);
188 *num_channels = nist_get_param_int(header,
"channel_count",1);
189 sample_width = nist_get_param_int(header,
"sample_n_bytes",2);
191 nist_get_param_int(header,
"sample_rate",def_load_sample_rate);
192 byte_order = nist_get_param_str(header,
"sample_byte_format",
193 (EST_BIG_ENDIAN ?
"10" :
"01"));
194 sample_coding = nist_get_param_str(header,
"sample_coding",
"pcm");
195 if (streq(byte_order,
"mu-law"))
197 byte_order = wstrdup((EST_BIG_ENDIAN ?
"10" :
"01"));
198 sample_coding = wstrdup(
"ULAW");
200 if (streq(byte_order,
"a-law"))
202 byte_order = wstrdup((EST_BIG_ENDIAN ?
"10" :
"01"));
203 sample_coding = wstrdup(
"ALAW");
213 if (streq(sample_coding,
"pcm,embedded-shorten-v1.1"))
215 char *tmpfile, *cmdstr;
216 enum EST_read_status rval;
218 tmpfile = cmake_tmp_filename();
219 cmdstr = walloc(
char,strlen(tmpfile)+200);
220 sprintf(cmdstr,
"cstrshorten %s %s",
221 (
const char*)ts.
filename(),tmpfile);
222 printf(
"Command: %s\n", cmdstr);
227 rval = load_wave_nist(tt, data, num_samples,
228 num_channels, word_size, sample_rate,
229 sample_type, bo, offset, length);
238 data_length = (samps - offset)*(*num_channels);
240 data_length = length*(*num_channels);
242 file_data = walloc(
unsigned char,sample_width * data_length);
244 ts.
seek(current_pos+NIST_HDR_SIZE+(sample_width*offset*(*num_channels)));
246 n = ts.
fread(file_data,sample_width,data_length);
248 if ((n < 1) && (n != data_length))
251 wfree(sample_coding);
253 return misc_read_error;
255 else if ((n < data_length) && (data_length/(*num_channels) == n))
257 fprintf(stderr,
"WAVE read: nist header is (probably) non-standard\n");
258 fprintf(stderr,
"WAVE read: assuming different num_channel interpretation\n");
261 else if (n < data_length)
263 fprintf(stderr,
"WAVE read: short file %s\n",
265 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
266 offset,n,data_length);
270 actual_sample_type = nist_to_sample_type(sample_coding);
271 actual_bo = ((strcmp(byte_order,
"10") == 0) ? bo_big : bo_little);
273 *data = convert_raw_data(file_data,data_length,
274 actual_sample_type,actual_bo);
276 *num_samples = data_length/ (*num_channels);
277 *sample_type = st_short;
280 wfree(sample_coding);
286 enum EST_write_status save_wave_nist_header(FILE *fp,
287 int num_samples,
int num_channels,
289 enum EST_sample_type_t sample_type,
int bo)
291 char h[1024], p[1024];
297 sprintf(p,
"channel_count -i %d\n", num_channels);
299 sprintf(p,
"sample_count -i %d\n", num_samples);
301 sprintf(p,
"sample_rate -i %d\n", sample_rate);
304 t = sample_type_to_nist(sample_type);
307 sprintf(p,
"sample_coding -s%d %s\n", (
signed)strlen(t), t);
309 sprintf(p,
"sample_n_bytes -i %d\n", get_word_size(sample_type));
313 if (get_word_size(sample_type) > 1)
315 sprintf(p,
"sample_byte_format -s%d %s\n", 2,
316 ((bo == bo_big) ?
"10" :
"01"));
320 strcat(h, NIST_END_SIG);
322 strcat(h,
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
324 if (fwrite(&h, 1024, 1, fp) != 1)
325 return misc_write_error;
331 enum EST_write_status save_wave_nist_data(FILE *fp,
const short *data,
int offset,
332 int num_samples,
int num_channels,
334 enum EST_sample_type_t sample_type,
int bo)
339 return save_raw_data(fp,data,offset,num_samples,num_channels,
344 enum EST_write_status save_wave_nist(FILE *fp,
const short *data,
int offset,
345 int num_samples,
int num_channels,
347 enum EST_sample_type_t sample_type,
int bo)
349 save_wave_nist_header(fp, num_samples, num_channels,
350 sample_rate, sample_type, bo);
351 return save_wave_nist_data(fp, data, offset,
352 num_samples, num_channels,
353 sample_rate, sample_type, bo);
360 enum EST_read_status load_wave_est(
EST_TokenStream &ts,
short **data,
int 361 *num_samples,
int *num_channels,
int 362 *word_size,
int *sample_rate,
enum 363 EST_sample_type_t *sample_type,
int *bo,
364 int offset,
int length)
366 int data_length, actual_bo;
374 EST_sample_type_t actual_sample_type;
378 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
380 if (t != est_file_wave)
381 return misc_read_error;
383 *num_samples = hinfo.
ival(
"NumSamples");
384 *num_channels = hinfo.
ival(
"NumChannels");
385 *sample_rate = hinfo.
ival(
"SampleRate");
387 byte_order = hinfo.
val(
"ByteOrder");
390 data_length = (*num_samples)*(*num_channels);
392 data_length = length*(*num_channels);
394 file_data = walloc(
short, data_length);
396 n = ts.
fread(file_data,
sizeof(
short), data_length);
397 if ((n != data_length) && (n < 1))
400 cerr <<
"failed to read file\n";
402 return misc_read_error;
404 else if (n != data_length)
406 cerr <<
"Wrong number of samples/channels in EST wave file\n";
408 cerr <<
"expected " << data_length <<
" got " << n << endl;
412 actual_bo = (byte_order ==
"10") ? bo_big : bo_little;
413 if (hinfo.
present(
"SampleType"))
414 actual_sample_type = str_to_sample_type(hinfo.
val(
"SampleType"));
416 actual_sample_type = st_short;
418 *data = convert_raw_data((
unsigned char *)file_data,
419 data_length, actual_sample_type, actual_bo);
421 *sample_type = st_short;
428 enum EST_write_status save_wave_est_header(FILE *fp,
429 int num_samples,
int num_channels,
431 enum EST_sample_type_t sample_type,
int bo)
433 fprintf(fp,
"EST_File wave\n");
434 fprintf(fp,
"DataType binary\n");
435 fprintf(fp,
"SampleRate %d\n", sample_rate);
436 fprintf(fp,
"NumSamples %d\n", num_samples);
437 fprintf(fp,
"NumChannels %d\n", num_channels);
438 fprintf(fp,
"SampleType %s\n", sample_type_to_str(sample_type));
439 if (get_word_size(sample_type) > 1)
440 fprintf(fp,
"ByteOrder %s\n", ((bo == bo_big) ?
"10" :
"01"));
442 fprintf(fp,
"EST_Header_End\n");
446 enum EST_write_status save_wave_est_data(FILE *fp,
const short *data,
int offset,
447 int num_samples,
int num_channels,
449 enum EST_sample_type_t sample_type,
int bo)
454 return save_raw_data(fp, data, offset, num_samples, num_channels,
458 enum EST_write_status save_wave_est(FILE *fp,
const short *data,
int offset,
459 int num_samples,
int num_channels,
461 enum EST_sample_type_t sample_type,
int bo)
463 save_wave_est_header(fp, num_samples, num_channels,
464 sample_rate, sample_type, bo);
466 return save_wave_est_data(fp, data, offset,
467 num_samples, num_channels,
468 sample_rate, sample_type, bo);
482 #define WAVE_FORMAT_PCM 0x0001 483 #define WAVE_FORMAT_ADPCM 0x0002 484 #define WAVE_FORMAT_ALAW 0x0006 485 #define WAVE_FORMAT_MULAW 0x0007 487 enum EST_read_status load_wave_riff(
EST_TokenStream &ts,
short **data,
int 488 *num_samples,
int *num_channels,
int 489 *word_size,
int *sample_rate,
enum 490 EST_sample_type_t *sample_type,
int *bo ,
int 494 int samps,sample_width,data_length;
497 unsigned char *file_data;
498 enum EST_sample_type_t actual_sample_type;
500 if (ts.
fread(info,
sizeof(
char),4) != 4)
502 if (strncmp(info,
"RIFF",4) != 0)
507 if(ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
509 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
510 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
511 (strncmp(info,
"WAVE",4) != 0))
513 fprintf(stderr,
"RIFF file is not of type WAVE\n");
514 return misc_read_error;
516 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
517 (strncmp(info,
"fmt ",4) != 0))
518 return misc_read_error;
520 if (ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
521 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
522 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
523 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
528 case WAVE_FORMAT_PCM:
529 actual_sample_type = st_short;
break;
531 case WAVE_FORMAT_MULAW:
532 actual_sample_type = st_mulaw;
break;
533 case WAVE_FORMAT_ALAW:
534 actual_sample_type = st_alaw;
break;
535 case WAVE_FORMAT_ADPCM:
536 fprintf(stderr,
"RIFF file: unsupported proprietary sample format ADPCM\n");
537 actual_sample_type = st_short;
541 fprintf(stderr,
"RIFF file: unknown sample format\n");
542 actual_sample_type = st_short;
545 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
546 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
547 *num_channels = shortdata;
548 if (ts.
fread(sample_rate,4,1) != 1)
return misc_read_error;
549 if (EST_BIG_ENDIAN) *sample_rate = SWAPINT(*sample_rate);
550 if (ts.
fread(&intdata,4,1) != 1)
return misc_read_error;
551 if (EST_BIG_ENDIAN) intdata = SWAPINT(intdata);
552 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
553 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
554 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
555 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
557 sample_width = (shortdata+7)/8;
558 if ((sample_width == 1) && (actual_sample_type == st_short))
559 actual_sample_type = st_uchar;
564 if (ts.
fread(info,
sizeof(
char),4) != 4)
566 fprintf(stderr,
"RIFF file truncated\n");
567 return misc_read_error;
569 if (strncmp(info,
"data",4) == 0)
571 if (ts.
fread(&samps,4,1) != 1)
return misc_read_error;
572 if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
573 samps /= (sample_width*(*num_channels));
576 else if (strncmp(info,
"fact",4) == 0)
578 if (ts.
fread(&samps,4,1) != 1)
return misc_read_error;
579 if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
588 if(ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
589 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
594 data_length = (samps - offset)*(*num_channels);
596 data_length = length*(*num_channels);
598 file_data = walloc(
unsigned char,sample_width * data_length);
600 ts.
seek((sample_width*offset*(*num_channels))+ts.
tell());
601 if ((dsize=ts.
fread(file_data,sample_width,data_length)) != data_length)
606 fprintf(stderr,
"Unexpected end of file but continuing (apparently missing %d samples)\n",data_length-dsize);
609 fprintf(stderr,
"Unexpected end of file: (missing %d samples)\n",data_length-dsize);
611 return misc_read_error;
615 *data = convert_raw_data(file_data,dsize,
616 actual_sample_type, bo_little);
618 *num_samples = dsize / (*num_channels);
619 *sample_type = st_short;
626 enum EST_write_status save_wave_riff_header(FILE *fp,
int num_samples,
627 int num_channels,
int sample_rate,
628 enum EST_sample_type_t sample_type,
int bo)
632 int data_size, data_int;
635 if (sample_type == st_schar)
637 EST_warning(
"RIFF format: Signed 8-bit not allowed by this file format");
638 sample_type=st_uchar;
641 info =
"RIFF"; fwrite(info,4,1,fp);
642 data_size = num_channels*num_samples*get_word_size(sample_type)+ 8+16+12;
644 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
645 fwrite(&data_size,1,4,fp);
646 info =
"WAVE"; fwrite(info,4,1,fp);
647 info =
"fmt "; fwrite(info,4,1,fp);
649 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
650 fwrite(&data_size,1,4,fp);
653 case st_short: data_short = WAVE_FORMAT_PCM;
break;
654 case st_uchar: data_short = WAVE_FORMAT_PCM;
break;
655 case st_mulaw: data_short = WAVE_FORMAT_MULAW;
break;
656 case st_alaw: data_short = WAVE_FORMAT_ALAW;
break;
657 case st_adpcm: data_short = WAVE_FORMAT_ADPCM;
break;
659 fprintf(stderr,
"RIFF format: unsupported data format %d\n",
661 return misc_write_error;
663 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
664 fwrite(&data_short,1,2,fp);
665 data_short = num_channels;
666 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
667 fwrite(&data_short,1,2,fp);
668 data_int = sample_rate;
669 if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
670 fwrite(&data_int,1,4,fp);
671 data_int = sample_rate * num_channels * get_word_size(sample_type);
672 if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
673 fwrite(&data_int,1,4,fp);
674 data_short = num_channels * get_word_size(sample_type);
675 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
676 fwrite(&data_short,1,2,fp);
677 data_short = get_word_size(sample_type) * 8;
678 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
679 fwrite(&data_short,1,2,fp);
680 info =
"data"; fwrite(info,4,1,fp);
681 data_size = num_channels*num_samples*get_word_size(sample_type);
682 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
683 fwrite(&data_size,1,4,fp);
688 enum EST_write_status save_wave_riff_data(FILE *fp,
const short *data,
689 int offset,
int num_samples,
int num_channels,
691 enum EST_sample_type_t sample_type,
int bo)
696 return save_raw_data(fp,data,offset,num_samples,num_channels,
697 sample_type,bo_little);
701 enum EST_write_status save_wave_riff(FILE *fp,
const short *data,
int offset,
702 int num_samples,
int num_channels,
704 enum EST_sample_type_t sample_type,
int bo)
706 save_wave_riff_header(fp, num_samples, num_channels, sample_rate,
709 return save_wave_riff_data(fp, data, offset, num_samples,
710 num_channels, sample_rate, sample_type, bo);
731 enum EST_read_status load_wave_aiff(
EST_TokenStream &ts,
short **data,
int 732 *num_samples,
int *num_channels,
int 733 *word_size,
int *sample_rate,
enum 734 EST_sample_type_t *sample_type,
int *bo ,
int 738 struct AIFFchunk chunk;
742 unsigned char ieee_ext_sample_rate[10];
743 struct AIFFssnd ssndchunk;
744 enum EST_sample_type_t actual_sample_type;
745 int dsize,data_length,n;
746 unsigned char *file_data;
748 if (ts.
fread(info,
sizeof(
char),4) != 4)
750 if (strncmp(info,
"FORM",4) != 0)
754 if (ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
755 if (EST_LITTLE_ENDIAN)
756 dsize = SWAPINT(dsize);
757 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
758 (strncmp(info,
"AIFF",4) != 0))
760 fprintf(stderr,
"AIFF file does not have AIFF chunk\n");
761 return misc_read_error;
764 for ( ; ts.
fread(&chunk,
sizeof(chunk), 1) == 1 ; )
766 if (EST_LITTLE_ENDIAN)
767 chunk.size = SWAPINT(chunk.size);
768 if (strncmp(chunk.id,
"COMM",4) == 0)
770 if (chunk.size != 18)
772 fprintf(stderr,
"AIFF chunk: bad size\n");
773 return misc_read_error;
775 if (ts.
fread(&comm_channels,
sizeof(
short), 1) != 1)
776 return misc_read_error;
777 if (ts.
fread(&comm_samples,
sizeof(
int), 1) != 1)
778 return misc_read_error;
779 if (ts.
fread(&comm_bits,
sizeof(
short), 1) != 1)
780 return misc_read_error;
781 if (ts.
fread(ieee_ext_sample_rate, 10, 1) != 1)
783 fprintf(stderr,
"AIFF chunk: eof within COMM chunk\n");
784 return misc_read_error;
786 if (EST_LITTLE_ENDIAN)
788 comm_channels = SWAPSHORT(comm_channels);
789 comm_samples = SWAPINT(comm_samples);
790 comm_bits = SWAPSHORT(comm_bits);
792 *sample_rate = (int)ConvertFromIeeeExtended(ieee_ext_sample_rate);
794 else if (strncmp(chunk.id,
"SSND",4) == 0)
796 if (ts.
fread(&ssndchunk,
sizeof(ssndchunk), 1) != 1)
798 fprintf(stderr,
"AIFF chunk: eof within SSND chunk\n");
799 return misc_read_error;
801 if (EST_LITTLE_ENDIAN)
803 ssndchunk.offset = SWAPINT(ssndchunk.offset);
804 ssndchunk.blocksize = SWAPINT(ssndchunk.blocksize);
807 *num_channels = comm_channels;
810 case 8: actual_sample_type = st_uchar;
break;
811 case 16: actual_sample_type = st_short;
break;
813 fprintf(stderr,
"AIFF: unsupported sample width %d bits\n",
815 return misc_read_error;
818 ts.
seek(ssndchunk.offset+(comm_channels*offset)+ts.
tell());
820 data_length = (comm_samples-offset)*comm_channels;
822 data_length = length*comm_channels;
823 file_data = walloc(
unsigned char,
824 data_length*comm_channels*
825 get_word_size(actual_sample_type));
826 if ((n=ts.
fread(file_data,get_word_size(actual_sample_type),
827 data_length)) != data_length)
829 fprintf(stderr,
"AIFF read: short file %s\n",
831 fprintf(stderr,
"AIFF read: at %d got %d instead of %d samples\n",
832 offset,n,data_length);
836 *data = convert_raw_data(file_data,data_length,
837 actual_sample_type,bo_big);
838 *num_samples = data_length/comm_channels;
839 *sample_type = st_short;
854 enum EST_write_status save_wave_aiff_header(FILE *fp,
855 int num_samples,
int num_channels,
857 enum EST_sample_type_t sample_type,
int bo)
861 int data_size, data_int;
862 unsigned char ieee_ext_buf[10];
868 data_size = 54+(num_samples*num_channels*get_word_size(sample_type));
869 if (EST_LITTLE_ENDIAN)
870 data_size = SWAPINT(data_size);
871 fwrite(&data_size,1,4,fp);
877 if (EST_LITTLE_ENDIAN)
878 data_int = SWAPINT(data_int);
879 fwrite(&data_int,1,4,fp);
880 data_short = num_channels;
881 if (EST_LITTLE_ENDIAN)
882 data_short = SWAPSHORT(data_short);
883 fwrite(&data_short,1,2,fp);
884 data_int = num_samples;
885 if (EST_LITTLE_ENDIAN)
886 data_int = SWAPINT(data_int);
887 fwrite(&data_int,1,4,fp);
888 data_short = 8*get_word_size(sample_type);
889 if (EST_LITTLE_ENDIAN)
890 data_short = SWAPSHORT(data_short);
891 fwrite(&data_short,1,2,fp);
892 ConvertToIeeeExtended((
double)sample_rate,ieee_ext_buf);
893 fwrite(ieee_ext_buf,1,10,fp);
896 data_int = 8 + (num_samples*num_channels*get_word_size(sample_type));
897 if (EST_LITTLE_ENDIAN)
898 data_int = SWAPINT(data_int);
899 fwrite(&data_int,1,4,fp);
901 if (EST_LITTLE_ENDIAN)
902 data_int = SWAPINT(data_int);
903 fwrite(&data_int,1,4,fp);
904 if (EST_LITTLE_ENDIAN)
905 data_int = SWAPINT(data_int);
906 fwrite(&data_int,1,4,fp);
912 enum EST_write_status save_wave_aiff_data(FILE *fp,
const short *data,
int offset,
913 int num_samples,
int num_channels,
915 enum EST_sample_type_t sample_type,
int bo)
920 if ((sample_type == st_short) || (sample_type == st_uchar))
921 return save_raw_data(fp,data, offset, num_samples, num_channels,
922 sample_type, bo_big);
925 fprintf(stderr,
"AIFF: requested data type not uchar or short\n");
926 return misc_write_error;
931 enum EST_write_status save_wave_aiff(FILE *fp,
const short *data,
int offset,
932 int num_samples,
int num_channels,
934 enum EST_sample_type_t sample_type,
int bo)
936 save_wave_aiff_header(fp, num_samples, num_channels,
937 sample_rate, sample_type, bo);
939 return save_wave_aiff_data(fp, data, offset,
940 num_samples, num_channels,
941 sample_rate, sample_type, bo);
948 enum EST_read_status load_wave_ulaw(
EST_TokenStream &ts,
short **data,
int 949 *num_samples,
int *num_channels,
int *word_size,
int 950 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
951 int offset,
int length)
955 int data_length,samps;
961 data_length = samps - offset;
963 data_length = length;
965 ulaw = walloc(
unsigned char, data_length);
967 if (ts.
fread(ulaw,1,data_length) != data_length)
970 return misc_read_error;
973 *data = walloc(
short,data_length);
974 ulaw_to_short(ulaw,*data,data_length);
977 *num_samples = data_length;
980 *sample_type = st_short;
987 enum EST_write_status save_wave_ulaw_header(FILE *fp,
988 int num_samples,
int num_channels,
990 enum EST_sample_type_t sample_type,
int bo)
1001 enum EST_write_status save_wave_ulaw_data(FILE *fp,
const short *data,
int offset,
1002 int num_samples,
int num_channels,
1004 enum EST_sample_type_t sample_type,
int bo)
1009 return save_wave_raw(fp,data,offset,num_samples,num_channels,
1013 enum EST_write_status save_wave_ulaw(FILE *fp,
const short *data,
int offset,
1014 int num_samples,
int num_channels,
1016 enum EST_sample_type_t sample_type,
int bo)
1018 save_wave_ulaw_header(fp, num_samples, num_channels,
1019 sample_rate, sample_type, bo);
1021 return save_wave_ulaw_data(fp, data, offset,
1022 num_samples, num_channels,
1023 sample_rate, sample_type, bo);
1028 enum EST_read_status load_wave_alaw(
EST_TokenStream &ts,
short **data,
int 1029 *num_samples,
int *num_channels,
int *word_size,
int 1030 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
1031 int offset,
int length)
1034 unsigned char *alaw;
1035 int data_length,samps;
1041 data_length = samps - offset;
1043 data_length = length;
1045 alaw = walloc(
unsigned char, data_length);
1047 if (ts.
fread(alaw,1,data_length) != data_length)
1050 return misc_read_error;
1053 *data = walloc(
short,data_length);
1054 alaw_to_short(alaw,*data,data_length);
1057 *num_samples = data_length;
1058 *sample_rate = 8000;
1060 *sample_type = st_short;
1062 *bo = EST_NATIVE_BO;
1067 enum EST_write_status save_wave_alaw_header(FILE *fp,
1068 int num_samples,
int num_channels,
1070 enum EST_sample_type_t sample_type,
int bo)
1076 (void) num_channels;
1081 enum EST_write_status save_wave_alaw_data(FILE *fp,
const short *data,
int offset,
1082 int num_samples,
int num_channels,
1084 enum EST_sample_type_t sample_type,
int bo)
1090 return save_wave_raw(fp,data,offset,num_samples,num_channels,
1094 enum EST_write_status save_wave_alaw(FILE *fp,
const short *data,
int offset,
1095 int num_samples,
int num_channels,
1097 enum EST_sample_type_t sample_type,
int bo)
1099 save_wave_alaw_header(fp, num_samples, num_channels,
1100 sample_rate, sample_type, bo);
1102 return save_wave_alaw_data(fp, data, offset,
1103 num_samples, num_channels,
1104 sample_rate, sample_type, bo);
1114 unsigned int hdr_size;
1116 unsigned int encoding;
1117 unsigned int sample_rate;
1118 unsigned int channels;
1121 enum EST_read_status load_wave_snd(
EST_TokenStream &ts,
short **data,
int 1122 *num_samples,
int *num_channels,
int *word_size,
int 1123 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo ,
1124 int offset,
int length)
1127 Sun_au_header header;
1128 enum EST_sample_type_t encoding_type;
1129 int data_length, sample_width, bytes, samps, n;
1130 unsigned char *file_data;
1133 current_pos = ts.
tell();
1134 if (ts.
fread(&header,
sizeof(Sun_au_header), 1) != 1)
1135 return misc_read_error;
1138 if ((EST_LITTLE_ENDIAN) &&
1139 ((
unsigned int)0x2e736e64 == SWAPINT(header.magic)))
1141 header.hdr_size = SWAPINT(header.hdr_size);
1142 header.data_size = SWAPINT(header.data_size);
1143 header.encoding = SWAPINT(header.encoding);
1144 header.sample_rate = SWAPINT(header.sample_rate);
1145 header.channels = SWAPINT(header.channels);
1147 else if ((
unsigned int)0x2e736e64 != header.magic)
1148 return wrong_format;
1150 switch (header.encoding)
1153 encoding_type = st_mulaw;
1156 encoding_type = st_uchar;
1159 encoding_type = st_short;
1162 fprintf(stderr,
"Unsupported data type in SND header\n");
1163 return misc_read_error;
1166 *num_channels = header.channels;
1167 sample_width = get_word_size(encoding_type);
1168 *sample_rate = header.sample_rate;
1170 if ((header.data_size == 0) ||
1171 (header.data_size == -1))
1174 bytes = ts.
tell() - header.hdr_size;
1177 bytes = header.data_size;
1178 samps = bytes/sample_width;
1181 data_length = (samps - offset)*(*num_channels);
1183 data_length = length *(*num_channels);
1185 file_data = walloc(
unsigned char, sample_width * data_length);
1186 ts.
seek(current_pos+header.hdr_size+(sample_width*offset*(*num_channels)));
1187 if ((n=ts.
fread(file_data,sample_width,data_length)) != data_length)
1189 fprintf(stderr,
"WAVE read: short file %s\n",
1191 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1192 offset,n,data_length);
1196 *data = convert_raw_data(file_data,data_length,encoding_type,bo_big);
1201 *num_samples = data_length/ (*num_channels);
1202 *sample_type = st_short;
1203 *bo = EST_NATIVE_BO;
1208 enum EST_write_status save_wave_snd_header(FILE *fp,
1209 int num_samples,
int num_channels,
1211 enum EST_sample_type_t sample_type,
int bo)
1215 Sun_au_header header;
1218 header.magic = (
unsigned int)0x2e736e64;
1219 header.hdr_size =
sizeof(header);
1220 header.data_size = get_word_size(sample_type) * num_channels * num_samples;
1222 switch (sample_type) {
1224 header.encoding = 1;
1227 header.encoding = 2;
1230 header.encoding = 3;
1235 "Unsupported sample type cannot be saved in SND format\n");
1236 return misc_write_error;
1242 header.sample_rate = sample_rate;
1244 header.channels = num_channels;
1246 if (EST_LITTLE_ENDIAN)
1249 header.magic = SWAPINT(header.magic);
1250 header.hdr_size = SWAPINT(header.hdr_size);
1251 header.data_size = SWAPINT(header.data_size);
1252 header.encoding = SWAPINT(header.encoding);
1253 header.sample_rate = SWAPINT(header.sample_rate);
1254 header.channels = SWAPINT(header.channels);
1258 if (fwrite(&header,
sizeof(header), 1, fp) != 1)
1259 return misc_write_error;
1264 enum EST_write_status save_wave_snd_data(FILE *fp,
const short *data,
int offset,
1265 int num_samples,
int num_channels,
1267 enum EST_sample_type_t sample_type,
int bo)
1273 return save_raw_data(fp,data,offset,num_samples,num_channels,
1274 sample_type,bo_big);
1278 enum EST_write_status save_wave_snd(FILE *fp,
const short *data,
int offset,
1279 int num_samples,
int num_channels,
1281 enum EST_sample_type_t sample_type,
int bo)
1283 save_wave_snd_header(fp, num_samples, num_channels, sample_rate,
1285 return save_wave_snd_data(fp, data, offset, num_samples,
1286 num_channels, sample_rate, sample_type, bo);
1340 enum EST_read_status load_wave_audlab(
EST_TokenStream &ts,
short **data,
int 1341 *num_samples,
int *num_channels,
int *word_size,
int 1342 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
int 1349 int data_length,sample_count;
1354 current_pos = ts.
tell();
1356 if (ts.
fread(&fh,
sizeof(
struct audlabfh), 1) != 1)
1357 return misc_read_error;
1358 if (strcmp(fh.file_type,
"Sample") != 0)
1359 return wrong_format;
1361 if (ts.
fread(&sh,
sizeof(
struct audlabsh), 1) != 1)
1362 return misc_read_error;
1363 if (ts.
fread(&sd,
sizeof(
struct audlabsd), 1) != 1)
1364 return misc_read_error;
1365 hdr_length =
sizeof(
struct audlabfh) +
1366 sizeof(struct audlabsh) +
1367 sizeof(struct audlabsd);
1371 *num_channels = sh.channel_count;
1372 *sample_rate = sh.sample_rate;
1373 sample_count = sd.sample_count;
1377 *num_channels = SWAPINT(sh.channel_count);
1378 *sample_rate = SWAPINT(sh.sample_rate);
1379 sample_count = SWAPINT(sd.sample_count);
1382 data_length = (sample_count - offset) * (*num_channels);
1384 data_length = length *(*num_channels);
1386 *data = walloc(
short,
sizeof(
short) * data_length);
1387 ts.
seek(current_pos+hdr_length+(
sizeof(
short)*offset*(*num_channels)));
1389 if ((
int)ts.
fread(*data,
sizeof(
short), data_length) != data_length)
1392 return misc_read_error;
1394 if (EST_LITTLE_ENDIAN)
1395 swap_bytes_short(*data,data_length);
1397 *num_samples = data_length / (*num_channels);
1398 *sample_type = st_short;
1399 *word_size =
sizeof(short);
1400 *bo = EST_NATIVE_BO;
1405 enum EST_write_status save_wave_audlab_header(FILE *fp,
1406 int num_samples,
int num_channels,
1408 enum EST_sample_type_t sample_type,
int bo)
1417 fh.start =
sizeof (
struct audlabfh) +
1418 sizeof (struct audlabsh) + sizeof (struct audlabsd);
1420 strcpy(fh.file_type,
"Sample");
1422 if (EST_LITTLE_ENDIAN)
1424 sh.channel_count = SWAPINT(num_channels);
1426 sh.sample_rate = SWAPINT(sample_rate);
1428 sd.sample_count = SWAPINT(num_samples);
1429 sd.nbits = SWAPINT(16);
1433 sh.channel_count = num_channels;
1435 sh.sample_rate = sample_rate;
1437 sd.sample_count = num_samples;
1440 sprintf(sd.descr,
"Filter 1");
1443 fwrite (&fh,
sizeof(fh), 1, fp);
1444 fwrite (&sh,
sizeof(sh), 1, fp);
1445 fwrite (&sd,
sizeof(sd), 1, fp);
1449 enum EST_write_status save_wave_audlab_data(FILE *fp,
const short *data,
int offset,
1450 int num_samples,
int num_channels,
1452 enum EST_sample_type_t sample_type,
int bo)
1458 return save_raw_data(fp,data,offset,num_samples,num_channels,
1462 enum EST_write_status save_wave_audlab(FILE *fp,
const short *data,
int offset,
1463 int num_samples,
int num_channels,
1465 enum EST_sample_type_t sample_type,
int bo)
1467 save_wave_audlab_header(fp, num_samples, num_channels,
1468 sample_rate, sample_type, bo);
1469 return save_wave_audlab_data(fp, data, offset,
1470 num_samples, num_channels,
1471 sample_rate, sample_type, bo);
1485 #include "esps_utils.h" 1486 enum EST_read_status load_wave_sd(
EST_TokenStream &ts,
short **data,
int 1487 *num_samples,
int *num_channels,
int 1488 *word_size,
int *sample_rate,
enum 1489 EST_sample_type_t *sample_type,
int *bo ,
int 1495 int actual_bo, sample_width, data_length;
1496 enum EST_read_status rv;
1498 enum EST_sample_type_t actual_sample_type;
1500 unsigned char *file_data;
1504 fprintf(stderr,
"Can't open esps file %s for reading\n",
1506 return misc_read_error;
1509 if ((rv=read_esps_hdr(&hdr,fd)) != format_ok)
1512 if (hdr->file_type != ESPS_SD)
1514 fprintf(stderr,
"ESPS file: not an FEA_SD file\n");
1515 delete_esps_hdr(hdr);
1516 return misc_read_error;
1519 if (fea_value_d(
"record_freq",0,hdr,&d) != 0)
1521 fprintf(stderr,
"ESPS file: can't find sample_rate in header assuming 16000\n");
1522 *sample_rate = 16000;
1525 *sample_rate = (int)d;
1526 actual_sample_type = st_short;
1527 sample_width = get_word_size(actual_sample_type);
1528 *num_channels = hdr->field_dimension[0];
1530 actual_bo = (EST_BIG_ENDIAN ? bo_little : bo_big);
1532 actual_bo = (EST_BIG_ENDIAN ? bo_big : bo_little);
1535 data_length = (hdr->num_records - offset)*(*num_channels);
1537 data_length = length *(*num_channels);
1539 file_data = walloc(
unsigned char, sample_width * data_length);
1540 fseek(fd,hdr->hdr_size+(sample_width*offset*(*num_channels)),
1542 if ((dl=fread(file_data,sample_width,data_length,fd)) != data_length)
1544 fprintf(stderr,
"WAVE read: esps short file %s\n",
1546 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1547 offset,dl,data_length);
1551 *data = convert_raw_data(file_data,data_length,
1555 *num_samples = data_length/ (*num_channels);
1556 *sample_type = st_short;
1557 *bo = EST_NATIVE_BO;
1559 delete_esps_hdr(hdr);
1565 enum EST_write_status save_wave_sd_header(FILE *fp,
1566 int num_samples,
int num_channels,
1568 enum EST_sample_type_t sample_type,
int bo)
1572 esps_hdr hdr = make_esps_sd_hdr();
1573 enum EST_write_status rv;
1576 hdr->num_records = num_samples;
1577 switch (sample_type)
1579 case st_short: esps_type = ESPS_SHORT;
break;
1580 case st_schar: esps_type = ESPS_CHAR;
break;
1581 case st_int: esps_type = ESPS_INT;
break;
1582 case st_float: esps_type = ESPS_FLOAT;
break;
1583 case st_double: esps_type = ESPS_DOUBLE;
break;
1585 fprintf(stderr,
"ESPS file: no support for sample_type %s\n",
1586 sample_type_to_str(sample_type));
1587 return misc_write_error;
1590 add_field(hdr,
"samples",esps_type,num_channels);
1592 add_fea_special(hdr,ESPS_FEA_DIRECTORY,
"margo:/disk/disk10/home/awb/projects/speech_tools/main");
1593 add_fea_special(hdr,ESPS_FEA_COMMAND,
1594 "EDST waveform written as ESPS FEA_SD.\n\ 1596 add_fea_d(hdr,
"start_time",0,(
double)0);
1597 add_fea_d(hdr,
"record_freq",0,(
double)sample_rate);
1598 add_fea_d(hdr,
"max_value",0,(
double)27355);
1600 if ((rv=write_esps_hdr(hdr,fp)) != write_ok)
1602 delete_esps_hdr(hdr);
1606 delete_esps_hdr(hdr);
1611 enum EST_write_status save_wave_sd_data(FILE *fp,
const short *data,
1613 int num_samples,
int num_channels,
1615 enum EST_sample_type_t sample_type,
int bo)
1621 return save_raw_data(fp,data,offset,num_samples,num_channels,
1622 sample_type,EST_NATIVE_BO);
1625 enum EST_write_status save_wave_sd(FILE *fp,
const short *data,
int offset,
1626 int num_samples,
int num_channels,
1628 enum EST_sample_type_t sample_type,
int bo)
1631 save_wave_sd_header(fp, num_samples, num_channels, sample_rate,
1633 return save_wave_sd_data(fp, data, offset, num_samples,
1634 num_channels, sample_rate, sample_type, bo);
1645 enum EST_read_status load_wave_raw(
EST_TokenStream &ts,
short **data,
int 1646 *num_samples,
int *num_channels,
1649 enum EST_sample_type_t *sample_type,
1650 int *bo,
int offset,
int length,
1652 enum EST_sample_type_t isample_type,
1655 unsigned char *file_data;
1656 int data_length,samps,sample_width;
1660 if (isample_type == st_ascii)
1663 if ((offset != 0) || (length != 0))
1665 fprintf(stderr,
"Load ascii wave: doesn't support offets and lengths\n");
1666 return misc_read_error;
1670 guess = (int)(1.2*ts.
tell()/7)+10;
1672 *data = walloc(
short, guess);
1676 samp = atoi(ts.
get().string());
1679 ndata = walloc(
short,(
int)(guess*1.2));
1680 memmove(ndata,*data,guess*
sizeof(
short));
1683 guess = (int)(guess*1.2);
1687 fprintf(stderr,
"Load ascii wave: sample %d underflow clipping\n",
1689 (*data)[i] = -32768;
1691 else if (samp > 32767)
1693 fprintf(stderr,
"Load ascii wave: sample %d overflow clipping\n",
1698 (*data)[i] = (short)samp;
1706 sample_width = get_word_size(isample_type);
1707 samps = ts.
tell()/sample_width;
1710 data_length = samps - offset;
1712 data_length = length;
1714 file_data = walloc(
unsigned char, data_length * sample_width *inc);
1715 ts.
seek(offset*sample_width*inc);
1716 if ((
int)ts.
fread(file_data,sample_width,data_length) != data_length)
1717 return misc_read_error;
1719 *data = convert_raw_data(file_data,data_length,isample_type,ibo);
1722 *num_samples = data_length/inc;
1723 *sample_rate = isample_rate;
1724 *num_channels = inc;
1725 *sample_type = st_short;
1727 *bo = EST_NATIVE_BO;
1732 enum EST_write_status save_wave_raw_header(FILE *fp,
1733 int num_samples,
int num_channels,
1735 enum EST_sample_type_t sample_type,
int bo)
1740 enum EST_write_status save_wave_raw_data(FILE *fp,
const short *data,
1742 int num_samples,
int num_channels,
1744 enum EST_sample_type_t sample_type,
int bo)
1749 return save_raw_data(fp,data,offset,num_samples,num_channels,
1753 enum EST_write_status save_wave_raw(FILE *fp,
const short *data,
1755 int num_samples,
int num_channels,
1757 enum EST_sample_type_t sample_type,
int bo)
1761 return save_wave_raw_data(fp, data, offset, num_samples,
1762 num_channels, sample_rate, sample_type, bo);
1771 enum EST_write_status wave_io_save_header(FILE *fp,
1772 const int num_samples,
const int num_channels,
1773 const int sample_rate,
1777 EST_WaveFileType t = EST_WaveFile::map.token(ftype);
1778 EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
1782 return save_wave_nist_header(fp, num_samples, num_channels,
1783 sample_rate, sample_type, bo);
1786 return save_wave_sd_header(fp, num_samples, num_channels,
1787 sample_rate, sample_type, bo);
1790 return save_wave_est_header(fp, num_samples, num_channels,
1791 sample_rate, sample_type, bo);
1794 return save_wave_audlab_header(fp, num_samples, num_channels,
1795 sample_rate, sample_type, bo);
1798 return save_wave_snd_header(fp, num_samples, num_channels,
1799 sample_rate, sample_type, bo);
1802 return save_wave_aiff_header(fp, num_samples, num_channels,
1803 sample_rate, sample_type, bo);
1806 return save_wave_riff_header(fp, num_samples, num_channels,
1807 sample_rate, sample_type, bo);
1810 return save_wave_raw_header(fp, num_samples, num_channels,
1811 sample_rate, sample_type, bo);
1814 return save_wave_ulaw_header(fp, num_samples, num_channels,
1815 sample_rate, sample_type, bo);
1819 cerr <<
"Can't save wave header to files type " << ftype << endl;
1826 enum EST_write_status wave_io_save_data(FILE *fp,
const short * data,
1828 const int num_samples,
const int num_channels,
1829 const int sample_rate,
1833 EST_WaveFileType t = EST_WaveFile::map.token(ftype);
1834 EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
1838 return save_wave_nist_data(fp, data, offset, num_samples, num_channels,
1839 sample_rate, sample_type, bo);
1842 return save_wave_sd_data(fp, data, offset, num_samples, num_channels,
1843 sample_rate, sample_type, bo);
1846 return save_wave_est_data(fp, data, offset, num_samples, num_channels,
1847 sample_rate, sample_type, bo);
1850 return save_wave_audlab_data(fp, data, offset, num_samples, num_channels,
1851 sample_rate, sample_type, bo);
1854 return save_wave_snd_data(fp, data, offset, num_samples, num_channels,
1855 sample_rate, sample_type, bo);
1858 return save_wave_aiff_data(fp, data, offset, num_samples, num_channels,
1859 sample_rate, sample_type, bo);
1862 return save_wave_riff_data(fp, data, offset, num_samples, num_channels,
1863 sample_rate, sample_type, bo);
1866 return save_wave_raw_data(fp, data, offset, num_samples, num_channels,
1867 sample_rate, sample_type, bo);
1870 return save_wave_ulaw_data(fp, data, offset, num_samples, num_channels,
1871 sample_rate, sample_type, bo);
1875 cerr <<
"Can't save wave data to files type " << ftype << endl;
EST_TokenStream & get(EST_Token &t)
get next token in stream
int tell(void) const
tell, synonym for filepos
FILE * filedescriptor()
For the people who need the actual description (if possible)
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don't use peek() immediately beforehand)
void close(void)
Close stream.
const EST_String filename() const
The originating filename (if there is one)
int open(const EST_String &filename)
open a {EST_TokenStream} for a file.
const int present(const K &rkey) const
Returns true if key is present.
int ival(const EST_String &rkey, int m=1) const
const EST_String pos_description()
A string describing current position, suitable for error messages.
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
int seek(int position)
seek, reposition file pointer