Debian Codegraph @

src/wav_io.c

raw
1 /* Copyright (C) 2002 Jean-Marc Valin
2 File: wav_io.c
3 Routines to handle wav (RIFF) headers
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <stdio.h>
38 #include <string.h>
39 #include "wav_io.h"
40
41
42 int read_wav_header(FILE *file, int *rate, int *channels, int *format, spx_int32_t *size)
43 {
44 char ch[5];
45 spx_int32_t itmp;
46 spx_int16_t stmp;
47 spx_int32_t bpersec;
48 spx_int16_t balign;
49 int skip_bytes;
50 int i;
51
52 ch[4]=0;
53 #if 0
54 fread(ch, 1, 4, file);
55 if (strcmp(ch, "RIFF")!=0)
56 {
57 fseek(file, 0, SEEK_SET);
58 return 0;
59 }
60
61 fread(&itmp, 4, 1, file);
62 *size = le_int(itmp-36);
63
64 fread(ch, 1, 4, file);
65 if (strcmp(ch, "WAVE")!=0)
66 {
67 fprintf (stderr, "RIFF file is not a WAVE file\n");
68 return -1;
69 }
70 #endif
71 fread(ch, 1, 4, file);
72 while (strcmp(ch, "fmt ")!=0)
73 {
74 fread(&itmp, 4, 1, file);
75 itmp = le_int(itmp);
76 /*fprintf (stderr, "skip=%d\n", itmp);*/
77 /*strange way of seeking, but it works even for pipes*/
78 for (i=0;i<itmp;i++) {
79 if (fgetc(file) == EOF) {
80 break;
81 }
82 }
83 /*fseek(file, itmp, SEEK_CUR);*/
84 fread(ch, 1, 4, file);
85 if (feof(file))
86 {
87 fprintf (stderr, "Corrupted WAVE file: no \"fmt \"\n");
88 return -1;
89 }
90 }
91 /*if (strcmp(ch, "fmt ")!=0)
92 {
93 fprintf (stderr, "Corrupted WAVE file: no \"fmt \"\n");
94 return -1;
95 }*/
96
97 fread(&itmp, 4, 1, file);
98 itmp = le_int(itmp);
99 skip_bytes=itmp-16;
100 /*fprintf (stderr, "skip=%d\n", skip_bytes);*/
101
102 fread(&stmp, 2, 1, file);
103 stmp = le_short(stmp);
104 if (stmp!=1)
105 {
106 fprintf (stderr, "Only PCM encoding is supported\n");
107 return -1;
108 }
109
110 fread(&stmp, 2, 1, file);
111 stmp = le_short(stmp);
112 *channels = stmp;
113
114 if (stmp>2 || stmp<1)
115 {
116 fprintf (stderr, "Only mono and (intensity) stereo supported\n");
117 return -1;
118 }
119
120 fread(&itmp, 4, 1, file);
121 itmp = le_int(itmp);
122 *rate = itmp;
123 if (*rate != 8000 && *rate != 16000 && *rate != 11025 && *rate != 22050 && *rate != 32000 && *rate != 44100 && *rate != 48000)
124 {
125 fprintf (stderr, "Only 8 kHz (narrowband) and 16 kHz (wideband) supported (plus 11.025 kHz and 22.05 kHz, but your mileage may vary)\n");
126 return -1;
127 }
128
129 fread(&itmp, 4, 1, file);
130 bpersec = le_int(itmp);
131
132 fread(&stmp, 2, 1, file);
133 balign = le_short(stmp);
134
135 fread(&stmp, 2, 1, file);
136 stmp = le_short(stmp);
137 if (stmp!=16 && stmp!=8)
138 {
139 fprintf (stderr, "Only 8/16-bit linear supported\n");
140 return -1;
141 }
142 *format=stmp;
143
144 if (bpersec!=*rate**channels*stmp/8)
145 {
146 fprintf (stderr, "Corrupted header: ByteRate mismatch\n");
147 return -1;
148 }
149
150 if (balign!=*channels*stmp/8)
151 {
152 fprintf (stderr, "Corrupted header: BlockAlign mismatch\n");
153 return -1;
154 }
155
156
157 /*strange way of seeking, but it works even for pipes*/
158 if (skip_bytes>0) {
159 for (i=0;i<skip_bytes;i++) {
160 if (fgetc(file) == EOF) {
161 break;
162 }
163 }
164 }
165
166 /*fseek(file, skip_bytes, SEEK_CUR);*/
167
168 fread(ch, 1, 4, file);
169 while (strcmp(ch, "data")!=0)
170 {
171 fread(&itmp, 4, 1, file);
172 itmp = le_int(itmp);
173 /*strange way of seeking, but it works even for pipes*/
174 for (i=0;i<itmp;i++) {
175 if (fgetc(file) == EOF) {
176 break;
177 }
178 }
179 /*fseek(file, itmp, SEEK_CUR);*/
180 fread(ch, 1, 4, file);
181 if (feof(file))
182 {
183 fprintf (stderr, "Corrupted WAVE file: no \"data\"\n");
184 return -1;
185 }
186 }
187
188 /*Ignore this for now*/
189 fread(&itmp, 4, 1, file);
190 itmp = le_int(itmp);
191
192 *size=itmp;
193
194 return 1;
195 }
196
197
198
199 void write_wav_header(FILE *file, int rate, int channels, int format, int size)
200 {
201 spx_int32_t itmp;
202 spx_int16_t stmp;
203
204 fprintf (file, "RIFF");
205
206 itmp = 0x7fffffff;
207 fwrite(&itmp, 4, 1, file);
208
209 fprintf (file, "WAVEfmt ");
210
211 itmp = le_int(16);
212 fwrite(&itmp, 4, 1, file);
213
214 stmp = le_short(1);
215 fwrite(&stmp, 2, 1, file);
216
217 stmp = le_short(channels);
218 fwrite(&stmp, 2, 1, file);
219
220 itmp = le_int(rate);
221 fwrite(&itmp, 4, 1, file);
222
223 itmp = le_int(rate*channels*2);
224 fwrite(&itmp, 4, 1, file);
225
226 stmp = le_short(2*channels);
227 fwrite(&stmp, 2, 1, file);
228
229 stmp = le_short(16);
230 fwrite(&stmp, 2, 1, file);
231
232 fprintf (file, "data");
233
234 itmp = le_int(0x7fffffff);
235 fwrite(&itmp, 4, 1, file);
236
237
238 }