3 * Copyright 2004,2006,2007,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 <gr_wavfile_sink.h>
28 #include <gr_io_signature.h>
29 #include <gri_wavfile.h>
36 // win32 (mingw/msvc) specific
41 #define OUR_O_BINARY O_BINARY
43 #define OUR_O_BINARY 0
46 // should be handled via configure
48 #define OUR_O_LARGEFILE O_LARGEFILE
50 #define OUR_O_LARGEFILE 0
55 gr_make_wavfile_sink(const char *filename,
57 unsigned int sample_rate,
60 return gr_wavfile_sink_sptr (new gr_wavfile_sink (filename,
66 gr_wavfile_sink::gr_wavfile_sink(const char *filename,
68 unsigned int sample_rate,
70 : gr_sync_block ("wavfile_sink",
71 gr_make_io_signature(1, n_channels, sizeof(float)),
72 gr_make_io_signature(0, 0, 0)),
73 d_sample_rate(sample_rate), d_nchans(n_channels),
74 d_fp(0), d_new_fp(0), d_updated(false)
76 if (bits_per_sample != 8 && bits_per_sample != 16) {
77 throw std::runtime_error("Invalid bits per sample (supports 8 and 16)");
79 d_bytes_per_sample = bits_per_sample / 8;
80 d_bytes_per_sample_new = d_bytes_per_sample;
82 if (!open(filename)) {
83 throw std::runtime_error ("can't open file");
86 if (bits_per_sample == 8) {
87 d_max_sample_val = 0xFF;
89 d_normalize_fac = d_max_sample_val/2;
90 d_normalize_shift = 1;
92 d_max_sample_val = 0x7FFF;
93 d_min_sample_val = -0x7FFF;
94 d_normalize_fac = d_max_sample_val;
95 d_normalize_shift = 0;
96 if (bits_per_sample != 16) {
97 fprintf(stderr, "Invalid bits per sample value requested, using 16");
104 gr_wavfile_sink::open(const char* filename)
106 omni_mutex_lock l(d_mutex);
108 // we use the open system call to get access to the O_LARGEFILE flag.
110 if ((fd = ::open (filename,
111 O_WRONLY|O_CREAT|O_TRUNC|OUR_O_LARGEFILE|OUR_O_BINARY,
117 if (d_new_fp) { // if we've already got a new one open, close it
122 if ((d_new_fp = fdopen (fd, "wb")) == NULL) {
124 ::close(fd); // don't leak file descriptor if fdopen fails.
129 if (!gri_wavheader_write(d_new_fp,
132 d_bytes_per_sample_new)) {
133 fprintf(stderr, "[%s] could not write to WAV file\n", __FILE__);
142 gr_wavfile_sink::close()
144 omni_mutex_lock l(d_mutex);
152 void gr_wavfile_sink::close_wav()
154 unsigned int byte_count = d_sample_count * d_bytes_per_sample;
156 gri_wavheader_complete(d_fp, byte_count);
163 gr_wavfile_sink::~gr_wavfile_sink ()
174 gr_wavfile_sink::work (int noutput_items,
175 gr_vector_const_void_star &input_items,
176 gr_vector_void_star &output_items)
178 float **in = (float **) &input_items[0];
179 int n_in_chans = input_items.size();
181 short int sample_buf_s;
185 do_update(); // update: d_fp is reqd
186 if (!d_fp) // drop output on the floor
187 return noutput_items;
189 for (nwritten = 0; nwritten < noutput_items; nwritten++) {
190 for (int chan = 0; chan < d_nchans; chan++) {
191 // Write zeros to channels which are in the WAV file
192 // but don't have any inputs here
193 if (chan < n_in_chans) {
195 convert_to_short(in[chan][nwritten]);
200 gri_wav_write_sample(d_fp, sample_buf_s, d_bytes_per_sample);
202 if (feof(d_fp) || ferror(d_fp)) {
203 fprintf(stderr, "[%s] file i/o error\n", __FILE__);
216 gr_wavfile_sink::convert_to_short(float sample)
218 sample += d_normalize_shift;
219 sample *= d_normalize_fac;
220 if (sample > d_max_sample_val) {
221 sample = d_max_sample_val;
222 } else if (sample < d_min_sample_val) {
223 sample = d_min_sample_val;
226 return (short int) roundf(sample);
231 gr_wavfile_sink::set_bits_per_sample(int bits_per_sample)
233 omni_mutex_lock l(d_mutex);
234 if (bits_per_sample == 8 || bits_per_sample == 16) {
235 d_bytes_per_sample_new = bits_per_sample / 8;
241 gr_wavfile_sink::set_sample_rate(unsigned int sample_rate)
243 omni_mutex_lock l(d_mutex);
244 d_sample_rate = sample_rate;
249 gr_wavfile_sink::do_update()
255 omni_mutex_lock l(d_mutex); // hold mutex for duration of this block
260 d_fp = d_new_fp; // install new file pointer
263 d_bytes_per_sample = d_bytes_per_sample_new;
265 if (d_bytes_per_sample == 1) {
266 d_max_sample_val = UCHAR_MAX;
267 d_min_sample_val = 0;
268 d_normalize_fac = d_max_sample_val/2;
269 d_normalize_shift = 1;
270 } else if (d_bytes_per_sample == 2) {
271 d_max_sample_val = SHRT_MAX;
272 d_min_sample_val = SHRT_MIN;
273 d_normalize_fac = d_max_sample_val;
274 d_normalize_shift = 0;