47#include "EST_wave_aux.h"
48#include "EST_wave_utils.h"
49#include "EST_strcasecmp.h"
51#include "EST_FileType.h"
53static int def_load_sample_rate = 16000;
65static const char *NIST_SIG =
"NIST_1A\n 1024\n";
66static const char *NIST_END_SIG =
"end_head\n";
67#define NIST_HDR_SIZE 1024
69int 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);
85char *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);
106const char *sample_type_to_nist(
enum EST_sample_type_t sample_type)
109 switch (sample_type) {
127 fprintf(stderr,
"Unknown sample type for nist");
133enum 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);
165enum 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");
214 if (streq(sample_coding,
"pcm,embedded-shorten-v1.1"))
217 char *tmpfile, *cmdstr;
218 enum EST_read_status rval;
221 fprintf(stderr,
"WAVE read: nist type is shorten\n");
222 fprintf(stderr,
"WAVE read: no support for shorten -- you need to use some external program to unshorten the data\n");
223 return misc_read_error;
226 tmpfile = cmake_tmp_filename();
227 cmdstr = walloc(
char,strlen(tmpfile)+200);
230 sprintf(cmdstr,
"cstrshorten %s %s",
231 (
const char*)ts.
filename(),tmpfile);
232 printf(
"Command: %s\n", cmdstr);
237 rval = load_wave_nist(tt, data, num_samples,
238 num_channels, word_size, sample_rate,
239 sample_type, bo, offset, length);
249 data_length = (samps - offset)*(*num_channels);
251 data_length = length*(*num_channels);
253 file_data = walloc(
unsigned char,sample_width * data_length);
255 ts.
seek(current_pos+NIST_HDR_SIZE+(sample_width*offset*(*num_channels)));
257 n = ts.
fread(file_data,sample_width,data_length);
259 if ((n < 1) && (n != data_length))
262 wfree(sample_coding);
264 return misc_read_error;
266 else if ((n < data_length) && (data_length/(*num_channels) == n))
268 fprintf(stderr,
"WAVE read: nist header is (probably) non-standard\n");
269 fprintf(stderr,
"WAVE read: assuming different num_channel interpretation\n");
272 else if (n < data_length)
274 fprintf(stderr,
"WAVE read: short file %s\n",
276 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
277 offset,n,data_length);
281 actual_sample_type = nist_to_sample_type(sample_coding);
282 actual_bo = ((strcmp(byte_order,
"10") == 0) ? bo_big : bo_little);
284 *data = convert_raw_data(file_data,data_length,
285 actual_sample_type,actual_bo);
287 *num_samples = data_length/ (*num_channels);
288 *sample_type = st_short;
291 wfree(sample_coding);
297enum EST_write_status save_wave_nist_header(FILE *fp,
298 int num_samples,
int num_channels,
300 enum EST_sample_type_t sample_type,
int bo)
302 char h[1024], p[1024];
308 sprintf(p,
"channel_count -i %d\n", num_channels);
310 sprintf(p,
"sample_count -i %d\n", num_samples);
312 sprintf(p,
"sample_rate -i %d\n", sample_rate);
315 t = sample_type_to_nist(sample_type);
318 sprintf(p,
"sample_coding -s%d %s\n", (
signed)strlen(t), t);
320 sprintf(p,
"sample_n_bytes -i %d\n", get_word_size(sample_type));
324 if (get_word_size(sample_type) > 1)
326 sprintf(p,
"sample_byte_format -s%d %s\n", 2,
327 ((bo == bo_big) ?
"10" :
"01"));
331 strcat(h, NIST_END_SIG);
333 strcat(h,
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
335 if (fwrite(&h, 1024, 1, fp) != 1)
336 return misc_write_error;
342enum EST_write_status save_wave_nist_data(FILE *fp,
const short *data,
int offset,
343 int num_samples,
int num_channels,
345 enum EST_sample_type_t sample_type,
int bo)
350 return save_raw_data(fp,data,offset,num_samples,num_channels,
355enum EST_write_status save_wave_nist(FILE *fp,
const short *data,
int offset,
356 int num_samples,
int num_channels,
358 enum EST_sample_type_t sample_type,
int bo)
360 save_wave_nist_header(fp, num_samples, num_channels,
361 sample_rate, sample_type, bo);
362 return save_wave_nist_data(fp, data, offset,
363 num_samples, num_channels,
364 sample_rate, sample_type, bo);
371enum EST_read_status load_wave_est(
EST_TokenStream &ts,
short **data,
int
372 *num_samples,
int *num_channels,
int
373 *word_size,
int *sample_rate,
enum
374 EST_sample_type_t *sample_type,
int *bo,
375 int offset,
int length)
377 int data_length, actual_bo;
385 EST_sample_type_t actual_sample_type;
389 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
391 if (t != est_file_wave)
392 return misc_read_error;
394 *num_samples = hinfo.
ival(
"NumSamples");
395 *num_channels = hinfo.
ival(
"NumChannels");
396 *sample_rate = hinfo.
ival(
"SampleRate");
398 byte_order = hinfo.
val(
"ByteOrder");
401 data_length = (*num_samples)*(*num_channels);
403 data_length = length*(*num_channels);
405 file_data = walloc(
short, data_length);
407 n = ts.
fread(file_data,
sizeof(
short), data_length);
408 if ((n != data_length) && (n < 1))
411 cerr <<
"failed to read file\n";
413 return misc_read_error;
415 else if (n != data_length)
417 cerr <<
"Wrong number of samples/channels in EST wave file\n";
419 cerr <<
"expected " << data_length <<
" got " << n << endl;
423 actual_bo = (byte_order ==
"10") ? bo_big : bo_little;
424 if (hinfo.
present(
"SampleType"))
425 actual_sample_type = str_to_sample_type(hinfo.
val(
"SampleType"));
427 actual_sample_type = st_short;
429 *data = convert_raw_data((
unsigned char *)file_data,
430 data_length, actual_sample_type, actual_bo);
432 *sample_type = st_short;
439enum EST_write_status save_wave_est_header(FILE *fp,
440 int num_samples,
int num_channels,
442 enum EST_sample_type_t sample_type,
int bo)
444 fprintf(fp,
"EST_File wave\n");
445 fprintf(fp,
"DataType binary\n");
446 fprintf(fp,
"SampleRate %d\n", sample_rate);
447 fprintf(fp,
"NumSamples %d\n", num_samples);
448 fprintf(fp,
"NumChannels %d\n", num_channels);
449 fprintf(fp,
"SampleType %s\n", sample_type_to_str(sample_type));
450 if (get_word_size(sample_type) > 1)
451 fprintf(fp,
"ByteOrder %s\n", ((bo == bo_big) ?
"10" :
"01"));
453 fprintf(fp,
"EST_Header_End\n");
457enum EST_write_status save_wave_est_data(FILE *fp,
const short *data,
int offset,
458 int num_samples,
int num_channels,
460 enum EST_sample_type_t sample_type,
int bo)
465 return save_raw_data(fp, data, offset, num_samples, num_channels,
469enum EST_write_status save_wave_est(FILE *fp,
const short *data,
int offset,
470 int num_samples,
int num_channels,
472 enum EST_sample_type_t sample_type,
int bo)
474 save_wave_est_header(fp, num_samples, num_channels,
475 sample_rate, sample_type, bo);
477 return save_wave_est_data(fp, data, offset,
478 num_samples, num_channels,
479 sample_rate, sample_type, bo);
493#define WAVE_FORMAT_PCM 0x0001
494#define WAVE_FORMAT_ADPCM 0x0002
495#define WAVE_FORMAT_ALAW 0x0006
496#define WAVE_FORMAT_MULAW 0x0007
498enum EST_read_status load_wave_riff(
EST_TokenStream &ts,
short **data,
int
499 *num_samples,
int *num_channels,
int
500 *word_size,
int *sample_rate,
enum
501 EST_sample_type_t *sample_type,
int *bo ,
int
505 int samps,sample_width,data_length;
508 unsigned char *file_data;
509 enum EST_sample_type_t actual_sample_type;
511 if (ts.
fread(info,
sizeof(
char),4) != 4)
513 if (strncmp(info,
"RIFF",4) != 0)
518 if(ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
520 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
521 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
522 (strncmp(info,
"WAVE",4) != 0))
524 fprintf(stderr,
"RIFF file is not of type WAVE\n");
525 return misc_read_error;
527 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
528 (strncmp(info,
"fmt ",4) != 0))
529 return misc_read_error;
531 if (ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
532 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
533 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
534 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
539 case WAVE_FORMAT_PCM:
540 actual_sample_type = st_short;
break;
542 case WAVE_FORMAT_MULAW:
543 actual_sample_type = st_mulaw;
break;
544 case WAVE_FORMAT_ALAW:
545 actual_sample_type = st_alaw;
break;
546 case WAVE_FORMAT_ADPCM:
547 fprintf(stderr,
"RIFF file: unsupported proprietary sample format ADPCM\n");
548 actual_sample_type = st_short;
552 fprintf(stderr,
"RIFF file: unknown sample format\n");
553 actual_sample_type = st_short;
556 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
557 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
558 *num_channels = shortdata;
559 if (ts.
fread(sample_rate,4,1) != 1)
return misc_read_error;
560 if (EST_BIG_ENDIAN) *sample_rate = SWAPINT(*sample_rate);
561 if (ts.
fread(&intdata,4,1) != 1)
return misc_read_error;
562 if (EST_BIG_ENDIAN) intdata = SWAPINT(intdata);
563 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
564 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
565 if (ts.
fread(&shortdata,2,1) != 1)
return misc_read_error;
566 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
568 sample_width = (shortdata+7)/8;
569 if ((sample_width == 1) && (actual_sample_type == st_short))
570 actual_sample_type = st_uchar;
575 if (ts.
fread(info,
sizeof(
char),4) != 4)
577 fprintf(stderr,
"RIFF file truncated\n");
578 return misc_read_error;
580 if (strncmp(info,
"data",4) == 0)
582 if (ts.
fread(&samps,4,1) != 1)
return misc_read_error;
583 if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
584 samps /= (sample_width*(*num_channels));
587 else if (strncmp(info,
"fact",4) == 0)
589 if (ts.
fread(&samps,4,1) != 1)
return misc_read_error;
590 if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
599 if(ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
600 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
605 data_length = (samps - offset)*(*num_channels);
607 data_length = length*(*num_channels);
609 file_data = walloc(
unsigned char,sample_width * data_length);
611 ts.
seek((sample_width*offset*(*num_channels))+ts.
tell());
612 if ((dsize=ts.
fread(file_data,sample_width,data_length)) != data_length)
617 fprintf(stderr,
"Unexpected end of file but continuing (apparently missing %d samples)\n",data_length-dsize);
620 fprintf(stderr,
"Unexpected end of file: (missing %d samples)\n",data_length-dsize);
622 return misc_read_error;
626 *data = convert_raw_data(file_data,dsize,
627 actual_sample_type, bo_little);
629 *num_samples = dsize / (*num_channels);
630 *sample_type = st_short;
637enum EST_write_status save_wave_riff_header(FILE *fp,
int num_samples,
638 int num_channels,
int sample_rate,
639 enum EST_sample_type_t sample_type,
int bo)
643 int data_size, data_int;
646 if (sample_type == st_schar)
648 EST_warning(
"RIFF format: Signed 8-bit not allowed by this file format");
649 sample_type=st_uchar;
652 info =
"RIFF"; fwrite(info,4,1,fp);
653 data_size = num_channels*num_samples*get_word_size(sample_type)+ 8+16+12;
655 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
656 fwrite(&data_size,1,4,fp);
657 info =
"WAVE"; fwrite(info,4,1,fp);
658 info =
"fmt "; fwrite(info,4,1,fp);
660 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
661 fwrite(&data_size,1,4,fp);
664 case st_short: data_short = WAVE_FORMAT_PCM;
break;
665 case st_uchar: data_short = WAVE_FORMAT_PCM;
break;
666 case st_mulaw: data_short = WAVE_FORMAT_MULAW;
break;
667 case st_alaw: data_short = WAVE_FORMAT_ALAW;
break;
668 case st_adpcm: data_short = WAVE_FORMAT_ADPCM;
break;
670 fprintf(stderr,
"RIFF format: unsupported data format %d\n",
672 return misc_write_error;
674 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
675 fwrite(&data_short,1,2,fp);
676 data_short = num_channels;
677 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
678 fwrite(&data_short,1,2,fp);
679 data_int = sample_rate;
680 if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
681 fwrite(&data_int,1,4,fp);
682 data_int = sample_rate * num_channels * get_word_size(sample_type);
683 if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
684 fwrite(&data_int,1,4,fp);
685 data_short = num_channels * get_word_size(sample_type);
686 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
687 fwrite(&data_short,1,2,fp);
688 data_short = get_word_size(sample_type) * 8;
689 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
690 fwrite(&data_short,1,2,fp);
691 info =
"data"; fwrite(info,4,1,fp);
692 data_size = num_channels*num_samples*get_word_size(sample_type);
693 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
694 fwrite(&data_size,1,4,fp);
699enum EST_write_status save_wave_riff_data(FILE *fp,
const short *data,
700 int offset,
int num_samples,
int num_channels,
702 enum EST_sample_type_t sample_type,
int bo)
707 return save_raw_data(fp,data,offset,num_samples,num_channels,
708 sample_type,bo_little);
712enum EST_write_status save_wave_riff(FILE *fp,
const short *data,
int offset,
713 int num_samples,
int num_channels,
715 enum EST_sample_type_t sample_type,
int bo)
717 save_wave_riff_header(fp, num_samples, num_channels, sample_rate,
720 return save_wave_riff_data(fp, data, offset, num_samples,
721 num_channels, sample_rate, sample_type, bo);
742enum EST_read_status load_wave_aiff(
EST_TokenStream &ts,
short **data,
int
743 *num_samples,
int *num_channels,
int
744 *word_size,
int *sample_rate,
enum
745 EST_sample_type_t *sample_type,
int *bo ,
int
749 struct AIFFchunk chunk;
753 unsigned char ieee_ext_sample_rate[10];
754 struct AIFFssnd ssndchunk;
755 enum EST_sample_type_t actual_sample_type;
756 int dsize,data_length,n;
757 unsigned char *file_data;
759 if (ts.
fread(info,
sizeof(
char),4) != 4)
761 if (strncmp(info,
"FORM",4) != 0)
765 if (ts.
fread(&dsize,4,1) != 1)
return misc_read_error;
766 if (EST_LITTLE_ENDIAN)
767 dsize = SWAPINT(dsize);
768 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
769 (strncmp(info,
"AIFF",4) != 0))
771 fprintf(stderr,
"AIFF file does not have AIFF chunk\n");
772 return misc_read_error;
775 for ( ; ts.
fread(&chunk,
sizeof(chunk), 1) == 1 ; )
777 if (EST_LITTLE_ENDIAN)
778 chunk.size = SWAPINT(chunk.size);
779 if (strncmp(chunk.id,
"COMM",4) == 0)
781 if (chunk.size != 18)
783 fprintf(stderr,
"AIFF chunk: bad size\n");
784 return misc_read_error;
786 if (ts.
fread(&comm_channels,
sizeof(
short), 1) != 1)
787 return misc_read_error;
788 if (ts.
fread(&comm_samples,
sizeof(
int), 1) != 1)
789 return misc_read_error;
790 if (ts.
fread(&comm_bits,
sizeof(
short), 1) != 1)
791 return misc_read_error;
792 if (ts.
fread(ieee_ext_sample_rate, 10, 1) != 1)
794 fprintf(stderr,
"AIFF chunk: eof within COMM chunk\n");
795 return misc_read_error;
797 if (EST_LITTLE_ENDIAN)
799 comm_channels = SWAPSHORT(comm_channels);
800 comm_samples = SWAPINT(comm_samples);
801 comm_bits = SWAPSHORT(comm_bits);
803 *sample_rate = (int)ConvertFromIeeeExtended(ieee_ext_sample_rate);
805 else if (strncmp(chunk.id,
"SSND",4) == 0)
807 if (ts.
fread(&ssndchunk,
sizeof(ssndchunk), 1) != 1)
809 fprintf(stderr,
"AIFF chunk: eof within SSND chunk\n");
810 return misc_read_error;
812 if (EST_LITTLE_ENDIAN)
814 ssndchunk.offset = SWAPINT(ssndchunk.offset);
815 ssndchunk.blocksize = SWAPINT(ssndchunk.blocksize);
818 *num_channels = comm_channels;
821 case 8: actual_sample_type = st_uchar;
break;
822 case 16: actual_sample_type = st_short;
break;
824 fprintf(stderr,
"AIFF: unsupported sample width %d bits\n",
826 return misc_read_error;
829 ts.
seek(ssndchunk.offset+(comm_channels*offset)+ts.
tell());
831 data_length = (comm_samples-offset)*comm_channels;
833 data_length = length*comm_channels;
834 file_data = walloc(
unsigned char,
835 data_length*comm_channels*
836 get_word_size(actual_sample_type));
837 if ((n=ts.
fread(file_data,get_word_size(actual_sample_type),
838 data_length)) != data_length)
840 fprintf(stderr,
"AIFF read: short file %s\n",
842 fprintf(stderr,
"AIFF read: at %d got %d instead of %d samples\n",
843 offset,n,data_length);
847 *data = convert_raw_data(file_data,data_length,
848 actual_sample_type,bo_big);
849 *num_samples = data_length/comm_channels;
850 *sample_type = st_short;
865enum EST_write_status save_wave_aiff_header(FILE *fp,
866 int num_samples,
int num_channels,
868 enum EST_sample_type_t sample_type,
int bo)
872 int data_size, data_int;
873 unsigned char ieee_ext_buf[10];
879 data_size = 54+(num_samples*num_channels*get_word_size(sample_type));
880 if (EST_LITTLE_ENDIAN)
881 data_size = SWAPINT(data_size);
882 fwrite(&data_size,1,4,fp);
888 if (EST_LITTLE_ENDIAN)
889 data_int = SWAPINT(data_int);
890 fwrite(&data_int,1,4,fp);
891 data_short = num_channels;
892 if (EST_LITTLE_ENDIAN)
893 data_short = SWAPSHORT(data_short);
894 fwrite(&data_short,1,2,fp);
895 data_int = num_samples;
896 if (EST_LITTLE_ENDIAN)
897 data_int = SWAPINT(data_int);
898 fwrite(&data_int,1,4,fp);
899 data_short = 8*get_word_size(sample_type);
900 if (EST_LITTLE_ENDIAN)
901 data_short = SWAPSHORT(data_short);
902 fwrite(&data_short,1,2,fp);
903 ConvertToIeeeExtended((
double)sample_rate,ieee_ext_buf);
904 fwrite(ieee_ext_buf,1,10,fp);
907 data_int = 8 + (num_samples*num_channels*get_word_size(sample_type));
908 if (EST_LITTLE_ENDIAN)
909 data_int = SWAPINT(data_int);
910 fwrite(&data_int,1,4,fp);
912 if (EST_LITTLE_ENDIAN)
913 data_int = SWAPINT(data_int);
914 fwrite(&data_int,1,4,fp);
915 if (EST_LITTLE_ENDIAN)
916 data_int = SWAPINT(data_int);
917 fwrite(&data_int,1,4,fp);
923enum EST_write_status save_wave_aiff_data(FILE *fp,
const short *data,
int offset,
924 int num_samples,
int num_channels,
926 enum EST_sample_type_t sample_type,
int bo)
931 if ((sample_type == st_short) || (sample_type == st_uchar))
932 return save_raw_data(fp,data, offset, num_samples, num_channels,
933 sample_type, bo_big);
936 fprintf(stderr,
"AIFF: requested data type not uchar or short\n");
937 return misc_write_error;
942enum EST_write_status save_wave_aiff(FILE *fp,
const short *data,
int offset,
943 int num_samples,
int num_channels,
945 enum EST_sample_type_t sample_type,
int bo)
947 save_wave_aiff_header(fp, num_samples, num_channels,
948 sample_rate, sample_type, bo);
950 return save_wave_aiff_data(fp, data, offset,
951 num_samples, num_channels,
952 sample_rate, sample_type, bo);
959enum EST_read_status load_wave_ulaw(
EST_TokenStream &ts,
short **data,
int
960 *num_samples,
int *num_channels,
int *word_size,
int
961 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
962 int offset,
int length)
966 int data_length,samps;
972 data_length = samps - offset;
974 data_length = length;
976 ulaw = walloc(
unsigned char, data_length);
978 if (ts.
fread(ulaw,1,data_length) != data_length)
981 return misc_read_error;
984 *data = walloc(
short,data_length);
985 ulaw_to_short(ulaw,*data,data_length);
988 *num_samples = data_length;
991 *sample_type = st_short;
998enum EST_write_status save_wave_ulaw_header(FILE *fp,
999 int num_samples,
int num_channels,
1001 enum EST_sample_type_t sample_type,
int bo)
1007 (void) num_channels;
1012enum EST_write_status save_wave_ulaw_data(FILE *fp,
const short *data,
int offset,
1013 int num_samples,
int num_channels,
1015 enum EST_sample_type_t sample_type,
int bo)
1020 return save_wave_raw(fp,data,offset,num_samples,num_channels,
1024enum EST_write_status save_wave_ulaw(FILE *fp,
const short *data,
int offset,
1025 int num_samples,
int num_channels,
1027 enum EST_sample_type_t sample_type,
int bo)
1029 save_wave_ulaw_header(fp, num_samples, num_channels,
1030 sample_rate, sample_type, bo);
1032 return save_wave_ulaw_data(fp, data, offset,
1033 num_samples, num_channels,
1034 sample_rate, sample_type, bo);
1039enum EST_read_status load_wave_alaw(
EST_TokenStream &ts,
short **data,
int
1040 *num_samples,
int *num_channels,
int *word_size,
int
1041 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
1042 int offset,
int length)
1045 unsigned char *alaw;
1046 int data_length,samps;
1052 data_length = samps - offset;
1054 data_length = length;
1056 alaw = walloc(
unsigned char, data_length);
1058 if (ts.
fread(alaw,1,data_length) != data_length)
1061 return misc_read_error;
1064 *data = walloc(
short,data_length);
1065 alaw_to_short(alaw,*data,data_length);
1068 *num_samples = data_length;
1069 *sample_rate = 8000;
1071 *sample_type = st_short;
1073 *bo = EST_NATIVE_BO;
1078enum EST_write_status save_wave_alaw_header(FILE *fp,
1079 int num_samples,
int num_channels,
1081 enum EST_sample_type_t sample_type,
int bo)
1087 (void) num_channels;
1092enum EST_write_status save_wave_alaw_data(FILE *fp,
const short *data,
int offset,
1093 int num_samples,
int num_channels,
1095 enum EST_sample_type_t sample_type,
int bo)
1101 return save_wave_raw(fp,data,offset,num_samples,num_channels,
1105enum EST_write_status save_wave_alaw(FILE *fp,
const short *data,
int offset,
1106 int num_samples,
int num_channels,
1108 enum EST_sample_type_t sample_type,
int bo)
1110 save_wave_alaw_header(fp, num_samples, num_channels,
1111 sample_rate, sample_type, bo);
1113 return save_wave_alaw_data(fp, data, offset,
1114 num_samples, num_channels,
1115 sample_rate, sample_type, bo);
1125 unsigned int hdr_size;
1127 unsigned int encoding;
1128 unsigned int sample_rate;
1129 unsigned int channels;
1132enum EST_read_status load_wave_snd(
EST_TokenStream &ts,
short **data,
int
1133 *num_samples,
int *num_channels,
int *word_size,
int
1134 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo ,
1135 int offset,
int length)
1138 Sun_au_header header;
1139 enum EST_sample_type_t encoding_type;
1140 int data_length, sample_width, bytes, samps, n;
1141 unsigned char *file_data;
1144 current_pos = ts.
tell();
1145 if (ts.
fread(&header,
sizeof(Sun_au_header), 1) != 1)
1146 return misc_read_error;
1149 if ((EST_LITTLE_ENDIAN) &&
1150 ((
unsigned int)0x2e736e64 == SWAPINT(header.magic)))
1152 header.hdr_size = SWAPINT(header.hdr_size);
1153 header.data_size = SWAPINT(header.data_size);
1154 header.encoding = SWAPINT(header.encoding);
1155 header.sample_rate = SWAPINT(header.sample_rate);
1156 header.channels = SWAPINT(header.channels);
1158 else if ((
unsigned int)0x2e736e64 != header.magic)
1159 return wrong_format;
1161 switch (header.encoding)
1164 encoding_type = st_mulaw;
1167 encoding_type = st_uchar;
1170 encoding_type = st_short;
1173 fprintf(stderr,
"Unsupported data type in SND header\n");
1174 return misc_read_error;
1177 *num_channels = header.channels;
1178 sample_width = get_word_size(encoding_type);
1179 *sample_rate = header.sample_rate;
1181 if ((header.data_size == 0) ||
1182 (header.data_size == -1))
1185 bytes = ts.
tell() - header.hdr_size;
1188 bytes = header.data_size;
1189 samps = bytes/sample_width;
1192 data_length = (samps - offset)*(*num_channels);
1194 data_length = length *(*num_channels);
1196 file_data = walloc(
unsigned char, sample_width * data_length);
1197 ts.
seek(current_pos+header.hdr_size+(sample_width*offset*(*num_channels)));
1198 if ((n=ts.
fread(file_data,sample_width,data_length)) != data_length)
1200 fprintf(stderr,
"WAVE read: short file %s\n",
1202 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1203 offset,n,data_length);
1207 *data = convert_raw_data(file_data,data_length,encoding_type,bo_big);
1212 *num_samples = data_length/ (*num_channels);
1213 *sample_type = st_short;
1214 *bo = EST_NATIVE_BO;
1219enum EST_write_status save_wave_snd_header(FILE *fp,
1220 int num_samples,
int num_channels,
1222 enum EST_sample_type_t sample_type,
int bo)
1226 Sun_au_header header;
1229 header.magic = (
unsigned int)0x2e736e64;
1230 header.hdr_size =
sizeof(header);
1231 header.data_size = get_word_size(sample_type) * num_channels * num_samples;
1233 switch (sample_type) {
1235 header.encoding = 1;
1238 header.encoding = 2;
1241 header.encoding = 3;
1246 "Unsupported sample type cannot be saved in SND format\n");
1247 return misc_write_error;
1253 header.sample_rate = sample_rate;
1255 header.channels = num_channels;
1257 if (EST_LITTLE_ENDIAN)
1260 header.magic = SWAPINT(header.magic);
1261 header.hdr_size = SWAPINT(header.hdr_size);
1262 header.data_size = SWAPINT(header.data_size);
1263 header.encoding = SWAPINT(header.encoding);
1264 header.sample_rate = SWAPINT(header.sample_rate);
1265 header.channels = SWAPINT(header.channels);
1269 if (fwrite(&header,
sizeof(header), 1, fp) != 1)
1270 return misc_write_error;
1275enum EST_write_status save_wave_snd_data(FILE *fp,
const short *data,
int offset,
1276 int num_samples,
int num_channels,
1278 enum EST_sample_type_t sample_type,
int bo)
1284 return save_raw_data(fp,data,offset,num_samples,num_channels,
1285 sample_type,bo_big);
1289enum EST_write_status save_wave_snd(FILE *fp,
const short *data,
int offset,
1290 int num_samples,
int num_channels,
1292 enum EST_sample_type_t sample_type,
int bo)
1294 save_wave_snd_header(fp, num_samples, num_channels, sample_rate,
1296 return save_wave_snd_data(fp, data, offset, num_samples,
1297 num_channels, sample_rate, sample_type, bo);
1351enum EST_read_status load_wave_audlab(
EST_TokenStream &ts,
short **data,
int
1352 *num_samples,
int *num_channels,
int *word_size,
int
1353 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
int
1360 int data_length,sample_count;
1365 current_pos = ts.
tell();
1367 if (ts.
fread(&fh,
sizeof(
struct audlabfh), 1) != 1)
1368 return misc_read_error;
1369 if (strcmp(fh.file_type,
"Sample") != 0)
1370 return wrong_format;
1372 if (ts.
fread(&sh,
sizeof(
struct audlabsh), 1) != 1)
1373 return misc_read_error;
1374 if (ts.
fread(&sd,
sizeof(
struct audlabsd), 1) != 1)
1375 return misc_read_error;
1376 hdr_length =
sizeof(
struct audlabfh) +
1377 sizeof(struct audlabsh) +
1378 sizeof(struct audlabsd);
1382 *num_channels = sh.channel_count;
1383 *sample_rate = sh.sample_rate;
1384 sample_count = sd.sample_count;
1388 *num_channels = SWAPINT(sh.channel_count);
1389 *sample_rate = SWAPINT(sh.sample_rate);
1390 sample_count = SWAPINT(sd.sample_count);
1393 data_length = (sample_count - offset) * (*num_channels);
1395 data_length = length *(*num_channels);
1397 *data = walloc(
short,
sizeof(
short) * data_length);
1398 ts.
seek(current_pos+hdr_length+(
sizeof(
short)*offset*(*num_channels)));
1400 if ((
int)ts.
fread(*data,
sizeof(
short), data_length) != data_length)
1403 return misc_read_error;
1405 if (EST_LITTLE_ENDIAN)
1406 swap_bytes_short(*data,data_length);
1408 *num_samples = data_length / (*num_channels);
1409 *sample_type = st_short;
1410 *word_size =
sizeof(short);
1411 *bo = EST_NATIVE_BO;
1416enum EST_write_status save_wave_audlab_header(FILE *fp,
1417 int num_samples,
int num_channels,
1419 enum EST_sample_type_t sample_type,
int bo)
1428 fh.start =
sizeof (
struct audlabfh) +
1429 sizeof (struct audlabsh) + sizeof (struct audlabsd);
1431 strcpy(fh.file_type,
"Sample");
1433 if (EST_LITTLE_ENDIAN)
1435 sh.channel_count = SWAPINT(num_channels);
1437 sh.sample_rate = SWAPINT(sample_rate);
1439 sd.sample_count = SWAPINT(num_samples);
1440 sd.nbits = SWAPINT(16);
1444 sh.channel_count = num_channels;
1446 sh.sample_rate = sample_rate;
1448 sd.sample_count = num_samples;
1451 sprintf(sd.descr,
"Filter 1");
1454 fwrite (&fh,
sizeof(fh), 1, fp);
1455 fwrite (&sh,
sizeof(sh), 1, fp);
1456 fwrite (&sd,
sizeof(sd), 1, fp);
1460enum EST_write_status save_wave_audlab_data(FILE *fp,
const short *data,
int offset,
1461 int num_samples,
int num_channels,
1463 enum EST_sample_type_t sample_type,
int bo)
1469 return save_raw_data(fp,data,offset,num_samples,num_channels,
1473enum EST_write_status save_wave_audlab(FILE *fp,
const short *data,
int offset,
1474 int num_samples,
int num_channels,
1476 enum EST_sample_type_t sample_type,
int bo)
1478 save_wave_audlab_header(fp, num_samples, num_channels,
1479 sample_rate, sample_type, bo);
1480 return save_wave_audlab_data(fp, data, offset,
1481 num_samples, num_channels,
1482 sample_rate, sample_type, bo);
1496#include "esps_utils.h"
1497enum EST_read_status load_wave_sd(
EST_TokenStream &ts,
short **data,
int
1498 *num_samples,
int *num_channels,
int
1499 *word_size,
int *sample_rate,
enum
1500 EST_sample_type_t *sample_type,
int *bo ,
int
1506 int actual_bo, sample_width, data_length;
1507 enum EST_read_status rv;
1509 enum EST_sample_type_t actual_sample_type;
1511 unsigned char *file_data;
1515 fprintf(stderr,
"Can't open esps file %s for reading\n",
1517 return misc_read_error;
1520 if ((rv=read_esps_hdr(&hdr,fd)) != format_ok)
1523 if (hdr->file_type != ESPS_SD)
1525 fprintf(stderr,
"ESPS file: not an FEA_SD file\n");
1526 delete_esps_hdr(hdr);
1527 return misc_read_error;
1530 if (fea_value_d(
"record_freq",0,hdr,&d) != 0)
1532 fprintf(stderr,
"ESPS file: can't find sample_rate in header assuming 16000\n");
1533 *sample_rate = 16000;
1536 *sample_rate = (int)d;
1537 actual_sample_type = st_short;
1538 sample_width = get_word_size(actual_sample_type);
1539 *num_channels = hdr->field_dimension[0];
1541 actual_bo = (EST_BIG_ENDIAN ? bo_little : bo_big);
1543 actual_bo = (EST_BIG_ENDIAN ? bo_big : bo_little);
1546 data_length = (hdr->num_records - offset)*(*num_channels);
1548 data_length = length *(*num_channels);
1550 file_data = walloc(
unsigned char, sample_width * data_length);
1551 fseek(fd,hdr->hdr_size+(sample_width*offset*(*num_channels)),
1553 if ((dl=fread(file_data,sample_width,data_length,fd)) != data_length)
1555 fprintf(stderr,
"WAVE read: esps short file %s\n",
1557 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1558 offset,dl,data_length);
1562 *data = convert_raw_data(file_data,data_length,
1566 *num_samples = data_length/ (*num_channels);
1567 *sample_type = st_short;
1568 *bo = EST_NATIVE_BO;
1570 delete_esps_hdr(hdr);
1576enum EST_write_status save_wave_sd_header(FILE *fp,
1577 int num_samples,
int num_channels,
1579 enum EST_sample_type_t sample_type,
int bo)
1583 esps_hdr hdr = make_esps_sd_hdr();
1584 enum EST_write_status rv;
1587 hdr->num_records = num_samples;
1588 switch (sample_type)
1590 case st_short: esps_type = ESPS_SHORT;
break;
1591 case st_schar: esps_type = ESPS_CHAR;
break;
1592 case st_int: esps_type = ESPS_INT;
break;
1593 case st_float: esps_type = ESPS_FLOAT;
break;
1594 case st_double: esps_type = ESPS_DOUBLE;
break;
1596 fprintf(stderr,
"ESPS file: no support for sample_type %s\n",
1597 sample_type_to_str(sample_type));
1598 return misc_write_error;
1601 add_field(hdr,
"samples",esps_type,num_channels);
1603 add_fea_special(hdr,ESPS_FEA_DIRECTORY,
"margo:/disk/disk10/home/awb/projects/speech_tools/main");
1604 add_fea_special(hdr,ESPS_FEA_COMMAND,
1605 "EDST waveform written as ESPS FEA_SD.\n\
1607 add_fea_d(hdr,
"start_time",0,(
double)0);
1608 add_fea_d(hdr,
"record_freq",0,(
double)sample_rate);
1609 add_fea_d(hdr,
"max_value",0,(
double)27355);
1611 if ((rv=write_esps_hdr(hdr,fp)) != write_ok)
1613 delete_esps_hdr(hdr);
1617 delete_esps_hdr(hdr);
1622enum EST_write_status save_wave_sd_data(FILE *fp,
const short *data,
1624 int num_samples,
int num_channels,
1626 enum EST_sample_type_t sample_type,
int bo)
1632 return save_raw_data(fp,data,offset,num_samples,num_channels,
1633 sample_type,EST_NATIVE_BO);
1636enum EST_write_status save_wave_sd(FILE *fp,
const short *data,
int offset,
1637 int num_samples,
int num_channels,
1639 enum EST_sample_type_t sample_type,
int bo)
1642 save_wave_sd_header(fp, num_samples, num_channels, sample_rate,
1644 return save_wave_sd_data(fp, data, offset, num_samples,
1645 num_channels, sample_rate, sample_type, bo);
1656enum EST_read_status load_wave_raw(
EST_TokenStream &ts,
short **data,
int
1657 *num_samples,
int *num_channels,
1660 enum EST_sample_type_t *sample_type,
1661 int *bo,
int offset,
int length,
1663 enum EST_sample_type_t isample_type,
1666 unsigned char *file_data;
1667 int data_length,samps,sample_width;
1671 if (isample_type == st_ascii)
1674 if ((offset != 0) || (length != 0))
1676 fprintf(stderr,
"Load ascii wave: doesn't support offsets and lengths\n");
1677 return misc_read_error;
1681 guess = (int)(1.2*ts.
tell()/7)+10;
1683 *data = walloc(
short, guess);
1687 samp = atoi(ts.
get().string());
1690 ndata = walloc(
short,(
int)(guess*1.2));
1691 memmove(ndata,*data,guess*
sizeof(
short));
1694 guess = (int)(guess*1.2);
1698 fprintf(stderr,
"Load ascii wave: sample %d underflow clipping\n",
1700 (*data)[i] = -32768;
1702 else if (samp > 32767)
1704 fprintf(stderr,
"Load ascii wave: sample %d overflow clipping\n",
1709 (*data)[i] = (short)samp;
1717 sample_width = get_word_size(isample_type);
1718 samps = ts.
tell()/sample_width;
1721 data_length = samps - offset;
1723 data_length = length;
1725 file_data = walloc(
unsigned char, data_length * sample_width *inc);
1726 ts.
seek(offset*sample_width*inc);
1727 if ((
int)ts.
fread(file_data,sample_width,data_length) != data_length)
1728 return misc_read_error;
1730 *data = convert_raw_data(file_data,data_length,isample_type,ibo);
1733 *num_samples = data_length/inc;
1734 *sample_rate = isample_rate;
1735 *num_channels = inc;
1736 *sample_type = st_short;
1738 *bo = EST_NATIVE_BO;
1743enum EST_write_status save_wave_raw_header(FILE *fp,
1744 int num_samples,
int num_channels,
1746 enum EST_sample_type_t sample_type,
int bo)
1751enum EST_write_status save_wave_raw_data(FILE *fp,
const short *data,
1753 int num_samples,
int num_channels,
1755 enum EST_sample_type_t sample_type,
int bo)
1760 return save_raw_data(fp,data,offset,num_samples,num_channels,
1764enum EST_write_status save_wave_raw(FILE *fp,
const short *data,
1766 int num_samples,
int num_channels,
1768 enum EST_sample_type_t sample_type,
int bo)
1772 return save_wave_raw_data(fp, data, offset, num_samples,
1773 num_channels, sample_rate, sample_type, bo);
1782enum EST_write_status wave_io_save_header(FILE *fp,
1783 const int num_samples,
const int num_channels,
1784 const int sample_rate,
1788 EST_WaveFileType t = EST_WaveFile::map.token(ftype);
1789 EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
1793 return save_wave_nist_header(fp, num_samples, num_channels,
1794 sample_rate, sample_type, bo);
1797 return save_wave_sd_header(fp, num_samples, num_channels,
1798 sample_rate, sample_type, bo);
1801 return save_wave_est_header(fp, num_samples, num_channels,
1802 sample_rate, sample_type, bo);
1805 return save_wave_audlab_header(fp, num_samples, num_channels,
1806 sample_rate, sample_type, bo);
1809 return save_wave_snd_header(fp, num_samples, num_channels,
1810 sample_rate, sample_type, bo);
1813 return save_wave_aiff_header(fp, num_samples, num_channels,
1814 sample_rate, sample_type, bo);
1817 return save_wave_riff_header(fp, num_samples, num_channels,
1818 sample_rate, sample_type, bo);
1821 return save_wave_raw_header(fp, num_samples, num_channels,
1822 sample_rate, sample_type, bo);
1825 return save_wave_ulaw_header(fp, num_samples, num_channels,
1826 sample_rate, sample_type, bo);
1830 cerr <<
"Can't save wave header to files type " << ftype << endl;
1837enum EST_write_status wave_io_save_data(FILE *fp,
const short * data,
1839 const int num_samples,
const int num_channels,
1840 const int sample_rate,
1844 EST_WaveFileType t = EST_WaveFile::map.token(ftype);
1845 EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
1849 return save_wave_nist_data(fp, data, offset, num_samples, num_channels,
1850 sample_rate, sample_type, bo);
1853 return save_wave_sd_data(fp, data, offset, num_samples, num_channels,
1854 sample_rate, sample_type, bo);
1857 return save_wave_est_data(fp, data, offset, num_samples, num_channels,
1858 sample_rate, sample_type, bo);
1861 return save_wave_audlab_data(fp, data, offset, num_samples, num_channels,
1862 sample_rate, sample_type, bo);
1865 return save_wave_snd_data(fp, data, offset, num_samples, num_channels,
1866 sample_rate, sample_type, bo);
1869 return save_wave_aiff_data(fp, data, offset, num_samples, num_channels,
1870 sample_rate, sample_type, bo);
1873 return save_wave_riff_data(fp, data, offset, num_samples, num_channels,
1874 sample_rate, sample_type, bo);
1877 return save_wave_raw_data(fp, data, offset, num_samples, num_channels,
1878 sample_rate, sample_type, bo);
1881 return save_wave_ulaw_data(fp, data, offset, num_samples, num_channels,
1882 sample_rate, sample_type, bo);
1886 cerr <<
"Can't save wave data to files type " << ftype << endl;
int ival(const EST_String &rkey, int m=1) const
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
const int present(const K &rkey) const
Returns true if key is present.
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don't use peek() immediately beforehand)
const EST_String filename() const
The originating filename (if there is one)
const EST_String pos_description()
A string describing current position, suitable for error messages.
void close(void)
Close stream.
int tell(void) const
tell, synonym for filepos
int open(const EST_String &filename)
open a \Ref{EST_TokenStream} for a file.
FILE * filedescriptor()
For the people who need the actual description (if possible)
EST_TokenStream & get(EST_Token &t)
get next token in stream
int seek(int position)
seek, reposition file pointer