File: EST_WaveFile.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (798 lines) | stat: -rw-r--r-- 21,722 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
 /*************************************************************************/
 /*                                                                       */
 /*                Centre for Speech Technology Research                  */
 /*                     University of Edinburgh, UK                       */
 /*                      Copyright (c) 1995,1996                          */
 /*                        All Rights Reserved.                           */
 /*                                                                       */
 /*  Permission is hereby granted, free of charge, to use and distribute  */
 /*  this software and its documentation without restriction, including   */
 /*  without limitation the rights to use, copy, modify, merge, publish,  */
 /*  distribute, sublicense, and/or sell copies of this work, and to      */
 /*  permit persons to whom this work is furnished to do so, subject to   */
 /*  the following conditions:                                            */
 /*   1. The code must retain the above copyright notice, this list of    */
 /*      conditions and the following disclaimer.                         */
 /*   2. Any modifications must be clearly marked as such.                */
 /*   3. Original authors' names are not deleted.                         */
 /*   4. The authors' names are not used to endorse or promote products   */
 /*      derived from this software without specific prior written        */
 /*      permission.                                                      */
 /*                                                                       */
 /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
 /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
 /*  THIS SOFTWARE.                                                       */
 /*                                                                       */
 /*************************************************************************/
 /*                                                                       */
 /*             Author : Richard Caley <rjc@cstr.ed.ac.uk>                */
 /* -------------------------------------------------------------------   */
 /* Wave file input and output.                                           */
 /*                                                                       */
 /*************************************************************************/
#include <cstdlib>
#include "EST_Wave.h"
#include "EST_WaveFile.h"
#include "waveP.h"
#include "EST_cutils.h"
#include "EST_Option.h"
#include "EST_io_aux.h"
#include <cstdio>
#include <cmath>

void extract(EST_Wave &sig, EST_Option &al);

typedef 
EST_read_status (*standard_load_fn_fp)(EST_TokenStream &ts,
				    short **data, int *nsamp, int *nchan, 
				    int *wsize, 
				    int *srate, 
				    EST_sample_type_t *stype, int *bo,
				    int offset, int length);

typedef 
EST_write_status (*standard_save_fn_fp)(FILE *fp,
				    const short *data,
				    int offset, int nsamp,
				    int nchan, int srate,
				    EST_sample_type_t stype, int bo);

typedef
EST_write_status (*standard_save_header_fn_fp)(FILE *fp,
				    int nsamp,
				    int nchan, int srate,
				    EST_sample_type_t stype, int bo);
  
static 
EST_read_status load_using(standard_load_fn_fp fn,
			   EST_TokenStream &ts,
				  EST_Wave &wv,
				  int rate,
			          EST_sample_type_t stype, int bo, int nchan,
				  int offset, int length)
{
  short *data;
  int nsamp;
  int wsize;
  int srate = rate;

  EST_read_status status =  (*fn)(ts, 
				  &data, &nsamp, &nchan, 
				  &wsize, 
				  &srate, &stype, &bo,
				  offset, length);

  if (status == read_ok)
    {
      short *data2 = new short[nsamp*nchan];
      memcpy(data2, data, nsamp*nchan*sizeof(short));
      wfree(data);
      wv.values().set_memory(data2, 0, nsamp, nchan, TRUE);
      wv.set_sample_rate(srate);
    }

  return status;
}

static
EST_write_status save_using(standard_save_fn_fp fn,
			    FILE *fp, const EST_Wave wv,
			    EST_sample_type_t stype, int bo)
{
EST_write_status status =  (*fn)(fp,
				wv.values().memory(), 
				0, wv.num_samples(), wv.num_channels(),
				wv.sample_rate(),
				stype, bo);

return status; 
}

static
EST_write_status save_header_using(standard_save_header_fn_fp fn,
			    FILE *fp, const EST_Wave wv,
			    EST_sample_type_t stype, int bo)
{

EST_write_status status =  (*fn)(fp,
                wv.num_samples(), wv.num_channels(),
				wv.sample_rate(),
				stype, bo);
return status;
}

EST_read_status EST_WaveFile::load_nist(EST_TokenStream &ts,
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_nist,
		    ts, wv, rate,
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_nist(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_nist, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_nist_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_nist_data, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_nist_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_nist_header, fp, wv, stype, bo);
}

EST_read_status EST_WaveFile::load_est(EST_TokenStream &ts,
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
    (void) ts;
    return load_using(load_wave_est,
		    ts, wv, rate,
		    stype, bo, nchan,
		    offset, length);

}

EST_write_status EST_WaveFile::save_est(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_est,
		    fp, wv,
		    stype, bo);
}

EST_write_status EST_WaveFile::save_est_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_est_data,
		    fp, wv,
		    stype, bo);
}

EST_write_status EST_WaveFile::save_est_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_est_header,
		    fp, wv,
		    stype, bo);
}

EST_read_status EST_WaveFile::load_aiff(EST_TokenStream &ts, 
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_aiff,
		    ts, wv,  rate, 
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_aiff(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_aiff, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_aiff_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_aiff_data, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_aiff_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_aiff_header, fp, wv, stype, bo);
}

EST_read_status EST_WaveFile::load_riff(EST_TokenStream &ts,
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_riff,
		    ts, wv, rate, 
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_riff(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_riff, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_riff_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_riff_data, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_riff_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_riff_header, fp, wv, stype, bo);
}

EST_read_status EST_WaveFile::load_esps(EST_TokenStream &ts, 
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_sd,
		    ts, wv, rate, 
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_esps(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_sd,
		    fp, wv,
		    stype, bo);
}

EST_write_status EST_WaveFile::save_esps_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_sd_data,
		    fp, wv,
		    stype, bo);
}

EST_write_status EST_WaveFile::save_esps_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_sd_header,
		    fp, wv,
		    stype, bo);
}

EST_read_status EST_WaveFile::load_audlab(EST_TokenStream &ts, 
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_audlab,
		    ts, wv, rate, 
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_audlab(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_audlab, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_audlab_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_audlab_data, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_audlab_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_audlab_header, fp, wv, stype, bo);
}

EST_read_status EST_WaveFile::load_snd(EST_TokenStream &ts, 
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_snd,
		    ts, wv, rate, 
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_snd(FILE *fp,
					const EST_Wave &wv,
					EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_snd, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_snd_data(FILE *fp,
					const EST_Wave &wv,
					EST_sample_type_t stype, int bo)
{
  return save_using(save_wave_snd_data, fp, wv, stype, bo);
}

EST_write_status EST_WaveFile::save_snd_header(FILE *fp,
					const EST_Wave &wv,
					EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_snd_header, fp, wv, stype, bo);
}



EST_read_status EST_WaveFile::load_raw(EST_TokenStream &ts, 
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  short *data;
  int nsamp;
  int wsize;
  int srate = rate;

  EST_read_status status =  load_wave_raw(ts, 
					  &data, &nsamp, &nchan, 
					  &wsize, 
					  &srate, &stype, &bo,
					  offset, length,
					  rate, stype, bo, nchan);

  if (status == read_ok)
    {
      wv.values().set_memory(data, 0, nsamp, nchan, TRUE);
      wv.set_sample_rate(srate);
    }

  return status;
}

EST_write_status EST_WaveFile::save_raw(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
EST_write_status status =  save_wave_raw(fp,
					 (short *)wv.values().memory(), 
					 0, wv.num_samples(), wv.num_channels(),
					 wv.sample_rate(),
					 stype, bo);
return status; 
}

EST_write_status EST_WaveFile::save_raw_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
return save_raw(fp, wv, stype, bo);
}


EST_write_status EST_WaveFile::save_raw_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
  return save_header_using(save_wave_raw_header, fp, wv, stype, bo);
}

EST_read_status EST_WaveFile::load_ulaw(EST_TokenStream &ts,
			  EST_Wave &wv,
			  int rate,
			  EST_sample_type_t stype, int bo, int nchan,
			  int offset, int length)
{
  return load_using(load_wave_ulaw,
		    ts, wv, rate, 
		    stype, bo, nchan,
		    offset, length);
}

EST_write_status EST_WaveFile::save_ulaw(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
    EST_Wave localwv = wv;
    localwv.resample(8000);
    return save_using(save_wave_ulaw, fp, localwv, stype, bo);
}

EST_write_status EST_WaveFile::save_ulaw_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
    EST_Wave localwv = wv;
    localwv.resample(8000);
    return save_using(save_wave_ulaw_data, fp, localwv, stype, bo);
}


EST_write_status EST_WaveFile::save_ulaw_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
    EST_Wave localwv = wv;
    localwv.resample(8000);
    return save_header_using(save_wave_ulaw_header, fp, localwv, stype, bo);
}

EST_read_status EST_WaveFile::load_alaw(EST_TokenStream &ts,
  EST_Wave &wv,
  int rate,
  EST_sample_type_t stype, int bo, int nchan,
  int offset, int length)
{
  return load_using(load_wave_alaw,
    ts, wv, rate, 
    stype, bo, nchan,
    offset, length);
}

EST_write_status EST_WaveFile::save_alaw_header(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
    EST_Wave localwv = wv;
    localwv.resample(8000);
    return save_header_using(save_wave_alaw_header, fp, localwv, stype, bo);
}

EST_write_status EST_WaveFile::save_alaw_data(FILE *fp,
					 const EST_Wave &wv,
					 EST_sample_type_t stype, int bo)
{
    EST_Wave localwv = wv;
    localwv.resample(8000);
    return save_using(save_wave_alaw_data, fp, localwv, stype, bo);
}

EST_write_status EST_WaveFile::save_alaw(FILE *fp,
 const EST_Wave &wv,
 EST_sample_type_t stype, int bo)
{
    EST_Wave localwv = wv;
    localwv.resample(8000);
    return save_using(save_wave_alaw, fp, localwv, stype, bo);
}

static int parse_esps_r_option(EST_String arg, int &offset, int &length)
{
    EST_String s, e;

    if (arg.contains("-"))
    {
	s = arg.before("-");
	e = arg.after("-");
    }
    else if (arg.contains(":"))
    {
	s = arg.before(":");
	e = arg.after(":");
    }
    else
    {
	cerr << "Argument to -r is illformed " << arg << endl;
	return -1;
    }
    
    if (!s.matches(RXint))
    {
	cerr << "First argument to -r must be an integer " << arg << endl;
	return -1;
    }

    offset = atoi(s);
    if (e.contains("+"))
    {	
	e = e.after("+");
	length = atoi(e);
    }
    else
	length = atoi(e) - offset;

    if (length <= 0)
    {
	cerr << "length is negative or zero " << arg << endl;
	return -1;
    }

    return 0;
}

EST_read_status read_wave(EST_Wave &sig, const EST_String &in_file, 
			  EST_Option &al)
{
    char *sr;
    EST_String fname, file_type, sample_type;
    int sample_rate;
    EST_read_status rval;
    int num_channels;
    int offset=0, length=0;
    int bo;
    
    if (in_file == "-")
	fname = stdin_to_file();
    else
	fname = in_file;

    if (al.present("-n"))
	num_channels = al.ival("-n", 0);
    else 
	num_channels = 1;

    if (al.present("-ulaw"))
    {
	al.add_item("-itype","ulaw");
	al.add_item("-f","8000");
    }
    if (al.present("-alaw"))
    {
al.add_item("-itype","alaw");
al.add_item("-f","8000");
    }
    if (al.present("-iswap"))
	al.add_item("-ibo","other");

    if (al.present("-istype"))
	sample_type = al.val("-istype");
    else 
	sample_type = sig.sample_type(); // else default type;

    if (al.present("-itype"))
	file_type = al.val("-itype");  // else default type;
    else
	file_type = "undef";


    if (al.present("-f"))
	sample_rate = al.ival("-f", 0);
    else if ((sr = getenv("NA_PLAY_SAMPLE_RATE")) != NULL)
    {
	sample_rate = atoi(sr);
	cerr << "Warning: no sample rate specified, " <<
	    " using NA_PLAY_SAMPLE_RATE environment variable\n";
    }
    else
    {
      sample_rate = EST_Wave::default_sample_rate;
	if (file_type == "raw")
	    cerr << "Warning: no sample rate specified - using default " << 
		sample_rate << endl;
    }
    
    if (file_type == "ulaw")
    {
	sample_rate = 8000;
	sample_type = "mulaw";
    }
	
    if (al.present("-r")) // only load in part of waveform
    {
	if (parse_esps_r_option(al.val("-r"), offset, length) != 0)
	    return read_error;
    }
    else
	offset = length = 0;

    if (al.present("-iswap"))
	bo = str_to_bo("swap");
    else
	bo = str_to_bo("native");
    if (al.present("-ibo"))   // can override -iswap
	bo = str_to_bo(al.val("-ibo"));

    if (file_type == "" ||file_type == "undef")
      rval = sig.load(fname, offset, length, sample_rate);
    else
      rval = sig.load_file(fname,file_type, sample_rate,
			   sample_type, bo, num_channels, offset, length);

    if ((rval == wrong_format) && (al.present("-basic")))
    {
	// For HTML audio/basic, it seems to mean headered or ulaw 8k
	// so try to load it again as ulaw 8k.
	rval = sig.load_file(fname, "raw", 8000,
			     "mulaw", bo, 1, offset, length);
    }
    if (rval != format_ok)
    {
	if (in_file == "-") unlink(fname);
	cerr << "Cannot recognize file format or cannot access file: \"" << in_file << "\"\n";
	return read_error;
    }
    if (file_type == "alaw")
    {
sample_rate = 8000;
sample_type = "alaw";
    }

    if (al.present("-start") || al.present("-end") 
	|| al.present("-to") || al.present("-from"))
	extract(sig, al);

    if (in_file == "-") unlink(fname);
    return read_ok;
}

EST_write_status write_wave(EST_Wave &sig, const EST_String &out_file, 
			    EST_Option &al)
{
    EST_String file_type, sample_type;
    int bo;

    if (al.present("-otype"))
	file_type = al.val("-otype");
    else
	file_type = sig.file_type();

    if (al.present("-ostype"))
	sample_type = al.val("-ostype");
    else
	sample_type = "undef";

    if (al.present("-oswap"))
	bo = str_to_bo("swap");
    else
	bo = str_to_bo("native");

    if (al.present("-obo"))   // can over ride -oswap
	bo = str_to_bo(al.val("-obo"));

    if (sample_type == "undef" || sample_type == "")
      sample_type = "short";

    if (sig.save_file(out_file, file_type,
		      sample_type, bo) != write_ok)
    {
	cerr << "Cannot write file: \"" << out_file << "\"\n";
	return write_error;
    }

    return write_ok;
}

EST_String EST_WaveFile::options_short(void)
{
    EST_String s("");
    
    for(int n=0; n< EST_WaveFile::map.n() ; n++)
    {
	const char *nm = EST_WaveFile::map.name(EST_WaveFile::map.token(n));
	
	if (s != "")
	    s += ", ";
	
	s += nm;
	
    }
    return s;
}

EST_String EST_WaveFile::options_supported(void)
{
    EST_String s("Available wave file formats:\n");
    
    for(int n=0; n< EST_WaveFile::map.n() ; n++)
    {
	const char *nm = EST_WaveFile::map.name(EST_WaveFile::map.token(n));
	const char *d = EST_WaveFile::map.info(EST_WaveFile::map.token(n)).description;
	
	s += EST_String::cat("        ", nm, EST_String(" ")*(12-strlen(nm)), d, "\n");
    }
    return s;
}

typedef struct TInfo {
  bool recognise;
  const char *description;
} TInfo;

// note the order here defines the order in which loads are tried.


static 
EST_TValuedEnumDefinition<EST_WaveFileType, const char *, EST_WaveFile::Info> wavefile_names[] =
{
  { wff_none,	{ NULL }, 
    { FALSE, NULL, NULL, NULL, NULL, "unknown track file type"} },
  { wff_nist,	{ "nist", "timit" }, 
    { TRUE, EST_WaveFile::load_nist,  EST_WaveFile::save_nist,
      EST_WaveFile::save_nist_header, EST_WaveFile::save_nist_data,
      "nist/timit" } },
  { wff_est,	{ "est"}, 
    { TRUE, EST_WaveFile::load_est,  EST_WaveFile::save_est,
      EST_WaveFile::save_est_header, EST_WaveFile::save_est_data,
      "est" } },
  { wff_esps,	{ "esps", "sd"}, 
    { TRUE,  EST_WaveFile::load_esps,  EST_WaveFile::save_esps,
      EST_WaveFile::save_esps_header, EST_WaveFile::save_esps_data,
      "esps SD waveform" } },
  { wff_audlab, { "audlab", "vox"}, 
    { TRUE,  EST_WaveFile::load_audlab,  EST_WaveFile::save_audlab,
      EST_WaveFile::save_audlab_header, EST_WaveFile::save_audlab_data,
      "audlab waveform" } },
  { wff_snd,	{ "snd", "au"}, 
    { TRUE,  EST_WaveFile::load_snd,  EST_WaveFile::save_snd,
      EST_WaveFile::save_snd_header, EST_WaveFile::save_snd_data,
      "Sun snd file" } },
  { wff_aiff,	{ "aiff" }, 
    { TRUE,  EST_WaveFile::load_aiff,  EST_WaveFile::save_aiff,
      EST_WaveFile::save_aiff_header, EST_WaveFile::save_aiff_data,
      "Apple aiff file" } },
  { wff_riff,	{ "riff", "wav" }, 
    { TRUE,  EST_WaveFile::load_riff,  EST_WaveFile::save_riff,
      EST_WaveFile::save_riff_header, EST_WaveFile::save_riff_data,
      "Microsoft wav/riff file" } },
  { wff_raw,	{ "raw" }, 
    { FALSE,  EST_WaveFile::load_raw,  EST_WaveFile::save_raw,
      EST_WaveFile::save_raw_header, EST_WaveFile::save_raw_data,
      "Headerless File" } },
  { wff_ulaw,	{ "ulaw", "basic" }, 
    { FALSE,  EST_WaveFile::load_ulaw,  EST_WaveFile::save_ulaw,
      EST_WaveFile::save_ulaw_header, EST_WaveFile::save_ulaw_data,
      "Headerless 8K ulaw  File" } },
  { wff_none,	{NULL} }
};

EST_TNamedEnumI<EST_WaveFileType, EST_WaveFile::Info> EST_WaveFile::map(wavefile_names);

#if defined(INSTANTIATE_TEMPLATES)

#include "../base_class/EST_TNamedEnum.cc"
template class EST_TNamedEnumI<EST_WaveFileType, EST_WaveFile::Info>;
template class EST_TValuedEnumI<EST_WaveFileType, const char *, 
EST_WaveFile::Info>;

#endif