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 "celt_types.h"
43 int read_wav_header(FILE *file, int *rate, int *channels, int *format, celt_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);
122 fread(&itmp, 4, 1, file);
123 bpersec = le_int(itmp);
125 fread(&stmp, 2, 1, file);
126 balign = le_short(stmp);
128 fread(&stmp, 2, 1, file);
129 stmp = le_short(stmp);
130 if (stmp!=16 && stmp!=8)
132 fprintf (stderr, "Only 8/16-bit linear supported\n");
137 if (bpersec!=*rate**channels*stmp/8)
139 fprintf (stderr, "Corrupted header: ByteRate mismatch\n");
143 if (balign!=*channels*stmp/8)
145 fprintf (stderr, "Corrupted header: BlockAlign mismatch\n");
150 /*strange way of seeking, but it works even for pipes*/
152 for (i=0;i<skip_bytes;i++)
155 /*fseek(file, skip_bytes, SEEK_CUR);*/
157 fread(ch, 1, 4, file);
158 while (strcmp(ch, "data")!=0)
160 fread(&itmp, 4, 1, file);
162 /*strange way of seeking, but it works even for pipes*/
165 /*fseek(file, itmp, SEEK_CUR);*/
166 fread(ch, 1, 4, file);
169 fprintf (stderr, "Corrupted WAVE file: no \"data\"\n");
174 /*Ignore this for now*/
175 fread(&itmp, 4, 1, file);
185 void write_wav_header(FILE *file, int rate, int channels, int format, int size)
193 fprintf (file, "RIFF");
196 fwrite(&itmp, 4, 1, file);
198 fprintf (file, "WAVEfmt ");
201 fwrite(&itmp, 4, 1, file);
204 fwrite(&stmp, 2, 1, file);
206 stmp = le_short(channels);
207 fwrite(&stmp, 2, 1, file);
210 fwrite(&itmp, 4, 1, file);
212 itmp = le_int(rate*channels*2);
213 fwrite(&itmp, 4, 1, file);
215 stmp = le_short(2*channels);
216 fwrite(&stmp, 2, 1, file);
219 fwrite(&stmp, 2, 1, file);
221 fprintf (file, "data");
223 itmp = le_int(0x7fffffff);
224 fwrite(&itmp, 4, 1, file);