Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_wavfile_source.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2008 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 #ifndef INCLUDED_GR_WAVFILE_SOURCE_H
24 #define INCLUDED_GR_WAVFILE_SOURCE_H
25
26 #include <gr_sync_block.h>
27
28 class gr_wavfile_source;
29 typedef boost::shared_ptr<gr_wavfile_source> gr_wavfile_source_sptr;
30
31 gr_wavfile_source_sptr
32 gr_make_wavfile_source (const char *filename, bool repeat = false);
33
34 /*!
35  * \brief Read stream from a Microsoft PCM (.wav) file, output floats
36  *
37  * Unless otherwise called, values are within [-1;1].
38  * Check gr_make_wavfile_source() for extra info.
39  *
40  * \ingroup source_blk
41  */
42
43 class gr_wavfile_source : public gr_sync_block
44 {
45 private:
46   friend gr_wavfile_source_sptr gr_make_wavfile_source (const char *filename,
47                                                               bool repeat);
48   gr_wavfile_source(const char *filename, bool repeat);
49
50   FILE *d_fp;
51   bool d_repeat;
52   
53   unsigned d_sample_rate;
54   int d_nchans;
55   int d_bytes_per_sample;
56   int d_first_sample_pos;
57   unsigned d_samples_per_chan;
58   unsigned d_sample_idx;
59   int d_normalize_shift;
60   int d_normalize_fac;
61   
62   /*!
63    * \brief Convert an integer sample value to a float value within [-1;1]
64    */
65   float convert_to_float(short int sample);
66
67 public:
68   ~gr_wavfile_source ();
69   
70   int work(int noutput_items,
71            gr_vector_const_void_star &input_items,
72            gr_vector_void_star &output_items);
73
74   /*!
75    * \brief Read the sample rate as specified in the wav file header
76    */
77   unsigned int sample_rate() const { return d_sample_rate; };
78
79   /*!
80    * \brief Return the number of bits per sample as specified in the wav
81    * file header. Only 8 or 16 bit are supported here.
82    */
83   int bits_per_sample() const { return d_bytes_per_sample * 8; };
84   
85   /*!
86    * \brief Return the number of channels in the wav file as specified in
87    * the wav file header. This is also the max number of outputs you can
88    * have.
89    */
90   int channels() const { return d_nchans; };
91 };
92
93 #endif /* INCLUDED_GR_WAVFILE_SOURCE_H */