3 * Copyright 2004,2006,2007,2008,2009,2010 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>
35 #include <gruel/thread.h>
37 // win32 (mingw/msvc) specific
42 #define OUR_O_BINARY O_BINARY
44 #define OUR_O_BINARY 0
47 // should be handled via configure
49 #define OUR_O_LARGEFILE O_LARGEFILE
51 #define OUR_O_LARGEFILE 0
56 gr_make_wavfile_sink(const char *filename,
58 unsigned int sample_rate,
61 return gnuradio::get_initial_sptr(new gr_wavfile_sink (filename,
67 gr_wavfile_sink::gr_wavfile_sink(const char *filename,
69 unsigned int sample_rate,
71 : gr_sync_block ("wavfile_sink",
72 gr_make_io_signature(1, n_channels, sizeof(float)),
73 gr_make_io_signature(0, 0, 0)),
74 d_sample_rate(sample_rate), d_nchans(n_channels),
75 d_fp(0), d_new_fp(0), d_updated(false)
77 if (bits_per_sample != 8 && bits_per_sample != 16) {
78 throw std::runtime_error("Invalid bits per sample (supports 8 and 16)");
80 d_bytes_per_sample = bits_per_sample / 8;
81 d_bytes_per_sample_new = d_bytes_per_sample;
83 if (!open(filename)) {
84 throw std::runtime_error ("can't open file");
87 if (bits_per_sample == 8) {
88 d_max_sample_val = 0xFF;
90 d_normalize_fac = d_max_sample_val/2;
91 d_normalize_shift = 1;
93 d_max_sample_val = 0x7FFF;
94 d_min_sample_val = -0x7FFF;
95 d_normalize_fac = d_max_sample_val;
96 d_normalize_shift = 0;
97 if (bits_per_sample != 16) {
98 fprintf(stderr, "Invalid bits per sample value requested, using 16");
105 gr_wavfile_sink::open(const char* filename)
107 gruel::scoped_lock guard(d_mutex);
109 // we use the open system call to get access to the O_LARGEFILE flag.
111 if ((fd = ::open (filename,
112 O_WRONLY|O_CREAT|O_TRUNC|OUR_O_LARGEFILE|OUR_O_BINARY,
118 if (d_new_fp) { // if we've already got a new one open, close it
123 if ((d_new_fp = fdopen (fd, "wb")) == NULL) {
125 ::close(fd); // don't leak file descriptor if fdopen fails.
130 if (!gri_wavheader_write(d_new_fp,
133 d_bytes_per_sample_new)) {
134 fprintf(stderr, "[%s] could not write to WAV file\n", __FILE__);
143 gr_wavfile_sink::close()
145 gruel::scoped_lock guard(d_mutex);
153 void gr_wavfile_sink::close_wav()
155 unsigned int byte_count = d_sample_count * d_bytes_per_sample;
157 gri_wavheader_complete(d_fp, byte_count);
164 gr_wavfile_sink::~gr_wavfile_sink ()
175 gr_wavfile_sink::work (int noutput_items,
176 gr_vector_const_void_star &input_items,
177 gr_vector_void_star &output_items)
179 float **in = (float **) &input_items[0];
180 int n_in_chans = input_items.size();
182 short int sample_buf_s;
186 do_update(); // update: d_fp is reqd
187 if (!d_fp) // drop output on the floor
188 return noutput_items;
190 for (nwritten = 0; nwritten < noutput_items; nwritten++) {
191 for (int chan = 0; chan < d_nchans; chan++) {
192 // Write zeros to channels which are in the WAV file
193 // but don't have any inputs here
194 if (chan < n_in_chans) {
196 convert_to_short(in[chan][nwritten]);
201 gri_wav_write_sample(d_fp, sample_buf_s, d_bytes_per_sample);
203 if (feof(d_fp) || ferror(d_fp)) {
204 fprintf(stderr, "[%s] file i/o error\n", __FILE__);
217 gr_wavfile_sink::convert_to_short(float sample)
219 sample += d_normalize_shift;
220 sample *= d_normalize_fac;
221 if (sample > d_max_sample_val) {
222 sample = d_max_sample_val;
223 } else if (sample < d_min_sample_val) {
224 sample = d_min_sample_val;
227 return (short int) roundf(sample);
232 gr_wavfile_sink::set_bits_per_sample(int bits_per_sample)
234 gruel::scoped_lock guard(d_mutex);
235 if (bits_per_sample == 8 || bits_per_sample == 16) {
236 d_bytes_per_sample_new = bits_per_sample / 8;
242 gr_wavfile_sink::set_sample_rate(unsigned int sample_rate)
244 gruel::scoped_lock guard(d_mutex);
245 d_sample_rate = sample_rate;
250 gr_wavfile_sink::do_update()
256 gruel::scoped_lock guard(d_mutex); // hold mutex for duration of this block
261 d_fp = d_new_fp; // install new file pointer
264 d_bytes_per_sample = d_bytes_per_sample_new;
266 if (d_bytes_per_sample == 1) {
267 d_max_sample_val = UCHAR_MAX;
268 d_min_sample_val = 0;
269 d_normalize_fac = d_max_sample_val/2;
270 d_normalize_shift = 1;
271 } else if (d_bytes_per_sample == 2) {
272 d_max_sample_val = SHRT_MAX;
273 d_min_sample_val = SHRT_MIN;
274 d_normalize_fac = d_max_sample_val;
275 d_normalize_shift = 0;