3 * Copyright 2004,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <gri_wavfile.h>
31 # define VALID_COMPRESSION_TYPE 0x0001
33 // WAV files are always little-endian, so we need some byte switching macros
35 // FIXME: Use libgruel versions
37 #ifdef WORDS_BIGENDIAN
39 #ifdef HAVE_BYTESWAP_H
42 #warning Using non-portable code (likely wrong other than ILP32).
44 static inline short int
45 bswap_16 (unsigned short int x)
47 return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
50 static inline unsigned int
51 bswap_32 (unsigned int x)
53 return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \
54 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
56 #endif // HAVE_BYTESWAP_H
58 static inline uint32_t
59 host_to_wav(uint32_t x)
64 static inline uint16_t
65 host_to_wav(uint16_t x)
71 host_to_wav(int16_t x)
76 static inline uint32_t
77 wav_to_host(uint32_t x)
82 static inline uint16_t
83 wav_to_host(uint16_t x)
89 wav_to_host(int16_t x)
96 static inline uint32_t
97 host_to_wav(uint32_t x)
102 static inline uint16_t
103 host_to_wav(uint16_t x)
108 static inline int16_t
109 host_to_wav(int16_t x)
114 static inline uint32_t
115 wav_to_host(uint32_t x)
120 static inline uint16_t
121 wav_to_host(uint16_t x)
126 static inline int16_t
127 wav_to_host(int16_t x)
132 #endif // WORDS_BIGENDIAN
136 gri_wavheader_parse(FILE *fp,
137 unsigned int &sample_rate_o,
139 int &bytes_per_sample_o,
140 int &first_sample_pos_o,
141 unsigned int &samples_per_chan_o)
143 // _o variables take return values
144 char str_buf[8] = {0};
147 uint32_t fmt_hdr_skip;
148 uint16_t compression_type;
150 uint32_t sample_rate;
151 uint32_t avg_bytes_per_sec;
152 uint16_t block_align;
153 uint16_t bits_per_sample;
158 fresult = fread(str_buf, 1, 4, fp);
159 if (fresult != 4 || strncmp(str_buf, "RIFF", 4) || feof(fp)) {
163 fread(&file_size, 1, 4, fp);
165 fresult = fread(str_buf, 1, 8, fp);
166 if (fresult != 8 || strncmp(str_buf, "WAVEfmt ", 8) || feof(fp)) {
170 fread(&fmt_hdr_skip, 1, 4, fp);
172 fread(&compression_type, 1, 2, fp);
173 if (wav_to_host(compression_type) != VALID_COMPRESSION_TYPE) {
177 fread(&nchans, 1, 2, fp);
178 fread(&sample_rate, 1, 4, fp);
179 fread(&avg_bytes_per_sec, 1, 4, fp);
180 fread(&block_align, 1, 2, fp);
181 fread(&bits_per_sample, 1, 2, fp);
187 fmt_hdr_skip = wav_to_host(fmt_hdr_skip);
188 nchans = wav_to_host(nchans);
189 sample_rate = wav_to_host(sample_rate);
190 bits_per_sample = wav_to_host(bits_per_sample);
192 if (bits_per_sample != 8 && bits_per_sample != 16) {
198 fseek(fp, fmt_hdr_skip, SEEK_CUR);
202 fresult = fread(str_buf, 1, 4, fp);
203 if (strncmp(str_buf, "data", 4)) {
207 fread(&chunk_size, 1, 4, fp);
212 // More byte swapping
213 chunk_size = wav_to_host(chunk_size);
216 sample_rate_o = (unsigned) sample_rate;
217 nchans_o = (int) nchans;
218 bytes_per_sample_o = (int) (bits_per_sample / 8);
219 first_sample_pos_o = (int) ftell(fp);
220 samples_per_chan_o = (unsigned) (chunk_size / (bytes_per_sample_o * nchans));
226 gri_wav_read_sample(FILE *fp, int bytes_per_sample)
229 fread(&buf, bytes_per_sample, 1, fp);
231 return (short) wav_to_host(buf);
236 gri_wavheader_write(FILE *fp,
237 unsigned int sample_rate,
239 int bytes_per_sample)
241 const int header_len = 44;
242 char wav_hdr[header_len] = "RIFF\0\0\0\0WAVEfmt \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0data\0\0\0";
243 uint16_t nchans_f = (uint16_t) nchans;
244 uint32_t sample_rate_f = (uint32_t) sample_rate;
245 uint16_t block_align = bytes_per_sample * nchans;
246 uint32_t avg_bytes = sample_rate * block_align;
247 uint16_t bits_per_sample = bytes_per_sample * 8;
249 nchans_f = host_to_wav(nchans_f);
250 sample_rate_f = host_to_wav(sample_rate_f);
251 block_align = host_to_wav(block_align);
252 avg_bytes = host_to_wav(avg_bytes);
253 bits_per_sample = host_to_wav(bits_per_sample);
255 wav_hdr[16] = 0x10; // no extra bytes
256 wav_hdr[20] = 0x01; // no compression
257 memcpy((void *) (wav_hdr + 22), (void *) &nchans_f, 2);
258 memcpy((void *) (wav_hdr + 24), (void *) &sample_rate_f, 4);
259 memcpy((void *) (wav_hdr + 28), (void *) &avg_bytes, 4);
260 memcpy((void *) (wav_hdr + 32), (void *) &block_align, 2);
261 memcpy((void *) (wav_hdr + 34), (void *) &bits_per_sample, 2);
263 fwrite(&wav_hdr, 1, header_len, fp);
273 gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample)
276 unsigned char buf_8bit;
279 if (bytes_per_sample == 1) {
280 buf_8bit = (unsigned char) sample;
281 data_ptr = (void *) &buf_8bit;
283 buf_16bit = host_to_wav((int16_t) sample);
284 data_ptr = (void *) &buf_16bit;
287 fwrite(data_ptr, 1, bytes_per_sample, fp);
292 gri_wavheader_complete(FILE *fp, unsigned int byte_count)
294 uint32_t chunk_size = (uint32_t) byte_count;
295 chunk_size = host_to_wav(chunk_size);
297 fseek(fp, 40, SEEK_SET);
298 fwrite(&chunk_size, 1, 4, fp);
300 chunk_size = (uint32_t) byte_count + 36; // fmt chunk and data header
301 chunk_size = host_to_wav(chunk_size);
302 fseek(fp, 4, SEEK_SET);
304 fwrite(&chunk_size, 1, 4, fp);