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