GNU Radio 3.4.2 C++ API
gr_wavfile_sink.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008,2009 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_WAVFILE_SINK_H
00024 #define INCLUDED_GR_WAVFILE_SINK_H
00025 
00026 #include <gr_sync_block.h>
00027 #include <gr_file_sink_base.h>
00028 #include <boost/thread.hpp>
00029 
00030 class gr_wavfile_sink;
00031 typedef boost::shared_ptr<gr_wavfile_sink> gr_wavfile_sink_sptr;
00032 
00033 /*
00034  * \p filename The .wav file to be opened
00035  * \p n_channels Number of channels (2 = stereo or I/Q output)
00036  * \p sample_rate Sample rate [S/s]
00037  * \p bits_per_sample 16 or 8 bit, default is 16
00038  */
00039 gr_wavfile_sink_sptr
00040 gr_make_wavfile_sink (const char *filename,
00041                       int n_channels,
00042                       unsigned int sample_rate,
00043                       int bits_per_sample = 16);
00044 
00045 /*!
00046  * \brief Read stream from a Microsoft PCM (.wav) file, output floats
00047  *
00048  * Values are within [-1;1].
00049  * Check gr_make_wavfile_source() for extra info.
00050  *
00051  * \ingroup sink_blk
00052  */
00053 class gr_wavfile_sink : public gr_sync_block
00054 {
00055 private:
00056   friend gr_wavfile_sink_sptr gr_make_wavfile_sink (const char *filename,
00057                                                     int n_channels,
00058                                                     unsigned int sample_rate,
00059                                                     int bits_per_sample);
00060 
00061   gr_wavfile_sink(const char *filename,
00062                   int n_channels,
00063                   unsigned int sample_rate,
00064                   int bits_per_sample);
00065 
00066   unsigned d_sample_rate;
00067   int d_nchans;
00068   unsigned d_sample_count;
00069   int d_bytes_per_sample;
00070   int d_bytes_per_sample_new;
00071   int d_max_sample_val;
00072   int d_min_sample_val;
00073   int d_normalize_shift;
00074   int d_normalize_fac;
00075 
00076   FILE *d_fp;
00077   FILE *d_new_fp;
00078   bool d_updated;
00079   boost::mutex d_mutex;
00080   
00081   /*!
00082    * \brief Convert a sample value within [-1;+1] to a corresponding
00083    *  short integer value
00084    */
00085   short convert_to_short(float sample);
00086 
00087   /*!
00088    * \brief Writes information to the WAV header which is not available
00089    * a-priori (chunk size etc.) and closes the file. Not thread-safe and
00090    * assumes d_fp is a valid file pointer, should thus only be called by
00091    * other methods.
00092    */
00093   void close_wav();
00094 
00095 public:
00096   ~gr_wavfile_sink ();
00097 
00098   /*!
00099    * \brief Opens a new file and writes a WAV header. Thread-safe.
00100    */
00101   bool open(const char* filename);
00102 
00103   /*!
00104    * \brief Closes the currently active file and completes the WAV
00105    * header. Thread-safe.
00106    */
00107   void close();
00108 
00109   /*!
00110    * \brief If any file changes have occurred, update now. This is called
00111    * internally by work() and thus doesn't usually need to be called by
00112    * hand.
00113    */
00114   void do_update();
00115   
00116   /*!
00117    * \brief Set the sample rate. This will not affect the WAV file
00118    * currently opened. Any following open() calls will use this new
00119    * sample rate.
00120    */
00121   void set_sample_rate(unsigned int sample_rate);
00122   
00123   /*!
00124    * \brief Set bits per sample. This will not affect the WAV file
00125    * currently opened (see set_sample_rate()). If the value is neither
00126    * 8 nor 16, the call is ignored and the current value is kept.
00127    */
00128   void set_bits_per_sample(int bits_per_sample);
00129   
00130   
00131   int work(int noutput_items,
00132            gr_vector_const_void_star &input_items,
00133            gr_vector_void_star &output_items);
00134   
00135 };
00136 
00137 #endif /* INCLUDED_GR_WAVFILE_SINK_H */