1 /* -*- c++ -*- */
2 /*
3 * Copyright 2004,2008,2010,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_source_impl.h"
28 #include <gnuradio/blocks/wavfile.h>
29 #include <gnuradio/io_signature.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <stdexcept>
33
34 // win32 (mingw/msvc) specific
35 #ifdef HAVE_IO_H
36 #include <io.h>
37 #endif
38 #ifdef O_BINARY
39 #define OUR_O_BINARY O_BINARY
40 #else
41 #define OUR_O_BINARY 0
42 #endif
43 // should be handled via configure
44 #ifdef O_LARGEFILE
45 #define OUR_O_LARGEFILE O_LARGEFILE
46 #else
47 #define OUR_O_LARGEFILE 0
48 #endif
49
50 namespace gr {
51 namespace blocks {
52
make(const char * filename,bool repeat)53 wavfile_source::sptr wavfile_source::make(const char* filename, bool repeat)
54 {
55 return gnuradio::get_initial_sptr(new wavfile_source_impl(filename, repeat));
56 }
57
wavfile_source_impl(const char * filename,bool repeat)58 wavfile_source_impl::wavfile_source_impl(const char* filename, bool repeat)
59 : sync_block("wavfile_source",
60 io_signature::make(0, 0, 0),
61 io_signature::make(1, 2, sizeof(float))),
62 d_fp(NULL),
63 d_repeat(repeat),
64 d_sample_rate(1),
65 d_nchans(1),
66 d_bytes_per_sample(2),
67 d_first_sample_pos(0),
68 d_samples_per_chan(0),
69 d_sample_idx(0)
70 {
71 // we use "open" to use to the O_LARGEFILE flag
72
73 int fd;
74 if ((fd = open(filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0) {
75 perror(filename);
76 throw std::runtime_error("can't open file");
77 }
78
79 if ((d_fp = fdopen(fd, "rb")) == NULL) {
80 perror(filename);
81 throw std::runtime_error("can't open file");
82 }
83
84 // Scan headers, check file validity
85 if (!wavheader_parse(d_fp,
86 d_sample_rate,
87 d_nchans,
88 d_bytes_per_sample,
89 d_first_sample_pos,
90 d_samples_per_chan)) {
91 throw std::runtime_error("is not a valid wav file");
92 }
93
94 if (d_samples_per_chan == 0) {
95 throw std::runtime_error("WAV file does not contain any samples");
96 }
97
98 if (d_bytes_per_sample == 1) {
99 d_normalize_fac = 128;
100 d_normalize_shift = 1;
101 } else {
102 d_normalize_fac = 0x7FFF;
103 d_normalize_shift = 0;
104 }
105
106 // Re-set the output signature
107 set_output_signature(io_signature::make(1, d_nchans, sizeof(float)));
108 }
109
~wavfile_source_impl()110 wavfile_source_impl::~wavfile_source_impl() { fclose(d_fp); }
111
convert_to_float(short int sample)112 float wavfile_source_impl::convert_to_float(short int sample)
113 {
114 float sample_out = (float)sample;
115 sample_out /= d_normalize_fac;
116 sample_out -= d_normalize_shift;
117 return sample_out;
118 }
119
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)120 int wavfile_source_impl::work(int noutput_items,
121 gr_vector_const_void_star& input_items,
122 gr_vector_void_star& output_items)
123 {
124 float** out = (float**)&output_items[0];
125 int n_out_chans = output_items.size();
126
127 int i;
128 short sample;
129
130 for (i = 0; i < noutput_items; i++) {
131 if (d_sample_idx >= d_samples_per_chan) {
132 if (!d_repeat) {
133 // if nothing was read at all, say we're done.
134 return i ? i : -1;
135 }
136
137 if (fseek(d_fp, d_first_sample_pos, SEEK_SET) == -1) {
138 fprintf(stderr, "[%s] fseek failed\n", __FILE__);
139 exit(-1);
140 }
141
142 d_sample_idx = 0;
143 }
144
145 for (int chan = 0; chan < d_nchans; chan++) {
146 sample = wav_read_sample(d_fp, d_bytes_per_sample);
147
148 if (chan < n_out_chans) {
149 out[chan][i] = convert_to_float(sample);
150 }
151 }
152
153 d_sample_idx++;
154
155 // OK, EOF is not necessarily an error. But we're not going to
156 // deal with handling corrupt wav files, so if they give us any
157 // trouble they won't be processed. Serves them bloody right.
158 if (feof(d_fp) || ferror(d_fp)) {
159 if (i == 0) {
160 fprintf(stderr,
161 "[%s] WAV file has corrupted header or i/o error\n",
162 __FILE__);
163 return -1;
164 }
165 return i;
166 }
167 }
168
169 return noutput_items;
170 }
171
172 } /* namespace blocks */
173 } /* namespace gr */
174