Merged r11377:11390 from jcorgan/usrp-headers in to trunk.
[debian/gnuradio] / gnuradio-core / src / lib / io / gri_wavfile.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2008 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gri_wavfile.h>
28 #include <cstring>
29 #include <stdint.h>
30
31 # define VALID_COMPRESSION_TYPE 0x0001
32
33 // WAV files are always little-endian, so we need some byte switching macros
34
35 // FIXME: Use libgruel versions
36
37 #ifdef WORDS_BIGENDIAN
38
39 #ifdef HAVE_BYTESWAP_H
40 #include <byteswap.h>
41 #else
42 #warning Using non-portable code (likely wrong other than ILP32).
43
44 static inline short int
45 bswap_16 (unsigned short int x)
46 {
47   return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
48 }
49
50 static inline unsigned int
51 bswap_32 (unsigned int x)
52 {
53   return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8)       \
54           | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
55 }
56 #endif // HAVE_BYTESWAP_H
57
58 static inline uint32_t
59 host_to_wav(uint32_t x)
60 {
61   return bswap_32(x);
62 }
63
64 static inline uint16_t
65 host_to_wav(uint16_t x)
66 {
67   return bswap_16(x);
68 }
69
70 static inline int16_t
71 host_to_wav(int16_t x)
72 {
73   return bswap_16(x);
74 }
75
76 static inline uint32_t
77 wav_to_host(uint32_t x)
78 {
79   return bswap_32(x);
80 }
81
82 static inline uint16_t
83 wav_to_host(uint16_t x)
84 {
85   return bswap_16(x);
86 }
87
88 static inline int16_t
89 wav_to_host(int16_t x)
90 {
91   return bswap_16(x);
92 }
93
94 #else
95
96 static inline uint32_t
97 host_to_wav(uint32_t x)
98 {
99   return x;
100 }
101
102 static inline uint16_t
103 host_to_wav(uint16_t x)
104 {
105   return x;
106 }
107
108 static inline int16_t
109 host_to_wav(int16_t x)
110 {
111   return x;
112 }
113
114 static inline uint32_t
115 wav_to_host(uint32_t x)
116 {
117   return x;
118 }
119
120 static inline uint16_t
121 wav_to_host(uint16_t x)
122 {
123   return x;
124 }
125
126 static inline int16_t
127 wav_to_host(int16_t x)
128 {
129   return x;
130 }
131
132 #endif // WORDS_BIGENDIAN
133
134
135 bool
136 gri_wavheader_parse(FILE *fp,
137                     unsigned int &sample_rate_o,
138                     int &nchans_o,
139                     int &bytes_per_sample_o,
140                     int &first_sample_pos_o,
141                     unsigned int &samples_per_chan_o)
142 {
143   // _o variables take return values
144   char str_buf[8] = {0};
145   
146   uint32_t file_size;
147   uint32_t fmt_hdr_skip;
148   uint16_t compression_type;
149   uint16_t nchans;
150   uint32_t sample_rate;
151   uint32_t avg_bytes_per_sec;
152   uint16_t block_align;
153   uint16_t bits_per_sample;
154   uint32_t chunk_size;
155   
156   size_t fresult;
157
158   fresult = fread(str_buf, 1, 4, fp);
159   if (fresult != 4 || strncmp(str_buf, "RIFF", 4) || feof(fp)) {
160     return false;
161   }
162   
163   fread(&file_size, 1, 4, fp);
164   
165   fresult = fread(str_buf, 1, 8, fp);
166   if (fresult != 8 || strncmp(str_buf, "WAVEfmt ", 8) || feof(fp)) {
167     return false;
168   }
169   
170   fread(&fmt_hdr_skip, 1, 4, fp);
171   
172   fread(&compression_type, 1, 2, fp);
173   if (wav_to_host(compression_type) != VALID_COMPRESSION_TYPE) {
174     return false;
175   }
176   
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);
182   
183   if (ferror(fp)) {
184     return false;
185   }
186   
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);
191   
192   if (bits_per_sample != 8 && bits_per_sample != 16) {
193     return false;
194   }
195   
196   fmt_hdr_skip -= 16;
197   if (fmt_hdr_skip) {
198     fseek(fp, fmt_hdr_skip, SEEK_CUR);
199   }
200   
201   // data chunk
202   fresult = fread(str_buf, 1, 4, fp);
203   if (strncmp(str_buf, "data", 4)) {
204     return false;
205   }
206
207   fread(&chunk_size, 1, 4, fp);
208   if (ferror(fp)) {
209     return false;
210   }
211   
212   // More byte swapping
213   chunk_size = wav_to_host(chunk_size);
214   
215   // Output values
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));
221   return true;
222 }
223
224
225 short int
226 gri_wav_read_sample(FILE *fp, int bytes_per_sample)
227 {
228   int16_t buf = 0;
229   fread(&buf, bytes_per_sample, 1, fp);
230   
231   return (short) wav_to_host(buf);
232 }
233
234
235 bool
236 gri_wavheader_write(FILE *fp,
237                     unsigned int sample_rate,
238                     int nchans,
239                     int bytes_per_sample)
240 {
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;
248   
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);
254   
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);
262   
263   fwrite(&wav_hdr, 1, header_len, fp);
264   if (ferror(fp)) {
265     return false;
266   }
267   
268   return true;
269 }
270
271
272 void
273 gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample)
274 {
275   void *data_ptr;
276   unsigned char buf_8bit;
277   int16_t       buf_16bit;
278   
279   if (bytes_per_sample == 1) {
280     buf_8bit = (unsigned char) sample;
281     data_ptr = (void *) &buf_8bit;
282   } else {
283     buf_16bit = host_to_wav((int16_t) sample);
284     data_ptr  = (void *) &buf_16bit;
285   }
286   
287   fwrite(data_ptr, 1, bytes_per_sample, fp);
288 }
289
290
291 bool
292 gri_wavheader_complete(FILE *fp, unsigned int byte_count)
293 {
294   uint32_t chunk_size = (uint32_t) byte_count;
295   chunk_size = host_to_wav(chunk_size);
296   
297   fseek(fp, 40, SEEK_SET);
298   fwrite(&chunk_size, 1, 4, fp);
299   
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);
303   
304   fwrite(&chunk_size, 1, 4, fp);
305   
306   if (ferror(fp)) {
307     return false;
308   }
309   
310   return true;
311 }