-
Notifications
You must be signed in to change notification settings - Fork 25
/
EST_wave_io.cc
1895 lines (1665 loc) · 59 KB
/
EST_wave_io.cc
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
/*************************************************************************/
/* */
/* 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"
using namespace std;
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) return misc_read_error;
while (strncmp(info,"fmt ",4) != 0)
{
if (ts.fread(&dsize,4,1) != 1) return misc_read_error;
if (EST_BIG_ENDIAN) dsize = SWAPINT(dsize);
ts.seek(dsize+ts.tell());
if (ts.fread(info,sizeof(char),4) != 4) return misc_read_error;
}
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;