3
* Copyright 2004,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_source.h>
28
#include <gr_io_signature.h>
29
#include <gri_wavfile.h>
31
#include <sys/types.h>
35
// win32 (mingw/msvc) specific
40
#define OUR_O_BINARY O_BINARY
42
#define OUR_O_BINARY 0
44
// should be handled via configure
46
#define OUR_O_LARGEFILE O_LARGEFILE
48
#define OUR_O_LARGEFILE 0
52
gr_wavfile_source_sptr
53
gr_make_wavfile_source (const char *filename, bool repeat)
55
return gr_wavfile_source_sptr (new gr_wavfile_source (filename, repeat));
59
gr_wavfile_source::gr_wavfile_source (const char *filename, bool repeat)
60
: gr_sync_block ("wavfile_source",
61
gr_make_io_signature (0, 0, 0),
62
gr_make_io_signature (1, 2, sizeof(float))),
63
d_fp(NULL), d_repeat(repeat),
64
d_sample_rate(1), d_nchans(1), d_bytes_per_sample(2), d_first_sample_pos(0),
65
d_samples_per_chan(0), d_sample_idx(0)
67
// we use "open" to use to the O_LARGEFILE flag
70
if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0) {
72
throw std::runtime_error ("can't open file");
75
if ((d_fp = fdopen (fd, "rb")) == NULL) {
77
throw std::runtime_error ("can't open file");
80
// Scan headers, check file validity
81
if (!gri_wavheader_parse(d_fp,
86
d_samples_per_chan)) {
87
throw std::runtime_error("is not a valid wav file");
90
if (d_samples_per_chan == 0) {
91
throw std::runtime_error("WAV file does not contain any samples");
94
if (d_bytes_per_sample == 1) {
95
d_normalize_fac = 128;
96
d_normalize_shift = 1;
98
d_normalize_fac = 0x7FFF;
99
d_normalize_shift = 0;
102
// Re-set the output signature
103
set_output_signature(gr_make_io_signature(1, d_nchans, sizeof(float)));
107
gr_wavfile_source::~gr_wavfile_source ()
114
gr_wavfile_source::work(int noutput_items,
115
gr_vector_const_void_star &input_items,
116
gr_vector_void_star &output_items)
118
float **out = (float **) &output_items[0];
119
int n_out_chans = output_items.size();
124
for (i = 0; i < noutput_items; i++) {
125
if (d_sample_idx >= d_samples_per_chan) {
127
// if nothing was read at all, say we're done.
131
if (fseek (d_fp, d_first_sample_pos, SEEK_SET) == -1) {
132
fprintf(stderr, "[%s] fseek failed\n", __FILE__);
139
for (int chan = 0; chan < d_nchans; chan++) {
140
sample = gri_wav_read_sample(d_fp, d_bytes_per_sample);
142
if (chan < n_out_chans) {
143
out[chan][i] = convert_to_float(sample);
149
// OK, EOF is not necessarily an error. But we're not going to
150
// deal with handling corrupt wav files, so if they give us any
151
// trouble they won't be processed. Serves them bloody right.
152
if (feof(d_fp) || ferror(d_fp)) {
154
fprintf(stderr, "[%s] WAV file has corrupted header or i/o error\n", __FILE__);
161
return noutput_items;
166
gr_wavfile_source::convert_to_float(short int sample)
168
float sample_out = (float) sample;
169
sample_out /= d_normalize_fac;
170
sample_out -= d_normalize_shift;