File: EST_wave_io.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 (1890 lines) | stat: -rw-r--r-- 60,312 bytes parent folder | download | duplicates (2)
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                         Copyright (c) 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 :  Alan Black and Paul Taylor                 */
/*                  Date   :  June 1996                                  */
/*-----------------------------------------------------------------------*/
/*   File I/O functions specific to various file formats                 */
/*                                                                       */
/*   Note that internally data will always be shorts and                 */
/*   native byte order, conversions to/from other byte formats           */
/*   or encodings happend at read/write time                             */
/*                                                                       */
/*=======================================================================*/
#include <cstdlib> 
#include <cstdio>
#include "EST_unix.h"
#include <cstring>
#include "EST_wave_aux.h"
#include "EST_wave_utils.h"
#include "EST_strcasecmp.h"
#include "waveP.h"
#include "EST_FileType.h"

static int def_load_sample_rate = 16000;

/*************************************************************************/
/*                                                                       */
/* Functions specific for each file format                               */
/*                                                                       */
/*************************************************************************/

/*=======================================================================*/
/* Sphere Nist files                                                     */
/*=======================================================================*/

static const char *NIST_SIG = "NIST_1A\n   1024\n";
static const char *NIST_END_SIG = "end_head\n";
#define NIST_HDR_SIZE 1024

int nist_get_param_int(const char *hdr, const char *field, int def_val)
{
    const char *p;
    int val;

    if (((p=strstr(hdr,field)) != NULL) &&
	(strncmp(" -i ",p+strlen(field),4) == 0))
    {
	sscanf(p+strlen(field)+4,"%d",&val);
	return val;
    }
    else
	return def_val;

}

char *nist_get_param_str(const char *hdr, const char *field, const char *def_val)
{
    const char *p;
    char *val;
    int size;

    if (((p=strstr(hdr,field)) != NULL) &&
	(strncmp(" -s",p+strlen(field),3) == 0))
    {
	sscanf(p+strlen(field)+3,"%d",&size);
	val = walloc(char,size+1);
	/* Hmm don't know how long the %d is so do it again */
	sscanf(p+strlen(field)+3,"%d %s",&size,val);
	return val;
    }
    else
	return wstrdup(def_val);


}

const char *sample_type_to_nist(enum EST_sample_type_t sample_type)
{
    const char *c;
    switch (sample_type) {
    case st_unknown:
	c = ""; break;
    case st_schar:
	c = "PCM-1"; break;
    case st_alaw:
	c = "ALAW"; break;
    case st_mulaw:
	c = "ULAW"; break;
    case st_short:
	c = "pcm"; break;
    case st_int:
	c = "PCM-4"; break;
    case st_float:
	c = "REAL"; break;
      case st_double:
	c = "REAL"; break;
    default:
	fprintf(stderr,"Unknown sample type for nist");
	c = "";
    }
    return c;
}

enum EST_sample_type_t nist_to_sample_type(char *type)
{
    if ((streq(type,"pcm")) ||
	(streq(type,"PCM")) ||
	(streq(type,"pcm-2")))
	return st_short;
    if (strcmp(type,"pcm,embedded-shorten-v1.1") == 0)
	return st_shorten;
    else if ((EST_strcasecmp(type,"ULAW",NULL) == 0) ||
	     (EST_strcasecmp(type,"U-LAW",NULL) == 0) ||
	     (EST_strcasecmp(type,"mu-law",NULL) == 0) ||
	     (EST_strcasecmp(type,"mulaw",NULL) == 0))
	return st_mulaw;
    else if ((EST_strcasecmp(type,"ALAW",NULL) == 0) ||
	     (EST_strcasecmp(type,"A-LAW",NULL) == 0))
	return st_alaw;
    else if (strcmp(type,"alaw") == 0)
	return st_alaw;
    else if (strcmp(type,"PCM-1") == 0)
	return st_schar;
    else if (strcmp(type,"PCM-4") == 0)
	return st_int;
    else if (strcmp(type,"REAL") == 0)
	return st_float;
    else

    {
	fprintf(stderr,"NIST: unknown sample type: %s\n",type);
	return st_unknown;
    }
}

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)

{
    char header[NIST_HDR_SIZE];
    int samps,sample_width,data_length,actual_bo;
    unsigned char *file_data;
    enum EST_sample_type_t actual_sample_type;
    char *byte_order, *sample_coding;
    int n;
    int current_pos;

    current_pos = ts.tell();
    if (ts.fread(header,NIST_HDR_SIZE,1) != 1)
	return wrong_format;

    if (strncmp(header,NIST_SIG,strlen(NIST_SIG)) != 0)
	return wrong_format;

    samps = nist_get_param_int(header,"sample_count",-1);
    *num_channels = nist_get_param_int(header,"channel_count",1);
    sample_width = nist_get_param_int(header,"sample_n_bytes",2);
    *sample_rate = 
	nist_get_param_int(header,"sample_rate",def_load_sample_rate);
    byte_order = nist_get_param_str(header,"sample_byte_format",
				    (EST_BIG_ENDIAN ? "10" : "01"));
    sample_coding = nist_get_param_str(header,"sample_coding","pcm");
    if (streq(byte_order,"mu-law"))
    {
	byte_order = wstrdup((EST_BIG_ENDIAN ? "10" : "01"));
	sample_coding = wstrdup("ULAW");
    }
    if (streq(byte_order,"a-law"))
    {
	byte_order = wstrdup((EST_BIG_ENDIAN ? "10" : "01"));
	sample_coding = wstrdup("ALAW");
    }

    /* code for reading in Tony Robinson's shorten files.
       This is a temporary fix which calls the unshorten program on the
       speech file and reads in the answer by calling this function.
       It would be nice to have a simple library routine which did the
       unshortening. 
       removed Jun 14th 2016 by awb , security risk, and only works in CSTR 
       */
    
    if (streq(sample_coding,"pcm,embedded-shorten-v1.1"))
    {
#if 0
	char *tmpfile, *cmdstr;
	enum EST_read_status rval;
#endif

	fprintf(stderr,"WAVE read: nist type is shorten\n");
	fprintf(stderr,"WAVE read: no support for shorten -- you need to use some external program to unshorten the data\n");
	return misc_read_error;

#if 0
	tmpfile = cmake_tmp_filename();
	cmdstr = walloc(char,strlen(tmpfile)+200);
        /* This doesn't work, unless you have cstrshorten, and */
        /* doesn't work if your file name is interesting */
	sprintf(cmdstr,"cstrshorten %s %s",
		(const char*)ts.filename(),tmpfile);
	printf("Command: %s\n", cmdstr);
	system(cmdstr);
	EST_TokenStream tt;
	tt.open(tmpfile);
	
	rval = load_wave_nist(tt, data, num_samples,
			      num_channels, word_size, sample_rate,
			      sample_type, bo, offset, length);
	unlink(tmpfile);
	wfree(tmpfile);
	wfree(cmdstr);
	tt.close();
	return rval;
#endif
    }

    if (length == 0)
	data_length = (samps - offset)*(*num_channels);
    else
	data_length = length*(*num_channels);

    file_data = walloc(unsigned char,sample_width * data_length);

    ts.seek(current_pos+NIST_HDR_SIZE+(sample_width*offset*(*num_channels)));

    n = ts.fread(file_data,sample_width,data_length);

    if ((n < 1) && (n != data_length))
    {
	wfree(file_data); 
	wfree(sample_coding);
	wfree(byte_order);
	return misc_read_error;
    }
    else if ((n < data_length) && (data_length/(*num_channels) == n))
    {
	fprintf(stderr,"WAVE read: nist header is (probably) non-standard\n");
	fprintf(stderr,"WAVE read: assuming different num_channel interpretation\n");
	data_length = n;   /* wrongly headered file */
    }
    else if (n < data_length)
    {
	fprintf(stderr,"WAVE read: short file %s\n",
		(const char *)ts.filename());
	fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
		offset,n,data_length);
	data_length = n;
    }

    actual_sample_type = nist_to_sample_type(sample_coding);
    actual_bo = ((strcmp(byte_order,"10") == 0) ? bo_big : bo_little);

    *data = convert_raw_data(file_data,data_length,
			     actual_sample_type,actual_bo);

    *num_samples = data_length/ (*num_channels);
    *sample_type = st_short;
    *bo = EST_NATIVE_BO;
    *word_size = 2;
    wfree(sample_coding);
    wfree(byte_order);

    return format_ok;
}

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)
{
    char h[1024], p[1024];
    const char *t;
    
    memset(h,0,1024);
    
    strcat(h, NIST_SIG);
    sprintf(p, "channel_count -i %d\n", num_channels);
    strcat(h, p);
    sprintf(p, "sample_count -i %d\n", num_samples);	
    strcat(h, p);
    sprintf(p, "sample_rate -i %d\n", sample_rate);	
    strcat(h, p);
    
    t = sample_type_to_nist(sample_type);
    if (t)
    {
	sprintf(p, "sample_coding -s%d %s\n", (signed)strlen(t), t);  
	strcat(h, p);
	sprintf(p, "sample_n_bytes -i %d\n", get_word_size(sample_type));
	strcat(h, p);
    }
    
    if (get_word_size(sample_type) > 1)
    {
	sprintf(p, "sample_byte_format -s%d %s\n", 2, 
		((bo == bo_big) ? "10" : "01"));
	strcat(h, p);
    }
    
    strcat(h, NIST_END_SIG);
    /*makes it nice to read */
    strcat(h, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); 
    
    if (fwrite(&h, 1024, 1, fp) != 1)
	    return misc_write_error;
    
    return write_ok;
}


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)
{
    if (data == NULL)
       return write_ok;

    return save_raw_data(fp,data,offset,num_samples,num_channels,
			 sample_type,bo);
    
}

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)
{
    save_wave_nist_header(fp, num_samples, num_channels,
				      sample_rate, sample_type, bo);
    return save_wave_nist_data(fp, data, offset,
				     num_samples, num_channels,
				     sample_rate, sample_type, bo);
}

/*=======================================================================*/
/* EST's own format                                                      */
/*=======================================================================*/

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)
{
    int data_length, actual_bo;
    short *file_data;
    EST_String byte_order;
    int n;
    EST_EstFileType t;
    EST_Option hinfo;
    bool ascii;
    EST_read_status r;
    EST_sample_type_t actual_sample_type;
    
    offset = 0;
    
    if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
	return r;
    if (t != est_file_wave)
	return misc_read_error;
    
    *num_samples = hinfo.ival("NumSamples");
    *num_channels = hinfo.ival("NumChannels");
    *sample_rate = hinfo.ival("SampleRate");
    
    byte_order = hinfo.val("ByteOrder");
    
    if (length == 0)
	data_length = (*num_samples)*(*num_channels);
    else
	data_length = length*(*num_channels);
    
    file_data = walloc(short, data_length);
    
    n = ts.fread(file_data, sizeof(short), data_length);
    if ((n != data_length) && (n < 1))
    {
	cerr << "EST wave load: " << ts.pos_description() << endl;
	cerr << "failed to read file\n";
	wfree(file_data); 
	return misc_read_error;
    }
    else if (n != data_length)
    {
	cerr << "Wrong number of samples/channels in EST wave file\n";  
	cerr << ts.pos_description() << " ";
	cerr << "expected " << data_length << " got " << n << endl;
	data_length = n;
    }
    
    actual_bo = (byte_order == "10") ? bo_big : bo_little;
    if (hinfo.present("SampleType"))
	actual_sample_type = str_to_sample_type(hinfo.val("SampleType"));
    else
	actual_sample_type = st_short; // some older files don't have this
    
    *data = convert_raw_data((unsigned char *)file_data, 
			     data_length, actual_sample_type, actual_bo);
    // because internally data is always shorts
    *sample_type = st_short;
    *bo = EST_NATIVE_BO;
    *word_size = 2;
    
    return format_ok;
}

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)
{
    fprintf(fp, "EST_File wave\n");
    fprintf(fp, "DataType binary\n");
    fprintf(fp, "SampleRate %d\n", sample_rate);
    fprintf(fp, "NumSamples %d\n", num_samples);
    fprintf(fp, "NumChannels %d\n", num_channels);
    fprintf(fp, "SampleType %s\n", sample_type_to_str(sample_type));
    if (get_word_size(sample_type) > 1)
	fprintf(fp, "ByteOrder %s\n", ((bo == bo_big) ? "10" : "01"));
    
    fprintf(fp, "EST_Header_End\n");
    return write_ok;
}

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)
{
    if (data == NULL)
       return write_ok;

    return save_raw_data(fp, data, offset, num_samples, num_channels,
			 sample_type, 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)
{
    save_wave_est_header(fp, num_samples, num_channels,
				         sample_rate, sample_type, bo);
    
    return save_wave_est_data(fp, data, offset,
				       num_samples, num_channels,
				       sample_rate, sample_type, bo);
}

/*=======================================================================*/
/*  Microsoft RIFF (.wav) audio files                                    */
/*                                                                       */
/*  The information on this format was gained by reading a document      */
/*  found on the net called "Multimedia Programming Interface and        */
/*  Data Specification v1.0" and by looking at Rick Richardson,          */
/*  Lance Norskog And Sundry Contributors code in SOX.  All this code    */
/*  is rewritten from scratch though, but I couldn't do it without       */
/*  other's explanations. I would have used the SOX code directly but    */
/*  was not really in the right form so starting again was easier        */
/*=======================================================================*/
#define WAVE_FORMAT_PCM    0x0001
#define WAVE_FORMAT_ADPCM  0x0002
#define WAVE_FORMAT_ALAW   0x0006
#define WAVE_FORMAT_MULAW  0x0007

enum EST_read_status load_wave_riff(EST_TokenStream &ts, short **data, int
				    *num_samples, int *num_channels, int
				    *word_size, int *sample_rate, enum
				    EST_sample_type_t *sample_type, int *bo , int
				    offset, int length)
{
    char info[4];
    int samps,sample_width,data_length;
    short shortdata;
    int dsize,intdata;
    unsigned char *file_data;
    enum EST_sample_type_t actual_sample_type;
    
    if (ts.fread(info,sizeof(char),4) != 4)
	return wrong_format;	/* its almost definitely an error */
    if (strncmp(info,"RIFF",4) != 0)
	return wrong_format;

    /* We've got a riff file */
    /* Next 4 bytes are the file size */
    if(ts.fread(&dsize,4,1) != 1) return misc_read_error;
    /* .wav files are always little endian */
    if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
    if ((ts.fread(info,sizeof(char),4) != 4) ||
	(strncmp(info,"WAVE",4) != 0))
    {
	fprintf(stderr, "RIFF file is not of type WAVE\n");
	return misc_read_error;	/* not a wave file */
    }
    if ((ts.fread(info,sizeof(char),4) != 4) ||
	(strncmp(info,"fmt ",4) != 0))
	return misc_read_error;	/* something else wrong */

    if (ts.fread(&dsize,4,1) != 1) return misc_read_error;
    if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
    if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;
    if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);

    switch (shortdata)
    {
	/* This is a non-proprietary format */
    case WAVE_FORMAT_PCM:
	actual_sample_type = st_short; break;
	/* The follow are registered proprietary WAVE formats  (?) */
    case WAVE_FORMAT_MULAW:
	actual_sample_type = st_mulaw; break;
    case WAVE_FORMAT_ALAW:
	actual_sample_type = st_alaw; break;
    case WAVE_FORMAT_ADPCM:
	fprintf(stderr, "RIFF file: unsupported proprietary sample format ADPCM\n");
	actual_sample_type = st_short;
	break;
	/*	  actual_sample_type = st_adpcm; break; */ /* yes but which adpcm ! */
    default:
	fprintf(stderr, "RIFF file: unknown sample format\n");
	actual_sample_type = st_short;
	/*	return misc_read_error; */
    }
    if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;
    if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
    *num_channels = shortdata;
    if (ts.fread(sample_rate,4,1) != 1) return misc_read_error;
    if (EST_BIG_ENDIAN) *sample_rate = SWAPINT(*sample_rate);
    if (ts.fread(&intdata,4,1) != 1) return misc_read_error; /* average bytes per second -- ignored */
    if (EST_BIG_ENDIAN) intdata = SWAPINT(intdata);
    if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;	/* block align ? */
    if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);
    if (ts.fread(&shortdata,2,1) != 1) return misc_read_error;
    if (EST_BIG_ENDIAN) shortdata = SWAPSHORT(shortdata);

    sample_width = (shortdata+7)/8;
    if ((sample_width == 1) && (actual_sample_type == st_short))
	actual_sample_type = st_uchar; /* oops I meant 8 bit */

    ts.seek((dsize-16)+ts.tell());     /* skip rest of header */
    while (1)
    {
	if (ts.fread(info,sizeof(char),4) != 4)
	{
	    fprintf(stderr,"RIFF file truncated\n");
	    return misc_read_error;	/* something else wrong */
	}
	if (strncmp(info,"data",4) == 0)
	{
	    if (ts.fread(&samps,4,1) != 1) return misc_read_error;
	    if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
	    samps /= (sample_width*(*num_channels));
	    break;
	}
	else if (strncmp(info,"fact",4) == 0)
	{			/* some other type of chunk -- skip it */
	    if (ts.fread(&samps,4,1) != 1) return misc_read_error;
	    if (EST_BIG_ENDIAN) samps = SWAPINT(samps);
	    ts.seek(samps+ts.tell());	/* skip rest of header */
	    /* Hope this is the right amount */
	}
	else
	{
            //	    fprintf(stderr,"Ignoring unsupported chunk type \"%c%c%c%c\" in RIFF file\n",
            //    info[0],info[1],info[2],info[3]);
	    //return misc_read_error;
	    if(ts.fread(&dsize,4,1) != 1) return misc_read_error;
	    if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
	    ts.seek(dsize+ts.tell());     /* skip this chunk */
	}
    }
    if (length == 0)
	data_length = (samps - offset)*(*num_channels);
    else
	data_length = length*(*num_channels);
    
    file_data = walloc(unsigned char,sample_width * data_length);
    
    ts.seek((sample_width*offset*(*num_channels))+ts.tell());
    if ((dsize=ts.fread(file_data,sample_width,data_length)) != data_length)
    {
	/*  It seems so many WAV files have their datasize wrong I'll */
	/*  let it through -- I think SOX is a major culprit          */
	if (length == 0) /* the file did the identification */
	    fprintf(stderr,"Unexpected end of file but continuing (apparently missing %d samples)\n",data_length-dsize);
	else
	{
	    fprintf(stderr,"Unexpected end of file: (missing %d samples)\n",data_length-dsize);
	    wfree(file_data);
	    return misc_read_error;
	}
    }
    
    *data = convert_raw_data(file_data,dsize,
			     actual_sample_type, bo_little);
    
    *num_samples = dsize / (*num_channels);
    *sample_type = st_short;
    *bo = EST_NATIVE_BO;
    *word_size = 2;
    
    return format_ok;
}

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)
{
    (void)bo;
    const char *info;
    int data_size, data_int;
    short data_short;

    if (sample_type == st_schar)
      {
	EST_warning("RIFF format: Signed 8-bit not allowed by this file format");
	sample_type=st_uchar; 
      }
    
    info = "RIFF"; fwrite(info,4,1,fp);
    data_size = num_channels*num_samples*get_word_size(sample_type)+ 8+16+12;
    /* WAV files are always LITTLE_ENDIAN (i.e. intel x86 format) */
    if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
    fwrite(&data_size,1,4,fp);	/* total number of bytes in file */
    info = "WAVE"; fwrite(info,4,1,fp);
    info = "fmt "; fwrite(info,4,1,fp);
    data_size = 16;
    if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
    fwrite(&data_size,1,4,fp);	/* size of header */
    switch (sample_type)
    {
    case st_short:  data_short = WAVE_FORMAT_PCM; break;
    case st_uchar: data_short = WAVE_FORMAT_PCM; break;
    case st_mulaw: data_short = WAVE_FORMAT_MULAW; break;
    case st_alaw: data_short = WAVE_FORMAT_ALAW; break;
    case st_adpcm: data_short = WAVE_FORMAT_ADPCM; break;
    default:
      fprintf(stderr,"RIFF format: unsupported data format %d\n",
	      sample_type);
      return misc_write_error;
	       }
    if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
    fwrite(&data_short,1,2,fp);	/* sample type */
    data_short = num_channels;
    if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
    fwrite(&data_short,1,2,fp);	/* number of channels */
    data_int = sample_rate;
    if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);	/* sample rate */
    data_int = sample_rate * num_channels * get_word_size(sample_type);
    if (EST_BIG_ENDIAN) data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);	/* Average bytes per second */
    data_short = num_channels * get_word_size(sample_type);
    if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
    fwrite(&data_short,1,2,fp);	/* block align */
    data_short = get_word_size(sample_type) * 8;
    if (EST_BIG_ENDIAN) data_short = SWAPSHORT(data_short);
    fwrite(&data_short,1,2,fp);	/* bits per sample */
    info = "data"; fwrite(info,4,1,fp);
    data_size = num_channels*num_samples*get_word_size(sample_type);
    if (EST_BIG_ENDIAN) data_size = SWAPINT(data_size);
    fwrite(&data_size,1,4,fp);	/* total number of bytes in data */
    
    return write_ok;
}

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)
{
    if (data == NULL)
       return write_ok;

    return save_raw_data(fp,data,offset,num_samples,num_channels,
			 sample_type,bo_little);
}


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)
{
    save_wave_riff_header(fp, num_samples, num_channels, sample_rate,
				     sample_type, bo);

    return save_wave_riff_data(fp, data, offset, num_samples,
                        num_channels, sample_rate, sample_type, bo);

}

/*=======================================================================*/
/* Amiga/Apple AIFF waveform format                                      */
/* This was constructed using info in AudioIFF1.3.hqx found on the web   */
/* and also I did look at SOX's aiff.c written by Guido van Rossum       */
/* and Sundry Contributors.                                              */
/*=======================================================================*/

struct AIFFchunk {
    char id[4];
    int size;
};

struct AIFFssnd {		/* Sound Data Chunk */
    int offset;
    int blocksize;
};

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)
{
    char info[4];
    struct AIFFchunk chunk;
    short comm_channels;
    int comm_samples;
    short comm_bits;
    unsigned char ieee_ext_sample_rate[10];
    struct AIFFssnd ssndchunk;
    enum EST_sample_type_t actual_sample_type;
    int dsize,data_length,n;
    unsigned char *file_data;
    
    if (ts.fread(info,sizeof(char),4) != 4)
	return wrong_format;	/* but its almost definitely an error */
    if (strncmp(info,"FORM",4) != 0)
	return wrong_format;

    /* We've got an aiff file, I hope */
    if (ts.fread(&dsize,4,1) != 1) return misc_read_error;
    if (EST_LITTLE_ENDIAN)	/* file is in different byte order */
	dsize = SWAPINT(dsize);
    if ((ts.fread(info,sizeof(char),4) != 4) ||
	(strncmp(info,"AIFF",4) != 0))
    {
	fprintf(stderr, "AIFF file does not have AIFF chunk\n");
	return misc_read_error; 
    }
    
    for ( ; ts.fread(&chunk, sizeof(chunk), 1) == 1 ; )
    {				/* for each chunk in the file */
	if (EST_LITTLE_ENDIAN)	/* file is in different byte order */
	    chunk.size = SWAPINT(chunk.size);
	if (strncmp(chunk.id,"COMM",4) == 0)
	{
	    if (chunk.size != 18)
	    {
		fprintf(stderr,"AIFF chunk: bad size\n");
		return misc_read_error;
	    }
	    if (ts.fread(&comm_channels, sizeof(short), 1) != 1)
            return misc_read_error;
	    if (ts.fread(&comm_samples, sizeof(int), 1) != 1)
            return misc_read_error;
	    if (ts.fread(&comm_bits, sizeof(short), 1) != 1)
            return misc_read_error;
	    if (ts.fread(ieee_ext_sample_rate, 10, 1) != 1)
	    {
		fprintf(stderr,"AIFF chunk: eof within COMM chunk\n");
		return misc_read_error;
	    }
	    if (EST_LITTLE_ENDIAN)
	    {
		comm_channels = SWAPSHORT(comm_channels);
		comm_samples = SWAPINT(comm_samples);
		comm_bits = SWAPSHORT(comm_bits);
	    }
	    *sample_rate = (int)ConvertFromIeeeExtended(ieee_ext_sample_rate);
	}
	else if (strncmp(chunk.id,"SSND",4) == 0)
	{
	    if (ts.fread(&ssndchunk, sizeof(ssndchunk), 1) != 1)
	    {
		fprintf(stderr,"AIFF chunk: eof within SSND chunk\n");
		return misc_read_error;
	    }
	    if (EST_LITTLE_ENDIAN)
	    {
		ssndchunk.offset = SWAPINT(ssndchunk.offset);
		ssndchunk.blocksize = SWAPINT(ssndchunk.blocksize);
	    }
	    
	    *num_channels = comm_channels;
	    switch (comm_bits)
	    {
	    case 8: actual_sample_type = st_uchar; break;
		case 16: actual_sample_type = st_short; break;
		default:
		    fprintf(stderr,"AIFF: unsupported sample width %d bits\n",
			    comm_bits);
		    return misc_read_error;
		}
	    
	    ts.seek(ssndchunk.offset+(comm_channels*offset)+ts.tell());
	    if (length == 0)
 		data_length = (comm_samples-offset)*comm_channels;
  	    else
 		data_length = length*comm_channels;
	    file_data = walloc(unsigned char, 
			       data_length*comm_channels*
			       get_word_size(actual_sample_type));
	    if ((n=ts.fread(file_data,get_word_size(actual_sample_type),
			 data_length)) != data_length)
	    {
		fprintf(stderr,"AIFF read: short file %s\n",
			(const char *)ts.filename());
		fprintf(stderr,"AIFF read: at %d got %d instead of %d samples\n",
			offset,n,data_length);
		data_length = n;
	    }
	    
	    *data = convert_raw_data(file_data,data_length,
				     actual_sample_type,bo_big);
	    *num_samples = data_length/comm_channels;
	    *sample_type = st_short;
	    *word_size = 2;
	    *bo = EST_NATIVE_BO;
	    break;			/* only care about the first SSND chunk */
	}
	else 
	{			/* skip bytes in chunk */
	    ts.seek(ts.tell()+chunk.size);
	}
    }
    
    return format_ok;
}


enum EST_write_status save_wave_aiff_header(FILE *fp,
				     int num_samples, int num_channels,
				     int sample_rate,
				     enum EST_sample_type_t sample_type, int bo)
{
    (void)bo;
    const char *info;
    int data_size, data_int;
    unsigned char ieee_ext_buf[10];
    short data_short;
    
    info = "FORM";
    fwrite(info,1,4,fp);
    /* This number seems to be derived different for each example */
    data_size = 54+(num_samples*num_channels*get_word_size(sample_type));
    if (EST_LITTLE_ENDIAN)
	data_size = SWAPINT(data_size);
    fwrite(&data_size,1,4,fp);
    info = "AIFF";
    fwrite(info,1,4,fp);
    info = "COMM";
    fwrite(info,1,4,fp);
    data_int = 18;
    if (EST_LITTLE_ENDIAN)
	data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);
    data_short = num_channels;
    if (EST_LITTLE_ENDIAN)
	data_short = SWAPSHORT(data_short);
    fwrite(&data_short,1,2,fp);
    data_int = num_samples;
    if (EST_LITTLE_ENDIAN)
	data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);
    data_short = 8*get_word_size(sample_type);
    if (EST_LITTLE_ENDIAN)
	data_short = SWAPSHORT(data_short);
    fwrite(&data_short,1,2,fp);
    ConvertToIeeeExtended((double)sample_rate,ieee_ext_buf);
    fwrite(ieee_ext_buf,1,10,fp);
    info = "SSND";
    fwrite(info,1,4,fp);
    data_int = 8 + (num_samples*num_channels*get_word_size(sample_type));
    if (EST_LITTLE_ENDIAN)
	data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);
    data_int = 0;
    if (EST_LITTLE_ENDIAN)
	data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);   /* offset */
    if (EST_LITTLE_ENDIAN)
	data_int = SWAPINT(data_int);
    fwrite(&data_int,1,4,fp);   /* blocksize */
    
    return write_ok;

}

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)
{

    if (data == NULL)
       return write_ok;
    if ((sample_type == st_short) || (sample_type == st_uchar))
	    return save_raw_data(fp,data, offset, num_samples, num_channels,
                             sample_type, bo_big);
    else
    {
	fprintf(stderr,"AIFF: requested data type not uchar or short\n");
	return misc_write_error;
    }
}


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)
{
    save_wave_aiff_header(fp, num_samples, num_channels,
                          sample_rate, sample_type, bo);

    return save_wave_aiff_data(fp, data, offset,
				     num_samples, num_channels,
				     sample_rate, sample_type, bo);
}

/*=======================================================================*/
/* ulaw EST_filetype are just raw data with 8K ulaw contents                 */
/*=======================================================================*/

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)

{
    unsigned char *ulaw;
    int data_length,samps;

    ts.seek_end();
    samps = ts.tell();

    if (length == 0)
	data_length = samps - offset;
    else
	data_length = length;

    ulaw = walloc(unsigned char, data_length);
    ts.seek(offset);
    if (ts.fread(ulaw,1,data_length) != data_length)
    {
	wfree(ulaw);
	return misc_read_error;
    }

    *data = walloc(short,data_length);
    ulaw_to_short(ulaw,*data,data_length);
    wfree(ulaw);

    *num_samples = data_length;
    *sample_rate = 8000;
    *num_channels = 1;
    *sample_type = st_short;
    *word_size = 2;
    *bo = EST_NATIVE_BO;

    return format_ok;
}

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)
{
    (void) sample_rate;
    (void) sample_type;
    (void) fp;
    (void) num_samples;
    (void) num_channels;
    (void) bo;
    return write_ok;
}

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)
{
    if (data == NULL)
       return write_ok;

    return save_wave_raw(fp,data,offset,num_samples,num_channels,
			 8000,st_mulaw,bo);
}

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)
{
    save_wave_ulaw_header(fp, num_samples, num_channels,
                          sample_rate, sample_type, bo);

    return save_wave_ulaw_data(fp, data, offset,
				     num_samples, num_channels,
				     sample_rate, sample_type, bo);
}



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)

{
    unsigned char *alaw;
    int data_length,samps;

    ts.seek_end();
    samps = ts.tell();

    if (length == 0)
	data_length = samps - offset;
    else
	data_length = length;

    alaw = walloc(unsigned char, data_length);
    ts.seek(offset);
    if (ts.fread(alaw,1,data_length) != data_length)
    {
	wfree(alaw);
	return misc_read_error;
    }

    *data = walloc(short,data_length);
    alaw_to_short(alaw,*data,data_length);
    wfree(alaw);

    *num_samples = data_length;
    *sample_rate = 8000;
    *num_channels = 1;
    *sample_type = st_short;
    *word_size = 2;
    *bo = EST_NATIVE_BO;

    return format_ok;
}

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)
{
    (void) sample_rate;
    (void) sample_type;
    (void) fp;
    (void) num_samples;
    (void) num_channels;
    (void) bo;
    return write_ok;
}

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)
{
    (void)sample_rate;
    (void)sample_type;
    if (data == NULL)
       return write_ok;
    return save_wave_raw(fp,data,offset,num_samples,num_channels,
			 8000,st_alaw,bo);
}

enum EST_write_status save_wave_alaw(FILE *fp, const short *data, int offset,
				     int num_samples, int num_channels,
				     int sample_rate,
				     enum EST_sample_type_t sample_type, int bo)
{
    save_wave_alaw_header(fp, num_samples, num_channels,
                          sample_rate, sample_type, bo);

    return save_wave_alaw_data(fp, data, offset,
				     num_samples, num_channels,
				     sample_rate, sample_type, bo);
}


/*=======================================================================*/
/* Sun and Next snd files                                                */
/*=======================================================================*/

typedef struct {
    unsigned int    magic;	/* magic number */
    unsigned int    hdr_size;	/* size of this header */
    int    data_size;	        /* length of data (optional) */
    unsigned int    encoding;	/* data encoding format */
    unsigned int    sample_rate; /* samples per second */
    unsigned int    channels;	 /* number of interleaved channels */
} Sun_au_header;

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)
{
    /* Header structures */
    Sun_au_header header;
    enum EST_sample_type_t encoding_type;
    int data_length, sample_width, bytes, samps, n;
    unsigned char *file_data;
    int current_pos;
    
    current_pos = ts.tell();
    if (ts.fread(&header, sizeof(Sun_au_header), 1) != 1)
        return misc_read_error;
    
    /* test for magic number */
    if ((EST_LITTLE_ENDIAN) && 
	((unsigned int)0x2e736e64 == SWAPINT(header.magic)))
    {				/* wrong byte order, swap header */
	header.hdr_size = SWAPINT(header.hdr_size);
	header.data_size = SWAPINT(header.data_size);
	header.encoding = SWAPINT(header.encoding);
	header.sample_rate = SWAPINT(header.sample_rate);
	header.channels = SWAPINT(header.channels);
    }
    else if ((unsigned int)0x2e736e64 != header.magic)
	return wrong_format;
    
    switch (header.encoding) 
    {
    case 1:
	encoding_type = st_mulaw;	
	break;
    case 2:
	encoding_type = st_uchar;	
	break;
    case 3:
	encoding_type = st_short;	
	break;
    default:
	fprintf(stderr, "Unsupported data type in SND header\n");
	return misc_read_error;
    }
    
    *num_channels = header.channels;
    sample_width = get_word_size(encoding_type);
    *sample_rate = header.sample_rate;
    
    if ((header.data_size == 0) ||
	(header.data_size == -1))
    {
	ts.seek_end();
	bytes = ts.tell() - header.hdr_size;
    }
    else
	bytes = header.data_size;
    samps = bytes/sample_width;
    
    if (length == 0)
	data_length = (samps - offset)*(*num_channels);
    else
	data_length = length *(*num_channels);
    
    file_data = walloc(unsigned char, sample_width * data_length);
    ts.seek(current_pos+header.hdr_size+(sample_width*offset*(*num_channels)));
    if ((n=ts.fread(file_data,sample_width,data_length)) != data_length)
    {
	fprintf(stderr,"WAVE read: short file %s\n",
		(const char *)ts.filename());
	fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
		offset,n,data_length);
	data_length = n;
    }
    
    *data = convert_raw_data(file_data,data_length,encoding_type,bo_big);

    if (*data == NULL)
      return read_error;
    
    *num_samples = data_length/ (*num_channels);
    *sample_type = st_short;
    *bo = EST_NATIVE_BO;
    *word_size = 2;
    return read_ok;
}

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)
{
    (void)bo;
    /* Header structures */
    Sun_au_header header;
    
    /* Fill in header structure */
    header.magic = (unsigned int)0x2e736e64; /* should be a macro surely */
    header.hdr_size = sizeof(header);	     /* ! */
    header.data_size = get_word_size(sample_type) * num_channels * num_samples;
    
    switch (sample_type) {
    case st_mulaw:
	header.encoding = 1;
	break;
    case st_uchar:
	header.encoding = 2;
	break;
    case st_short:
	header.encoding = 3;
	break;
	
    default:
	fprintf(stderr, 
		"Unsupported sample type cannot be saved in SND format\n");
	return misc_write_error;
	
    }
    
    /* check consistency */
    
    header.sample_rate = sample_rate;
    
    header.channels = num_channels;
    
    if (EST_LITTLE_ENDIAN)
    {
	/* snd files all in big endian format */
	header.magic = SWAPINT(header.magic);
	header.hdr_size = SWAPINT(header.hdr_size);
	header.data_size = SWAPINT(header.data_size);
	header.encoding = SWAPINT(header.encoding);
	header.sample_rate = SWAPINT(header.sample_rate);
	header.channels = SWAPINT(header.channels);
	
    }
    /* write header */
    if (fwrite(&header, sizeof(header), 1, fp) != 1)
	return misc_write_error;

    return write_ok;
}

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)
{
    if (data == NULL)
       return write_ok;

    /* snd files are always in BIG_ENDIAN (sun) byte order */
    return save_raw_data(fp,data,offset,num_samples,num_channels,
			 sample_type,bo_big);
}


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)
{
    save_wave_snd_header(fp, num_samples, num_channels, sample_rate,
                         sample_type, bo);
    return save_wave_snd_data(fp, data, offset, num_samples,
             num_channels, sample_rate, sample_type, bo);
}


/*=======================================================================*/
/* CSTR Audlab files (from the last century)                             */
/* They are always bigendian                                             */
/*=======================================================================*/
struct s1 {
    char   c[17];
    float  f1;
    float  f2;
};

struct s2 {
    float f1;
    float f2;   
    float f3;
    char c1;
    char c2;
    int i1;
    int i2;
};

struct audlabfh {
    struct s1 z; 
    char  file_type[8];
    char  c1[17];
    char  c2[17];
    char  c3[17];
    int   start;
    char  data_type;    
    char  c5[64];
};

struct audlabsh {
    int   channel_count;
    char  serial;
    int   sample_rate;
    char  c1[20];
    int   i1;
    char  c2;
    char  c3[121];
    char  c4[121];
    
};
struct audlabsd {
    char descr[17];
    int sample_count;
    int nbits;
    float f1;
    struct s2 z;
};

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)
{
    /* Header structures */
    struct audlabfh fh;
    struct audlabsh sh;
    struct audlabsd sd;
    int data_length,sample_count;
    int hdr_length;
    int current_pos;
    
    /* Read header structures from char array */
    current_pos = ts.tell();

    if (ts.fread(&fh, sizeof(struct audlabfh), 1) != 1)
        return misc_read_error;
    if (strcmp(fh.file_type, "Sample") != 0) 
	return wrong_format;
    
    if (ts.fread(&sh, sizeof(struct audlabsh), 1) != 1)
        return misc_read_error;
    if (ts.fread(&sd, sizeof(struct audlabsd), 1) != 1)
        return misc_read_error;
    hdr_length = sizeof(struct audlabfh) +
	sizeof(struct audlabsh) +
	    sizeof(struct audlabsd);
    
    if (EST_BIG_ENDIAN)
    {
	*num_channels = sh.channel_count;
	*sample_rate = sh.sample_rate;
	sample_count = sd.sample_count;
    }
    else			// audlab files are bigendian
    {
	*num_channels = SWAPINT(sh.channel_count);
	*sample_rate = SWAPINT(sh.sample_rate);
	sample_count = SWAPINT(sd.sample_count);
    }
    if (length == 0)
	data_length = (sample_count - offset) * (*num_channels);
    else
	data_length = length *(*num_channels);
    
    *data = walloc(short,sizeof(short) * data_length);
    ts.seek(current_pos+hdr_length+(sizeof(short)*offset*(*num_channels)));
    
    if ((int)ts.fread(*data, sizeof(short), data_length) != data_length)
    {
	wfree(*data);
	return misc_read_error;
    }
    if (EST_LITTLE_ENDIAN)
	swap_bytes_short(*data,data_length);
    
    *num_samples = data_length / (*num_channels);
    *sample_type = st_short;	/* set internal type*/
    *word_size = sizeof(short);
    *bo = EST_NATIVE_BO;
    
    return format_ok;
}

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)
{
    (void)bo;
    (void)sample_type;
    /* Header structures */
    struct audlabfh fh;
    struct audlabsh sh;
    struct audlabsd sd;
    
    fh.start = sizeof (struct audlabfh) +
	sizeof (struct audlabsh) + sizeof (struct audlabsd);
    fh.data_type = 2;
    strcpy(fh.file_type, "Sample");
    
    if (EST_LITTLE_ENDIAN) 
    {				// need to swap some of those numbers 
	sh.channel_count = SWAPINT(num_channels);
	sh.serial = 1;
	sh.sample_rate = SWAPINT(sample_rate);
	
	sd.sample_count = SWAPINT(num_samples);
	sd.nbits = SWAPINT(16);
    }
    else
    {
	sh.channel_count = num_channels;
	sh.serial = 1;
	sh.sample_rate = sample_rate;
	
	sd.sample_count = num_samples;
	sd.nbits = 16;
    }
    sprintf(sd.descr, "Filter 1");
    
    /* write headers */
    fwrite (&fh, sizeof(fh), 1, fp);
    fwrite (&sh, sizeof(sh), 1, fp);
    fwrite (&sd, sizeof(sd), 1, fp);
    return write_ok;
}

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)
{
    if (data == NULL)
       return write_ok;

    /* write data*/
    return save_raw_data(fp,data,offset,num_samples,num_channels,
			 st_short,bo_big);
}

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)
{
    save_wave_audlab_header(fp, num_samples, num_channels,
                        sample_rate, sample_type, bo);
    return save_wave_audlab_data(fp, data, offset,
				       num_samples, num_channels,
				       sample_rate, sample_type, bo);
}

/*=======================================================================*/
/* Entropic ESPS SD files: portable (non-proprietary) method             */
/*=======================================================================*/

/* Deep thanks go to Peter Kabal from McGill University whose AF code */
/* showed me this was even possible.  I looked at his code to find    */
/* parts I couldn't work out myself.  Also to Rodney Johnson of       */
/* Entropic whose document "ESPS APPLICATION NOTE: Non-ESPS Programs  */
/* and the ESPS File System" gave details of how to access ESPS files */
/* without using the ESPS library code.                               */

#include "esps_utils.h"
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)
{
    /* A license free version of an esps file reading program */
    FILE *fd;
    esps_hdr hdr;
    int actual_bo, sample_width, data_length;
    enum EST_read_status rv;
    int dl;
    enum EST_sample_type_t actual_sample_type;
    double d;
    unsigned char *file_data;

    if ((fd = ts.filedescriptor()) == NULL)
    {
	fprintf(stderr, "Can't open esps file %s for reading\n",
		(const char *)ts.filename());
	return misc_read_error;
    }
    
    if ((rv=read_esps_hdr(&hdr,fd)) != format_ok)
	return rv;
    
    if (hdr->file_type != ESPS_SD)
    {
	fprintf(stderr,"ESPS file: not an FEA_SD file\n");
	delete_esps_hdr(hdr);
	return misc_read_error;
    }
    
    if (fea_value_d("record_freq",0,hdr,&d) != 0)
    {
	fprintf(stderr,"ESPS file: can't find sample_rate in header assuming 16000\n");
	*sample_rate = 16000;
    }
    else
	*sample_rate = (int)d;
    actual_sample_type = st_short; /* you're not trying hard enough */
    sample_width = get_word_size(actual_sample_type);
    *num_channels = hdr->field_dimension[0];
    if (hdr->swapped)
	actual_bo = (EST_BIG_ENDIAN ? bo_little : bo_big);
    else
	actual_bo = (EST_BIG_ENDIAN ? bo_big : bo_little);
    
    if (length == 0)
	data_length = (hdr->num_records - offset)*(*num_channels);
    else
	data_length = length *(*num_channels);
    
    file_data = walloc(unsigned char, sample_width * data_length);
    fseek(fd,hdr->hdr_size+(sample_width*offset*(*num_channels)),
	  SEEK_SET);
    if ((dl=fread(file_data,sample_width,data_length,fd)) != data_length)
    {
	fprintf(stderr,"WAVE read: esps short file %s\n",
		(const char *)ts.filename());
	fprintf(stderr,"WAVE read: at %d got %d instead of %d samples\n",
		offset,dl,data_length);
	data_length = dl;
    }
    
    *data = convert_raw_data(file_data,data_length,
			     actual_sample_type,
			     actual_bo);
    
    *num_samples = data_length/ (*num_channels);
    *sample_type = st_short;
    *bo = EST_NATIVE_BO;
    *word_size = 2;
    delete_esps_hdr(hdr);
    return format_ok;
    
}


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)

{
    (void)bo;
    esps_hdr hdr = make_esps_sd_hdr();
    enum EST_write_status rv;
    short esps_type;
    
    hdr->num_records = num_samples;
    switch (sample_type)
    {
    case st_short:  esps_type = ESPS_SHORT; break;
		case st_schar:  esps_type = ESPS_CHAR; break; /* maybe should be BYTE */
		case st_int:    esps_type = ESPS_INT; break;
		case st_float:  esps_type = ESPS_FLOAT; break;
		case st_double: esps_type = ESPS_DOUBLE; break;
		default:
		    fprintf(stderr,"ESPS file: no support for sample_type %s\n",
			    sample_type_to_str(sample_type));
		    return misc_write_error;
		}
    /* I believe all of the following are necessary and in this order */
    add_field(hdr,"samples",esps_type,num_channels);
    /* FIXME: What is doing this path here?? */
    add_fea_special(hdr,ESPS_FEA_DIRECTORY,"margo:/disk/disk10/home/awb/projects/speech_tools/main");
    add_fea_special(hdr,ESPS_FEA_COMMAND,
		    "EDST waveform written as ESPS FEA_SD.\n\
			");
    add_fea_d(hdr,"start_time",0,(double)0);
    add_fea_d(hdr,"record_freq",0,(double)sample_rate);
    add_fea_d(hdr,"max_value",0,(double)27355);
    
    if ((rv=write_esps_hdr(hdr,fp)) != write_ok)
    {
	delete_esps_hdr(hdr);
	return rv;
    }
    /* lets ignore desired bo and sample type for the time being */
    delete_esps_hdr(hdr);
    return write_ok;
}


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)

{
    if (data == NULL)
       return write_ok;

    return save_raw_data(fp,data,offset,num_samples,num_channels,
			 sample_type,EST_NATIVE_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)

{
    save_wave_sd_header(fp, num_samples, num_channels, sample_rate,
                        sample_type, bo);
    return save_wave_sd_data(fp, data, offset, num_samples,
                num_channels, sample_rate, sample_type, bo);

}

/*=======================================================================*/
/* Raw data files -- unheadered                                          */
/* THESE FUNCTIONS ARE DIFFERENT FROM THE REST                           */
/* These function have more parameters than the standard file i/o        */
/* as information cannot be gleamed from the file                        */
/*=======================================================================*/

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)
{
    unsigned char *file_data;
    int data_length,samps,sample_width;
    int guess,i,samp;
    short *ndata;
    
    if (isample_type == st_ascii)
    {
	/* Guess the size */
	if ((offset != 0) || (length != 0))
	{
	    fprintf(stderr,"Load ascii wave: doesn't support offsets and lengths\n");
	    return misc_read_error;
	}
	
	ts.seek_end();
	guess = (int)(1.2*ts.tell()/7)+10; /* rough guess of the num of samps */
	ts.seek(0);
	*data = walloc(short, guess);
	i=0;
	while (!ts.eof())
	{
	    samp = atoi(ts.get().string());
	    if (i == guess)
	    {
		ndata = walloc(short,(int)(guess*1.2));
		memmove(ndata,*data,guess*sizeof(short));
		wfree(*data);
		*data = ndata;
		guess = (int)(guess*1.2);
	    }
	    if (samp < -32768)
	    {
		fprintf(stderr,"Load ascii wave: sample %d underflow clipping\n",
			i);
		(*data)[i] = -32768;
	    }
	    else if (samp > 32767)
	    {
		fprintf(stderr,"Load ascii wave: sample %d overflow clipping\n",
			i);
		(*data)[i] = 32767;
	    }
	    else
		(*data)[i] = (short)samp;
	    i++;
	}
	data_length = i;
    }
    else
    {
	ts.seek_end();
	sample_width = get_word_size(isample_type);
	samps = ts.tell()/sample_width;
	
	if (length == 0)
	    data_length = samps - offset;
	else
	    data_length = length;
	
	file_data = walloc(unsigned char, data_length * sample_width *inc);
	ts.seek(offset*sample_width*inc);
	if ((int)ts.fread(file_data,sample_width,data_length) != data_length)
	    return misc_read_error;
	
	*data = convert_raw_data(file_data,data_length,isample_type,ibo);
    }
    
    *num_samples = data_length/inc;
    *sample_rate = isample_rate;
    *num_channels = inc;
    *sample_type = st_short;
    *word_size = 2;
    *bo = EST_NATIVE_BO;
    
    return format_ok;
}

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)
{
    return write_ok;
}

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)
{
    if (data == NULL)
       return write_ok;
    
    return save_raw_data(fp,data,offset,num_samples,num_channels,
			 sample_type,bo);
}

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)
{
    (void)sample_rate;

    return save_wave_raw_data(fp, data, offset, num_samples,
                    num_channels, sample_rate, sample_type, bo);
}

/***********************************************************************/
/*                                                                     */
/* end of file type specific functions                                 */
/*                                                                     */
/***********************************************************************/

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)
{
    EST_WaveFileType t = EST_WaveFile::map.token(ftype);
    EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
    switch(t)
    {
        case wff_nist:
            return save_wave_nist_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_esps:
            return save_wave_sd_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_est:
            return save_wave_est_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_audlab:
            return save_wave_audlab_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_snd:
            return save_wave_snd_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_aiff:
            return save_wave_aiff_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_riff:
            return save_wave_riff_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_raw:
            return save_wave_raw_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_ulaw:
            return save_wave_ulaw_header(fp, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        default:
        case wff_none:
            cerr << "Can't save wave header to files type " << ftype << endl;
            break;
    }
    return write_ok;
}


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)
{
    EST_WaveFileType t = EST_WaveFile::map.token(ftype);
    EST_sample_type_t sample_type = EST_sample_type_map.token(stype);
    switch(t)
    {
        case wff_nist:
            return save_wave_nist_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_esps:
            return save_wave_sd_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_est:
            return save_wave_est_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_audlab:
            return save_wave_audlab_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_snd:
            return save_wave_snd_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_aiff:
            return save_wave_aiff_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_riff:
            return save_wave_riff_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_raw:
            return save_wave_raw_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        case wff_ulaw:
            return save_wave_ulaw_data(fp, data, offset, num_samples, num_channels,
                       sample_rate, sample_type, bo);
            break;
        default:
        case wff_none:
            cerr << "Can't save wave data to files type " << ftype << endl;
            break;
    }
    return write_ok;
}