Imported Upstream version 3.2.2
[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: These need to be refactored into a separate endianess header file
36 // as they duplicate routines defined in usrp/host/lib/legacy/usrp_bytesex.h
37
38 #ifdef WORDS_BIGENDIAN
39
40 #ifdef HAVE_BYTESWAP_H
41 #include <byteswap.h>
42 #else
43 #warning Using non-portable code (likely wrong other than ILP32).
44
45 static inline short int
46 bswap_16 (unsigned short int x)
47 {
48   return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
49 }
50
51 static inline unsigned int
52 bswap_32 (unsigned int x)
53 {
54   return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8)       \
55           | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
56 }
57 #endif // HAVE_BYTESWAP_H
58
59 static inline uint32_t
60 host_to_wav(uint32_t x)
61 {
62   return bswap_32(x);
63 }
64
65 static inline uint16_t
66 host_to_wav(uint16_t x)
67 {
68   return bswap_16(x);
69 }
70
71 static inline int16_t
72 host_to_wav(int16_t x)
73 {
74   return bswap_16(x);
75 }
76
77 static inline uint32_t
78 wav_to_host(uint32_t x)
79 {
80   return bswap_32(x);
81 }
82
83 static inline uint16_t
84 wav_to_host(uint16_t x)
85 {
86   return bswap_16(x);
87 }
88
89 static inline int16_t
90 wav_to_host(int16_t x)
91 {
92   return bswap_16(x);
93 }
94
95 #else
96
97 static inline uint32_t
98 host_to_wav(uint32_t x)
99 {
100   return x;
101 }
102
103 static inline uint16_t
104 host_to_wav(uint16_t x)
105 {
106   return x;
107 }
108
109 static inline int16_t
110 host_to_wav(int16_t x)
111 {
112   return x;
113 }
114
115 static inline uint32_t
116 wav_to_host(uint32_t x)
117 {
118   return x;
119 }
120
121 static inline uint16_t
122 wav_to_host(uint16_t x)
123 {
124   return x;
125 }
126
127 static inline int16_t
128 wav_to_host(int16_t x)
129 {
130   return x;
131 }
132
133 #endif // WORDS_BIGENDIAN
134
135
136 bool
137 gri_wavheader_parse(FILE *fp,
138                     unsigned int &sample_rate_o,
139                     int &nchans_o,
140                     int &bytes_per_sample_o,
141                     int &first_sample_pos_o,
142                     unsigned int &samples_per_chan_o)
143 {
144   // _o variables take return values
145   char str_buf[8] = {0};
146   
147   uint32_t file_size;
148   uint32_t fmt_hdr_skip;
149   uint16_t compression_type;
150   uint16_t nchans;
151   uint32_t sample_rate;
152   uint32_t avg_bytes_per_sec;
153   uint16_t block_align;
154   uint16_t bits_per_sample;
155   uint32_t chunk_size;
156   
157   size_t fresult;
158
159   fresult = fread(str_buf, 1, 4, fp);
160   if (fresult != 4 || strncmp(str_buf, "RIFF", 4) || feof(fp)) {
161     return false;
162   }
163   
164   fread(&file_size, 1, 4, fp);
165   
166   fresult = fread(str_buf, 1, 8, fp);
167   if (fresult != 8 || strncmp(str_buf, "WAVEfmt ", 8) || feof(fp)) {
168     return false;
169   }
170   
171   fread(&fmt_hdr_skip, 1, 4, fp);
172   
173   fread(&compression_type, 1, 2, fp);
174   if (wav_to_host(compression_type) != VALID_COMPRESSION_TYPE) {
175     return false;
176   }
177   
178   fread(&nchans,            1, 2, fp);
179   fread(&sample_rate,       1, 4, fp);
180   fread(&avg_bytes_per_sec, 1, 4, fp);
181   fread(&block_align,       1, 2, fp);
182   fread(&bits_per_sample,   1, 2, fp);
183   
184   if (ferror(fp)) {
185     return false;
186   }
187   
188   fmt_hdr_skip    = wav_to_host(fmt_hdr_skip);
189   nchans          = wav_to_host(nchans);
190   sample_rate     = wav_to_host(sample_rate);
191   bits_per_sample = wav_to_host(bits_per_sample);
192   
193   if (bits_per_sample != 8 && bits_per_sample != 16) {
194     return false;
195   }
196   
197   fmt_hdr_skip -= 16;
198   if (fmt_hdr_skip) {
199     fseek(fp, fmt_hdr_skip, SEEK_CUR);
200   }
201   
202   // data chunk
203   fresult = fread(str_buf, 1, 4, fp);
204   if (strncmp(str_buf, "data", 4)) {
205     return false;
206   }
207
208   fread(&chunk_size, 1, 4, fp);
209   if (ferror(fp)) {
210     return false;
211   }
212   
213   // More byte swapping
214   chunk_size = wav_to_host(chunk_size);
215   
216   // Output values
217   sample_rate_o      = (unsigned) sample_rate;
218   nchans_o           = (int) nchans;
219   bytes_per_sample_o = (int) (bits_per_sample / 8);
220   first_sample_pos_o = (int) ftell(fp);
221   samples_per_chan_o = (unsigned) (chunk_size / (bytes_per_sample_o * nchans));
222   return true;
223 }
224
225
226 short int
227 gri_wav_read_sample(FILE *fp, int bytes_per_sample)
228 {
229   int16_t buf = 0;
230   fread(&buf, bytes_per_sample, 1, fp);
231   
232   return (short) wav_to_host(buf);
233 }
234
235
236 bool
237 gri_wavheader_write(FILE *fp,
238                     unsigned int sample_rate,
239                     int nchans,
240                     int bytes_per_sample)
241 {
242   const int header_len = 44;
243   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";
244   uint16_t nchans_f        = (uint16_t) nchans;
245   uint32_t sample_rate_f   = (uint32_t) sample_rate;
246   uint16_t block_align     = bytes_per_sample * nchans;
247   uint32_t avg_bytes       = sample_rate * block_align;
248   uint16_t bits_per_sample = bytes_per_sample * 8;
249   
250   nchans_f        = host_to_wav(nchans_f);
251   sample_rate_f   = host_to_wav(sample_rate_f);
252   block_align     = host_to_wav(block_align);
253   avg_bytes       = host_to_wav(avg_bytes);
254   bits_per_sample = host_to_wav(bits_per_sample);
255   
256   wav_hdr[16] = 0x10; // no extra bytes
257   wav_hdr[20] = 0x01; // no compression
258   memcpy((void *) (wav_hdr + 22), (void *) &nchans_f,        2);
259   memcpy((void *) (wav_hdr + 24), (void *) &sample_rate_f,   4);
260   memcpy((void *) (wav_hdr + 28), (void *) &avg_bytes,       4);
261   memcpy((void *) (wav_hdr + 32), (void *) &block_align,     2);
262   memcpy((void *) (wav_hdr + 34), (void *) &bits_per_sample, 2);
263   
264   fwrite(&wav_hdr, 1, header_len, fp);
265   if (ferror(fp)) {
266     return false;
267   }
268   
269   return true;
270 }
271
272
273 void
274 gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample)
275 {
276   void *data_ptr;
277   unsigned char buf_8bit;
278   int16_t       buf_16bit;
279   
280   if (bytes_per_sample == 1) {
281     buf_8bit = (unsigned char) sample;
282     data_ptr = (void *) &buf_8bit;
283   } else {
284     buf_16bit = host_to_wav((int16_t) sample);
285     data_ptr  = (void *) &buf_16bit;
286   }
287   
288   fwrite(data_ptr, 1, bytes_per_sample, fp);
289 }
290
291
292 bool
293 gri_wavheader_complete(FILE *fp, unsigned int byte_count)
294 {
295   uint32_t chunk_size = (uint32_t) byte_count;
296   chunk_size = host_to_wav(chunk_size);
297   
298   fseek(fp, 40, SEEK_SET);
299   fwrite(&chunk_size, 1, 4, fp);
300   
301   chunk_size = (uint32_t) byte_count + 36; // fmt chunk and data header
302   chunk_size = host_to_wav(chunk_size);
303   fseek(fp, 4, SEEK_SET);
304   
305   fwrite(&chunk_size, 1, 4, fp);
306   
307   if (ferror(fp)) {
308     return false;
309   }
310   
311   return true;
312 }