Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_wavfile_sink.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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_SINK_H
24 #define INCLUDED_GR_WAVFILE_SINK_H
25
26 #include <gr_sync_block.h>
27 #include <gr_file_sink_base.h>
28 #include <gnuradio/omnithread.h>
29
30 class gr_wavfile_sink;
31 typedef boost::shared_ptr<gr_wavfile_sink> gr_wavfile_sink_sptr;
32
33 /*
34  * \p filename The .wav file to be opened
35  * \p n_channels Number of channels (2 = stereo or I/Q output)
36  * \p sample_rate Sample rate [S/s]
37  * \p bits_per_sample 16 or 8 bit, default is 16
38  */
39 gr_wavfile_sink_sptr
40 gr_make_wavfile_sink (const char *filename,
41                       int n_channels,
42                       unsigned int sample_rate,
43                       int bits_per_sample = 16);
44
45 /*!
46  * \brief Read stream from a Microsoft PCM (.wav) file, output floats
47  *
48  * Values are within [-1;1].
49  * Check gr_make_wavfile_source() for extra info.
50  *
51  * \ingroup sink_blk
52  */
53 class gr_wavfile_sink : public gr_sync_block
54 {
55 private:
56   friend gr_wavfile_sink_sptr gr_make_wavfile_sink (const char *filename,
57                                                     int n_channels,
58                                                     unsigned int sample_rate,
59                                                     int bits_per_sample);
60
61   gr_wavfile_sink(const char *filename,
62                   int n_channels,
63                   unsigned int sample_rate,
64                   int bits_per_sample);
65
66   unsigned d_sample_rate;
67   int d_nchans;
68   unsigned d_sample_count;
69   int d_bytes_per_sample;
70   int d_bytes_per_sample_new;
71   int d_max_sample_val;
72   int d_min_sample_val;
73   int d_normalize_shift;
74   int d_normalize_fac;
75
76   FILE *d_fp;
77   FILE *d_new_fp;
78   bool d_updated;
79   omni_mutex d_mutex;
80   
81   /*!
82    * \brief Convert a sample value within [-1;+1] to a corresponding
83    *  short integer value
84    */
85   short convert_to_short(float sample);
86
87   /*!
88    * \brief Writes information to the WAV header which is not available
89    * a-priori (chunk size etc.) and closes the file. Not thread-safe and
90    * assumes d_fp is a valid file pointer, should thus only be called by
91    * other methods.
92    */
93   void close_wav();
94
95 public:
96   ~gr_wavfile_sink ();
97
98   /*!
99    * \brief Opens a new file and writes a WAV header. Thread-safe.
100    */
101   bool open(const char* filename);
102
103   /*!
104    * \brief Closes the currently active file and completes the WAV
105    * header. Thread-safe.
106    */
107   void close();
108
109   /*!
110    * \brief If any file changes have occurred, update now. This is called
111    * internally by work() and thus doesn't usually need to be called by
112    * hand.
113    */
114   void do_update();
115   
116   /*!
117    * \brief Set the sample rate. This will not affect the WAV file
118    * currently opened. Any following open() calls will use this new
119    * sample rate.
120    */
121   void set_sample_rate(unsigned int sample_rate);
122   
123   /*!
124    * \brief Set bits per sample. This will not affect the WAV file
125    * currently opened (see set_sample_rate()). If the value is neither
126    * 8 nor 16, the call is ignored and the current value is kept.
127    */
128   void set_bits_per_sample(int bits_per_sample);
129   
130   
131   int work(int noutput_items,
132            gr_vector_const_void_star &input_items,
133            gr_vector_void_star &output_items);
134   
135 };
136
137 #endif /* INCLUDED_GR_WAVFILE_SINK_H */