1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006-2011,2013 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 "wavfile_sink_impl.h"
28 #include <gnuradio/blocks/wavfile.h>
29 #include <gnuradio/io_signature.h>
30 #include <gnuradio/thread/thread.h>
31 #include <fcntl.h>
32 #include <boost/math/special_functions/round.hpp>
33 #include <climits>
34 #include <cmath>
35 #include <cstring>
36 #include <stdexcept>
37 
38 // win32 (mingw/msvc) specific
39 #ifdef HAVE_IO_H
40 #include <io.h>
41 #endif
42 #ifdef O_BINARY
43 #define OUR_O_BINARY O_BINARY
44 #else
45 #define OUR_O_BINARY 0
46 #endif
47 
48 // should be handled via configure
49 #ifdef O_LARGEFILE
50 #define OUR_O_LARGEFILE O_LARGEFILE
51 #else
52 #define OUR_O_LARGEFILE 0
53 #endif
54 
55 namespace gr {
56 namespace blocks {
57 
make(const char * filename,int n_channels,unsigned int sample_rate,int bits_per_sample)58 wavfile_sink::sptr wavfile_sink::make(const char* filename,
59                                       int n_channels,
60                                       unsigned int sample_rate,
61                                       int bits_per_sample)
62 {
63     return gnuradio::get_initial_sptr(
64         new wavfile_sink_impl(filename, n_channels, sample_rate, bits_per_sample));
65 }
66 
wavfile_sink_impl(const char * filename,int n_channels,unsigned int sample_rate,int bits_per_sample)67 wavfile_sink_impl::wavfile_sink_impl(const char* filename,
68                                      int n_channels,
69                                      unsigned int sample_rate,
70                                      int bits_per_sample)
71     : sync_block("wavfile_sink",
72                  io_signature::make(1, n_channels, sizeof(float)),
73                  io_signature::make(0, 0, 0)),
74       d_sample_rate(sample_rate),
75       d_nchans(n_channels),
76       d_fp(0),
77       d_new_fp(0),
78       d_updated(false)
79 {
80     if (bits_per_sample != 8 && bits_per_sample != 16) {
81         throw std::runtime_error("Invalid bits per sample (supports 8 and 16)");
82     }
83     d_bytes_per_sample = bits_per_sample / 8;
84     d_bytes_per_sample_new = d_bytes_per_sample;
85 
86     if (!open(filename)) {
87         throw std::runtime_error("can't open file");
88     }
89 
90     if (bits_per_sample == 8) {
91         d_max_sample_val = 0xFF;
92         d_min_sample_val = 0;
93         d_normalize_fac = d_max_sample_val / 2;
94         d_normalize_shift = 1;
95     } else {
96         d_max_sample_val = 0x7FFF;
97         d_min_sample_val = -0x7FFF;
98         d_normalize_fac = d_max_sample_val;
99         d_normalize_shift = 0;
100     }
101 }
102 
open(const char * filename)103 bool wavfile_sink_impl::open(const char* filename)
104 {
105     gr::thread::scoped_lock guard(d_mutex);
106 
107     // we use the open system call to get access to the O_LARGEFILE flag.
108     int fd;
109     if ((fd = ::open(filename,
110                      O_WRONLY | O_CREAT | O_TRUNC | OUR_O_LARGEFILE | OUR_O_BINARY,
111                      0664)) < 0) {
112         perror(filename);
113         return false;
114     }
115 
116     if (d_new_fp) { // if we've already got a new one open, close it
117         fclose(d_new_fp);
118         d_new_fp = 0;
119     }
120 
121     if ((d_new_fp = fdopen(fd, "wb")) == NULL) {
122         perror(filename);
123         ::close(fd); // don't leak file descriptor if fdopen fails.
124         return false;
125     }
126     d_updated = true;
127 
128     if (!wavheader_write(d_new_fp, d_sample_rate, d_nchans, d_bytes_per_sample_new)) {
129         fprintf(stderr, "[%s] could not write to WAV file\n", __FILE__);
130         exit(-1);
131     }
132 
133     return true;
134 }
135 
close()136 void wavfile_sink_impl::close()
137 {
138     gr::thread::scoped_lock guard(d_mutex);
139 
140     if (!d_fp)
141         return;
142 
143     close_wav();
144 }
145 
close_wav()146 void wavfile_sink_impl::close_wav()
147 {
148     unsigned int byte_count = d_sample_count * d_bytes_per_sample;
149 
150     wavheader_complete(d_fp, byte_count);
151 
152     fclose(d_fp);
153     d_fp = NULL;
154 }
155 
~wavfile_sink_impl()156 wavfile_sink_impl::~wavfile_sink_impl() { stop(); }
157 
stop()158 bool wavfile_sink_impl::stop()
159 {
160     if (d_new_fp) {
161         fclose(d_new_fp);
162         d_new_fp = NULL;
163     }
164 
165     close();
166 
167     return true;
168 }
169 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)170 int wavfile_sink_impl::work(int noutput_items,
171                             gr_vector_const_void_star& input_items,
172                             gr_vector_void_star& output_items)
173 {
174     float** in = (float**)&input_items[0];
175     int n_in_chans = input_items.size();
176 
177     short int sample_buf_s;
178 
179     int nwritten;
180 
181     gr::thread::scoped_lock guard(d_mutex); // hold mutex for duration of this block
182     do_update();                            // update: d_fp is reqd
183     if (!d_fp)                              // drop output on the floor
184         return noutput_items;
185 
186     for (nwritten = 0; nwritten < noutput_items; nwritten++) {
187         for (int chan = 0; chan < d_nchans; chan++) {
188             // Write zeros to channels which are in the WAV file
189             // but don't have any inputs here
190             if (chan < n_in_chans) {
191                 sample_buf_s = convert_to_short(in[chan][nwritten]);
192             } else {
193                 sample_buf_s = 0;
194             }
195 
196             wav_write_sample(d_fp, sample_buf_s, d_bytes_per_sample);
197 
198             if (feof(d_fp) || ferror(d_fp)) {
199                 fprintf(stderr, "[%s] file i/o error\n", __FILE__);
200                 close();
201                 exit(-1);
202             }
203             d_sample_count++;
204         }
205     }
206 
207     return nwritten;
208 }
209 
convert_to_short(float sample)210 short int wavfile_sink_impl::convert_to_short(float sample)
211 {
212     sample += d_normalize_shift;
213     sample *= d_normalize_fac;
214     if (sample > d_max_sample_val) {
215         sample = d_max_sample_val;
216     } else if (sample < d_min_sample_val) {
217         sample = d_min_sample_val;
218     }
219 
220     return (short int)boost::math::iround(sample);
221 }
222 
set_bits_per_sample(int bits_per_sample)223 void wavfile_sink_impl::set_bits_per_sample(int bits_per_sample)
224 {
225     gr::thread::scoped_lock guard(d_mutex);
226     if (bits_per_sample == 8 || bits_per_sample == 16) {
227         d_bytes_per_sample_new = bits_per_sample / 8;
228     }
229 }
230 
set_sample_rate(unsigned int sample_rate)231 void wavfile_sink_impl::set_sample_rate(unsigned int sample_rate)
232 {
233     gr::thread::scoped_lock guard(d_mutex);
234     d_sample_rate = sample_rate;
235 }
236 
bits_per_sample()237 int wavfile_sink_impl::bits_per_sample() { return d_bytes_per_sample_new; }
238 
sample_rate()239 unsigned int wavfile_sink_impl::sample_rate() { return d_sample_rate; }
240 
do_update()241 void wavfile_sink_impl::do_update()
242 {
243     if (!d_updated) {
244         return;
245     }
246 
247     if (d_fp) {
248         close_wav();
249     }
250 
251     d_fp = d_new_fp; // install new file pointer
252     d_new_fp = 0;
253     d_sample_count = 0;
254     d_bytes_per_sample = d_bytes_per_sample_new;
255 
256     if (d_bytes_per_sample == 1) {
257         d_max_sample_val = UCHAR_MAX;
258         d_min_sample_val = 0;
259         d_normalize_fac = d_max_sample_val / 2;
260         d_normalize_shift = 1;
261     } else if (d_bytes_per_sample == 2) {
262         d_max_sample_val = SHRT_MAX;
263         d_min_sample_val = SHRT_MIN;
264         d_normalize_fac = d_max_sample_val;
265         d_normalize_shift = 0;
266     }
267 
268     d_updated = false;
269 }
270 
271 } /* namespace blocks */
272 } /* namespace gr */
273