GNU Radio 3.5.1 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_core_api.h>
00027 #include <gr_sync_block.h>
00028 #include <gr_file_sink_base.h>
00029 #include <boost/thread.hpp>
00030 
00031 class gr_wavfile_sink;
00032 typedef boost::shared_ptr<gr_wavfile_sink> gr_wavfile_sink_sptr;
00033 
00034 /*
00035  * \p filename The .wav file to be opened
00036  * \p n_channels Number of channels (2 = stereo or I/Q output)
00037  * \p sample_rate Sample rate [S/s]
00038  * \p bits_per_sample 16 or 8 bit, default is 16
00039  */
00040 GR_CORE_API gr_wavfile_sink_sptr
00041 gr_make_wavfile_sink (const char *filename,
00042                       int n_channels,
00043                       unsigned int sample_rate,
00044                       int bits_per_sample = 16);
00045 
00046 /*!
00047  * \brief Read stream from a Microsoft PCM (.wav) file, output floats
00048  *
00049  * Values are within [-1;1].
00050  * Check gr_make_wavfile_source() for extra info.
00051  *
00052  * \ingroup sink_blk
00053  */
00054 class GR_CORE_API gr_wavfile_sink : public gr_sync_block
00055 {
00056 private:
00057   friend GR_CORE_API gr_wavfile_sink_sptr gr_make_wavfile_sink (const char *filename,
00058                                                     int n_channels,
00059                                                     unsigned int sample_rate,
00060                                                     int bits_per_sample);
00061 
00062   gr_wavfile_sink(const char *filename,
00063                   int n_channels,
00064                   unsigned int sample_rate,
00065                   int bits_per_sample);
00066 
00067   unsigned d_sample_rate;
00068   int d_nchans;
00069   unsigned d_sample_count;
00070   int d_bytes_per_sample;
00071   int d_bytes_per_sample_new;
00072   int d_max_sample_val;
00073   int d_min_sample_val;
00074   int d_normalize_shift;
00075   int d_normalize_fac;
00076 
00077   FILE *d_fp;
00078   FILE *d_new_fp;
00079   bool d_updated;
00080   boost::mutex d_mutex;
00081   
00082   /*!
00083    * \brief Convert a sample value within [-1;+1] to a corresponding
00084    *  short integer value
00085    */
00086   short convert_to_short(float sample);
00087 
00088   /*!
00089    * \brief Writes information to the WAV header which is not available
00090    * a-priori (chunk size etc.) and closes the file. Not thread-safe and
00091    * assumes d_fp is a valid file pointer, should thus only be called by
00092    * other methods.
00093    */
00094   void close_wav();
00095 
00096 public:
00097   ~gr_wavfile_sink ();
00098 
00099   /*!
00100    * \brief Opens a new file and writes a WAV header. Thread-safe.
00101    */
00102   bool open(const char* filename);
00103 
00104   /*!
00105    * \brief Closes the currently active file and completes the WAV
00106    * header. Thread-safe.
00107    */
00108   void close();
00109 
00110   /*!
00111    * \brief If any file changes have occurred, update now. This is called
00112    * internally by work() and thus doesn't usually need to be called by
00113    * hand.
00114    */
00115   void do_update();
00116   
00117   /*!
00118    * \brief Set the sample rate. This will not affect the WAV file
00119    * currently opened. Any following open() calls will use this new
00120    * sample rate.
00121    */
00122   void set_sample_rate(unsigned int sample_rate);
00123   
00124   /*!
00125    * \brief Set bits per sample. This will not affect the WAV file
00126    * currently opened (see set_sample_rate()). If the value is neither
00127    * 8 nor 16, the call is ignored and the current value is kept.
00128    */
00129   void set_bits_per_sample(int bits_per_sample);
00130   
00131   
00132   int work(int noutput_items,
00133            gr_vector_const_void_star &input_items,
00134            gr_vector_void_star &output_items);
00135   
00136 };
00137 
00138 #endif /* INCLUDED_GR_WAVFILE_SINK_H */