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) {
125 fprintf(stderr,
"Unknown sample type for nist");
131 enum EST_sample_type_t nist_to_sample_type(
char *type)
133 if ((streq(type,
"pcm")) ||
134 (streq(type,
"PCM")) ||
135 (streq(type,
"pcm-2")))
137 if (strcmp(type,
"pcm,embedded-shorten-v1.1") == 0)
139 else if ((EST_strcasecmp(type,
"ULAW",NULL) == 0) ||
140 (EST_strcasecmp(type,
"U-LAW",NULL) == 0) ||
141 (EST_strcasecmp(type,
"mu-law",NULL) == 0) ||
142 (EST_strcasecmp(type,
"mulaw",NULL) == 0))
144 else if (strcmp(type,
"alaw") == 0)
146 else if (strcmp(type,
"PCM-1") == 0)
148 else if (strcmp(type,
"PCM-4") == 0)
150 else if (strcmp(type,
"REAL") == 0)
155 fprintf(stderr,
"NIST: unknown sample type: %s\n",type);
160 enum EST_read_status load_wave_nist(
EST_TokenStream &ts,
short **data,
int
161 *num_samples,
int *num_channels,
int
162 *word_size,
int *sample_rate,
enum
163 EST_sample_type_t *sample_type,
int *bo ,
int
167 char header[NIST_HDR_SIZE];
168 int samps,sample_width,data_length,actual_bo;
169 unsigned char *file_data;
170 enum EST_sample_type_t actual_sample_type;
171 char *byte_order, *sample_coding;
175 current_pos = ts.
tell();
176 if (ts.
fread(header,NIST_HDR_SIZE,1) != 1)
179 if (strncmp(header,NIST_SIG,strlen(NIST_SIG)) != 0)
182 samps = nist_get_param_int(header,
"sample_count",-1);
183 *num_channels = nist_get_param_int(header,
"channel_count",1);
184 sample_width = nist_get_param_int(header,
"sample_n_bytes",2);
186 nist_get_param_int(header,
"sample_rate",def_load_sample_rate);
187 byte_order = nist_get_param_str(header,
"sample_byte_format",
188 (EST_BIG_ENDIAN ?
"10" :
"01"));
189 sample_coding = nist_get_param_str(header,
"sample_coding",
"pcm");
190 if (streq(byte_order,
"mu-law"))
192 byte_order = wstrdup((EST_BIG_ENDIAN ?
"10" :
"01"));
193 sample_coding = wstrdup(
"ULAW");
203 if (streq(sample_coding,
"pcm,embedded-shorten-v1.1"))
205 char *tmpfile, *cmdstr;
206 enum EST_read_status rval;
208 tmpfile = cmake_tmp_filename();
209 cmdstr = walloc(
char,strlen(tmpfile)+200);
210 sprintf(cmdstr,
"cstrshorten %s %s",
211 (
const char*)ts.
filename(),tmpfile);
212 printf(
"Command: %s\n", cmdstr);
217 rval = load_wave_nist(tt, data, num_samples,
218 num_channels, word_size, sample_rate,
219 sample_type, bo, offset, length);
228 data_length = (samps - offset)*(*num_channels);
230 data_length = length*(*num_channels);
232 file_data = walloc(
unsigned char,sample_width * data_length);
234 ts.
seek(current_pos+NIST_HDR_SIZE+(sample_width*offset*(*num_channels)));
236 n = ts.
fread(file_data,sample_width,data_length);
238 if ((n < 1) && (n != data_length))
241 wfree(sample_coding);
243 return misc_read_error;
245 else if ((n < data_length) && (data_length/(*num_channels) == n))
247 fprintf(stderr,
"WAVE read: nist header is (probably) non-standard\n");
248 fprintf(stderr,
"WAVE read: assuming different num_channel interpretation\n");
251 else if (n < data_length)
253 fprintf(stderr,
"WAVE read: short file %s\n",
255 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
256 offset,n,data_length);
260 actual_sample_type = nist_to_sample_type(sample_coding);
261 actual_bo = ((strcmp(byte_order,
"10") == 0) ? bo_big : bo_little);
263 *data = convert_raw_data(file_data,data_length,
264 actual_sample_type,actual_bo);
266 *num_samples = data_length/ (*num_channels);
267 *sample_type = st_short;
270 wfree(sample_coding);
276 enum EST_write_status save_wave_nist(FILE *fp,
const short *data,
int offset,
277 int num_samples,
int num_channels,
279 enum EST_sample_type_t sample_type,
int bo)
281 char h[1024], p[1024];
287 sprintf(p,
"channel_count -i %d\n", num_channels);
289 sprintf(p,
"sample_count -i %d\n", num_samples);
291 sprintf(p,
"sample_rate -i %d\n", sample_rate);
294 t = sample_type_to_nist(sample_type);
297 sprintf(p,
"sample_coding -s%d %s\n", (
signed)strlen(t), t);
299 sprintf(p,
"sample_n_bytes -i %d\n", get_word_size(sample_type));
303 if (get_word_size(sample_type) > 1)
305 sprintf(p,
"sample_byte_format -s%d %s\n", 2,
306 ((bo == bo_big) ?
"10" :
"01"));
310 strcat(h, NIST_END_SIG);
312 strcat(h,
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
314 if (fwrite(&h, 1024, 1, fp) != 1)
315 return misc_write_error;
317 return save_raw_data(fp,data,offset,num_samples,num_channels,
326 enum EST_read_status load_wave_est(
EST_TokenStream &ts,
short **data,
int
327 *num_samples,
int *num_channels,
int
328 *word_size,
int *sample_rate,
enum
329 EST_sample_type_t *sample_type,
int *bo,
330 int offset,
int length)
332 int data_length, actual_bo;
340 EST_sample_type_t actual_sample_type;
344 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
346 if (t != est_file_wave)
347 return misc_read_error;
349 *num_samples = hinfo.
ival(
"NumSamples");
350 *num_channels = hinfo.
ival(
"NumChannels");
351 *sample_rate = hinfo.
ival(
"SampleRate");
353 byte_order = hinfo.
val(
"ByteOrder");
356 data_length = (*num_samples)*(*num_channels);
358 data_length = length*(*num_channels);
360 file_data = walloc(
short, data_length);
362 n = ts.
fread(file_data,
sizeof(
short), data_length);
363 if ((n != data_length) && (n < 1))
366 cerr <<
"failed to read file\n";
368 return misc_read_error;
370 else if (n != data_length)
372 cerr <<
"Wrong number of samples/channels in EST wave file\n";
374 cerr <<
"expected " << data_length <<
" got " << n << endl;
378 actual_bo = (byte_order ==
"10") ? bo_big : bo_little;
379 if (hinfo.
present(
"SampleType"))
380 actual_sample_type = str_to_sample_type(hinfo.
val(
"SampleType"));
382 actual_sample_type = st_short;
384 *data = convert_raw_data((
unsigned char *)file_data,
385 data_length, actual_sample_type, actual_bo);
387 *sample_type = st_short;
394 enum EST_write_status save_wave_est(FILE *fp,
const short *data,
int offset,
395 int num_samples,
int num_channels,
397 enum EST_sample_type_t sample_type,
int bo)
399 fprintf(fp,
"EST_File wave\n");
400 fprintf(fp,
"DataType binary\n");
401 fprintf(fp,
"SampleRate %d\n", sample_rate);
402 fprintf(fp,
"NumSamples %d\n", num_samples);
403 fprintf(fp,
"NumChannels %d\n", num_channels);
404 fprintf(fp,
"SampleType %s\n", sample_type_to_str(sample_type));
405 if (get_word_size(sample_type) > 1)
406 fprintf(fp,
"ByteOrder %s\n", ((bo == bo_big) ?
"10" :
"01"));
408 fprintf(fp,
"EST_Header_End\n");
410 return save_raw_data(fp, data, offset, num_samples, num_channels,
426 #define WAVE_FORMAT_PCM 0x0001
427 #define WAVE_FORMAT_ADPCM 0x0002
428 #define WAVE_FORMAT_ALAW 0x0006
429 #define WAVE_FORMAT_MULAW 0x0007
431 enum EST_read_status load_wave_riff(
EST_TokenStream &ts,
short **data,
int
432 *num_samples,
int *num_channels,
int
433 *word_size,
int *sample_rate,
enum
434 EST_sample_type_t *sample_type,
int *bo ,
int
438 int samps,sample_width,data_length;
441 unsigned char *file_data;
442 enum EST_sample_type_t actual_sample_type;
444 if (ts.
fread(info,
sizeof(
char),4) != 4)
446 if (strncmp(info,
"RIFF",4) != 0)
450 ts.
fread(&dsize,4,1);
452 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
453 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
454 (strncmp(info,
"WAVE",4) != 0))
456 fprintf(stderr,
"RIFF file is not of type WAVE\n");
457 return misc_read_error;
459 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
460 (strncmp(info,
"fmt ",4) != 0))
461 return misc_read_error;
463 ts.
fread(&dsize,4,1);
464 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
465 ts.
fread(&shortdata,2,1);
466 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
471 case WAVE_FORMAT_PCM:
472 actual_sample_type = st_short;
break;
474 case WAVE_FORMAT_MULAW:
475 actual_sample_type = st_mulaw;
break;
476 case WAVE_FORMAT_ADPCM:
477 fprintf(stderr,
"RIFF file: unsupported proprietary sample format ADPCM\n");
478 actual_sample_type = st_short;
481 case WAVE_FORMAT_ALAW:
483 fprintf(stderr,
"RIFF file: unknown sample format\n");
484 actual_sample_type = st_short;
487 ts.
fread(&shortdata,2,1);
488 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
489 *num_channels = shortdata;
490 ts.
fread(sample_rate,4,1);
491 if (EST_BIG_ENDIAN) *sample_rate = SWAPINT(*sample_rate);
492 ts.
fread(&intdata,4,1);
493 if (EST_BIG_ENDIAN) intdata = SWAPINT(intdata);
494 ts.
fread(&shortdata,2,1);
495 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
496 ts.
fread(&shortdata,2,1);
497 if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
499 sample_width = (shortdata+7)/8;
500 if ((sample_width == 1) && (actual_sample_type == st_short))
501 actual_sample_type = st_uchar;
506 if (ts.
fread(info,
sizeof(
char),4) != 4)
508 fprintf(stderr,
"RIFF file truncated\n");
509 return misc_read_error;
511 if (strncmp(info,
"data",4) == 0)
513 ts.
fread(&samps,4,1);
514 if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
515 samps /= (sample_width*(*num_channels));
518 else if (strncmp(info,
"fact",4) == 0)
520 ts.
fread(&samps,4,1);
521 if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
530 ts.
fread(&dsize,4,1);
531 if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
536 data_length = (samps - offset)*(*num_channels);
538 data_length = length*(*num_channels);
540 file_data = walloc(
unsigned char,sample_width * data_length);
542 ts.
seek((sample_width*offset*(*num_channels))+ts.
tell());
543 if ((dsize=ts.
fread(file_data,sample_width,data_length)) != data_length)
548 fprintf(stderr,
"Unexpected end of file but continuing (apparently missing %d samples)\n",data_length-dsize);
551 fprintf(stderr,
"Unexpected end of file: (missing %d samples)\n",data_length-dsize);
553 return misc_read_error;
557 *data = convert_raw_data(file_data,dsize,
558 actual_sample_type, bo_little);
560 *num_samples = dsize / (*num_channels);
561 *sample_type = st_short;
568 enum EST_write_status save_wave_riff(FILE *fp,
const short *data,
int offset,
569 int num_samples,
int num_channels,
571 enum EST_sample_type_t sample_type,
int bo)
575 int data_size, data_int;
578 if (sample_type == st_schar)
580 EST_warning(
"RIFF format: Signed 8-bit not allowed by this file format");
581 sample_type=st_uchar;
584 info =
"RIFF"; fwrite(info,4,1,fp);
585 data_size = num_channels*num_samples*get_word_size(sample_type)+ 8+16+12;
587 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
588 fwrite(&data_size,1,4,fp);
589 info =
"WAVE"; fwrite(info,4,1,fp);
590 info =
"fmt "; fwrite(info,4,1,fp);
592 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
593 fwrite(&data_size,1,4,fp);
596 case st_short: data_short = WAVE_FORMAT_PCM;
break;
597 case st_uchar: data_short = WAVE_FORMAT_PCM;
break;
598 case st_mulaw: data_short = WAVE_FORMAT_MULAW;
break;
599 case st_alaw: data_short = WAVE_FORMAT_ALAW;
break;
600 case st_adpcm: data_short = WAVE_FORMAT_ADPCM;
break;
602 fprintf(stderr,
"RIFF format: unsupported data format %d\n",
604 return misc_write_error;
606 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
607 fwrite(&data_short,1,2,fp);
608 data_short = num_channels;
609 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
610 fwrite(&data_short,1,2,fp);
611 data_int = sample_rate;
612 if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
613 fwrite(&data_int,1,4,fp);
614 data_int = sample_rate * num_channels * get_word_size(sample_type);
615 if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
616 fwrite(&data_int,1,4,fp);
617 data_short = num_channels * get_word_size(sample_type);
618 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
619 fwrite(&data_short,1,2,fp);
620 data_short = get_word_size(sample_type) * 8;
621 if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
622 fwrite(&data_short,1,2,fp);
623 info =
"data"; fwrite(info,4,1,fp);
624 data_size = num_channels*num_samples*get_word_size(sample_type);
625 if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
626 fwrite(&data_size,1,4,fp);
628 return save_raw_data(fp,data,offset,num_samples,num_channels,
629 sample_type,bo_little);
649 enum EST_read_status load_wave_aiff(
EST_TokenStream &ts,
short **data,
int
650 *num_samples,
int *num_channels,
int
651 *word_size,
int *sample_rate,
enum
652 EST_sample_type_t *sample_type,
int *bo ,
int
656 struct AIFFchunk chunk;
660 unsigned char ieee_ext_sample_rate[10];
661 struct AIFFssnd ssndchunk;
662 enum EST_sample_type_t actual_sample_type;
663 int dsize,data_length,n;
664 unsigned char *file_data;
666 if (ts.
fread(info,
sizeof(
char),4) != 4)
668 if (strncmp(info,
"FORM",4) != 0)
672 ts.
fread(&dsize,4,1);
673 if (EST_LITTLE_ENDIAN)
674 dsize = SWAPINT(dsize);
675 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
676 (strncmp(info,
"AIFF",4) != 0))
678 fprintf(stderr,
"AIFF file does not have AIFF chunk\n");
679 return misc_read_error;
682 for ( ; ts.
fread(&chunk,1,
sizeof(chunk)) ==
sizeof(chunk) ; )
684 if (EST_LITTLE_ENDIAN)
685 chunk.size = SWAPINT(chunk.size);
686 if (strncmp(chunk.id,
"COMM",4) == 0)
688 if (chunk.size != 18)
690 fprintf(stderr,
"AIFF chunk: bad size\n");
691 return misc_read_error;
693 ts.
fread(&comm_channels,1,
sizeof(
short));
694 ts.
fread(&comm_samples,1,
sizeof(
int));
695 ts.
fread(&comm_bits,1,
sizeof(
short));
696 if (ts.
fread(ieee_ext_sample_rate,1,10) != 10)
698 fprintf(stderr,
"AIFF chunk: eof within COMM chunk\n");
699 return misc_read_error;
701 if (EST_LITTLE_ENDIAN)
703 comm_channels = SWAPSHORT(comm_channels);
704 comm_samples = SWAPINT(comm_samples);
705 comm_bits = SWAPSHORT(comm_bits);
707 *sample_rate = (int)ConvertFromIeeeExtended(ieee_ext_sample_rate);
709 else if (strncmp(chunk.id,
"SSND",4) == 0)
711 if (ts.
fread(&ssndchunk,1,
sizeof(ssndchunk)) !=
sizeof(ssndchunk))
713 fprintf(stderr,
"AIFF chunk: eof within SSND chunk\n");
714 return misc_read_error;
716 if (EST_LITTLE_ENDIAN)
718 ssndchunk.offset = SWAPINT(ssndchunk.offset);
719 ssndchunk.blocksize = SWAPINT(ssndchunk.blocksize);
722 *num_channels = comm_channels;
725 case 8: actual_sample_type = st_uchar;
break;
726 case 16: actual_sample_type = st_short;
break;
728 fprintf(stderr,
"AIFF: unsupported sample width %d bits\n",
730 return misc_read_error;
733 ts.
seek(ssndchunk.offset+(comm_channels*offset)+ts.
tell());
735 data_length = (comm_samples-offset)*comm_channels;
737 data_length = length*comm_channels;
738 file_data = walloc(
unsigned char,
739 data_length*comm_channels*
740 get_word_size(actual_sample_type));
741 if ((n=ts.
fread(file_data,get_word_size(actual_sample_type),
742 data_length)) != data_length)
744 fprintf(stderr,
"AIFF read: short file %s\n",
746 fprintf(stderr,
"AIFF read: at %d got %d instead of %d samples\n",
747 offset,n,data_length);
751 *data = convert_raw_data(file_data,data_length,
752 actual_sample_type,bo_big);
753 *num_samples = data_length/comm_channels;
754 *sample_type = st_short;
768 enum EST_write_status save_wave_aiff(FILE *fp,
const short *data,
int offset,
769 int num_samples,
int num_channels,
771 enum EST_sample_type_t sample_type,
int bo)
775 int data_size, data_int;
776 unsigned char ieee_ext_buf[10];
782 data_size = 54+(num_samples*num_channels*get_word_size(sample_type));
783 if (EST_LITTLE_ENDIAN)
784 data_size = SWAPINT(data_size);
785 fwrite(&data_size,1,4,fp);
791 if (EST_LITTLE_ENDIAN)
792 data_int = SWAPINT(data_int);
793 fwrite(&data_int,1,4,fp);
794 data_short = num_channels;
795 if (EST_LITTLE_ENDIAN)
796 data_short = SWAPSHORT(data_short);
797 fwrite(&data_short,1,2,fp);
798 data_int = num_samples;
799 if (EST_LITTLE_ENDIAN)
800 data_int = SWAPINT(data_int);
801 fwrite(&data_int,1,4,fp);
802 data_short = 8*get_word_size(sample_type);
803 if (EST_LITTLE_ENDIAN)
804 data_short = SWAPSHORT(data_short);
805 fwrite(&data_short,1,2,fp);
806 ConvertToIeeeExtended((
double)sample_rate,ieee_ext_buf);
807 fwrite(ieee_ext_buf,1,10,fp);
810 data_int = 8 + (num_samples*num_channels*get_word_size(sample_type));
811 if (EST_LITTLE_ENDIAN)
812 data_int = SWAPINT(data_int);
813 fwrite(&data_int,1,4,fp);
815 if (EST_LITTLE_ENDIAN)
816 data_int = SWAPINT(data_int);
817 fwrite(&data_int,1,4,fp);
818 if (EST_LITTLE_ENDIAN)
819 data_int = SWAPINT(data_int);
820 fwrite(&data_int,1,4,fp);
822 if ((sample_type == st_short) ||
823 (sample_type == st_uchar))
824 return save_raw_data(fp,data,offset,num_samples,num_channels,
828 fprintf(stderr,
"AIFF: requested data type not uchar or short\n");
829 return misc_write_error;
838 enum EST_read_status load_wave_ulaw(
EST_TokenStream &ts,
short **data,
int
839 *num_samples,
int *num_channels,
int *word_size,
int
840 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
841 int offset,
int length)
845 int data_length,samps;
851 data_length = samps - offset;
853 data_length = length;
855 ulaw = walloc(
unsigned char, data_length);
857 if (ts.
fread(ulaw,1,data_length) != data_length)
860 return misc_read_error;
863 *data = walloc(
short,data_length);
864 ulaw_to_short(ulaw,*data,data_length);
867 *num_samples = data_length;
870 *sample_type = st_short;
877 enum EST_write_status save_wave_ulaw(FILE *fp,
const short *data,
int offset,
878 int num_samples,
int num_channels,
880 enum EST_sample_type_t sample_type,
int bo)
884 return save_wave_raw(fp,data,offset,num_samples,num_channels,
896 unsigned int hdr_size;
898 unsigned int encoding;
899 unsigned int sample_rate;
900 unsigned int channels;
903 enum EST_read_status load_wave_snd(
EST_TokenStream &ts,
short **data,
int
904 *num_samples,
int *num_channels,
int *word_size,
int
905 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo ,
906 int offset,
int length)
909 Sun_au_header header;
910 enum EST_sample_type_t encoding_type;
911 int data_length, sample_width, bytes, samps, n;
912 unsigned char *file_data;
915 current_pos = ts.
tell();
916 ts.
fread(&header,
sizeof(Sun_au_header), 1);
919 if ((EST_LITTLE_ENDIAN) &&
920 ((
unsigned int)0x2e736e64 == SWAPINT(header.magic)))
922 header.hdr_size = SWAPINT(header.hdr_size);
923 header.data_size = SWAPINT(header.data_size);
924 header.encoding = SWAPINT(header.encoding);
925 header.sample_rate = SWAPINT(header.sample_rate);
926 header.channels = SWAPINT(header.channels);
928 else if ((
unsigned int)0x2e736e64 != header.magic)
931 switch (header.encoding)
934 encoding_type = st_mulaw;
937 encoding_type = st_uchar;
940 encoding_type = st_short;
943 fprintf(stderr,
"Unsupported data type in SND header\n");
944 return misc_read_error;
947 *num_channels = header.channels;
948 sample_width = get_word_size(encoding_type);
949 *sample_rate = header.sample_rate;
951 if ((header.data_size == 0) ||
952 (header.data_size == -1))
955 bytes = ts.
tell() - header.hdr_size;
958 bytes = header.data_size;
959 samps = bytes/sample_width;
962 data_length = (samps - offset)*(*num_channels);
964 data_length = length *(*num_channels);
966 file_data = walloc(
unsigned char, sample_width * data_length);
967 ts.
seek(current_pos+header.hdr_size+(sample_width*offset*(*num_channels)));
968 if ((n=ts.
fread(file_data,sample_width,data_length)) != data_length)
970 fprintf(stderr,
"WAVE read: short file %s\n",
972 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
973 offset,n,data_length);
977 *data = convert_raw_data(file_data,data_length,encoding_type,bo_big);
982 *num_samples = data_length/ (*num_channels);
983 *sample_type = st_short;
989 enum EST_write_status save_wave_snd(FILE *fp,
const short *data,
int offset,
990 int num_samples,
int num_channels,
992 enum EST_sample_type_t sample_type,
int bo)
996 Sun_au_header header;
999 header.magic = (
unsigned int)0x2e736e64;
1000 header.hdr_size =
sizeof(header);
1001 header.data_size = get_word_size(sample_type) * num_channels * num_samples;
1003 switch (sample_type) {
1005 header.encoding = 1;
1008 header.encoding = 2;
1011 header.encoding = 3;
1016 "Unsupported sample type cannot be saved in SND format\n");
1017 return misc_write_error;
1023 header.sample_rate = sample_rate;
1025 header.channels = num_channels;
1027 if (EST_LITTLE_ENDIAN)
1030 header.magic = SWAPINT(header.magic);
1031 header.hdr_size = SWAPINT(header.hdr_size);
1032 header.data_size = SWAPINT(header.data_size);
1033 header.encoding = SWAPINT(header.encoding);
1034 header.sample_rate = SWAPINT(header.sample_rate);
1035 header.channels = SWAPINT(header.channels);
1039 if (fwrite(&header,
sizeof(header), 1, fp) != 1)
1040 return misc_write_error;
1043 return save_raw_data(fp,data,offset,num_samples,num_channels,
1044 sample_type,bo_big);
1097 enum EST_read_status load_wave_audlab(
EST_TokenStream &ts,
short **data,
int
1098 *num_samples,
int *num_channels,
int *word_size,
int
1099 *sample_rate,
enum EST_sample_type_t *sample_type,
int *bo,
int
1106 int data_length,sample_count;
1111 current_pos = ts.
tell();
1112 ts.
fread(&fh,
sizeof(
struct audlabfh), 1);
1113 if (strcmp(fh.file_type,
"Sample") != 0)
1114 return wrong_format;
1116 ts.
fread(&sh,
sizeof(
struct audlabsh), 1);
1117 ts.
fread(&sd,
sizeof(
struct audlabsd), 1);
1118 hdr_length =
sizeof(
struct audlabfh) +
1119 sizeof(struct audlabsh) +
1120 sizeof(struct audlabsd);
1124 *num_channels = sh.channel_count;
1125 *sample_rate = sh.sample_rate;
1126 sample_count = sd.sample_count;
1130 *num_channels = SWAPINT(sh.channel_count);
1131 *sample_rate = SWAPINT(sh.sample_rate);
1132 sample_count = SWAPINT(sd.sample_count);
1135 data_length = (sample_count - offset) * (*num_channels);
1137 data_length = length *(*num_channels);
1139 *data = walloc(
short,
sizeof(
short) * data_length);
1140 ts.
seek(current_pos+hdr_length+(
sizeof(
short)*offset*(*num_channels)));
1142 if ((
int)ts.
fread(*data,
sizeof(
short), data_length) != data_length)
1145 return misc_read_error;
1147 if (EST_LITTLE_ENDIAN)
1148 swap_bytes_short(*data,data_length);
1150 *num_samples = data_length / (*num_channels);
1151 *sample_type = st_short;
1152 *word_size =
sizeof(short);
1153 *bo = EST_NATIVE_BO;
1158 enum EST_write_status save_wave_audlab(FILE *fp,
const short *data,
int offset,
1159 int num_samples,
int num_channels,
1161 enum EST_sample_type_t sample_type,
int bo)
1170 fh.start =
sizeof (
struct audlabfh) +
1171 sizeof (struct audlabsh) + sizeof (struct audlabsd);
1173 strcpy(fh.file_type,
"Sample");
1175 if (EST_LITTLE_ENDIAN)
1177 sh.channel_count = SWAPINT(num_channels);
1179 sh.sample_rate = SWAPINT(sample_rate);
1181 sd.sample_count = SWAPINT(num_samples);
1182 sd.nbits = SWAPINT(16);
1186 sh.channel_count = num_channels;
1188 sh.sample_rate = sample_rate;
1190 sd.sample_count = num_samples;
1193 sprintf(sd.descr,
"Filter 1");
1196 fwrite (&fh,
sizeof(fh), 1, fp);
1197 fwrite (&sh,
sizeof(sh), 1, fp);
1198 fwrite (&sd,
sizeof(sd), 1, fp);
1201 return save_raw_data(fp,data,offset,num_samples,num_channels,
1216 #include "esps_utils.h"
1217 enum EST_read_status load_wave_sd(
EST_TokenStream &ts,
short **data,
int
1218 *num_samples,
int *num_channels,
int
1219 *word_size,
int *sample_rate,
enum
1220 EST_sample_type_t *sample_type,
int *bo ,
int
1226 int actual_bo, sample_width, data_length;
1227 enum EST_read_status rv;
1229 enum EST_sample_type_t actual_sample_type;
1231 unsigned char *file_data;
1235 fprintf(stderr,
"Can't open esps file %s for reading\n",
1237 return misc_read_error;
1240 if ((rv=read_esps_hdr(&hdr,fd)) != format_ok)
1243 if (hdr->file_type != ESPS_SD)
1245 fprintf(stderr,
"ESPS file: not an FEA_SD file\n");
1246 delete_esps_hdr(hdr);
1247 return misc_read_error;
1250 if (fea_value_d(
"record_freq",0,hdr,&d) != 0)
1252 fprintf(stderr,
"ESPS file: can't find sample_rate in header assuming 16000\n");
1253 *sample_rate = 16000;
1256 *sample_rate = (int)d;
1257 actual_sample_type = st_short;
1258 sample_width = get_word_size(actual_sample_type);
1259 *num_channels = hdr->field_dimension[0];
1261 actual_bo = (EST_BIG_ENDIAN ? bo_little : bo_big);
1263 actual_bo = (EST_BIG_ENDIAN ? bo_big : bo_little);
1266 data_length = (hdr->num_records - offset)*(*num_channels);
1268 data_length = length *(*num_channels);
1270 file_data = walloc(
unsigned char, sample_width * data_length);
1271 fseek(fd,hdr->hdr_size+(sample_width*offset*(*num_channels)),
1273 if ((dl=fread(file_data,sample_width,data_length,fd)) != data_length)
1275 fprintf(stderr,
"WAVE read: esps short file %s\n",
1277 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1278 offset,dl,data_length);
1282 *data = convert_raw_data(file_data,data_length,
1286 *num_samples = data_length/ (*num_channels);
1287 *sample_type = st_short;
1288 *bo = EST_NATIVE_BO;
1290 delete_esps_hdr(hdr);
1295 enum EST_write_status save_wave_sd(FILE *fp,
const short *data,
int offset,
1296 int num_samples,
int num_channels,
1298 enum EST_sample_type_t sample_type,
int bo)
1302 esps_hdr hdr = make_esps_sd_hdr();
1303 enum EST_write_status rv;
1306 hdr->num_records = num_samples;
1307 switch (sample_type)
1309 case st_short: esps_type = ESPS_SHORT;
break;
1310 case st_schar: esps_type = ESPS_CHAR;
break;
1311 case st_int: esps_type = ESPS_INT;
break;
1312 case st_float: esps_type = ESPS_FLOAT;
break;
1313 case st_double: esps_type = ESPS_DOUBLE;
break;
1315 fprintf(stderr,
"ESPS file: no support for sample_type %s\n",
1316 sample_type_to_str(sample_type));
1317 return misc_write_error;
1320 add_field(hdr,
"samples",esps_type,num_channels);
1321 add_fea_special(hdr,ESPS_FEA_DIRECTORY,
"margo:/disk/disk10/home/awb/projects/speech_tools/main");
1322 add_fea_special(hdr,ESPS_FEA_COMMAND,
1323 "EDST waveform written as ESPS FEA_SD.\n\
1325 add_fea_d(hdr,
"start_time",0,(
double)0);
1326 add_fea_d(hdr,
"record_freq",0,(
double)sample_rate);
1327 add_fea_d(hdr,
"max_value",0,(
double)27355);
1329 if ((rv=write_esps_hdr(hdr,fp)) != write_ok)
1331 delete_esps_hdr(hdr);
1335 delete_esps_hdr(hdr);
1337 return save_raw_data(fp,data,offset,num_samples,num_channels,
1338 sample_type,EST_NATIVE_BO);
1348 enum EST_read_status load_wave_raw(
EST_TokenStream &ts,
short **data,
int
1349 *num_samples,
int *num_channels,
1352 enum EST_sample_type_t *sample_type,
1353 int *bo,
int offset,
int length,
1355 enum EST_sample_type_t isample_type,
1358 unsigned char *file_data;
1359 int data_length,samps,sample_width;
1363 if (isample_type == st_ascii)
1366 if ((offset != 0) || (length != 0))
1368 fprintf(stderr,
"Load ascii wave: doesn't support offets and lengths\n");
1369 return misc_read_error;
1373 guess = (int)(1.2*ts.
tell()/7)+10;
1375 *data = walloc(
short, guess);
1379 samp = atoi(ts.
get().string());
1382 ndata = walloc(
short,(
int)(guess*1.2));
1383 memmove(ndata,*data,guess*
sizeof(
short));
1386 guess = (int)(guess*1.2);
1390 fprintf(stderr,
"Load ascii wave: sample %d underflow clipping\n",
1392 (*data)[i] = -32768;
1394 else if (samp > 32767)
1396 fprintf(stderr,
"Load ascii wave: sample %d overflow clipping\n",
1401 (*data)[i] = (short)samp;
1409 sample_width = get_word_size(isample_type);
1410 samps = ts.
tell()/sample_width;
1413 data_length = samps - offset;
1415 data_length = length;
1417 file_data = walloc(
unsigned char, data_length * sample_width *inc);
1418 ts.
seek(offset*sample_width*inc);
1419 if ((
int)ts.
fread(file_data,sample_width,data_length) != data_length)
1420 return misc_read_error;
1422 *data = convert_raw_data(file_data,data_length,isample_type,ibo);
1425 *num_samples = data_length/inc;
1426 *sample_rate = isample_rate;
1427 *num_channels = inc;
1428 *sample_type = st_short;
1430 *bo = EST_NATIVE_BO;
1435 enum EST_write_status save_wave_raw(FILE *fp,
const short *data,
1437 int num_samples,
int num_channels,
1439 enum EST_sample_type_t sample_type,
int bo)
1443 return save_raw_data(fp,data,offset,num_samples,num_channels,