GNU Radio 3.4.2 C++ API
gri_wavfile.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008 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 // This file stores all the RIFF file type knowledge for the gr_wavfile_*
00024 // blocks.
00025 
00026 #include <cstdio>
00027 
00028 /*!
00029  * \brief Read signal information from a given WAV file.
00030  *
00031  * \p fp File pointer to an opened, empty file.
00032  * \p sample_rate Stores the sample rate [S/s]
00033  * \p nchans      Number of channels
00034  * \p bytes_per_sample Bytes per sample, can either be 1 or 2 (corresponding to
00035  *         8 or 16 bit samples, respectively)
00036  * \p first_sample_pos Number of the first byte containing a sample. Use this
00037  *         with fseek() to jump from the end of the file to the first sample
00038  *         when in repeat mode.
00039  * \p samples_per_chan Number of samples per channel
00040  * \p normalize_fac The normalization factor with which you need to divide the
00041  *         integer values of the samples to get them within [-1;1]
00042  * \p normalize_shift The value by which the sample values need to be shifted
00043  *         after normalization (reason being, 8-bit WAV files store samples as
00044  *         unsigned char and 16-bit as signed short int)
00045  * \return True on a successful read, false if the file could not be read or is
00046  *         not a valid WAV file.
00047  */
00048 bool
00049 gri_wavheader_parse(FILE *fp,
00050                     unsigned int &sample_rate,
00051                     int &nchans,
00052                     int &bytes_per_sample,
00053                     int &first_sample_pos,
00054                     unsigned int &samples_per_chan);
00055 
00056 
00057 /*!
00058  * \brief Read one sample from an open WAV file at the current position.
00059  *
00060  * Takes care of endianness.
00061  */
00062 short int
00063 gri_wav_read_sample(FILE *fp, int bytes_per_sample);
00064 
00065 
00066 /*!
00067  * \brief Write a valid RIFF file header
00068  *
00069  * Note: Some header values are kept blank because they're usually not known
00070  * a-priori (file and chunk lengths). Use gri_wavheader_complete() to fill
00071  * these in.
00072  */
00073 bool
00074 gri_wavheader_write(FILE *fp,
00075                  unsigned int sample_rate,
00076                  int nchans,
00077                  int bytes_per_sample);
00078 
00079 /*!
00080  * \brief Write one sample to an open WAV file at the current position.
00081  *
00082  * Takes care of endianness.
00083  */
00084 void
00085 gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample);
00086 
00087 
00088 /*!
00089  * \brief Complete a WAV header
00090  *
00091  * Note: The stream position is changed during this function. If anything
00092  * needs to be written to the WAV file after calling this function (which
00093  * shouldn't happen), you  need to fseek() to the end of the file (or 
00094  * whereever).
00095  *
00096  * \p fp File pointer to an open WAV file with a blank header
00097  * \p byte_count Length of all samples written to the file in bytes.
00098  */
00099 bool
00100 gri_wavheader_complete(FILE *fp, unsigned int byte_count);