GNU Radio 3.6.5 C++ API

wavfile_sink_impl.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008,2009,2013 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_IMPL_H
00024 #define INCLUDED_GR_WAVFILE_SINK_IMPL_H
00025 
00026 #include <blocks/wavfile_sink.h>
00027 #include <gr_file_sink_base.h>
00028 
00029 namespace gr {
00030   namespace blocks {
00031 
00032     class wavfile_sink_impl : public wavfile_sink
00033     {
00034     private:
00035       unsigned d_sample_rate;
00036       int d_nchans;
00037       unsigned d_sample_count;
00038       int d_bytes_per_sample;
00039       int d_bytes_per_sample_new;
00040       int d_max_sample_val;
00041       int d_min_sample_val;
00042       int d_normalize_shift;
00043       int d_normalize_fac;
00044 
00045       FILE *d_fp;
00046       FILE *d_new_fp;
00047       bool d_updated;
00048       boost::mutex d_mutex;
00049 
00050       /*!
00051        * \brief Convert a sample value within [-1;+1] to a corresponding
00052        *  short integer value
00053        */
00054       short convert_to_short(float sample);
00055 
00056       /*!
00057        * \brief If any file changes have occurred, update now. This is called
00058        * internally by work() and thus doesn't usually need to be called by
00059        * hand.
00060        */
00061       void do_update();
00062 
00063       /*!
00064        * \brief Writes information to the WAV header which is not available
00065        * a-priori (chunk size etc.) and closes the file. Not thread-safe and
00066        * assumes d_fp is a valid file pointer, should thus only be called by
00067        * other methods.
00068        */
00069       void close_wav();
00070 
00071     public:
00072       wavfile_sink_impl(const char *filename,
00073                         int n_channels,
00074                         unsigned int sample_rate,
00075                         int bits_per_sample);
00076       ~wavfile_sink_impl();
00077 
00078       bool open(const char* filename);
00079       void close();
00080 
00081       void set_sample_rate(unsigned int sample_rate);
00082       void set_bits_per_sample(int bits_per_sample);
00083 
00084       int bits_per_sample();
00085       unsigned int sample_rate();
00086 
00087       int work(int noutput_items,
00088                gr_vector_const_void_star &input_items,
00089                gr_vector_void_star &output_items);
00090     };
00091 
00092   } /* namespace blocks */
00093 } /* namespace gr */
00094 
00095 #endif /* INCLUDED_GR_WAVFILE_SINK_IMPL_H */