1 /* Copyright (C) 2002 Jean-Marc Valin
3 Routines to handle wav (RIFF) headers
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 #include "speex/speex_types.h"
43 int read_wav_header(FILE *file, int *rate, int *channels, int *format, spx_int32_t *size)
55 fread(ch, 1, 4, file);
56 if (strcmp(ch, "RIFF")!=0)
58 fseek(file, 0, SEEK_SET);
62 fread(&itmp, 4, 1, file);
63 *size = le_int(itmp-36);
65 fread(ch, 1, 4, file);
66 if (strcmp(ch, "WAVE")!=0)
68 fprintf (stderr, "RIFF file is not a WAVE file\n");
72 fread(ch, 1, 4, file);
73 while (strcmp(ch, "fmt ")!=0)
75 fread(&itmp, 4, 1, file);
77 /*fprintf (stderr, "skip=%d\n", itmp);*/
78 /*strange way of seeking, but it works even for pipes*/
81 /*fseek(file, itmp, SEEK_CUR);*/
82 fread(ch, 1, 4, file);
85 fprintf (stderr, "Corrupted WAVE file: no \"fmt \"\n");
89 /*if (strcmp(ch, "fmt ")!=0)
91 fprintf (stderr, "Corrupted WAVE file: no \"fmt \"\n");
95 fread(&itmp, 4, 1, file);
98 /*fprintf (stderr, "skip=%d\n", skip_bytes);*/
100 fread(&stmp, 2, 1, file);
101 stmp = le_short(stmp);
104 fprintf (stderr, "Only PCM encoding is supported\n");
108 fread(&stmp, 2, 1, file);
109 stmp = le_short(stmp);
114 fprintf (stderr, "Only mono and (intensity) stereo supported\n");
118 fread(&itmp, 4, 1, file);
121 if (*rate != 8000 && *rate != 16000 && *rate != 11025 && *rate != 22050 && *rate != 32000 && *rate != 44100 && *rate != 48000)
123 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");
127 fread(&itmp, 4, 1, file);
128 bpersec = le_int(itmp);
130 fread(&stmp, 2, 1, file);
131 balign = le_short(stmp);
133 fread(&stmp, 2, 1, file);
134 stmp = le_short(stmp);
135 if (stmp!=16 && stmp!=8)
137 fprintf (stderr, "Only 8/16-bit linear supported\n");
142 if (bpersec!=*rate**channels*stmp/8)
144 fprintf (stderr, "Corrupted header: ByteRate mismatch\n");
148 if (balign!=*channels*stmp/8)
150 fprintf (stderr, "Corrupted header: BlockAlign mismatch\n");
155 /*strange way of seeking, but it works even for pipes*/
157 for (i=0;i<skip_bytes;i++)
160 /*fseek(file, skip_bytes, SEEK_CUR);*/
162 fread(ch, 1, 4, file);
163 while (strcmp(ch, "data")!=0)
165 fread(&itmp, 4, 1, file);
167 /*strange way of seeking, but it works even for pipes*/
170 /*fseek(file, itmp, SEEK_CUR);*/
171 fread(ch, 1, 4, file);
174 fprintf (stderr, "Corrupted WAVE file: no \"data\"\n");
179 /*Ignore this for now*/
180 fread(&itmp, 4, 1, file);
190 void write_wav_header(FILE *file, int rate, int channels, int format, int size)
198 fprintf (file, "RIFF");
201 fwrite(&itmp, 4, 1, file);
203 fprintf (file, "WAVEfmt ");
206 fwrite(&itmp, 4, 1, file);
209 fwrite(&stmp, 2, 1, file);
211 stmp = le_short(channels);
212 fwrite(&stmp, 2, 1, file);
215 fwrite(&itmp, 4, 1, file);
217 itmp = le_int(rate*channels*2);
218 fwrite(&itmp, 4, 1, file);
220 stmp = le_short(2*channels);
221 fwrite(&stmp, 2, 1, file);
224 fwrite(&stmp, 2, 1, file);
226 fprintf (file, "data");
228 itmp = le_int(0x7fffffff);
229 fwrite(&itmp, 4, 1, file);