59 static int def_load_sample_rate = 16000;
71 static const char *NIST_SIG =
"NIST_1A\n 1024\n";
72 static const char *NIST_END_SIG =
"end_head\n";
73 #define NIST_HDR_SIZE 1024 80 if (((p=strstr(hdr,field)) !=
NULL) &&
81 (strncmp(
" -i ",p+strlen(field),4) == 0))
83 sscanf(p+strlen(field)+4,
"%d",&val);
97 if (((p=strstr(hdr,field)) !=
NULL) &&
98 (strncmp(
" -s",p+strlen(field),3) == 0))
100 sscanf(p+strlen(field)+3,
"%d",&size);
101 val =
walloc(
char,size+1);
103 sscanf(p+strlen(field)+3,
"%d %s",&size,val);
115 switch (sample_type) {
133 fprintf(stderr,
"Unknown sample type for nist");
141 if ((
streq(type,
"pcm")) ||
142 (
streq(type,
"PCM")) ||
143 (
streq(type,
"pcm-2")))
145 if (strcmp(type,
"pcm,embedded-shorten-v1.1") == 0)
155 else if (strcmp(type,
"alaw") == 0)
157 else if (strcmp(type,
"PCM-1") == 0)
159 else if (strcmp(type,
"PCM-4") == 0)
161 else if (strcmp(type,
"REAL") == 0)
166 fprintf(stderr,
"NIST: unknown sample type: %s\n",type);
172 *num_samples,
int *num_channels,
int 173 *word_size,
int *sample_rate,
enum 179 int samps,sample_width,data_length,actual_bo;
181 unsigned char *file_data;
183 char *byte_order, *sample_coding=0;
187 current_pos = ts.
tell();
191 if (strncmp(header,NIST_SIG,strlen(NIST_SIG)) != 0)
201 if (
streq(byte_order,
"mu-law"))
205 sample_coding =
wstrdup(
"ULAW");
207 else if (
streq(byte_order,
"a-law"))
211 sample_coding =
wstrdup(
"ALAW");
223 if (
streq(sample_coding,
"pcm,embedded-shorten-v1.1"))
225 char *tmpfile, *cmdstr;
229 cmdstr =
walloc(
char,strlen(tmpfile)+200);
230 sprintf(cmdstr,
"cstrshorten %s %s",
231 (
const char*)ts.
filename(),tmpfile);
232 printf(
"Command: %s\n", cmdstr);
233 system_return = system(cmdstr);
234 if (system_return != 0)
236 fprintf(stderr,
"Command failed. Could not read nist file\n");
237 fprintf(stderr,
"To read embedded-shorten-v1.1 nist files, \n");
238 fprintf(stderr,
"shorten utility from Tony Robinson is required\n");
239 wfree(sample_coding);
246 if (tt.
open(tmpfile) < 0) {
247 fprintf(stderr,
"Could not open %s\n", tmpfile);
248 wfree(sample_coding);
256 num_channels, word_size, sample_rate,
257 sample_type, bo, offset, length);
263 wfree(sample_coding);
268 data_length = (samps - offset)*(*num_channels);
270 data_length = length*(*num_channels);
272 if (ts.
seek(current_pos+
NIST_HDR_SIZE+(sample_width*offset*(*num_channels))) != 0) {
273 fprintf(stderr,
"WAVE read: Could not seek in file. Read error");
274 wfree(sample_coding);
278 file_data =
walloc(
unsigned char,sample_width * data_length);
280 n = ts.
fread(file_data,sample_width,data_length);
282 if ((n < 1) && (n != data_length))
285 wfree(sample_coding);
289 else if ((n < data_length) && (data_length/(*num_channels) == n))
291 fprintf(stderr,
"WAVE read: nist header is (probably) non-standard\n");
292 fprintf(stderr,
"WAVE read: assuming different num_channel interpretation\n");
295 else if (n < data_length)
297 fprintf(stderr,
"WAVE read: short file %s\n",
299 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
300 offset,n,data_length);
308 actual_sample_type,actual_bo);
310 *num_samples = data_length/ (*num_channels);
314 wfree(sample_coding);
321 int num_samples,
int num_channels,
325 char h[1024], p[1024];
330 strncat(h, NIST_SIG, 512);
331 sprintf(p,
"channel_count -i %d\n", num_channels);
333 sprintf(p,
"sample_count -i %d\n", num_samples);
335 sprintf(p,
"sample_rate -i %d\n", sample_rate);
341 sprintf(p,
"sample_coding -s%d %s\n", (
signed)strlen(t), t);
343 sprintf(p,
"sample_n_bytes -i %d\n",
get_word_size(sample_type));
349 sprintf(p,
"sample_byte_format -s%d %s\n", 2,
350 ((bo ==
bo_big) ?
"10" :
"01"));
354 if (strlen(h)+ strlen(NIST_END_SIG) > 1024) {
357 strcat(h, NIST_END_SIG);
359 strcat(h,
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
361 if (fwrite(&h, 1024, 1, fp) != 1)
369 int num_samples,
int num_channels,
377 return save_raw_data(fp,data,offset,num_samples,num_channels,
383 int num_samples,
int num_channels,
388 sample_rate, sample_type, bo);
390 num_samples, num_channels,
391 sample_rate, sample_type, bo);
399 *num_samples,
int *num_channels,
int 400 *word_size,
int *sample_rate,
enum 402 int offset,
int length)
405 int data_length, actual_bo;
422 *num_samples = hinfo.
ival(
"NumSamples");
423 *num_channels = hinfo.
ival(
"NumChannels");
424 *sample_rate = hinfo.
ival(
"SampleRate");
426 byte_order = hinfo.
val(
"ByteOrder");
429 data_length = (*num_samples)*(*num_channels);
431 data_length = length*(*num_channels);
433 file_data =
walloc(
short, data_length);
435 n = ts.
fread(file_data,
sizeof(
short), data_length);
436 if ((n != data_length) && (n < 1))
439 cerr <<
"failed to read file\n";
443 else if (n != data_length)
445 cerr <<
"Wrong number of samples/channels in EST wave file\n";
447 cerr <<
"expected " << data_length <<
" got " << n << endl;
452 if (hinfo.
present(
"SampleType"))
458 data_length, actual_sample_type, actual_bo);
468 int num_samples,
int num_channels,
472 fprintf(fp,
"EST_File wave\n");
473 fprintf(fp,
"DataType binary\n");
474 fprintf(fp,
"SampleRate %d\n", sample_rate);
475 fprintf(fp,
"NumSamples %d\n", num_samples);
476 fprintf(fp,
"NumChannels %d\n", num_channels);
479 fprintf(fp,
"ByteOrder %s\n", ((bo ==
bo_big) ?
"10" :
"01"));
481 fprintf(fp,
"EST_Header_End\n");
486 int num_samples,
int num_channels,
494 return save_raw_data(fp, data, offset, num_samples, num_channels,
499 int num_samples,
int num_channels,
504 sample_rate, sample_type, bo);
507 num_samples, num_channels,
508 sample_rate, sample_type, bo);
522 #define WAVE_FORMAT_PCM 0x0001 523 #define WAVE_FORMAT_ADPCM 0x0002 524 #define WAVE_FORMAT_ALAW 0x0006 525 #define WAVE_FORMAT_MULAW 0x0007 528 *num_samples,
int *num_channels,
int 529 *word_size,
int *sample_rate,
enum 534 int samps,sample_width,data_length;
537 unsigned char *file_data;
540 if (ts.
fread(info,
sizeof(
char),4) != 4)
542 if (strncmp(info,
"RIFF",4) != 0)
550 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
551 (strncmp(info,
"WAVE",4) != 0))
553 fprintf(stderr,
"RIFF file is not of type WAVE\n");
556 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
557 (strncmp(info,
"fmt ",4) != 0))
569 actual_sample_type =
st_short;
break;
572 actual_sample_type =
st_mulaw;
break;
574 actual_sample_type =
st_alaw;
break;
576 fprintf(stderr,
"RIFF file: unsupported proprietary sample format ADPCM\n");
581 fprintf(stderr,
"RIFF file: unknown sample format\n");
587 *num_channels = shortdata;
597 sample_width = (shortdata+7)/8;
598 if ((sample_width == 1) && (actual_sample_type ==
st_short))
601 if (ts.
seek((dsize-16)+ts.
tell()) != 0) {
602 fprintf(stderr,
"Could not skip header. Read error");
607 if (ts.
fread(info,
sizeof(
char),4) != 4)
609 fprintf(stderr,
"RIFF file truncated\n");
612 if (strncmp(info,
"data",4) == 0)
616 samps /= (sample_width*(*num_channels));
619 else if (strncmp(info,
"fact",4) == 0)
623 if (ts.
seek(samps+ts.
tell()) != 0) {
624 fprintf(stderr,
"Could not seek in file. Read error");
636 if (ts.
seek(dsize+ts.
tell()) != 0) {
637 fprintf(stderr,
"Wav file: Read error");
643 data_length = (samps - offset)*(*num_channels);
645 data_length = length*(*num_channels);
647 if (ts.
seek((sample_width*offset*(*num_channels))+ts.
tell()) != 0) {
648 fprintf(stderr,
"Read error\n");
651 if (data_length < 0) {
652 fprintf(stderr,
"Read error\n");
656 fprintf(stderr,
"Read error: Data length too big\n");
659 file_data =
walloc(
unsigned char,sample_width * data_length);
660 if ((dsize=ts.
fread(file_data,sample_width,data_length)) != data_length)
665 fprintf(stderr,
"Unexpected end of file but continuing (apparently missing %d samples)\n",data_length-dsize);
668 fprintf(stderr,
"Unexpected end of file: (missing %d samples)\n",data_length-dsize);
677 *num_samples = dsize / (*num_channels);
686 int num_channels,
int sample_rate,
691 int data_size, data_int;
696 EST_warning(
"RIFF format: Signed 8-bit not allowed by this file format");
700 info =
"RIFF"; fwrite(info,4,1,fp);
701 data_size = num_channels*num_samples*
get_word_size(sample_type)+ 8+16+12;
704 fwrite(&data_size,1,4,fp);
705 info =
"WAVE"; fwrite(info,4,1,fp);
706 info =
"fmt "; fwrite(info,4,1,fp);
709 fwrite(&data_size,1,4,fp);
718 fprintf(stderr,
"RIFF format: unsupported data format %d\n",
723 fwrite(&data_short,1,2,fp);
724 data_short = num_channels;
726 fwrite(&data_short,1,2,fp);
727 data_int = sample_rate;
729 fwrite(&data_int,1,4,fp);
730 data_int = sample_rate * num_channels *
get_word_size(sample_type);
732 fwrite(&data_int,1,4,fp);
735 fwrite(&data_short,1,2,fp);
738 fwrite(&data_short,1,2,fp);
739 info =
"data"; fwrite(info,4,1,fp);
740 data_size = num_channels*num_samples*
get_word_size(sample_type);
742 fwrite(&data_size,1,4,fp);
748 int offset,
int num_samples,
int num_channels,
757 return save_raw_data(fp,data,offset,num_samples,num_channels,
763 int num_samples,
int num_channels,
771 num_channels, sample_rate, sample_type, bo);
793 *num_samples,
int *num_channels,
int 794 *word_size,
int *sample_rate,
enum 799 struct AIFFchunk chunk;
800 short comm_channels = -2;
803 unsigned char ieee_ext_sample_rate[10];
804 struct AIFFssnd ssndchunk;
806 int dsize,data_length,n;
807 unsigned char *file_data;
809 if (ts.
fread(info,
sizeof(
char),4) != 4)
811 if (strncmp(info,
"FORM",4) != 0)
818 if ((ts.
fread(info,
sizeof(
char),4) != 4) ||
819 (strncmp(info,
"AIFF",4) != 0))
821 fprintf(stderr,
"AIFF file does not have AIFF chunk\n");
825 for ( ; ts.
fread(&chunk,
sizeof(chunk), 1) == 1 ; )
828 chunk.size =
SWAPINT(chunk.size);
829 if (strncmp(chunk.id,
"COMM",4) == 0)
831 if (chunk.size != 18)
833 fprintf(stderr,
"AIFF chunk: bad size\n");
836 if (ts.
fread(&comm_channels,
sizeof(
short), 1) != 1)
838 if (ts.
fread(&comm_samples,
sizeof(
int), 1) != 1)
840 if (ts.
fread(&comm_bits,
sizeof(
short), 1) != 1)
842 if (ts.
fread(ieee_ext_sample_rate, 10, 1) != 1)
844 fprintf(stderr,
"AIFF chunk: eof within COMM chunk\n");
849 comm_channels =
SWAPSHORT(comm_channels);
850 comm_samples =
SWAPINT(comm_samples);
854 if (comm_channels < 0)
856 fprintf(stderr,
"AIFF chunk: Wrong comm channels\n");
860 else if (strncmp(chunk.id,
"SSND",4) == 0)
862 if (ts.
fread(&ssndchunk,
sizeof(ssndchunk), 1) != 1)
864 fprintf(stderr,
"AIFF chunk: eof within SSND chunk\n");
869 ssndchunk.offset =
SWAPINT(ssndchunk.offset);
870 ssndchunk.blocksize =
SWAPINT(ssndchunk.blocksize);
872 if (comm_channels < 0) {
873 fprintf(stderr,
"AIFF: COMM chunk missing\n");
876 *num_channels = comm_channels;
879 case 8: actual_sample_type =
st_uchar;
break;
880 case 16: actual_sample_type =
st_short;
break;
882 fprintf(stderr,
"AIFF: unsupported sample width %d bits\n",
887 if (ts.
seek(ssndchunk.offset+(comm_channels*offset)+ts.
tell()) != 0) {
888 fprintf(stderr,
"AIFF: Read error\n");
892 data_length = (comm_samples-offset)*comm_channels;
894 data_length = length*comm_channels;
896 if (data_length < 0 || comm_channels < 0 ||
get_word_size(actual_sample_type) < 0) {
897 fprintf(stderr,
"AIFF: Read error\n");
901 size_t bytes_to_read;
903 bytes_to_read = data_length * comm_channels;
905 fprintf(stderr,
"AIFF: Read error\n");
911 fprintf(stderr,
"AIFF: Read error\n");
922 file_data =
walloc(
unsigned char, bytes_to_read);
924 data_length)) != data_length)
926 fprintf(stderr,
"AIFF read: short file %s\n",
928 fprintf(stderr,
"AIFF read: at %d got %d instead of %d samples\n",
929 offset,n,data_length);
934 actual_sample_type,
bo_big);
935 *num_samples = data_length/comm_channels;
943 if (ts.
seek(ts.
tell()+chunk.size) != 0) {
944 fprintf(stderr,
"AIFF: Read error\n");
954 int num_samples,
int num_channels,
960 int data_size, data_int;
961 unsigned char ieee_ext_buf[10];
967 data_size = 54+(num_samples*num_channels*
get_word_size(sample_type));
969 data_size =
SWAPINT(data_size);
970 fwrite(&data_size,1,4,fp);
978 fwrite(&data_int,1,4,fp);
979 data_short = num_channels;
982 fwrite(&data_short,1,2,fp);
983 data_int = num_samples;
986 fwrite(&data_int,1,4,fp);
990 fwrite(&data_short,1,2,fp);
992 fwrite(ieee_ext_buf,1,10,fp);
995 data_int = 8 + (num_samples*num_channels*
get_word_size(sample_type));
998 fwrite(&data_int,1,4,fp);
1002 fwrite(&data_int,1,4,fp);
1005 fwrite(&data_int,1,4,fp);
1012 int num_samples,
int num_channels,
1021 return save_raw_data(fp,data, offset, num_samples, num_channels,
1025 fprintf(stderr,
"AIFF: requested data type not uchar or short\n");
1032 int num_samples,
int num_channels,
1037 sample_rate, sample_type, bo);
1040 num_samples, num_channels,
1041 sample_rate, sample_type, bo);
1049 *num_samples,
int *num_channels,
int *word_size,
int 1051 int offset,
int length)
1054 unsigned char *ulaw;
1055 int data_length,samps;
1061 data_length = samps - offset;
1063 data_length = length;
1065 if (ts.
seek(offset) != 0) {
1066 fprintf(stderr,
"ulaw: Read error\n");
1069 ulaw =
walloc(
unsigned char, data_length);
1070 if (ts.
fread(ulaw,1,data_length) != data_length)
1076 *data =
walloc(
short,data_length);
1080 *num_samples = data_length;
1081 *sample_rate = 8000;
1091 int num_samples,
int num_channels,
1099 (void) num_channels;
1105 int num_samples,
int num_channels,
1115 return save_wave_raw(fp,data,offset,num_samples,num_channels,
1120 int num_samples,
int num_channels,
1125 sample_rate, sample_type, bo);
1128 num_samples, num_channels,
1129 sample_rate, sample_type, bo);
1135 *num_samples,
int *num_channels,
int *word_size,
int 1137 int offset,
int length)
1140 unsigned char *alaw;
1141 int data_length,samps;
1147 data_length = samps - offset;
1149 data_length = length;
1151 if (ts.
seek(offset) != 0) {
1152 fprintf(stderr,
"alaw: Read error\n");
1155 alaw =
walloc(
unsigned char, data_length);
1156 if (ts.
fread(alaw,1,data_length) != data_length)
1162 *data =
walloc(
short,data_length);
1166 *num_samples = data_length;
1167 *sample_rate = 8000;
1177 int num_samples,
int num_channels,
1185 (void) num_channels;
1191 int num_samples,
int num_channels,
1199 return save_wave_raw(fp,data,offset,num_samples,num_channels,
1204 int num_samples,
int num_channels,
1209 sample_rate, sample_type, bo);
1212 num_samples, num_channels,
1213 sample_rate, sample_type, bo);
1223 unsigned int hdr_size;
1226 unsigned int sample_rate;
1227 unsigned int channels;
1231 *num_samples,
int *num_channels,
int *word_size,
int 1233 int offset,
int length)
1236 Sun_au_header header;
1238 int data_length, sample_width, bytes, samps, n;
1239 unsigned char *file_data;
1242 current_pos = ts.
tell();
1243 if (ts.
fread(&(header.magic),
sizeof(
unsigned int), 1) != 1) {
1246 if (ts.
fread(&(header.hdr_size),
sizeof(
unsigned int), 1) != 1) {
1249 if (ts.
fread(&(header.data_size),
sizeof(
int), 1) != 1) {
1252 if (ts.
fread(&(header.encoding),
sizeof(
unsigned int), 1) != 1) {
1255 if (ts.
fread(&(header.sample_rate),
sizeof(
unsigned int), 1) != 1) {
1258 if (ts.
fread(&(header.channels),
sizeof(
unsigned int), 1) != 1) {
1264 ((
unsigned int)0x2e736e64 ==
SWAPINT(header.magic)))
1266 header.hdr_size =
SWAPINT(header.hdr_size);
1267 header.data_size =
SWAPINT(header.data_size);
1268 header.encoding =
SWAPINT(header.encoding);
1269 header.sample_rate =
SWAPINT(header.sample_rate);
1270 header.channels =
SWAPINT(header.channels);
1272 else if ((
unsigned int)0x2e736e64 != header.magic)
1275 switch (header.encoding)
1287 fprintf(stderr,
"Unsupported data type in SND header\n");
1291 *num_channels = header.channels;
1293 *sample_rate = header.sample_rate;
1295 if ((header.data_size == 0) ||
1296 (header.data_size == -1))
1299 bytes = ts.
tell() - header.hdr_size;
1302 bytes = header.data_size;
1303 samps = bytes/sample_width;
1306 data_length = (samps - offset)*(*num_channels);
1308 data_length = length *(*num_channels);
1310 if (ts.
seek(current_pos+header.hdr_size+(sample_width*offset*(*num_channels))) != 0) {
1311 fprintf(stderr,
"WAVE read: seek error\n");
1315 fprintf(stderr,
"WAVE read: Too much data\n");
1318 file_data =
walloc(
unsigned char, sample_width * data_length);
1319 if ((n=ts.
fread(file_data,sample_width,data_length)) != data_length)
1321 fprintf(stderr,
"WAVE read: short file %s\n",
1323 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1324 offset,n,data_length);
1333 *num_samples = data_length/ (*num_channels);
1341 int num_samples,
int num_channels,
1347 Sun_au_header header;
1350 header.magic = (
unsigned int)0x2e736e64;
1351 header.hdr_size =
sizeof(header);
1352 header.data_size =
get_word_size(sample_type) * num_channels * num_samples;
1354 switch (sample_type) {
1356 header.encoding = 1;
1359 header.encoding = 2;
1362 header.encoding = 3;
1367 "Unsupported sample type cannot be saved in SND format\n");
1374 header.sample_rate = sample_rate;
1376 header.channels = num_channels;
1381 header.magic =
SWAPINT(header.magic);
1382 header.hdr_size =
SWAPINT(header.hdr_size);
1383 header.data_size =
SWAPINT(header.data_size);
1384 header.encoding =
SWAPINT(header.encoding);
1385 header.sample_rate =
SWAPINT(header.sample_rate);
1386 header.channels =
SWAPINT(header.channels);
1390 if (fwrite(&header,
sizeof(header), 1, fp) != 1)
1397 int num_samples,
int num_channels,
1408 return save_raw_data(fp,data,offset,num_samples,num_channels,
1414 int num_samples,
int num_channels,
1421 num_channels, sample_rate, sample_type, bo);
1435 static void init_struct_s1(
struct s1& s) {
1452 static void init_struct_s2(
struct s2& s) {
1475 static void init_struct_audlabfh(
struct audlabfh & s) {
1476 init_struct_s1(s.z);
1477 memset(s.file_type,0,8);
1478 memset(s.c1, 0, 17);
1479 memset(s.c2, 0, 17);
1480 memset(s.c3, 0, 17);
1499 static void init_struct_audlabsh(
struct audlabsh & s) {
1500 s.channel_count = 0;
1503 memset(s.c1, 0, 20);
1506 memset(s.c3, 0, 121);
1507 memset(s.c4, 0, 121);
1519 static void init_struct_audlabsd(
struct audlabsd & s) {
1520 memset(s.descr, 0, 17);
1524 init_struct_s2(s.z);
1529 *num_samples,
int *num_channels,
int *word_size,
int 1537 init_struct_audlabfh(fh);
1538 init_struct_audlabsh(sh);
1539 init_struct_audlabsd(sd);
1540 int data_length,sample_count;
1545 current_pos = ts.
tell();
1547 if (ts.
fread(&fh,
sizeof(
struct audlabfh), 1) != 1)
1549 if (strcmp(fh.file_type,
"Sample") != 0)
1552 if (ts.
fread(&sh,
sizeof(
struct audlabsh), 1) != 1)
1554 if (ts.
fread(&sd,
sizeof(
struct audlabsd), 1) != 1)
1556 hdr_length =
sizeof(
struct audlabfh) +
1557 sizeof(struct audlabsh) +
1558 sizeof(struct audlabsd);
1562 *num_channels = sh.channel_count;
1563 *sample_rate = sh.sample_rate;
1564 sample_count = sd.sample_count;
1568 *num_channels =
SWAPINT(sh.channel_count);
1569 *sample_rate =
SWAPINT(sh.sample_rate);
1570 sample_count =
SWAPINT(sd.sample_count);
1573 data_length = (sample_count - offset) * (*num_channels);
1575 data_length = length *(*num_channels);
1577 if (ts.
seek(current_pos+hdr_length+(
sizeof(
short)*offset*(*num_channels))) != 0) {
1578 fprintf(stderr,
"audlab: read error\n");
1581 *data =
walloc(
short,
sizeof(
short) * data_length);
1583 if ((
int)ts.
fread(*data,
sizeof(
short), data_length) != data_length)
1591 *num_samples = data_length / (*num_channels);
1593 *word_size =
sizeof(short);
1600 int num_samples,
int num_channels,
1611 init_struct_audlabfh(fh);
1612 init_struct_audlabsh(sh);
1613 init_struct_audlabsd(sd);
1614 fh.start =
sizeof (
struct audlabfh) +
1615 sizeof (struct audlabsh) + sizeof (struct audlabsd);
1617 strcpy(fh.file_type,
"Sample");
1621 sh.channel_count =
SWAPINT(num_channels);
1623 sh.sample_rate =
SWAPINT(sample_rate);
1625 sd.sample_count =
SWAPINT(num_samples);
1630 sh.channel_count = num_channels;
1632 sh.sample_rate = sample_rate;
1634 sd.sample_count = num_samples;
1637 sprintf(sd.descr,
"Filter 1");
1640 fwrite (&fh,
sizeof(fh), 1, fp);
1641 fwrite (&sh,
sizeof(sh), 1, fp);
1642 fwrite (&sd,
sizeof(sd), 1, fp);
1647 int num_samples,
int num_channels,
1658 return save_raw_data(fp,data,offset,num_samples,num_channels,
1663 int num_samples,
int num_channels,
1668 sample_rate, sample_type, bo);
1670 num_samples, num_channels,
1671 sample_rate, sample_type, bo);
1687 *num_samples,
int *num_channels,
int 1688 *word_size,
int *sample_rate,
enum 1695 int actual_bo, sample_width, data_length;
1700 unsigned char *file_data;
1704 fprintf(stderr,
"Can't open esps file %s for reading\n",
1714 if (hdr->file_type !=
ESPS_SD)
1716 fprintf(stderr,
"ESPS file: not an FEA_SD file\n");
1724 fprintf(stderr,
"ESPS file: can't find sample_rate in header assuming 16000\n");
1725 *sample_rate = 16000;
1728 *sample_rate = (
int)d;
1731 *num_channels = hdr->field_dimension[0];
1738 data_length = (hdr->num_records - offset)*(*num_channels);
1740 data_length = length *(*num_channels);
1743 hdr->hdr_size+(sample_width*offset*(*num_channels)),
1745 fprintf(stderr,
"WAVE read: esps: could not set file to read position");
1748 file_data =
walloc(
unsigned char, sample_width * data_length);
1749 if ((dl=fread(file_data,sample_width,data_length,fd)) != data_length)
1751 fprintf(stderr,
"WAVE read: esps short file %s\n",
1753 fprintf(stderr,
"WAVE read: at %d got %d instead of %d samples\n",
1754 offset,dl,data_length);
1762 *num_samples = data_length/ (*num_channels);
1774 int num_samples,
int num_channels,
1784 hdr->num_records = num_samples;
1785 switch (sample_type)
1793 fprintf(stderr,
"ESPS file: no support for sample_type %s\n",
1800 add_field(hdr,
"samples",esps_type,num_channels);
1804 "EDST waveform written as ESPS FEA_SD.\n\ 1806 add_fea_d(hdr,
"start_time",0,(
double)0);
1807 add_fea_d(hdr,
"record_freq",0,(
double)sample_rate);
1808 add_fea_d(hdr,
"max_value",0,(
double)27355);
1825 int num_samples,
int num_channels,
1835 return save_raw_data(fp,data,offset,num_samples,num_channels,
1840 int num_samples,
int num_channels,
1848 num_channels, sample_rate, sample_type, bo);
1860 *num_samples,
int *num_channels,
1864 int *bo,
int offset,
int length,
1869 unsigned char *file_data;
1870 int data_length,samps,sample_width;
1877 if ((offset != 0) || (length != 0))
1879 fprintf(stderr,
"Load ascii wave: doesn't support offets and lengths\n");
1884 guess = (
int)(1.2*ts.
tell()/7)+10;
1885 if (ts.
seek(0) != 0) {
1886 fprintf(stderr,
"Load asci wave: seek error\n");
1889 *data =
walloc(
short, guess);
1893 samp = atoi(ts.
get().string());
1896 ndata =
walloc(
short,(
int)(guess*1.2));
1897 memmove(ndata,*data,guess*
sizeof(
short));
1900 guess = (
int)(guess*1.2);
1904 fprintf(stderr,
"Load ascii wave: sample %d underflow clipping\n",
1906 (*data)[i] = -32768;
1908 else if (samp > 32767)
1910 fprintf(stderr,
"Load ascii wave: sample %d overflow clipping\n",
1915 (*data)[i] = (short)samp;
1924 samps = ts.
tell()/sample_width;
1927 data_length = samps - offset;
1929 data_length = length;
1931 if (ts.
seek(offset*sample_width*inc) != 0) {
1932 fprintf(stderr,
"Error seeking in file\n");
1935 file_data =
walloc(
unsigned char, data_length * sample_width *inc);
1936 if ((
int)ts.
fread(file_data,sample_width,data_length) != data_length) {
1944 *num_samples = data_length/inc;
1945 *sample_rate = isample_rate;
1946 *num_channels = inc;
1955 int num_samples,
int num_channels,
1970 int num_samples,
int num_channels,
1978 return save_raw_data(fp,data,offset,num_samples,num_channels,
1984 int num_samples,
int num_channels,
1991 num_channels, sample_rate, sample_type, bo);
2001 const int num_samples,
const int num_channels,
2002 const int sample_rate,
2012 sample_rate, sample_type, bo);
2016 sample_rate, sample_type, bo);
2020 sample_rate, sample_type, bo);
2024 sample_rate, sample_type, bo);
2028 sample_rate, sample_type, bo);
2032 sample_rate, sample_type, bo);
2036 sample_rate, sample_type, bo);
2040 sample_rate, sample_type, bo);
2044 sample_rate, sample_type, bo);
2048 cerr <<
"Can't save wave header to files type " << ftype << endl;
2057 const int num_samples,
const int num_channels,
2058 const int sample_rate,
2068 sample_rate, sample_type, bo);
2072 sample_rate, sample_type, bo);
2076 sample_rate, sample_type, bo);
2080 sample_rate, sample_type, bo);
2084 sample_rate, sample_type, bo);
2088 sample_rate, sample_type, bo);
2092 sample_rate, sample_type, bo);
2096 sample_rate, sample_type, bo);
2100 sample_rate, sample_type, bo);
2104 cerr <<
"Can't save wave data to files type " << ftype << endl;
enum EST_write_status save_wave_aiff_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_read_status load_wave_audlab(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
enum EST_read_status load_wave_nist(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
enum EST_sample_type_t str_to_sample_type(const char *type)
enum EST_write_status save_wave_sd_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
EST_TokenStream & get(EST_Token &t)
get next token in stream
char * wstrdup(const char *s)
#define walloc(TYPE, SIZE)
FILE * filedescriptor()
For the people who need the actual description (if possible)
enum EST_write_status save_wave_alaw_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_alaw(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_riff_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_aiff(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
The file was read in successfully.
enum EST_write_status save_raw_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, enum EST_sample_type_t sample_type, int bo)
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don't use peek() immediately beforehand)
enum EST_write_status save_wave_snd(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_read_status load_wave_riff(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
char * cmake_tmp_filename()
int ival(const EST_String &rkey, int m=1) const
enum EST_write_status save_wave_sd_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
void add_fea_d(esps_hdr hdr, const char *name, int pos, double d)
void close(void)
Close stream.
EST_TNamedEnum< EST_sample_type_t > EST_sample_type_map
const EST_String filename() const
The originating filename (if there is one)
enum EST_read_status load_wave_est(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
EST_FilePos tell(void) const
tell, synonym for filepos
enum EST_write_status save_wave_ulaw_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
const char * sample_type_to_str(enum EST_sample_type_t type)
enum EST_write_status save_wave_nist_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
short * convert_raw_data(unsigned char *file_data, int data_length, enum EST_sample_type_t sample_type, int bo)
enum EST_read_status load_wave_aiff(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
void alaw_to_short(const unsigned char *alaw, short *data, int length)
enum EST_write_status save_wave_raw_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
ENUM token(VAL value) const
int open(const EST_String &filename)
open a EST_TokenStream for a file.
enum EST_write_status save_wave_raw_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
int nist_get_param_int(const char *hdr, const char *field, int def_val)
float max(float a, float b)
enum EST_sample_type_t nist_to_sample_type(char *type)
An error occurred while reading.
enum EST_read_status load_wave_alaw(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
void ulaw_to_short(const unsigned char *ulaw, short *data, int length)
enum EST_read_status load_wave_snd(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
enum EST_read_status read_esps_hdr(esps_hdr *uhdr, FILE *fd)
#define WAVE_FORMAT_ADPCM
enum EST_write_status save_wave_snd_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
double ConvertFromIeeeExtended(unsigned char *bytes)
enum EST_write_status save_wave_nist(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
The file was written successfully.
enum EST_read_status load_wave_raw(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length, int isample_rate, enum EST_sample_type_t isample_type, int ibo, int inc)
int get_word_size(enum EST_sample_type_t sample_type)
#define ESPS_FEA_DIRECTORY
CharacterEncoding encoding
void swap_bytes_short(short *data, int length)
EST_read_status read_est_header(EST_TokenStream &ts, EST_Option &hinfo, bool &ascii, EST_EstFileType &t)
enum EST_write_status save_wave_riff_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_audlab(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_alaw_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status write_esps_hdr(esps_hdr hdr, FILE *fd)
enum EST_write_status save_wave_nist_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
#define EST_LITTLE_ENDIAN
int EST_fseek(FILE *fp, EST_FilePos offset, int whence)
enum EST_write_status save_wave_est_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_read_status load_wave_sd(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
const char * sample_type_to_nist(enum EST_sample_type_t sample_type)
enum EST_write_status wave_io_save_data(FILE *fp, const short *data, const int offset, const int num_samples, const int num_channels, const int sample_rate, const EST_String &stype, const int bo, const EST_String &ftype)
enum EST_write_status save_wave_raw(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_est_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
float start(const EST_Item &item)
enum EST_write_status save_wave_audlab_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_sd(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_est(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_riff(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
int EST_strcasecmp(const char *s1, const char *s2, const unsigned char *charmap)
enum EST_write_status save_wave_snd_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
enum EST_write_status save_wave_aiff_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
void ConvertToIeeeExtended(double num, unsigned char *bytes)
const EST_String pos_description()
A string describing current position, suitable for error messages.
int present(const K &rkey) const
Returns true if key is present.
void add_fea_special(esps_hdr hdr, int type, const char *name)
#define WAVE_FORMAT_MULAW
int fea_value_d(const char *name, int pos, esps_hdr hdr, double *d)
enum EST_write_status save_wave_audlab_header(FILE *fp, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
int seek(int position)
seek, reposition file pointer
enum EST_write_status save_wave_ulaw(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
void delete_esps_hdr(esps_hdr h)
void add_field(esps_hdr hdr, const char *name, int type, int dimension)
enum EST_write_status save_wave_ulaw_data(FILE *fp, const short *data, int offset, int num_samples, int num_channels, int sample_rate, enum EST_sample_type_t sample_type, int bo)
esps_hdr make_esps_sd_hdr(void)
enum EST_read_status load_wave_ulaw(EST_TokenStream &ts, short **data, int *num_samples, int *num_channels, int *word_size, int *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int offset, int length)
char * nist_get_param_str(const char *hdr, const char *field, const char *def_val)
static EST_TNamedEnumI< EST_WaveFileType, Info > map
enum EST_write_status wave_io_save_header(FILE *fp, const int num_samples, const int num_channels, const int sample_rate, const EST_String &stype, const int bo, const EST_String &ftype)