Edinburgh Speech Tools  2.1-release
EST_wave_io.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Alan Black and Paul Taylor */
34 /* Date : June 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* File I/O functions specific to various file formats */
37 /* */
38 /* Note that internally data will always be shorts and */
39 /* native byte order, conversions to/from other byte formats */
40 /* or encodings happend at read/write time */
41 /* */
42 /*=======================================================================*/
43 #include <cstdlib>
44 #include <cstdio>
45 #include "EST_unix.h"
46 #include <cstring>
47 #include <limits>
48 
49 #include "EST_wave_aux.h"
50 #include "EST_wave_utils.h"
51 #include "EST_strcasecmp.h"
52 #include "waveP.h"
53 #include "EST_FileType.h"
54 #include "EST_File.h"
55 
56 
57 using namespace std;
58 
59 static int def_load_sample_rate = 16000;
60 
61 /*************************************************************************/
62 /* */
63 /* Functions specific for each file format */
64 /* */
65 /*************************************************************************/
66 
67 /*=======================================================================*/
68 /* Sphere Nist files */
69 /*=======================================================================*/
70 
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
74 
75 int nist_get_param_int(const char *hdr, const char *field, int def_val)
76 {
77  const char *p;
78  int val;
79 
80  if (((p=strstr(hdr,field)) != NULL) &&
81  (strncmp(" -i ",p+strlen(field),4) == 0))
82  {
83  sscanf(p+strlen(field)+4,"%d",&val);
84  return val;
85  }
86  else
87  return def_val;
88 
89 }
90 
91 char *nist_get_param_str(const char *hdr, const char *field, const char *def_val)
92 {
93  const char *p;
94  char *val;
95  int size;
96 
97  if (((p=strstr(hdr,field)) != NULL) &&
98  (strncmp(" -s",p+strlen(field),3) == 0))
99  {
100  sscanf(p+strlen(field)+3,"%d",&size);
101  val = walloc(char,size+1);
102  /* Hmm don't know how long the %d is so do it again */
103  sscanf(p+strlen(field)+3,"%d %s",&size,val);
104  return val;
105  }
106  else
107  return wstrdup(def_val);
108 
109 
110 }
111 
112 const char *sample_type_to_nist(enum EST_sample_type_t sample_type)
113 {
114  const char *c;
115  switch (sample_type) {
116  case st_unknown:
117  c = ""; break;
118  case st_schar:
119  c = "PCM-1"; break;
120  case st_alaw:
121  c = "ALAW"; break;
122  case st_mulaw:
123  c = "ULAW"; break;
124  case st_short:
125  c = "pcm"; break;
126  case st_int:
127  c = "PCM-4"; break;
128  case st_float:
129  c = "REAL"; break;
130  case st_double:
131  c = "REAL"; break;
132  default:
133  fprintf(stderr,"Unknown sample type for nist");
134  c = "";
135  }
136  return c;
137 }
138 
140 {
141  if ((streq(type,"pcm")) ||
142  (streq(type,"PCM")) ||
143  (streq(type,"pcm-2")))
144  return st_short;
145  if (strcmp(type,"pcm,embedded-shorten-v1.1") == 0)
146  return st_shorten;
147  else if ((EST_strcasecmp(type,"ULAW",NULL) == 0) ||
148  (EST_strcasecmp(type,"U-LAW",NULL) == 0) ||
149  (EST_strcasecmp(type,"mu-law",NULL) == 0) ||
150  (EST_strcasecmp(type,"mulaw",NULL) == 0))
151  return st_mulaw;
152  else if ((EST_strcasecmp(type,"ALAW",NULL) == 0) ||
153  (EST_strcasecmp(type,"A-LAW",NULL) == 0))
154  return st_alaw;
155  else if (strcmp(type,"alaw") == 0)
156  return st_alaw;
157  else if (strcmp(type,"PCM-1") == 0)
158  return st_schar;
159  else if (strcmp(type,"PCM-4") == 0)
160  return st_int;
161  else if (strcmp(type,"REAL") == 0)
162  return st_float;
163  else
164 
165  {
166  fprintf(stderr,"NIST: unknown sample type: %s\n",type);
167  return st_unknown;
168  }
169 }
170 
171 enum EST_read_status load_wave_nist(EST_TokenStream &ts, short **data, int
172  *num_samples, int *num_channels, int
173  *word_size, int *sample_rate, enum
174  EST_sample_type_t *sample_type, int *bo , int
175  offset, int length)
176 
177 {
178  char header[NIST_HDR_SIZE];
179  int samps,sample_width,data_length,actual_bo;
180  int system_return;
181  unsigned char *file_data;
182  enum EST_sample_type_t actual_sample_type;
183  char *byte_order, *sample_coding=0;
184  int n;
185  int current_pos;
186 
187  current_pos = ts.tell();
188  if (ts.fread(header,NIST_HDR_SIZE,1) != 1)
189  return wrong_format;
190 
191  if (strncmp(header,NIST_SIG,strlen(NIST_SIG)) != 0)
192  return wrong_format;
193 
194  samps = nist_get_param_int(header,"sample_count",-1);
195  *num_channels = nist_get_param_int(header,"channel_count",1);
196  sample_width = nist_get_param_int(header,"sample_n_bytes",2);
197  *sample_rate =
198  nist_get_param_int(header,"sample_rate",def_load_sample_rate);
199  byte_order = nist_get_param_str(header,"sample_byte_format",
200  (EST_BIG_ENDIAN ? "10" : "01"));
201  if (streq(byte_order,"mu-law"))
202  {
203  wfree(byte_order);
204  byte_order = wstrdup((EST_BIG_ENDIAN ? "10" : "01"));
205  sample_coding = wstrdup("ULAW");
206  }
207  else if (streq(byte_order,"a-law"))
208  {
209  wfree(byte_order);
210  byte_order = wstrdup((EST_BIG_ENDIAN ? "10" : "01"));
211  sample_coding = wstrdup("ALAW");
212  } else {
213  sample_coding = nist_get_param_str(header,"sample_coding","pcm");
214  }
215 
216  /* code for reading in Tony Robinson's shorten files.
217  This is a temporary fix which calls the unshorten program on the
218  speech file and reads in the answer by calling this function.
219  It would be nice to have a simple library routine which did the
220  unshortening.
221  */
222 
223  if (streq(sample_coding,"pcm,embedded-shorten-v1.1"))
224  {
225  char *tmpfile, *cmdstr;
226  enum EST_read_status rval;
227 
228  tmpfile = cmake_tmp_filename();
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)
235  {
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);
240  wfree(byte_order);
241  wfree(tmpfile);
242  wfree(cmdstr);
243  return misc_read_error;
244  }
245  EST_TokenStream tt;
246  if (tt.open(tmpfile) < 0) {
247  fprintf(stderr, "Could not open %s\n", tmpfile);
248  wfree(sample_coding);
249  wfree(byte_order);
250  wfree(tmpfile);
251  wfree(cmdstr);
252  return misc_read_error;
253  }
254 
255  rval = load_wave_nist(tt, data, num_samples,
256  num_channels, word_size, sample_rate,
257  sample_type, bo, offset, length);
258  unlink(tmpfile);
259  wfree(tmpfile);
260  wfree(cmdstr);
261  tt.close();
262  wfree(byte_order);
263  wfree(sample_coding);
264  return rval;
265  }
266 
267  if (length == 0)
268  data_length = (samps - offset)*(*num_channels);
269  else
270  data_length = length*(*num_channels);
271 
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);
275  wfree(byte_order);
276  return misc_read_error;
277  }
278  file_data = walloc(unsigned char,sample_width * data_length);
279 
280  n = ts.fread(file_data,sample_width,data_length);
281 
282  if ((n < 1) && (n != data_length))
283  {
284  wfree(file_data);
285  wfree(sample_coding);
286  wfree(byte_order);
287  return misc_read_error;
288  }
289  else if ((n < data_length) && (data_length/(*num_channels) == n))
290  {
291  fprintf(stderr,"WAVE read: nist header is (probably) non-standard\n");
292  fprintf(stderr,"WAVE read: assuming different num_channel interpretation\n");
293  data_length = n; /* wrongly headered file */
294  }
295  else if (n < data_length)
296  {
297  fprintf(stderr,"WAVE read: short file %s\n",
298  (const char *)ts.filename());
299  fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
300  offset,n,data_length);
301  data_length = n;
302  }
303 
304  actual_sample_type = nist_to_sample_type(sample_coding);
305  actual_bo = ((strcmp(byte_order,"10") == 0) ? bo_big : bo_little);
306 
307  *data = convert_raw_data(file_data,data_length,
308  actual_sample_type,actual_bo);
309 
310  *num_samples = data_length/ (*num_channels);
311  *sample_type = st_short;
312  *bo = EST_NATIVE_BO;
313  *word_size = 2;
314  wfree(sample_coding);
315  wfree(byte_order);
316 
317  return format_ok;
318 }
319 
321  int num_samples, int num_channels,
322  int sample_rate,
323  enum EST_sample_type_t sample_type, int bo)
324 {
325  char h[1024], p[1024];
326  const char *t;
327 
328  memset(h,0,1024);
329 
330  strncat(h, NIST_SIG, 512);
331  sprintf(p, "channel_count -i %d\n", num_channels);
332  strcat(h, p);
333  sprintf(p, "sample_count -i %d\n", num_samples);
334  strcat(h, p);
335  sprintf(p, "sample_rate -i %d\n", sample_rate);
336  strcat(h, p);
337 
338  t = sample_type_to_nist(sample_type);
339  if (t)
340  {
341  sprintf(p, "sample_coding -s%d %s\n", (signed)strlen(t), t);
342  strcat(h, p);
343  sprintf(p, "sample_n_bytes -i %d\n", get_word_size(sample_type));
344  strcat(h, p);
345  }
346 
347  if (get_word_size(sample_type) > 1)
348  {
349  sprintf(p, "sample_byte_format -s%d %s\n", 2,
350  ((bo == bo_big) ? "10" : "01"));
351  strcat(h, p);
352  }
353 
354  if (strlen(h)+ strlen(NIST_END_SIG) > 1024) {
355  return misc_write_error;
356  }
357  strcat(h, NIST_END_SIG);
358  /*makes it nice to read */
359  strcat(h, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
360 
361  if (fwrite(&h, 1024, 1, fp) != 1)
362  return misc_write_error;
363 
364  return write_ok;
365 }
366 
367 
368 enum EST_write_status save_wave_nist_data(FILE *fp, const short *data, int offset,
369  int num_samples, int num_channels,
370  int sample_rate,
371  enum EST_sample_type_t sample_type, int bo)
372 {
373  (void) sample_rate;
374  if (data == NULL)
375  return write_ok;
376 
377  return save_raw_data(fp,data,offset,num_samples,num_channels,
378  sample_type,bo);
379 
380 }
381 
382 enum EST_write_status save_wave_nist(FILE *fp, const short *data, int offset,
383  int num_samples, int num_channels,
384  int sample_rate,
385  enum EST_sample_type_t sample_type, int bo)
386 {
387  save_wave_nist_header(fp, num_samples, num_channels,
388  sample_rate, sample_type, bo);
389  return save_wave_nist_data(fp, data, offset,
390  num_samples, num_channels,
391  sample_rate, sample_type, bo);
392 }
393 
394 /*=======================================================================*/
395 /* EST's own format */
396 /*=======================================================================*/
397 
398 enum EST_read_status load_wave_est(EST_TokenStream &ts, short **data, int
399  *num_samples, int *num_channels, int
400  *word_size, int *sample_rate, enum
401  EST_sample_type_t *sample_type, int *bo,
402  int offset, int length)
403 {
404  (void) offset;
405  int data_length, actual_bo;
406  short *file_data;
407  EST_String byte_order;
408  int n;
409  EST_EstFileType t;
410  EST_Option hinfo;
411  bool ascii;
412  EST_read_status r;
413  EST_sample_type_t actual_sample_type;
414 
415  /*offset = 0;*/
416 
417  if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
418  return r;
419  if (t != est_file_wave)
420  return misc_read_error;
421 
422  *num_samples = hinfo.ival("NumSamples");
423  *num_channels = hinfo.ival("NumChannels");
424  *sample_rate = hinfo.ival("SampleRate");
425 
426  byte_order = hinfo.val("ByteOrder");
427 
428  if (length == 0)
429  data_length = (*num_samples)*(*num_channels);
430  else
431  data_length = length*(*num_channels);
432 
433  file_data = walloc(short, data_length);
434 
435  n = ts.fread(file_data, sizeof(short), data_length);
436  if ((n != data_length) && (n < 1))
437  {
438  cerr << "EST wave load: " << ts.pos_description() << endl;
439  cerr << "failed to read file\n";
440  wfree(file_data);
441  return misc_read_error;
442  }
443  else if (n != data_length)
444  {
445  cerr << "Wrong number of samples/channels in EST wave file\n";
446  cerr << ts.pos_description() << " ";
447  cerr << "expected " << data_length << " got " << n << endl;
448  data_length = n;
449  }
450 
451  actual_bo = (byte_order == "10") ? bo_big : bo_little;
452  if (hinfo.present("SampleType"))
453  actual_sample_type = str_to_sample_type(hinfo.val("SampleType"));
454  else
455  actual_sample_type = st_short; // some older files don't have this
456 
457  *data = convert_raw_data((unsigned char *)file_data,
458  data_length, actual_sample_type, actual_bo);
459  // because internally data is always shorts
460  *sample_type = st_short;
461  *bo = EST_NATIVE_BO;
462  *word_size = 2;
463 
464  return format_ok;
465 }
466 
468  int num_samples, int num_channels,
469  int sample_rate,
470  enum EST_sample_type_t sample_type, int bo)
471 {
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);
477  fprintf(fp, "SampleType %s\n", sample_type_to_str(sample_type));
478  if (get_word_size(sample_type) > 1)
479  fprintf(fp, "ByteOrder %s\n", ((bo == bo_big) ? "10" : "01"));
480 
481  fprintf(fp, "EST_Header_End\n");
482  return write_ok;
483 }
484 
485 enum EST_write_status save_wave_est_data(FILE *fp, const short *data, int offset,
486  int num_samples, int num_channels,
487  int sample_rate,
488  enum EST_sample_type_t sample_type, int bo)
489 {
490  (void) sample_rate;
491  if (data == NULL)
492  return write_ok;
493 
494  return save_raw_data(fp, data, offset, num_samples, num_channels,
495  sample_type, bo);
496 }
497 
498 enum EST_write_status save_wave_est(FILE *fp, const short *data, int offset,
499  int num_samples, int num_channels,
500  int sample_rate,
501  enum EST_sample_type_t sample_type, int bo)
502 {
503  save_wave_est_header(fp, num_samples, num_channels,
504  sample_rate, sample_type, bo);
505 
506  return save_wave_est_data(fp, data, offset,
507  num_samples, num_channels,
508  sample_rate, sample_type, bo);
509 }
510 
511 /*=======================================================================*/
512 /* Microsoft RIFF (.wav) audio files */
513 /* */
514 /* The information on this format was gained by reading a document */
515 /* found on the net called "Multimedia Programming Interface and */
516 /* Data Specification v1.0" and by looking at Rick Richardson, */
517 /* Lance Norskog And Sundry Contributors code in SOX. All this code */
518 /* is rewritten from scratch though, but I couldn't do it without */
519 /* other's explanations. I would have used the SOX code directly but */
520 /* was not really in the right form so starting again was easier */
521 /*=======================================================================*/
522 #define WAVE_FORMAT_PCM 0x0001
523 #define WAVE_FORMAT_ADPCM 0x0002
524 #define WAVE_FORMAT_ALAW 0x0006
525 #define WAVE_FORMAT_MULAW 0x0007
526 
527 enum EST_read_status load_wave_riff(EST_TokenStream &ts, short **data, int
528  *num_samples, int *num_channels, int
529  *word_size, int *sample_rate, enum
530  EST_sample_type_t *sample_type, int *bo , int
531  offset, int length)
532 {
533  char info[4];
534  int samps,sample_width,data_length;
535  short shortdata;
536  int dsize,intdata;
537  unsigned char *file_data;
538  enum EST_sample_type_t actual_sample_type;
539 
540  if (ts.fread(info,sizeof(char),4) != 4)
541  return wrong_format; /* its almost definitely an error */
542  if (strncmp(info,"RIFF",4) != 0)
543  return wrong_format;
544 
545  /* We've got a riff file */
546  /* Next 4 bytes are the file size */
547  if(ts.fread(&dsize,4,1) != 1) return misc_read_error;
548  /* .wav files are always little endian */
549  if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
550  if ((ts.fread(info,sizeof(char),4) != 4) ||
551  (strncmp(info,"WAVE",4) != 0))
552  {
553  fprintf(stderr, "RIFF file is not of type WAVE\n");
554  return misc_read_error; /* not a wave file */
555  }
556  if ((ts.fread(info,sizeof(char),4) != 4) ||
557  (strncmp(info,"fmt ",4) != 0))
558  return misc_read_error; /* something else wrong */
559 
560  if (ts.fread(&dsize,4,1) != 1) return misc_read_error;
561  if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
562  if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;
563  if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
564 
565  switch (shortdata)
566  {
567  /* This is a non-proprietary format */
568  case WAVE_FORMAT_PCM:
569  actual_sample_type = st_short; break;
570  /* The follow are registered proprietary WAVE formats (?) */
571  case WAVE_FORMAT_MULAW:
572  actual_sample_type = st_mulaw; break;
573  case WAVE_FORMAT_ALAW:
574  actual_sample_type = st_alaw; break;
575  case WAVE_FORMAT_ADPCM:
576  fprintf(stderr, "RIFF file: unsupported proprietary sample format ADPCM\n");
577  actual_sample_type = st_short;
578  break;
579  /* actual_sample_type = st_adpcm; break; */ /* yes but which adpcm ! */
580  default:
581  fprintf(stderr, "RIFF file: unknown sample format\n");
582  actual_sample_type = st_short;
583  /* return misc_read_error; */
584  }
585  if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;
586  if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
587  *num_channels = shortdata;
588  if (ts.fread(sample_rate,4,1) != 1) return misc_read_error;
589  if (EST_BIG_ENDIAN) *sample_rate = SWAPINT(*sample_rate);
590  if (ts.fread(&intdata,4,1) != 1) return misc_read_error; /* average bytes per second -- ignored */
591  if (EST_BIG_ENDIAN) intdata = SWAPINT(intdata);
592  if (ts.fread(&shortdata,2,1) != 1) return misc_read_error; /* block align ? */
593  if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
594  if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;
595  if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
596 
597  sample_width = (shortdata+7)/8;
598  if ((sample_width == 1) && (actual_sample_type == st_short))
599  actual_sample_type = st_uchar; /* oops I meant 8 bit */
600 
601  if (ts.seek((dsize-16)+ts.tell()) != 0) { /* skip rest of header */
602  fprintf(stderr, "Could not skip header. Read error");
603  return misc_read_error;
604  }
605  while (1)
606  {
607  if (ts.fread(info,sizeof(char),4) != 4)
608  {
609  fprintf(stderr,"RIFF file truncated\n");
610  return misc_read_error; /* something else wrong */
611  }
612  if (strncmp(info,"data",4) == 0)
613  {
614  if (ts.fread(&samps,4,1) != 1) return misc_read_error;
615  if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
616  samps /= (sample_width*(*num_channels));
617  break;
618  }
619  else if (strncmp(info,"fact",4) == 0)
620  { /* some other type of chunk -- skip it */
621  if (ts.fread(&samps,4,1) != 1) return misc_read_error;
622  if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
623  if (ts.seek(samps+ts.tell()) != 0) { /* skip rest of header */
624  fprintf(stderr, "Could not seek in file. Read error");
625  return misc_read_error;
626  }
627  /* Hope this is the right amount */
628  }
629  else
630  {
631  // fprintf(stderr,"Ignoring unsupported chunk type \"%c%c%c%c\" in RIFF file\n",
632  // info[0],info[1],info[2],info[3]);
633  //return misc_read_error;
634  if(ts.fread(&dsize,4,1) != 1) return misc_read_error;
635  if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
636  if (ts.seek(dsize+ts.tell()) != 0) { /* skip this chunk */
637  fprintf(stderr, "Wav file: Read error");
638  return misc_read_error;
639  }
640  }
641  }
642  if (length == 0)
643  data_length = (samps - offset)*(*num_channels);
644  else
645  data_length = length*(*num_channels);
646 
647  if (ts.seek((sample_width*offset*(*num_channels))+ts.tell()) != 0) {
648  fprintf(stderr, "Read error\n");
649  return misc_read_error;
650  }
651  if (data_length < 0) {
652  fprintf(stderr, "Read error\n");
653  return misc_read_error;
654  }
655  if ((size_t) data_length > std::numeric_limits<std::size_t>::max()/sample_width) {
656  fprintf(stderr, "Read error: Data length too big\n");
657  return misc_read_error;
658  }
659  file_data = walloc(unsigned char,sample_width * data_length);
660  if ((dsize=ts.fread(file_data,sample_width,data_length)) != data_length)
661  {
662  /* It seems so many WAV files have their datasize wrong I'll */
663  /* let it through -- I think SOX is a major culprit */
664  if (length == 0) /* the file did the identification */
665  fprintf(stderr,"Unexpected end of file but continuing (apparently missing %d samples)\n",data_length-dsize);
666  else
667  {
668  fprintf(stderr,"Unexpected end of file: (missing %d samples)\n",data_length-dsize);
669  wfree(file_data);
670  return misc_read_error;
671  }
672  }
673 
674  *data = convert_raw_data(file_data,dsize,
675  actual_sample_type, bo_little);
676 
677  *num_samples = dsize / (*num_channels);
678  *sample_type = st_short;
679  *bo = EST_NATIVE_BO;
680  *word_size = 2;
681 
682  return format_ok;
683 }
684 
685 enum EST_write_status save_wave_riff_header(FILE *fp, int num_samples,
686  int num_channels, int sample_rate,
687  enum EST_sample_type_t sample_type, int bo)
688 {
689  (void)bo;
690  const char *info;
691  int data_size, data_int;
692  short data_short;
693 
694  if (sample_type == st_schar)
695  {
696  EST_warning("RIFF format: Signed 8-bit not allowed by this file format");
697  sample_type=st_uchar;
698  }
699 
700  info = "RIFF"; fwrite(info,4,1,fp);
701  data_size = num_channels*num_samples*get_word_size(sample_type)+ 8+16+12;
702  /* WAV files are always LITTLE_ENDIAN (i.e. intel x86 format) */
703  if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
704  fwrite(&data_size,1,4,fp); /* total number of bytes in file */
705  info = "WAVE"; fwrite(info,4,1,fp);
706  info = "fmt "; fwrite(info,4,1,fp);
707  data_size = 16;
708  if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
709  fwrite(&data_size,1,4,fp); /* size of header */
710  switch (sample_type)
711  {
712  case st_short: data_short = WAVE_FORMAT_PCM; break;
713  case st_uchar: data_short = WAVE_FORMAT_PCM; break;
714  case st_mulaw: data_short = WAVE_FORMAT_MULAW; break;
715  case st_alaw: data_short = WAVE_FORMAT_ALAW; break;
716  case st_adpcm: data_short = WAVE_FORMAT_ADPCM; break;
717  default:
718  fprintf(stderr,"RIFF format: unsupported data format %d\n",
719  sample_type);
720  return misc_write_error;
721  }
722  if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
723  fwrite(&data_short,1,2,fp); /* sample type */
724  data_short = num_channels;
725  if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
726  fwrite(&data_short,1,2,fp); /* number of channels */
727  data_int = sample_rate;
728  if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
729  fwrite(&data_int,1,4,fp); /* sample rate */
730  data_int = sample_rate * num_channels * get_word_size(sample_type);
731  if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
732  fwrite(&data_int,1,4,fp); /* Average bytes per second */
733  data_short = num_channels * get_word_size(sample_type);
734  if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
735  fwrite(&data_short,1,2,fp); /* block align */
736  data_short = get_word_size(sample_type) * 8;
737  if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
738  fwrite(&data_short,1,2,fp); /* bits per sample */
739  info = "data"; fwrite(info,4,1,fp);
740  data_size = num_channels*num_samples*get_word_size(sample_type);
741  if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
742  fwrite(&data_size,1,4,fp); /* total number of bytes in data */
743 
744  return write_ok;
745 }
746 
747 enum EST_write_status save_wave_riff_data(FILE *fp, const short *data,
748  int offset, int num_samples, int num_channels,
749  int sample_rate,
750  enum EST_sample_type_t sample_type, int bo)
751 {
752  (void)sample_rate;
753  (void)bo;
754  if (data == NULL)
755  return write_ok;
756 
757  return save_raw_data(fp,data,offset,num_samples,num_channels,
758  sample_type,bo_little);
759 }
760 
761 
762 enum EST_write_status save_wave_riff(FILE *fp, const short *data, int offset,
763  int num_samples, int num_channels,
764  int sample_rate,
765  enum EST_sample_type_t sample_type, int bo)
766 {
767  save_wave_riff_header(fp, num_samples, num_channels, sample_rate,
768  sample_type, bo);
769 
770  return save_wave_riff_data(fp, data, offset, num_samples,
771  num_channels, sample_rate, sample_type, bo);
772 
773 }
774 
775 /*=======================================================================*/
776 /* Amiga/Apple AIFF waveform format */
777 /* This was constructed using info in AudioIFF1.3.hqx found on the web */
778 /* and also I did look at SOX's aiff.c written by Guido van Rossum */
779 /* and Sundry Contributors. */
780 /*=======================================================================*/
781 
782 struct AIFFchunk {
783  char id[4];
784  int size;
785 };
786 
787 struct AIFFssnd { /* Sound Data Chunk */
788  int offset;
789  int blocksize;
790 };
791 
792 enum EST_read_status load_wave_aiff(EST_TokenStream &ts, short **data, int
793  *num_samples, int *num_channels, int
794  *word_size, int *sample_rate, enum
795  EST_sample_type_t *sample_type, int *bo , int
796  offset, int length)
797 {
798  char info[4];
799  struct AIFFchunk chunk;
800  short comm_channels = -2;
801  int comm_samples;
802  short comm_bits;
803  unsigned char ieee_ext_sample_rate[10];
804  struct AIFFssnd ssndchunk;
805  enum EST_sample_type_t actual_sample_type;
806  int dsize,data_length,n;
807  unsigned char *file_data;
808 
809  if (ts.fread(info,sizeof(char),4) != 4)
810  return wrong_format; /* but its almost definitely an error */
811  if (strncmp(info,"FORM",4) != 0)
812  return wrong_format;
813 
814  /* We've got an aiff file, I hope */
815  if (ts.fread(&dsize,4,1) != 1) return misc_read_error;
816  if (EST_LITTLE_ENDIAN) /* file is in different byte order */
817  dsize = SWAPINT(dsize);
818  if ((ts.fread(info,sizeof(char),4) != 4) ||
819  (strncmp(info,"AIFF",4) != 0))
820  {
821  fprintf(stderr, "AIFF file does not have AIFF chunk\n");
822  return misc_read_error;
823  }
824 
825  for ( ; ts.fread(&chunk, sizeof(chunk), 1) == 1 ; )
826  { /* for each chunk in the file */
827  if (EST_LITTLE_ENDIAN) /* file is in different byte order */
828  chunk.size = SWAPINT(chunk.size);
829  if (strncmp(chunk.id,"COMM",4) == 0)
830  {
831  if (chunk.size != 18)
832  {
833  fprintf(stderr,"AIFF chunk: bad size\n");
834  return misc_read_error;
835  }
836  if (ts.fread(&comm_channels, sizeof(short), 1) != 1)
837  return misc_read_error;
838  if (ts.fread(&comm_samples, sizeof(int), 1) != 1)
839  return misc_read_error;
840  if (ts.fread(&comm_bits, sizeof(short), 1) != 1)
841  return misc_read_error;
842  if (ts.fread(ieee_ext_sample_rate, 10, 1) != 1)
843  {
844  fprintf(stderr,"AIFF chunk: eof within COMM chunk\n");
845  return misc_read_error;
846  }
847  if (EST_LITTLE_ENDIAN)
848  {
849  comm_channels = SWAPSHORT(comm_channels);
850  comm_samples = SWAPINT(comm_samples);
851  comm_bits = SWAPSHORT(comm_bits);
852  }
853  *sample_rate = (int)ConvertFromIeeeExtended(ieee_ext_sample_rate);
854  if (comm_channels < 0)
855  {
856  fprintf(stderr, "AIFF chunk: Wrong comm channels\n");
857  return wrong_format;
858  }
859  }
860  else if (strncmp(chunk.id,"SSND",4) == 0)
861  {
862  if (ts.fread(&ssndchunk, sizeof(ssndchunk), 1) != 1)
863  {
864  fprintf(stderr,"AIFF chunk: eof within SSND chunk\n");
865  return misc_read_error;
866  }
867  if (EST_LITTLE_ENDIAN)
868  {
869  ssndchunk.offset = SWAPINT(ssndchunk.offset);
870  ssndchunk.blocksize = SWAPINT(ssndchunk.blocksize);
871  }
872  if (comm_channels < 0) {
873  fprintf(stderr, "AIFF: COMM chunk missing\n");
874  return wrong_format;
875  }
876  *num_channels = comm_channels;
877  switch (comm_bits)
878  {
879  case 8: actual_sample_type = st_uchar; break;
880  case 16: actual_sample_type = st_short; break;
881  default:
882  fprintf(stderr,"AIFF: unsupported sample width %d bits\n",
883  comm_bits);
884  return misc_read_error;
885  }
886 
887  if (ts.seek(ssndchunk.offset+(comm_channels*offset)+ts.tell()) != 0) {
888  fprintf(stderr, "AIFF: Read error\n");
889  return misc_read_error;
890  }
891  if (length == 0)
892  data_length = (comm_samples-offset)*comm_channels;
893  else
894  data_length = length*comm_channels;
895 
896  if (data_length < 0 || comm_channels < 0 || get_word_size(actual_sample_type) < 0) {
897  fprintf(stderr, "AIFF: Read error\n");
898  return misc_read_error;
899  }
900 
901  size_t bytes_to_read;
902  if ((size_t)data_length < std::numeric_limits<std::size_t>::max()/comm_channels) {
903  bytes_to_read = data_length * comm_channels;
904  } else {
905  fprintf(stderr, "AIFF: Read error\n");
906  return misc_read_error;
907  }
908  if ((size_t)bytes_to_read < std::numeric_limits<std::size_t>::max()/get_word_size(actual_sample_type)) {
909  bytes_to_read *= get_word_size(actual_sample_type);
910  } else {
911  fprintf(stderr, "AIFF: Read error\n");
912  return misc_read_error;
913  }
914  /*
915  if ((size_t)bytes_to_read < std::numeric_limits<std::size_t>::max()/sizeof(unsigned char)) {
916  bytes_to_read *= sizeof(unsigned char);
917  } else {
918  fprintf(stderr, "AIFF: Read error\n");
919  return misc_read_error;
920  }
921  */
922  file_data = walloc(unsigned char, bytes_to_read);
923  if ((n=ts.fread(file_data,get_word_size(actual_sample_type),
924  data_length)) != data_length)
925  {
926  fprintf(stderr,"AIFF read: short file %s\n",
927  (const char *)ts.filename());
928  fprintf(stderr,"AIFF read: at %d got %d instead of %d samples\n",
929  offset,n,data_length);
930  data_length = n;
931  }
932 
933  *data = convert_raw_data(file_data,data_length,
934  actual_sample_type,bo_big);
935  *num_samples = data_length/comm_channels;
936  *sample_type = st_short;
937  *word_size = 2;
938  *bo = EST_NATIVE_BO;
939  break; /* only care about the first SSND chunk */
940  }
941  else
942  { /* skip bytes in chunk */
943  if (ts.seek(ts.tell()+chunk.size) != 0) {
944  fprintf(stderr, "AIFF: Read error\n");
945  return misc_read_error;
946  }
947  }
948  }
949  return format_ok;
950 }
951 
952 
954  int num_samples, int num_channels,
955  int sample_rate,
956  enum EST_sample_type_t sample_type, int bo)
957 {
958  (void)bo;
959  const char *info;
960  int data_size, data_int;
961  unsigned char ieee_ext_buf[10];
962  short data_short;
963 
964  info = "FORM";
965  fwrite(info,1,4,fp);
966  /* This number seems to be derived different for each example */
967  data_size = 54+(num_samples*num_channels*get_word_size(sample_type));
968  if (EST_LITTLE_ENDIAN)
969  data_size = SWAPINT(data_size);
970  fwrite(&data_size,1,4,fp);
971  info = "AIFF";
972  fwrite(info,1,4,fp);
973  info = "COMM";
974  fwrite(info,1,4,fp);
975  data_int = 18;
976  if (EST_LITTLE_ENDIAN)
977  data_int = SWAPINT(data_int);
978  fwrite(&data_int,1,4,fp);
979  data_short = num_channels;
980  if (EST_LITTLE_ENDIAN)
981  data_short = SWAPSHORT(data_short);
982  fwrite(&data_short,1,2,fp);
983  data_int = num_samples;
984  if (EST_LITTLE_ENDIAN)
985  data_int = SWAPINT(data_int);
986  fwrite(&data_int,1,4,fp);
987  data_short = 8*get_word_size(sample_type);
988  if (EST_LITTLE_ENDIAN)
989  data_short = SWAPSHORT(data_short);
990  fwrite(&data_short,1,2,fp);
991  ConvertToIeeeExtended((double)sample_rate,ieee_ext_buf);
992  fwrite(ieee_ext_buf,1,10,fp);
993  info = "SSND";
994  fwrite(info,1,4,fp);
995  data_int = 8 + (num_samples*num_channels*get_word_size(sample_type));
996  if (EST_LITTLE_ENDIAN)
997  data_int = SWAPINT(data_int);
998  fwrite(&data_int,1,4,fp);
999  data_int = 0;
1000  if (EST_LITTLE_ENDIAN)
1001  data_int = SWAPINT(data_int);
1002  fwrite(&data_int,1,4,fp); /* offset */
1003  if (EST_LITTLE_ENDIAN)
1004  data_int = SWAPINT(data_int);
1005  fwrite(&data_int,1,4,fp); /* blocksize */
1006 
1007  return write_ok;
1008 
1009 }
1010 
1011 enum EST_write_status save_wave_aiff_data(FILE *fp, const short *data, int offset,
1012  int num_samples, int num_channels,
1013  int sample_rate,
1014  enum EST_sample_type_t sample_type, int bo)
1015 {
1016  (void)bo;
1017  (void)sample_rate;
1018  if (data == NULL)
1019  return write_ok;
1020  if ((sample_type == st_short) || (sample_type == st_uchar))
1021  return save_raw_data(fp,data, offset, num_samples, num_channels,
1022  sample_type, bo_big);
1023  else
1024  {
1025  fprintf(stderr,"AIFF: requested data type not uchar or short\n");
1026  return misc_write_error;
1027  }
1028 }
1029 
1030 
1031 enum EST_write_status save_wave_aiff(FILE *fp, const short *data, int offset,
1032  int num_samples, int num_channels,
1033  int sample_rate,
1034  enum EST_sample_type_t sample_type, int bo)
1035 {
1036  save_wave_aiff_header(fp, num_samples, num_channels,
1037  sample_rate, sample_type, bo);
1038 
1039  return save_wave_aiff_data(fp, data, offset,
1040  num_samples, num_channels,
1041  sample_rate, sample_type, bo);
1042 }
1043 
1044 /*=======================================================================*/
1045 /* ulaw EST_filetype are just raw data with 8K ulaw contents */
1046 /*=======================================================================*/
1047 
1049  *num_samples, int *num_channels, int *word_size, int
1050  *sample_rate, enum EST_sample_type_t *sample_type, int *bo,
1051  int offset, int length)
1052 
1053 {
1054  unsigned char *ulaw;
1055  int data_length,samps;
1056 
1057  ts.seek_end();
1058  samps = ts.tell();
1059 
1060  if (length == 0)
1061  data_length = samps - offset;
1062  else
1063  data_length = length;
1064 
1065  if (ts.seek(offset) != 0) {
1066  fprintf(stderr, "ulaw: Read error\n");
1067  return misc_read_error;
1068  }
1069  ulaw = walloc(unsigned char, data_length);
1070  if (ts.fread(ulaw,1,data_length) != data_length)
1071  {
1072  wfree(ulaw);
1073  return misc_read_error;
1074  }
1075 
1076  *data = walloc(short,data_length);
1077  ulaw_to_short(ulaw,*data,data_length);
1078  wfree(ulaw);
1079 
1080  *num_samples = data_length;
1081  *sample_rate = 8000;
1082  *num_channels = 1;
1083  *sample_type = st_short;
1084  *word_size = 2;
1085  *bo = EST_NATIVE_BO;
1086 
1087  return format_ok;
1088 }
1089 
1091  int num_samples, int num_channels,
1092  int sample_rate,
1093  enum EST_sample_type_t sample_type, int bo)
1094 {
1095  (void) sample_rate;
1096  (void) sample_type;
1097  (void) fp;
1098  (void) num_samples;
1099  (void) num_channels;
1100  (void) bo;
1101  return write_ok;
1102 }
1103 
1104 enum EST_write_status save_wave_ulaw_data(FILE *fp, const short *data, int offset,
1105  int num_samples, int num_channels,
1106  int sample_rate,
1107  enum EST_sample_type_t sample_type, int bo)
1108 {
1109  (void)sample_rate;
1110  (void)sample_type;
1111 
1112  if (data == NULL)
1113  return write_ok;
1114 
1115  return save_wave_raw(fp,data,offset,num_samples,num_channels,
1116  8000,st_mulaw,bo);
1117 }
1118 
1119 enum EST_write_status save_wave_ulaw(FILE *fp, const short *data, int offset,
1120  int num_samples, int num_channels,
1121  int sample_rate,
1122  enum EST_sample_type_t sample_type, int bo)
1123 {
1124  save_wave_ulaw_header(fp, num_samples, num_channels,
1125  sample_rate, sample_type, bo);
1126 
1127  return save_wave_ulaw_data(fp, data, offset,
1128  num_samples, num_channels,
1129  sample_rate, sample_type, bo);
1130 }
1131 
1132 
1133 
1135  *num_samples, int *num_channels, int *word_size, int
1136  *sample_rate, enum EST_sample_type_t *sample_type, int *bo,
1137  int offset, int length)
1138 
1139 {
1140  unsigned char *alaw;
1141  int data_length,samps;
1142 
1143  ts.seek_end();
1144  samps = ts.tell();
1145 
1146  if (length == 0)
1147  data_length = samps - offset;
1148  else
1149  data_length = length;
1150 
1151  if (ts.seek(offset) != 0) {
1152  fprintf(stderr, "alaw: Read error\n");
1153  return misc_read_error;
1154  }
1155  alaw = walloc(unsigned char, data_length);
1156  if (ts.fread(alaw,1,data_length) != data_length)
1157  {
1158  wfree(alaw);
1159  return misc_read_error;
1160  }
1161 
1162  *data = walloc(short,data_length);
1163  alaw_to_short(alaw,*data,data_length);
1164  wfree(alaw);
1165 
1166  *num_samples = data_length;
1167  *sample_rate = 8000;
1168  *num_channels = 1;
1169  *sample_type = st_short;
1170  *word_size = 2;
1171  *bo = EST_NATIVE_BO;
1172 
1173  return format_ok;
1174 }
1175 
1177  int num_samples, int num_channels,
1178  int sample_rate,
1179  enum EST_sample_type_t sample_type, int bo)
1180 {
1181  (void) sample_rate;
1182  (void) sample_type;
1183  (void) fp;
1184  (void) num_samples;
1185  (void) num_channels;
1186  (void) bo;
1187  return write_ok;
1188 }
1189 
1190 enum EST_write_status save_wave_alaw_data(FILE *fp, const short *data, int offset,
1191  int num_samples, int num_channels,
1192  int sample_rate,
1193  enum EST_sample_type_t sample_type, int bo)
1194 {
1195  (void)sample_rate;
1196  (void)sample_type;
1197  if (data == NULL)
1198  return write_ok;
1199  return save_wave_raw(fp,data,offset,num_samples,num_channels,
1200  8000,st_alaw,bo);
1201 }
1202 
1203 enum EST_write_status save_wave_alaw(FILE *fp, const short *data, int offset,
1204  int num_samples, int num_channels,
1205  int sample_rate,
1206  enum EST_sample_type_t sample_type, int bo)
1207 {
1208  save_wave_alaw_header(fp, num_samples, num_channels,
1209  sample_rate, sample_type, bo);
1210 
1211  return save_wave_alaw_data(fp, data, offset,
1212  num_samples, num_channels,
1213  sample_rate, sample_type, bo);
1214 }
1215 
1216 
1217 /*=======================================================================*/
1218 /* Sun and Next snd files */
1219 /*=======================================================================*/
1220 
1221 typedef struct {
1222  unsigned int magic; /* magic number */
1223  unsigned int hdr_size; /* size of this header */
1224  int data_size; /* length of data (optional) */
1225  unsigned int encoding; /* data encoding format */
1226  unsigned int sample_rate; /* samples per second */
1227  unsigned int channels; /* number of interleaved channels */
1228 } Sun_au_header;
1229 
1230 enum EST_read_status load_wave_snd(EST_TokenStream &ts, short **data, int
1231  *num_samples, int *num_channels, int *word_size, int
1232  *sample_rate,enum EST_sample_type_t *sample_type, int *bo ,
1233  int offset, int length)
1234 {
1235  /* Header structures */
1236  Sun_au_header header;
1237  enum EST_sample_type_t encoding_type;
1238  int data_length, sample_width, bytes, samps, n;
1239  unsigned char *file_data;
1240  EST_FilePos current_pos;
1241 
1242  current_pos = ts.tell();
1243  if (ts.fread(&(header.magic), sizeof(unsigned int), 1) != 1) {
1244  return misc_read_error;
1245  }
1246  if (ts.fread(&(header.hdr_size), sizeof(unsigned int), 1) != 1) {
1247  return misc_read_error;
1248  }
1249  if (ts.fread(&(header.data_size), sizeof(int), 1) != 1) {
1250  return misc_read_error;
1251  }
1252  if (ts.fread(&(header.encoding), sizeof(unsigned int), 1) != 1) {
1253  return misc_read_error;
1254  }
1255  if (ts.fread(&(header.sample_rate), sizeof(unsigned int), 1) != 1) {
1256  return misc_read_error;
1257  }
1258  if (ts.fread(&(header.channels), sizeof(unsigned int), 1) != 1) {
1259  return misc_read_error;
1260  }
1261 
1262  /* test for magic number */
1263  if ((EST_LITTLE_ENDIAN) &&
1264  ((unsigned int)0x2e736e64 == SWAPINT(header.magic)))
1265  { /* wrong byte order, swap header */
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);
1271  }
1272  else if ((unsigned int)0x2e736e64 != header.magic)
1273  return wrong_format;
1274 
1275  switch (header.encoding)
1276  {
1277  case 1:
1278  encoding_type = st_mulaw;
1279  break;
1280  case 2:
1281  encoding_type = st_uchar;
1282  break;
1283  case 3:
1284  encoding_type = st_short;
1285  break;
1286  default:
1287  fprintf(stderr, "Unsupported data type in SND header\n");
1288  return misc_read_error;
1289  }
1290 
1291  *num_channels = header.channels;
1292  sample_width = get_word_size(encoding_type);
1293  *sample_rate = header.sample_rate;
1294 
1295  if ((header.data_size == 0) ||
1296  (header.data_size == -1))
1297  {
1298  ts.seek_end();
1299  bytes = ts.tell() - header.hdr_size;
1300  }
1301  else
1302  bytes = header.data_size;
1303  samps = bytes/sample_width;
1304 
1305  if (length == 0)
1306  data_length = (samps - offset)*(*num_channels);
1307  else
1308  data_length = length *(*num_channels);
1309 
1310  if (ts.seek(current_pos+header.hdr_size+(sample_width*offset*(*num_channels))) != 0) {
1311  fprintf(stderr, "WAVE read: seek error\n");
1312  return misc_read_error;
1313  }
1314  if (data_length < 0 || (size_t) data_length > std::numeric_limits<std::size_t>::max()/(sample_width*sizeof(unsigned char))) {
1315  fprintf(stderr, "WAVE read: Too much data\n");
1316  return misc_read_error;
1317  }
1318  file_data = walloc(unsigned char, sample_width * data_length);
1319  if ((n=ts.fread(file_data,sample_width,data_length)) != data_length)
1320  {
1321  fprintf(stderr,"WAVE read: short file %s\n",
1322  (const char *)ts.filename());
1323  fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
1324  offset,n,data_length);
1325  data_length = n;
1326  }
1327 
1328  *data = convert_raw_data(file_data,data_length,encoding_type,bo_big);
1329 
1330  if (*data == NULL)
1331  return read_error;
1332 
1333  *num_samples = data_length/ (*num_channels);
1334  *sample_type = st_short;
1335  *bo = EST_NATIVE_BO;
1336  *word_size = 2;
1337  return read_ok;
1338 }
1339 
1341  int num_samples, int num_channels,
1342  int sample_rate,
1343  enum EST_sample_type_t sample_type, int bo)
1344 {
1345  (void)bo;
1346  /* Header structures */
1347  Sun_au_header header;
1348 
1349  /* Fill in header structure */
1350  header.magic = (unsigned int)0x2e736e64; /* should be a macro surely */
1351  header.hdr_size = sizeof(header); /* ! */
1352  header.data_size = get_word_size(sample_type) * num_channels * num_samples;
1353 
1354  switch (sample_type) {
1355  case st_mulaw:
1356  header.encoding = 1;
1357  break;
1358  case st_uchar:
1359  header.encoding = 2;
1360  break;
1361  case st_short:
1362  header.encoding = 3;
1363  break;
1364 
1365  default:
1366  fprintf(stderr,
1367  "Unsupported sample type cannot be saved in SND format\n");
1368  return misc_write_error;
1369 
1370  }
1371 
1372  /* check consistency */
1373 
1374  header.sample_rate = sample_rate;
1375 
1376  header.channels = num_channels;
1377 
1378  if (EST_LITTLE_ENDIAN)
1379  {
1380  /* snd files all in big endian format */
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);
1387 
1388  }
1389  /* write header */
1390  if (fwrite(&header, sizeof(header), 1, fp) != 1)
1391  return misc_write_error;
1392 
1393  return write_ok;
1394 }
1395 
1396 enum EST_write_status save_wave_snd_data(FILE *fp, const short *data, int offset,
1397  int num_samples, int num_channels,
1398  int sample_rate,
1399  enum EST_sample_type_t sample_type, int bo)
1400 {
1401  (void)sample_rate;
1402  (void)bo;
1403 
1404  if (data == NULL)
1405  return write_ok;
1406 
1407  /* snd files are always in BIG_ENDIAN (sun) byte order */
1408  return save_raw_data(fp,data,offset,num_samples,num_channels,
1409  sample_type,bo_big);
1410 }
1411 
1412 
1413 enum EST_write_status save_wave_snd(FILE *fp, const short *data, int offset,
1414  int num_samples, int num_channels,
1415  int sample_rate,
1416  enum EST_sample_type_t sample_type, int bo)
1417 {
1418  save_wave_snd_header(fp, num_samples, num_channels, sample_rate,
1419  sample_type, bo);
1420  return save_wave_snd_data(fp, data, offset, num_samples,
1421  num_channels, sample_rate, sample_type, bo);
1422 }
1423 
1424 
1425 /*=======================================================================*/
1426 /* CSTR Audlab files (from the last century) */
1427 /* They are always bigendian */
1428 /*=======================================================================*/
1429 struct s1 {
1430  char c[17];
1431  float f1;
1432  float f2;
1433 };
1434 
1435 static void init_struct_s1(struct s1& s) {
1436  memset(s.c,0,17);
1437  s.f1=0;
1438  s.f2=0;
1439  return;
1440 }
1441 
1442 struct s2 {
1443  float f1;
1444  float f2;
1445  float f3;
1446  char c1;
1447  char c2;
1448  int i1;
1449  int i2;
1450 };
1451 
1452 static void init_struct_s2(struct s2& s) {
1453  s.f1=0;
1454  s.f2=0;
1455  s.f3=0;
1456  s.c1 = 0;
1457  s.c2 = 0;
1458  s.i1 = 0;
1459  s.i2 = 0;
1460  return;
1461 }
1462 
1463 
1464 struct audlabfh {
1465  struct s1 z;
1466  char file_type[8];
1467  char c1[17];
1468  char c2[17];
1469  char c3[17];
1470  int start;
1471  char data_type;
1472  char c5[64];
1473 };
1474 
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);
1481  s.start =0;
1482  s.data_type = 0;
1483  memset(s.c5,0,64);
1484  return;
1485 }
1486 
1487 struct audlabsh {
1488  int channel_count;
1489  char serial;
1490  int sample_rate;
1491  char c1[20];
1492  int i1;
1493  char c2;
1494  char c3[121];
1495  char c4[121];
1496 
1497 };
1498 
1499 static void init_struct_audlabsh(struct audlabsh & s) {
1500  s.channel_count = 0;
1501  s.serial = 0;
1502  s.sample_rate = 0;
1503  memset(s.c1, 0, 20);
1504  s.i1 = 0;
1505  s.c2 = 0;
1506  memset(s.c3, 0, 121);
1507  memset(s.c4, 0, 121);
1508  return;
1509 }
1510 
1511 struct audlabsd {
1512  char descr[17];
1513  int sample_count;
1514  int nbits;
1515  float f1;
1516  struct s2 z;
1517 };
1518 
1519 static void init_struct_audlabsd(struct audlabsd & s) {
1520  memset(s.descr, 0, 17);
1521  s.sample_count = 0;
1522  s.nbits = 0;
1523  s.f1 = 0;
1524  init_struct_s2(s.z);
1525  return;
1526 }
1527 
1529  *num_samples, int *num_channels, int *word_size, int
1530  *sample_rate, enum EST_sample_type_t *sample_type, int *bo, int
1531  offset, int length)
1532 {
1533  /* Header structures */
1534  struct audlabfh fh;
1535  struct audlabsh sh;
1536  struct audlabsd sd;
1537  init_struct_audlabfh(fh);
1538  init_struct_audlabsh(sh);
1539  init_struct_audlabsd(sd);
1540  int data_length,sample_count;
1541  int hdr_length;
1542  int current_pos;
1543 
1544  /* Read header structures from char array */
1545  current_pos = ts.tell();
1546 
1547  if (ts.fread(&fh, sizeof(struct audlabfh), 1) != 1)
1548  return misc_read_error;
1549  if (strcmp(fh.file_type, "Sample") != 0)
1550  return wrong_format;
1551 
1552  if (ts.fread(&sh, sizeof(struct audlabsh), 1) != 1)
1553  return misc_read_error;
1554  if (ts.fread(&sd, sizeof(struct audlabsd), 1) != 1)
1555  return misc_read_error;
1556  hdr_length = sizeof(struct audlabfh) +
1557  sizeof(struct audlabsh) +
1558  sizeof(struct audlabsd);
1559 
1560  if (EST_BIG_ENDIAN)
1561  {
1562  *num_channels = sh.channel_count;
1563  *sample_rate = sh.sample_rate;
1564  sample_count = sd.sample_count;
1565  }
1566  else // audlab files are bigendian
1567  {
1568  *num_channels = SWAPINT(sh.channel_count);
1569  *sample_rate = SWAPINT(sh.sample_rate);
1570  sample_count = SWAPINT(sd.sample_count);
1571  }
1572  if (length == 0)
1573  data_length = (sample_count - offset) * (*num_channels);
1574  else
1575  data_length = length *(*num_channels);
1576 
1577  if (ts.seek(current_pos+hdr_length+(sizeof(short)*offset*(*num_channels))) != 0) {
1578  fprintf(stderr, "audlab: read error\n");
1579  return misc_read_error;
1580  }
1581  *data = walloc(short,sizeof(short) * data_length);
1582 
1583  if ((int)ts.fread(*data, sizeof(short), data_length) != data_length)
1584  {
1585  wfree(*data);
1586  return misc_read_error;
1587  }
1588  if (EST_LITTLE_ENDIAN)
1589  swap_bytes_short(*data,data_length);
1590 
1591  *num_samples = data_length / (*num_channels);
1592  *sample_type = st_short; /* set internal type*/
1593  *word_size = sizeof(short);
1594  *bo = EST_NATIVE_BO;
1595 
1596  return format_ok;
1597 }
1598 
1600  int num_samples, int num_channels,
1601  int sample_rate,
1602  enum EST_sample_type_t sample_type, int bo)
1603 {
1604  (void)bo;
1605  (void)sample_type;
1606  /* Header structures */
1607  struct audlabfh fh;
1608  struct audlabsh sh;
1609  struct audlabsd sd;
1610 
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);
1616  fh.data_type = 2;
1617  strcpy(fh.file_type, "Sample");
1618 
1619  if (EST_LITTLE_ENDIAN)
1620  { // need to swap some of those numbers
1621  sh.channel_count = SWAPINT(num_channels);
1622  sh.serial = 1;
1623  sh.sample_rate = SWAPINT(sample_rate);
1624 
1625  sd.sample_count = SWAPINT(num_samples);
1626  sd.nbits = SWAPINT(16);
1627  }
1628  else
1629  {
1630  sh.channel_count = num_channels;
1631  sh.serial = 1;
1632  sh.sample_rate = sample_rate;
1633 
1634  sd.sample_count = num_samples;
1635  sd.nbits = 16;
1636  }
1637  sprintf(sd.descr, "Filter 1");
1638 
1639  /* write headers */
1640  fwrite (&fh, sizeof(fh), 1, fp);
1641  fwrite (&sh, sizeof(sh), 1, fp);
1642  fwrite (&sd, sizeof(sd), 1, fp);
1643  return write_ok;
1644 }
1645 
1646 enum EST_write_status save_wave_audlab_data(FILE *fp, const short *data, int offset,
1647  int num_samples, int num_channels,
1648  int sample_rate,
1649  enum EST_sample_type_t sample_type, int bo)
1650 {
1651  (void)sample_rate;
1652  (void)sample_type;
1653  (void)bo;
1654  if (data == NULL)
1655  return write_ok;
1656 
1657  /* write data*/
1658  return save_raw_data(fp,data,offset,num_samples,num_channels,
1659  st_short,bo_big);
1660 }
1661 
1662 enum EST_write_status save_wave_audlab(FILE *fp, const short *data, int offset,
1663  int num_samples, int num_channels,
1664  int sample_rate,
1665  enum EST_sample_type_t sample_type, int bo)
1666 {
1667  save_wave_audlab_header(fp, num_samples, num_channels,
1668  sample_rate, sample_type, bo);
1669  return save_wave_audlab_data(fp, data, offset,
1670  num_samples, num_channels,
1671  sample_rate, sample_type, bo);
1672 }
1673 
1674 /*=======================================================================*/
1675 /* Entropic ESPS SD files: portable (non-proprietary) method */
1676 /*=======================================================================*/
1677 
1678 /* Deep thanks go to Peter Kabal from McGill University whose AF code */
1679 /* showed me this was even possible. I looked at his code to find */
1680 /* parts I couldn't work out myself. Also to Rodney Johnson of */
1681 /* Entropic whose document "ESPS APPLICATION NOTE: Non-ESPS Programs */
1682 /* and the ESPS File System" gave details of how to access ESPS files */
1683 /* without using the ESPS library code. */
1684 
1685 #include "esps_utils.h"
1686 enum EST_read_status load_wave_sd(EST_TokenStream &ts, short **data, int
1687  *num_samples, int *num_channels, int
1688  *word_size, int *sample_rate, enum
1689  EST_sample_type_t *sample_type, int *bo , int
1690  offset, int length)
1691 {
1692  /* A license free version of an esps file reading program */
1693  FILE *fd;
1694  esps_hdr hdr;
1695  int actual_bo, sample_width, data_length;
1696  enum EST_read_status rv;
1697  int dl;
1698  enum EST_sample_type_t actual_sample_type;
1699  double d;
1700  unsigned char *file_data;
1701 
1702  if ((fd = ts.filedescriptor()) == NULL)
1703  {
1704  fprintf(stderr, "Can't open esps file %s for reading\n",
1705  (const char *)ts.filename());
1706  return misc_read_error;
1707  }
1708 
1709  rv=read_esps_hdr(&hdr,fd);
1710  if (rv != format_ok) {
1711  return rv;
1712  }
1713 
1714  if (hdr->file_type != ESPS_SD)
1715  {
1716  fprintf(stderr,"ESPS file: not an FEA_SD file\n");
1717  delete_esps_hdr(hdr);
1718  wfree(hdr);
1719  return misc_read_error;
1720  }
1721 
1722  if (fea_value_d("record_freq",0,hdr,&d) != 0)
1723  {
1724  fprintf(stderr,"ESPS file: can't find sample_rate in header assuming 16000\n");
1725  *sample_rate = 16000;
1726  }
1727  else
1728  *sample_rate = (int)d;
1729  actual_sample_type = st_short; /* you're not trying hard enough */
1730  sample_width = get_word_size(actual_sample_type);
1731  *num_channels = hdr->field_dimension[0];
1732  if (hdr->swapped)
1733  actual_bo = (EST_BIG_ENDIAN ? bo_little : bo_big);
1734  else
1735  actual_bo = (EST_BIG_ENDIAN ? bo_big : bo_little);
1736 
1737  if (length == 0)
1738  data_length = (hdr->num_records - offset)*(*num_channels);
1739  else
1740  data_length = length *(*num_channels);
1741 
1742  if (EST_fseek(fd,
1743  hdr->hdr_size+(sample_width*offset*(*num_channels)),
1744  SEEK_SET) != 0) {
1745  fprintf(stderr, "WAVE read: esps: could not set file to read position");
1746  return misc_read_error;
1747  }
1748  file_data = walloc(unsigned char, sample_width * data_length);
1749  if ((dl=fread(file_data,sample_width,data_length,fd)) != data_length)
1750  {
1751  fprintf(stderr,"WAVE read: esps short file %s\n",
1752  (const char *)ts.filename());
1753  fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
1754  offset,dl,data_length);
1755  data_length = dl;
1756  }
1757 
1758  *data = convert_raw_data(file_data,data_length,
1759  actual_sample_type,
1760  actual_bo);
1761 
1762  *num_samples = data_length/ (*num_channels);
1763  *sample_type = st_short;
1764  *bo = EST_NATIVE_BO;
1765  *word_size = 2;
1766  delete_esps_hdr(hdr);
1767  wfree(hdr);
1768  return format_ok;
1769 
1770 }
1771 
1772 
1774  int num_samples, int num_channels,
1775  int sample_rate,
1776  enum EST_sample_type_t sample_type, int bo)
1777 
1778 {
1779  (void)bo;
1780  esps_hdr hdr = make_esps_sd_hdr();
1781  enum EST_write_status rv;
1782  short esps_type;
1783 
1784  hdr->num_records = num_samples;
1785  switch (sample_type)
1786  {
1787  case st_short: esps_type = ESPS_SHORT; break;
1788  case st_schar: esps_type = ESPS_CHAR; break; /* maybe should be BYTE */
1789  case st_int: esps_type = ESPS_INT; break;
1790  case st_float: esps_type = ESPS_FLOAT; break;
1791  case st_double: esps_type = ESPS_DOUBLE; break;
1792  default:
1793  fprintf(stderr,"ESPS file: no support for sample_type %s\n",
1794  sample_type_to_str(sample_type));
1795  delete_esps_hdr(hdr);
1796  wfree(hdr);
1797  return misc_write_error;
1798  }
1799  /* I believe all of the following are necessary and in this order */
1800  add_field(hdr,"samples",esps_type,num_channels);
1801  /* FIXME: What is doing this path here?? */
1802  add_fea_special(hdr,ESPS_FEA_DIRECTORY,"margo:/disk/disk10/home/awb/projects/speech_tools/main");
1804  "EDST waveform written as ESPS FEA_SD.\n\
1805  ");
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);
1809 
1810  if ((rv=write_esps_hdr(hdr,fp)) != write_ok)
1811  {
1812  delete_esps_hdr(hdr);
1813  wfree(hdr);
1814  return rv;
1815  }
1816  /* lets ignore desired bo and sample type for the time being */
1817  delete_esps_hdr(hdr);
1818  wfree(hdr);
1819  return write_ok;
1820 }
1821 
1822 
1823 enum EST_write_status save_wave_sd_data(FILE *fp, const short *data,
1824  int offset,
1825  int num_samples, int num_channels,
1826  int sample_rate,
1827  enum EST_sample_type_t sample_type, int bo)
1828 
1829 {
1830  (void)bo;
1831  (void)sample_rate;
1832  if (data == NULL)
1833  return write_ok;
1834 
1835  return save_raw_data(fp,data,offset,num_samples,num_channels,
1836  sample_type,EST_NATIVE_BO);
1837 }
1838 
1839 enum EST_write_status save_wave_sd(FILE *fp, const short *data, int offset,
1840  int num_samples, int num_channels,
1841  int sample_rate,
1842  enum EST_sample_type_t sample_type, int bo)
1843 
1844 {
1845  save_wave_sd_header(fp, num_samples, num_channels, sample_rate,
1846  sample_type, bo);
1847  return save_wave_sd_data(fp, data, offset, num_samples,
1848  num_channels, sample_rate, sample_type, bo);
1849 
1850 }
1851 
1852 /*=======================================================================*/
1853 /* Raw data files -- unheadered */
1854 /* THESE FUNCTIONS ARE DIFFERENT FROM THE REST */
1855 /* These function have more parameters than the standard file i/o */
1856 /* as information cannot be gleamed from the file */
1857 /*=======================================================================*/
1858 
1859 enum EST_read_status load_wave_raw(EST_TokenStream &ts, short **data, int
1860  *num_samples, int *num_channels,
1861  int *word_size, int
1862  *sample_rate,
1863  enum EST_sample_type_t *sample_type,
1864  int *bo, int offset, int length,
1865  int isample_rate,
1866  enum EST_sample_type_t isample_type,
1867  int ibo, int inc)
1868 {
1869  unsigned char *file_data;
1870  int data_length,samps,sample_width;
1871  int guess,i,samp;
1872  short *ndata;
1873 
1874  if (isample_type == st_ascii)
1875  {
1876  /* Guess the size */
1877  if ((offset != 0) || (length != 0))
1878  {
1879  fprintf(stderr,"Load ascii wave: doesn't support offets and lengths\n");
1880  return misc_read_error;
1881  }
1882 
1883  ts.seek_end();
1884  guess = (int)(1.2*ts.tell()/7)+10; /* rough guess of the num of samps */
1885  if (ts.seek(0) != 0) {
1886  fprintf(stderr, "Load asci wave: seek error\n");
1887  return misc_read_error;
1888  }
1889  *data = walloc(short, guess);
1890  i=0;
1891  while (!ts.eof())
1892  {
1893  samp = atoi(ts.get().string());
1894  if (i == guess)
1895  {
1896  ndata = walloc(short,(int)(guess*1.2));
1897  memmove(ndata,*data,guess*sizeof(short));
1898  wfree(*data);
1899  *data = ndata;
1900  guess = (int)(guess*1.2);
1901  }
1902  if (samp < -32768)
1903  {
1904  fprintf(stderr,"Load ascii wave: sample %d underflow clipping\n",
1905  i);
1906  (*data)[i] = -32768;
1907  }
1908  else if (samp > 32767)
1909  {
1910  fprintf(stderr,"Load ascii wave: sample %d overflow clipping\n",
1911  i);
1912  (*data)[i] = 32767;
1913  }
1914  else
1915  (*data)[i] = (short)samp;
1916  i++;
1917  }
1918  data_length = i;
1919  }
1920  else
1921  {
1922  ts.seek_end();
1923  sample_width = get_word_size(isample_type);
1924  samps = ts.tell()/sample_width;
1925 
1926  if (length == 0)
1927  data_length = samps - offset;
1928  else
1929  data_length = length;
1930 
1931  if (ts.seek(offset*sample_width*inc) != 0) {
1932  fprintf(stderr, "Error seeking in file\n");
1933  return misc_read_error;
1934  }
1935  file_data = walloc(unsigned char, data_length * sample_width *inc);
1936  if ((int)ts.fread(file_data,sample_width,data_length) != data_length) {
1937  wfree(file_data);
1938  return misc_read_error;
1939  }
1940 
1941  *data = convert_raw_data(file_data,data_length,isample_type,ibo);
1942  }
1943 
1944  *num_samples = data_length/inc;
1945  *sample_rate = isample_rate;
1946  *num_channels = inc;
1947  *sample_type = st_short;
1948  *word_size = 2;
1949  *bo = EST_NATIVE_BO;
1950 
1951  return format_ok;
1952 }
1953 
1955  int num_samples, int num_channels,
1956  int sample_rate,
1957  enum EST_sample_type_t sample_type, int bo)
1958 {
1959  (void)fp;
1960  (void)num_samples;
1961  (void)num_channels;
1962  (void)sample_rate;
1963  (void)sample_type;
1964  (void)bo;
1965  return write_ok;
1966 }
1967 
1968 enum EST_write_status save_wave_raw_data(FILE *fp, const short *data,
1969  int offset,
1970  int num_samples, int num_channels,
1971  int sample_rate,
1972  enum EST_sample_type_t sample_type, int bo)
1973 {
1974  (void)sample_rate;
1975  if (data == NULL)
1976  return write_ok;
1977 
1978  return save_raw_data(fp,data,offset,num_samples,num_channels,
1979  sample_type,bo);
1980 }
1981 
1982 enum EST_write_status save_wave_raw(FILE *fp, const short *data,
1983  int offset,
1984  int num_samples, int num_channels,
1985  int sample_rate,
1986  enum EST_sample_type_t sample_type, int bo)
1987 {
1988  (void)sample_rate;
1989 
1990  return save_wave_raw_data(fp, data, offset, num_samples,
1991  num_channels, sample_rate, sample_type, bo);
1992 }
1993 
1994 /***********************************************************************/
1995 /* */
1996 /* end of file type specific functions */
1997 /* */
1998 /***********************************************************************/
1999 
2001  const int num_samples, const int num_channels,
2002  const int sample_rate,
2003  const EST_String& stype, const int bo,
2004  const EST_String& ftype)
2005 {
2007  EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
2008  switch(t)
2009  {
2010  case wff_nist:
2011  return save_wave_nist_header(fp, num_samples, num_channels,
2012  sample_rate, sample_type, bo);
2013  break;
2014  case wff_esps:
2015  return save_wave_sd_header(fp, num_samples, num_channels,
2016  sample_rate, sample_type, bo);
2017  break;
2018  case wff_est:
2019  return save_wave_est_header(fp, num_samples, num_channels,
2020  sample_rate, sample_type, bo);
2021  break;
2022  case wff_audlab:
2023  return save_wave_audlab_header(fp, num_samples, num_channels,
2024  sample_rate, sample_type, bo);
2025  break;
2026  case wff_snd:
2027  return save_wave_snd_header(fp, num_samples, num_channels,
2028  sample_rate, sample_type, bo);
2029  break;
2030  case wff_aiff:
2031  return save_wave_aiff_header(fp, num_samples, num_channels,
2032  sample_rate, sample_type, bo);
2033  break;
2034  case wff_riff:
2035  return save_wave_riff_header(fp, num_samples, num_channels,
2036  sample_rate, sample_type, bo);
2037  break;
2038  case wff_raw:
2039  return save_wave_raw_header(fp, num_samples, num_channels,
2040  sample_rate, sample_type, bo);
2041  break;
2042  case wff_ulaw:
2043  return save_wave_ulaw_header(fp, num_samples, num_channels,
2044  sample_rate, sample_type, bo);
2045  break;
2046  default:
2047  case wff_none:
2048  cerr << "Can't save wave header to files type " << ftype << endl;
2049  break;
2050  }
2051  return write_ok;
2052 }
2053 
2054 
2055 enum EST_write_status wave_io_save_data(FILE *fp, const short * data,
2056  const int offset,
2057  const int num_samples, const int num_channels,
2058  const int sample_rate,
2059  const EST_String& stype, const int bo,
2060  const EST_String& ftype)
2061 {
2063  EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
2064  switch(t)
2065  {
2066  case wff_nist:
2067  return save_wave_nist_data(fp, data, offset, num_samples, num_channels,
2068  sample_rate, sample_type, bo);
2069  break;
2070  case wff_esps:
2071  return save_wave_sd_data(fp, data, offset, num_samples, num_channels,
2072  sample_rate, sample_type, bo);
2073  break;
2074  case wff_est:
2075  return save_wave_est_data(fp, data, offset, num_samples, num_channels,
2076  sample_rate, sample_type, bo);
2077  break;
2078  case wff_audlab:
2079  return save_wave_audlab_data(fp, data, offset, num_samples, num_channels,
2080  sample_rate, sample_type, bo);
2081  break;
2082  case wff_snd:
2083  return save_wave_snd_data(fp, data, offset, num_samples, num_channels,
2084  sample_rate, sample_type, bo);
2085  break;
2086  case wff_aiff:
2087  return save_wave_aiff_data(fp, data, offset, num_samples, num_channels,
2088  sample_rate, sample_type, bo);
2089  break;
2090  case wff_riff:
2091  return save_wave_riff_data(fp, data, offset, num_samples, num_channels,
2092  sample_rate, sample_type, bo);
2093  break;
2094  case wff_raw:
2095  return save_wave_raw_data(fp, data, offset, num_samples, num_channels,
2096  sample_rate, sample_type, bo);
2097  break;
2098  case wff_ulaw:
2099  return save_wave_ulaw_data(fp, data, offset, num_samples, num_channels,
2100  sample_rate, sample_type, bo);
2101  break;
2102  default:
2103  case wff_none:
2104  cerr << "Can't save wave data to files type " << ftype << endl;
2105  break;
2106  }
2107  return write_ok;
2108 }
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)
Definition: EST_wave_io.cc:171
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
Definition: EST_Token.cc:499
char * wstrdup(const char *s)
Definition: walloc.c:117
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
FILE * filedescriptor()
For the people who need the actual description (if possible)
Definition: EST_Token.h:380
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)
#define WAVE_FORMAT_PCM
Definition: EST_wave_io.cc:522
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)
#define SWAPSHORT(x)
Definition: EST_cutils.h:79
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)
Definition: EST_wave_io.cc:747
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)
EST_write_status
#define SWAPINT(x)
Definition: EST_cutils.h:75
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&#39;t use peek() immediately beforehand)
Definition: EST_Token.cc:368
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)
Definition: EST_wave_io.cc:527
char * cmake_tmp_filename()
Definition: EST_cutils.c:66
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
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)
Definition: esps_utils.cc:167
#define ESPS_FLOAT
Definition: esps_utils.h:142
EST_EstFileType
Definition: EST_FileType.h:50
void close(void)
Close stream.
Definition: EST_Token.cc:419
EST_TNamedEnum< EST_sample_type_t > EST_sample_type_map
const EST_String filename() const
The originating filename (if there is one)
Definition: EST_Token.h:378
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)
Definition: EST_wave_io.cc:398
EST_FilePos tell(void) const
tell, synonym for filepos
Definition: EST_Token.h:369
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)
Definition: EST_wave_io.cc:320
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)
Definition: EST_wave_io.cc:792
#define ESPS_DOUBLE
Definition: esps_utils.h:141
#define streq(X, Y)
Definition: EST_cutils.h:57
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)
#define WAVE_FORMAT_ALAW
Definition: EST_wave_io.cc:524
ENUM token(VAL value) const
int open(const EST_String &filename)
open a EST_TokenStream for a file.
Definition: EST_Token.cc:213
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)
Definition: EST_wave_io.cc:75
float max(float a, float b)
Definition: EST_cluster.cc:143
enum EST_sample_type_t nist_to_sample_type(char *type)
Definition: EST_wave_io.cc:139
#define ESPS_SHORT
Definition: esps_utils.h:144
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)
int eof()
end of file
Definition: EST_Token.h:362
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)
Definition: esps_utils.cc:989
#define misc_write_error
#define WAVE_FORMAT_ADPCM
Definition: EST_wave_io.cc:523
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)
Definition: EST_wave_io.cc:382
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 wrong_format
#define ESPS_FEA_DIRECTORY
Definition: esps_utils.h:152
CharacterEncoding encoding
Definition: rxp.c:23
void swap_bytes_short(short *data, int length)
Definition: EST_swapping.cc:97
EST_read_status read_est_header(EST_TokenStream &ts, EST_Option &hinfo, bool &ascii, EST_EstFileType &t)
Definition: est_file.cc:143
#define misc_read_error
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)
Definition: EST_wave_io.cc:685
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)
NULL
Definition: EST_WFST.cc:55
#define ESPS_CHAR
Definition: esps_utils.h:145
enum EST_write_status write_esps_hdr(esps_hdr hdr, FILE *fd)
Definition: esps_utils.cc:1242
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)
Definition: EST_wave_io.cc:368
#define EST_LITTLE_ENDIAN
Definition: EST_cutils.h:71
int EST_fseek(FILE *fp, EST_FilePos offset, int whence)
Definition: EST_File.h:75
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)
Definition: EST_wave_io.cc:467
#define ESPS_INT
Definition: esps_utils.h:143
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)
getString int
Definition: EST_item_aux.cc:50
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
const char * sample_type_to_nist(enum EST_sample_type_t sample_type)
Definition: EST_wave_io.cc:112
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)
Definition: EST_wave_io.cc:485
EST_read_status
float start(const EST_Item &item)
Definition: EST_item_aux.cc:52
#define ESPS_FEA_COMMAND
Definition: esps_utils.h:153
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)
Definition: EST_wave_io.cc:498
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)
Definition: EST_wave_io.cc:762
#define EST_warning
Definition: EST_error.h:106
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)
Definition: EST_wave_io.cc:953
void ConvertToIeeeExtended(double num, unsigned char *bytes)
#define format_ok
const EST_String pos_description()
A string describing current position, suitable for error messages.
Definition: EST_Token.cc:882
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
EST_sample_type_t
Definition: EST_wave_aux.h:105
EST_WaveFileType
Definition: EST_WaveFile.h:50
#define EST_BIG_ENDIAN
Definition: EST_cutils.h:69
#define EST_NATIVE_BO
Definition: EST_cutils.h:72
void add_fea_special(esps_hdr hdr, int type, const char *name)
Definition: esps_utils.cc:322
#define WAVE_FORMAT_MULAW
Definition: EST_wave_io.cc:525
int fea_value_d(const char *name, int pos, esps_hdr hdr, double *d)
Definition: esps_utils.cc:338
LISP fp
Definition: kkcompile.cc:63
void wfree(void *p)
Definition: walloc.c:131
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
Definition: EST_Token.cc:318
off_t EST_FilePos
Definition: EST_File.h:69
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)
#define NIST_HDR_SIZE
Definition: EST_wave_io.cc:73
void delete_esps_hdr(esps_hdr h)
Definition: esps_utils.cc:760
void add_field(esps_hdr hdr, const char *name, int type, int dimension)
Definition: esps_utils.cc:138
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)
Definition: esps_utils.cc:971
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)
Definition: EST_wave_io.cc:91
static EST_TNamedEnumI< EST_WaveFileType, Info > map
Definition: EST_WaveFile.h:154
#define SEEK_SET
Definition: system.h:20
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)