[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r7727 - in gnuradio/branches/developers/jcorgan/wav/gn
From: |
jcorgan |
Subject: |
[Commit-gnuradio] r7727 - in gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src: lib/io python/gnuradio/gr |
Date: |
Sun, 17 Feb 2008 20:09:08 -0700 (MST) |
Author: jcorgan
Date: 2008-02-17 20:09:07 -0700 (Sun, 17 Feb 2008)
New Revision: 7727
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.cc
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.h
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.i
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.cc
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.h
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.i
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.cc
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.h
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/python/gnuradio/gr/qa_wavefile.py
Log:
Stylistic changes, mostly.
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.cc
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.cc
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.cc
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004,2006,2007 Free Software Foundation, Inc.
+ * Copyright 2004,2006,2007,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -52,225 +52,227 @@
gr_wavfile_sink_sptr
-gr_make_wavfile_sink (const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample)
+gr_make_wavfile_sink(const char *filename,
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample)
{
- return gr_wavfile_sink_sptr (new gr_wavfile_sink (filename,
- n_channels,
- sample_rate,
- bits_per_sample));
+ return gr_wavfile_sink_sptr (new gr_wavfile_sink (filename,
+ n_channels,
+ sample_rate,
+ bits_per_sample));
}
gr_wavfile_sink::gr_wavfile_sink(const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample)
- : gr_sync_block ("wavfile_sink",
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample)
+ : gr_sync_block ("wavfile_sink",
gr_make_io_signature(1, n_channels, sizeof(float)),
gr_make_io_signature(0, 0, 0)),
- d_sample_rate(sample_rate), d_nchans(n_channels),
- d_fp(0), d_new_fp(0), d_updated(false)
+ d_sample_rate(sample_rate), d_nchans(n_channels),
+ d_fp(0), d_new_fp(0), d_updated(false)
{
- if (bits_per_sample != 8 && bits_per_sample != 16) {
- throw std::runtime_error("Invalid bits per sample (supports 8
and 16)");
- }
- d_bytes_per_sample = bits_per_sample / 8;
- d_bytes_per_sample_new = d_bytes_per_sample;
+ if (bits_per_sample != 8 && bits_per_sample != 16) {
+ throw std::runtime_error("Invalid bits per sample (supports 8 and 16)");
+ }
+ d_bytes_per_sample = bits_per_sample / 8;
+ d_bytes_per_sample_new = d_bytes_per_sample;
+
+ if (!open(filename)) {
+ throw std::runtime_error ("can't open file");
+ }
- if (!open(filename)) {
- throw std::runtime_error ("can't open file");
- }
-
- if (bits_per_sample == 8) {
- d_max_sample_val = 0xFF;
- d_min_sample_val = 0;
- d_normalize_fac = d_max_sample_val/2;
- d_normalize_shift = 1;
- } else {
- d_max_sample_val = 0x7FFF;
- d_min_sample_val = -0x7FFF;
- d_normalize_fac = d_max_sample_val;
- d_normalize_shift = 0;
- if (bits_per_sample != 16) {
- fprintf(stderr, "Invalid bits per sample value
requested, using 16");
- }
- }
+ if (bits_per_sample == 8) {
+ d_max_sample_val = 0xFF;
+ d_min_sample_val = 0;
+ d_normalize_fac = d_max_sample_val/2;
+ d_normalize_shift = 1;
+ } else {
+ d_max_sample_val = 0x7FFF;
+ d_min_sample_val = -0x7FFF;
+ d_normalize_fac = d_max_sample_val;
+ d_normalize_shift = 0;
+ if (bits_per_sample != 16) {
+ fprintf(stderr, "Invalid bits per sample value requested, using 16");
+ }
+ }
}
bool
gr_wavfile_sink::open(const char* filename)
{
- omni_mutex_lock l(d_mutex);
-
- // we use the open system call to get access to the O_LARGEFILE flag.
- int fd;
- if ((fd = ::open (filename,
+ omni_mutex_lock l(d_mutex);
+
+ // we use the open system call to get access to the O_LARGEFILE flag.
+ int fd;
+ if ((fd = ::open (filename,
O_WRONLY|O_CREAT|O_TRUNC|OUR_O_LARGEFILE|OUR_O_BINARY,
0664)) < 0){
- perror (filename);
- return false;
- }
+ perror (filename);
+ return false;
+ }
- if (d_new_fp) { // if we've already got a new one open, close it
- fclose(d_new_fp);
- d_new_fp = 0;
- }
-
- if ((d_new_fp = fdopen (fd, "wb")) == NULL) {
- perror (filename);
- ::close(fd); // don't leak file descriptor if fdopen fails.
- return false;
- }
- d_updated = true;
-
- if (!gri_wavheader_write(d_new_fp,
- d_sample_rate,
- d_nchans,
- d_bytes_per_sample_new)) {
- fprintf(stderr, "[%s] could not write to WAV file\n", __FILE__);
- exit(-1);
- }
- return true;
+ if (d_new_fp) { // if we've already got a new one open, close it
+ fclose(d_new_fp);
+ d_new_fp = 0;
+ }
+
+ if ((d_new_fp = fdopen (fd, "wb")) == NULL) {
+ perror (filename);
+ ::close(fd); // don't leak file descriptor if fdopen fails.
+ return false;
+ }
+ d_updated = true;
+
+ if (!gri_wavheader_write(d_new_fp,
+ d_sample_rate,
+ d_nchans,
+ d_bytes_per_sample_new)) {
+ fprintf(stderr, "[%s] could not write to WAV file\n", __FILE__);
+ exit(-1);
+ }
+
+ return true;
}
void
gr_wavfile_sink::close()
{
- omni_mutex_lock l(d_mutex);
-
- if (!d_fp)
- return;
-
- close_wav();
+ omni_mutex_lock l(d_mutex);
+
+ if (!d_fp)
+ return;
+
+ close_wav();
}
void gr_wavfile_sink::close_wav()
{
- unsigned int byte_count = d_sample_count * d_bytes_per_sample;
-
- gri_wavheader_complete(d_fp, byte_count);
-
- fclose(d_fp);
- d_fp = NULL;
+ unsigned int byte_count = d_sample_count * d_bytes_per_sample;
+
+ gri_wavheader_complete(d_fp, byte_count);
+
+ fclose(d_fp);
+ d_fp = NULL;
}
gr_wavfile_sink::~gr_wavfile_sink ()
{
- if (d_new_fp) {
- fclose(d_new_fp);
- }
- close();
+ if (d_new_fp) {
+ fclose(d_new_fp);
+ }
+
+ close();
}
int
gr_wavfile_sink::work (int noutput_items,
- gr_vector_const_void_star &input_items,
- gr_vector_void_star &output_items)
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
{
- float **in = (float **) &input_items[0];
- int n_in_chans = input_items.size();
-
- short int sample_buf_s;
-
- int nwritten;
-
- do_update(); // update: d_fp is reqd
- if (!d_fp) // drop output on the floor
- return noutput_items;
-
- for (nwritten = 0; nwritten < noutput_items; nwritten++) {
- for (int chan = 0; chan < d_nchans; chan++) {
- // Write zeros to channels which are in the WAV file
- // but don't have any inputs here
- if (chan < n_in_chans) {
- sample_buf_s =
- convert_to_short(in[chan][nwritten]);
- } else {
- sample_buf_s = 0;
- }
-
- gri_wav_write_sample(d_fp, sample_buf_s,
d_bytes_per_sample);
-
- if (feof(d_fp) || ferror(d_fp)) {
- fprintf(stderr, "[%s] file i/o error\n",
__FILE__);
- close();
- exit(-1);
- }
- d_sample_count++;
- }
- }
-
- return nwritten;
+ float **in = (float **) &input_items[0];
+ int n_in_chans = input_items.size();
+
+ short int sample_buf_s;
+
+ int nwritten;
+
+ do_update(); // update: d_fp is reqd
+ if (!d_fp) // drop output on the floor
+ return noutput_items;
+
+ for (nwritten = 0; nwritten < noutput_items; nwritten++) {
+ for (int chan = 0; chan < d_nchans; chan++) {
+ // Write zeros to channels which are in the WAV file
+ // but don't have any inputs here
+ if (chan < n_in_chans) {
+ sample_buf_s =
+ convert_to_short(in[chan][nwritten]);
+ } else {
+ sample_buf_s = 0;
+ }
+
+ gri_wav_write_sample(d_fp, sample_buf_s, d_bytes_per_sample);
+
+ if (feof(d_fp) || ferror(d_fp)) {
+ fprintf(stderr, "[%s] file i/o error\n", __FILE__);
+ close();
+ exit(-1);
+ }
+ d_sample_count++;
+ }
+ }
+
+ return nwritten;
}
short int
gr_wavfile_sink::convert_to_short(float sample)
{
- sample += d_normalize_shift;
- sample *= d_normalize_fac;
- if (sample > d_max_sample_val) {
- sample = d_max_sample_val;
- } else if (sample < d_min_sample_val) {
- sample = d_min_sample_val;
- }
-
- return (short int) roundf(sample);
+ sample += d_normalize_shift;
+ sample *= d_normalize_fac;
+ if (sample > d_max_sample_val) {
+ sample = d_max_sample_val;
+ } else if (sample < d_min_sample_val) {
+ sample = d_min_sample_val;
+ }
+
+ return (short int) roundf(sample);
}
void
gr_wavfile_sink::set_bits_per_sample(int bits_per_sample)
{
- omni_mutex_lock l(d_mutex);
- if (bits_per_sample == 8 || bits_per_sample == 16) {
- d_bytes_per_sample_new = bits_per_sample / 8;
- }
+ omni_mutex_lock l(d_mutex);
+ if (bits_per_sample == 8 || bits_per_sample == 16) {
+ d_bytes_per_sample_new = bits_per_sample / 8;
+ }
}
void
gr_wavfile_sink::set_sample_rate(unsigned int sample_rate)
{
- omni_mutex_lock l(d_mutex);
- d_sample_rate = sample_rate;
+ omni_mutex_lock l(d_mutex);
+ d_sample_rate = sample_rate;
}
void
gr_wavfile_sink::do_update()
{
- if (!d_updated) {
- return;
- }
+ if (!d_updated) {
+ return;
+ }
+
+ omni_mutex_lock l(d_mutex); // hold mutex for duration of this block
+ if (d_fp) {
+ close_wav();
+ }
- omni_mutex_lock l(d_mutex); // hold mutex for duration of this
block
- if (d_fp) {
- close_wav();
- }
- d_fp = d_new_fp; // install new file pointer
- d_new_fp = 0;
+ d_fp = d_new_fp; // install new file pointer
+ d_new_fp = 0;
+ d_sample_count = 0;
+ d_bytes_per_sample = d_bytes_per_sample_new;
- d_sample_count = 0;
- d_bytes_per_sample = d_bytes_per_sample_new;
- if (d_bytes_per_sample == 1) {
- d_max_sample_val = UCHAR_MAX;
- d_min_sample_val = 0;
- d_normalize_fac = d_max_sample_val/2;
- d_normalize_shift = 1;
- } else if (d_bytes_per_sample == 2) {
- d_max_sample_val = SHRT_MAX;
- d_min_sample_val = SHRT_MIN;
- d_normalize_fac = d_max_sample_val;
- d_normalize_shift = 0;
- }
-
- d_updated = false;
+ if (d_bytes_per_sample == 1) {
+ d_max_sample_val = UCHAR_MAX;
+ d_min_sample_val = 0;
+ d_normalize_fac = d_max_sample_val/2;
+ d_normalize_shift = 1;
+ } else if (d_bytes_per_sample == 2) {
+ d_max_sample_val = SHRT_MAX;
+ d_min_sample_val = SHRT_MIN;
+ d_normalize_fac = d_max_sample_val;
+ d_normalize_shift = 0;
+ }
+
+ d_updated = false;
}
-
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.h
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.h
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.h
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -32,15 +32,15 @@
/*
* \p filename The .wav file to be opened
- * \p n_channes Number of channels (2 = stereo or I/Q output)
+ * \p n_channels Number of channels (2 = stereo or I/Q output)
* \p sample_rate Sample rate [S/s]
- * \p bits_per_sample 16 or 8 bit
+ * \p bits_per_sample 16 or 8 bit, default is 16
*/
gr_wavfile_sink_sptr
gr_make_wavfile_sink (const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample = 16);
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample = 16);
/*!
* \brief Read stream from a Microsoft PCM (.wav) file, output floats
@@ -52,87 +52,86 @@
*/
class gr_wavfile_sink : public gr_sync_block
{
- friend gr_wavfile_sink_sptr gr_make_wavfile_sink (const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample);
+private:
+ friend gr_wavfile_sink_sptr gr_make_wavfile_sink (const char *filename,
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample);
- private:
- unsigned d_sample_rate;
- int d_nchans;
- unsigned d_sample_count;
- int d_bytes_per_sample;
- int d_bytes_per_sample_new;
- int d_max_sample_val;
- int d_min_sample_val;
- int d_normalize_shift;
- int d_normalize_fac;
+ gr_wavfile_sink(const char *filename,
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample);
- FILE *d_fp;
- FILE *d_new_fp;
- bool d_updated;
- omni_mutex d_mutex;
+ unsigned d_sample_rate;
+ int d_nchans;
+ unsigned d_sample_count;
+ int d_bytes_per_sample;
+ int d_bytes_per_sample_new;
+ int d_max_sample_val;
+ int d_min_sample_val;
+ int d_normalize_shift;
+ int d_normalize_fac;
- /*!
- * \brief Convert a sample value within [-1;+1] to a corresponding
- * short integer value
- */
- short convert_to_short(float sample);
+ FILE *d_fp;
+ FILE *d_new_fp;
+ bool d_updated;
+ omni_mutex d_mutex;
+
+ /*!
+ * \brief Convert a sample value within [-1;+1] to a corresponding
+ * short integer value
+ */
+ short convert_to_short(float sample);
- /*!
- * \brief Writes information to the WAV header which is not available
- * a-priori (chunk size etc.) and closes the file. Not thread-safe and
- * assumes d_fp is a valid file pointer, should thus only be called by
- * other methods.
- */
- void close_wav();
+ /*!
+ * \brief Writes information to the WAV header which is not available
+ * a-priori (chunk size etc.) and closes the file. Not thread-safe and
+ * assumes d_fp is a valid file pointer, should thus only be called by
+ * other methods.
+ */
+ void close_wav();
- protected:
- gr_wavfile_sink(const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample);
+public:
+ ~gr_wavfile_sink ();
- public:
- ~gr_wavfile_sink ();
+ /*!
+ * \brief Opens a new file and writes a WAV header. Thread-safe.
+ */
+ bool open(const char* filename);
- /*!
- * \brief Opens a new file and writes a WAV header. Thread-safe.
- */
- bool open(const char* filename);
+ /*!
+ * \brief Closes the currently active file and completes the WAV
+ * header. Thread-safe.
+ */
+ void close();
- /*!
- * \brief Closes the currently active file and completes the WAV
- * header. Thread-safe.
- */
- void close();
-
- /*!
- * \brief If any file changes have occurred, update now. This is called
- * internally by work() and thus doesn't usually need to be called by
- * hand.
- */
- void do_update();
-
- /*!
- * \brief Set the sample rate. This will not affect the WAV file
- * currently opened. Any following open() calls will use this new
- * sample rate.
- */
- void set_sample_rate(unsigned int sample_rate);
-
- /*!
- * \brief Set bits per sample. This will not affect the WAV file
- * currently opened (see set_sample_rate()). If the value is neither
- * 8 nor 16, the call is ignored and the current value is kept.
- */
- void set_bits_per_sample(int bits_per_sample);
-
-
- int work (int noutput_items,
- gr_vector_const_void_star &input_items,
- gr_vector_void_star &output_items);
-
+ /*!
+ * \brief If any file changes have occurred, update now. This is called
+ * internally by work() and thus doesn't usually need to be called by
+ * hand.
+ */
+ void do_update();
+
+ /*!
+ * \brief Set the sample rate. This will not affect the WAV file
+ * currently opened. Any following open() calls will use this new
+ * sample rate.
+ */
+ void set_sample_rate(unsigned int sample_rate);
+
+ /*!
+ * \brief Set bits per sample. This will not affect the WAV file
+ * currently opened (see set_sample_rate()). If the value is neither
+ * 8 nor 16, the call is ignored and the current value is kept.
+ */
+ void set_bits_per_sample(int bits_per_sample);
+
+
+ int work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+
};
#endif /* INCLUDED_GR_WAVFILE_SINK_H */
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.i
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.i
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_sink.i
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -25,23 +25,23 @@
gr_wavfile_sink_sptr
gr_make_wavfile_sink (const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample = 16) throw (std::runtime_error);
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample = 16) throw (std::runtime_error);
class gr_wavfile_sink : public gr_sync_block
{
- protected:
- gr_wavfile_sink(const char *filename,
- int n_channels,
- unsigned int sample_rate,
- int bits_per_sample) throw (std::runtime_error);
-
- public:
- ~gr_wavfile_sink ();
- bool open(const char* filename);
- void close();
- void set_sample_rate(unsigned int sample_rate);
- void set_bits_per_sample(int bits_per_sample);
+protected:
+ gr_wavfile_sink(const char *filename,
+ int n_channels,
+ unsigned int sample_rate,
+ int bits_per_sample) throw (std::runtime_error);
+
+public:
+ ~gr_wavfile_sink ();
+ bool open(const char* filename);
+ void close();
+ void set_sample_rate(unsigned int sample_rate);
+ void set_bits_per_sample(int bits_per_sample);
};
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.cc
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.cc
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.cc
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2004,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -60,108 +60,113 @@
: gr_sync_block ("wavfile_source",
gr_make_io_signature (0, 0, 0),
gr_make_io_signature (1, 2, sizeof(float))),
- d_fp (NULL), d_repeat (repeat),
+ d_fp(NULL), d_repeat(repeat),
d_sample_rate(1), d_nchans(1), d_bytes_per_sample(2),
d_first_sample_pos(0),
d_samples_per_chan(0), d_sample_idx(0)
{
- // we use "open" to use to the O_LARGEFILE flag
+ // we use "open" to use to the O_LARGEFILE flag
- int fd;
- if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) <
0) {
- perror (filename);
- throw std::runtime_error ("can't open file");
- }
+ int fd;
+ if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0) {
+ perror (filename);
+ throw std::runtime_error ("can't open file");
+ }
- if ((d_fp = fdopen (fd, "rb")) == NULL) {
- perror (filename);
- throw std::runtime_error ("can't open file");
- }
+ if ((d_fp = fdopen (fd, "rb")) == NULL) {
+ perror (filename);
+ throw std::runtime_error ("can't open file");
+ }
- // Scan headers, check file validity
- if (!gri_wavheader_parse(d_fp,
- d_sample_rate,
- d_nchans,
- d_bytes_per_sample,
- d_first_sample_pos,
- d_samples_per_chan)) {
- throw std::runtime_error("is not a valid wav file");
- }
- if (d_samples_per_chan == 0) {
- throw std::runtime_error("WAV file does not contain any
samples");
- }
+ // Scan headers, check file validity
+ if (!gri_wavheader_parse(d_fp,
+ d_sample_rate,
+ d_nchans,
+ d_bytes_per_sample,
+ d_first_sample_pos,
+ d_samples_per_chan)) {
+ throw std::runtime_error("is not a valid wav file");
+ }
+
+ if (d_samples_per_chan == 0) {
+ throw std::runtime_error("WAV file does not contain any samples");
+ }
- if (d_bytes_per_sample == 1) {
- d_normalize_fac = 128;
- d_normalize_shift = 1;
- } else {
- d_normalize_fac = 0x7FFF;
- d_normalize_shift = 0;
- }
+ if (d_bytes_per_sample == 1) {
+ d_normalize_fac = 128;
+ d_normalize_shift = 1;
+ } else {
+ d_normalize_fac = 0x7FFF;
+ d_normalize_shift = 0;
+ }
- // Re-set the output signature
- set_output_signature(gr_make_io_signature(1, d_nchans, sizeof(float)));
+ // Re-set the output signature
+ set_output_signature(gr_make_io_signature(1, d_nchans, sizeof(float)));
}
gr_wavfile_source::~gr_wavfile_source ()
{
- fclose ((FILE *) d_fp);
+ fclose(d_fp);
}
int
-gr_wavfile_source::work (int noutput_items,
- gr_vector_const_void_star &input_items,
- gr_vector_void_star &output_items)
+gr_wavfile_source::work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
{
- float **out = (float **) &output_items[0];
- int n_out_chans = output_items.size();
+ float **out = (float **) &output_items[0];
+ int n_out_chans = output_items.size();
- int i;
- short sample;
+ int i;
+ short sample;
- for (i = 0; i < noutput_items; i++) {
- if (d_sample_idx >= d_samples_per_chan) {
- if (!d_repeat) {
- // if nothing was read at all, say we're done.
- return i ? i : -1;
- }
- if (fseek (d_fp, d_first_sample_pos, SEEK_SET) == -1) {
- fprintf(stderr, "[%s] fseek failed\n",
__FILE__);
- exit(-1);
- }
- d_sample_idx = 0;
- }
+ for (i = 0; i < noutput_items; i++) {
+ if (d_sample_idx >= d_samples_per_chan) {
+ if (!d_repeat) {
+ // if nothing was read at all, say we're done.
+ return i ? i : -1;
+ }
- for (int chan = 0; chan < d_nchans; chan++) {
- sample = gri_wav_read_sample(d_fp, d_bytes_per_sample);
- if (chan < n_out_chans) {
- out[chan][i] = convert_to_float(sample);
- }
- }
+ if (fseek (d_fp, d_first_sample_pos, SEEK_SET) == -1) {
+ fprintf(stderr, "[%s] fseek failed\n", __FILE__);
+ exit(-1);
+ }
- d_sample_idx++;
+ d_sample_idx = 0;
+ }
+
+ for (int chan = 0; chan < d_nchans; chan++) {
+ sample = gri_wav_read_sample(d_fp, d_bytes_per_sample);
- // OK, EOF is not necessarily an error. But we're not going to
- // deal with handling corrupt wav files, so if they give us any
- // trouble they won't be processed. Serves them bloody right.
- if (feof(d_fp) || ferror(d_fp)) {
- if (i == 0) {
- fprintf(stderr, "[%s] WAV file has corrupted
header or i/o error\n", __FILE__);
- return -1;
- }
- return i;
- }
- }
- return noutput_items;
+ if (chan < n_out_chans) {
+ out[chan][i] = convert_to_float(sample);
+ }
+ }
+
+ d_sample_idx++;
+
+ // OK, EOF is not necessarily an error. But we're not going to
+ // deal with handling corrupt wav files, so if they give us any
+ // trouble they won't be processed. Serves them bloody right.
+ if (feof(d_fp) || ferror(d_fp)) {
+ if (i == 0) {
+ fprintf(stderr, "[%s] WAV file has corrupted header or i/o error\n",
__FILE__);
+ return -1;
+ }
+ return i;
+ }
+ }
+
+ return noutput_items;
}
float
gr_wavfile_source::convert_to_float(short int sample)
{
- float sample_out = (float) sample;
- sample_out /= d_normalize_fac;
- sample_out -= d_normalize_shift;
- return sample_out;
+ float sample_out = (float) sample;
+ sample_out /= d_normalize_fac;
+ sample_out -= d_normalize_shift;
+ return sample_out;
}
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.h
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.h
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.h
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2004,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -42,53 +42,52 @@
class gr_wavfile_source : public gr_sync_block
{
- friend gr_wavfile_source_sptr gr_make_wavfile_source (const char
*filename,
+private:
+ friend gr_wavfile_source_sptr gr_make_wavfile_source (const char *filename,
bool repeat);
- private:
- FILE *d_fp;
- bool d_repeat;
+ gr_wavfile_source(const char *filename, bool repeat);
- unsigned d_sample_rate;
- int d_nchans;
- int d_bytes_per_sample;
- int d_first_sample_pos;
- unsigned d_samples_per_chan;
- unsigned d_sample_idx;
- int d_normalize_shift;
- int d_normalize_fac;
+ FILE *d_fp;
+ bool d_repeat;
+
+ unsigned d_sample_rate;
+ int d_nchans;
+ int d_bytes_per_sample;
+ int d_first_sample_pos;
+ unsigned d_samples_per_chan;
+ unsigned d_sample_idx;
+ int d_normalize_shift;
+ int d_normalize_fac;
+
+ /*!
+ * \brief Convert an integer sample value to a float value within [-1;1]
+ */
+ float convert_to_float(short int sample);
- gr_wavfile_source(const char *filename, bool repeat);
+public:
+ ~gr_wavfile_source ();
+
+ int work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
- /*!
- * \brief Convert an integer sample value to a float value within [-1;1]
- */
- float convert_to_float(short int sample);
+ /*!
+ * \brief Read the sample rate as specified in the wav file header
+ */
+ unsigned int sample_rate() const { return d_sample_rate; };
-
- public:
- ~gr_wavfile_source ();
-
- int work (int noutput_items,
- gr_vector_const_void_star &input_items,
- gr_vector_void_star &output_items);
-
- /*!
- * \brief Read the sample rate as specified in the wav file header
- */
- unsigned int get_sample_rate() { return d_sample_rate; };
-
- /*!
- * \brief Return the number of bits per sample as specified in the wav
- * file header. Only 8 or 16 bit are supported here.
- */
- int get_bits_per_sample() { return d_bytes_per_sample * 8; };
-
- /*!
- * \brief Return the number of channels in the wav file as specified in
- * the wav file header. This is also the max number of outputs you can
- * have.
- */
- int get_n_channels() { return d_nchans; };
+ /*!
+ * \brief Return the number of bits per sample as specified in the wav
+ * file header. Only 8 or 16 bit are supported here.
+ */
+ int bits_per_sample() const { return d_bytes_per_sample * 8; };
+
+ /*!
+ * \brief Return the number of channels in the wav file as specified in
+ * the wav file header. This is also the max number of outputs you can
+ * have.
+ */
+ int channels() const { return d_nchans; };
};
#endif /* INCLUDED_GR_WAVFILE_SOURCE_H */
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.i
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.i
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gr_wavfile_source.i
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -28,15 +28,15 @@
class gr_wavfile_source : public gr_sync_block
{
- private:
- gr_wavfile_source(const char *filename,
- bool repeat) throw (std::runtime_error);
+private:
+ gr_wavfile_source(const char *filename,
+ bool repeat) throw (std::runtime_error);
- public:
- ~gr_wavfile_source();
+public:
+ ~gr_wavfile_source();
- unsigned int get_sample_rate();
- int get_bits_per_sample();
- int get_n_channels();
+ unsigned int sample_rate();
+ int bits_per_sample();
+ int channels();
};
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.cc
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.cc
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.cc
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2004,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -31,6 +31,10 @@
# define VALID_COMPRESSION_TYPE 0x0001
// WAV files are always little-endian, so we need some byte switching macros
+
+// FIXME: These need to be refactored into a separate endianess header file
+// as they duplicate routines defined in usrp/host/lib/legacy/usrp_bytesex.h
+
#ifdef WORDS_BIGENDIAN
#ifdef HAVE_BYTESWAP_H
@@ -47,8 +51,8 @@
static inline unsigned int
bswap_32 (unsigned int x)
{
- return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \
- | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
+ return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \
+ | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
}
#endif // HAVE_BYTESWAP_H
@@ -131,177 +135,178 @@
bool
gri_wavheader_parse(FILE *fp,
- unsigned int &sample_rate_o,
- int &nchans_o,
- int &bytes_per_sample_o,
- int &first_sample_pos_o,
- unsigned int &samples_per_chan_o)
+ unsigned int &sample_rate_o,
+ int &nchans_o,
+ int &bytes_per_sample_o,
+ int &first_sample_pos_o,
+ unsigned int &samples_per_chan_o)
{
- // _o variables take return values
- char str_buf[8] = {0};
+ // _o variables take return values
+ char str_buf[8] = {0};
+
+ uint32_t file_size;
+ uint32_t fmt_hdr_skip;
+ uint16_t compression_type;
+ uint16_t nchans;
+ uint32_t sample_rate;
+ uint32_t avg_bytes_per_sec;
+ uint16_t block_align;
+ uint16_t bits_per_sample;
+ uint32_t chunk_size;
+
+ size_t fresult;
- uint32_t file_size;
- uint32_t fmt_hdr_skip;
- uint16_t compression_type;
- uint16_t nchans;
- uint32_t sample_rate;
- uint32_t avg_bytes_per_sec;
- uint16_t block_align;
- uint16_t bits_per_sample;
- uint32_t chunk_size;
+ fresult = fread(str_buf, 1, 4, fp);
+ if (fresult != 4 || strncmp(str_buf, "RIFF", 4) || feof(fp)) {
+ return false;
+ }
+
+ fread(&file_size, 1, 4, fp);
+
+ fresult = fread(str_buf, 1, 8, fp);
+ if (fresult != 8 || strncmp(str_buf, "WAVEfmt ", 8) || feof(fp)) {
+ return false;
+ }
+
+ fread(&fmt_hdr_skip, 1, 4, fp);
+
+ fread(&compression_type, 1, 2, fp);
+ if (wav_to_host(compression_type) != VALID_COMPRESSION_TYPE) {
+ return false;
+ }
+
+ fread(&nchans, 1, 2, fp);
+ fread(&sample_rate, 1, 4, fp);
+ fread(&avg_bytes_per_sec, 1, 4, fp);
+ fread(&block_align, 1, 2, fp);
+ fread(&bits_per_sample, 1, 2, fp);
+
+ if (ferror(fp)) {
+ return false;
+ }
+
+ fmt_hdr_skip = wav_to_host(fmt_hdr_skip);
+ nchans = wav_to_host(nchans);
+ sample_rate = wav_to_host(sample_rate);
+ bits_per_sample = wav_to_host(bits_per_sample);
+
+ if (bits_per_sample != 8 && bits_per_sample != 16) {
+ return false;
+ }
+
+ fmt_hdr_skip -= 16;
+ if (fmt_hdr_skip) {
+ fseek(fp, fmt_hdr_skip, SEEK_CUR);
+ }
+
+ // data chunk
+ fresult = fread(str_buf, 1, 4, fp);
+ if (strncmp(str_buf, "data", 4)) {
+ return false;
+ }
- size_t fresult;
-
- fresult = fread(str_buf, 1, 4, fp);
- if (fresult != 4 || strncmp(str_buf, "RIFF", 4) || feof(fp)) {
- return false;
- }
-
- fread(&file_size, 1, 4, fp);
-
- fresult = fread(str_buf, 1, 8, fp);
- if (fresult != 8 || strncmp(str_buf, "WAVEfmt ", 8) || feof(fp)) {
- return false;
- }
-
- fread(&fmt_hdr_skip, 1, 4, fp);
-
- fread(&compression_type, 1, 2, fp);
- if (wav_to_host(compression_type) != VALID_COMPRESSION_TYPE) {
- return false;
- }
-
- fread(&nchans, 1, 2, fp);
- fread(&sample_rate, 1, 4, fp);
- fread(&avg_bytes_per_sec, 1, 4, fp);
- fread(&block_align, 1, 2, fp);
- fread(&bits_per_sample, 1, 2, fp);
-
- if (ferror(fp)) {
- return false;
- }
-
- fmt_hdr_skip = wav_to_host(fmt_hdr_skip);
- nchans = wav_to_host(nchans);
- sample_rate = wav_to_host(sample_rate);
- bits_per_sample = wav_to_host(bits_per_sample);
-
- if (bits_per_sample != 8 && bits_per_sample != 16) {
- return false;
- }
-
- fmt_hdr_skip -= 16;
- if (fmt_hdr_skip) {
- fseek(fp, fmt_hdr_skip, SEEK_CUR);
- }
-
- // data chunk
- fresult = fread(str_buf, 1, 4, fp);
- if (strncmp(str_buf, "data", 4)) {
- return false;
- }
- fread(&chunk_size, 1, 4, fp);
- if (ferror(fp)) {
- return false;
- }
-
- // More byte swapping swapping
- chunk_size = wav_to_host(chunk_size);
-
- // Output values
- sample_rate_o = (unsigned) sample_rate;
- nchans_o = (int) nchans;
- bytes_per_sample_o = (int) (bits_per_sample / 8);
- first_sample_pos_o = (int) ftell(fp);
- samples_per_chan_o = (unsigned) (chunk_size / (bytes_per_sample_o *
nchans));
- return true;
+ fread(&chunk_size, 1, 4, fp);
+ if (ferror(fp)) {
+ return false;
+ }
+
+ // More byte swapping
+ chunk_size = wav_to_host(chunk_size);
+
+ // Output values
+ sample_rate_o = (unsigned) sample_rate;
+ nchans_o = (int) nchans;
+ bytes_per_sample_o = (int) (bits_per_sample / 8);
+ first_sample_pos_o = (int) ftell(fp);
+ samples_per_chan_o = (unsigned) (chunk_size / (bytes_per_sample_o * nchans));
+ return true;
}
short int
gri_wav_read_sample(FILE *fp, int bytes_per_sample)
{
- int16_t buf = 0;
- fread(&buf, bytes_per_sample, 1, fp);
-
- return (short) wav_to_host(buf);
+ int16_t buf = 0;
+ fread(&buf, bytes_per_sample, 1, fp);
+
+ return (short) wav_to_host(buf);
}
bool
gri_wavheader_write(FILE *fp,
- unsigned int sample_rate,
- int nchans,
- int bytes_per_sample)
+ unsigned int sample_rate,
+ int nchans,
+ int bytes_per_sample)
{
- const int header_len = 44;
- char wav_hdr[header_len] = "RIFF\0\0\0\0WAVEfmt
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0data\0\0\0";
- uint16_t nchans_f = (uint16_t) nchans;
- uint32_t sample_rate_f = (uint32_t) sample_rate;
- uint16_t block_align = bytes_per_sample * nchans;
- uint32_t avg_bytes = sample_rate * block_align;
- uint16_t bits_per_sample = bytes_per_sample * 8;
-
- nchans_f = host_to_wav(nchans_f);
- sample_rate_f = host_to_wav(sample_rate_f);
- block_align = host_to_wav(block_align);
- avg_bytes = host_to_wav(avg_bytes);
- bits_per_sample = host_to_wav(bits_per_sample);
-
- wav_hdr[16] = 0x10; // no extra bytes
- wav_hdr[20] = 0x01; // no compression
- memcpy((void *) (wav_hdr + 22), (void *) &nchans_f, 2);
- memcpy((void *) (wav_hdr + 24), (void *) &sample_rate_f, 4);
- memcpy((void *) (wav_hdr + 28), (void *) &avg_bytes, 4);
- memcpy((void *) (wav_hdr + 32), (void *) &block_align, 2);
- memcpy((void *) (wav_hdr + 34), (void *) &bits_per_sample, 2);
-
- fwrite(&wav_hdr, 1, header_len, fp);
- if (ferror(fp)) {
- return false;
- }
-
- return true;
+ const int header_len = 44;
+ char wav_hdr[header_len] = "RIFF\0\0\0\0WAVEfmt
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0data\0\0\0";
+ uint16_t nchans_f = (uint16_t) nchans;
+ uint32_t sample_rate_f = (uint32_t) sample_rate;
+ uint16_t block_align = bytes_per_sample * nchans;
+ uint32_t avg_bytes = sample_rate * block_align;
+ uint16_t bits_per_sample = bytes_per_sample * 8;
+
+ nchans_f = host_to_wav(nchans_f);
+ sample_rate_f = host_to_wav(sample_rate_f);
+ block_align = host_to_wav(block_align);
+ avg_bytes = host_to_wav(avg_bytes);
+ bits_per_sample = host_to_wav(bits_per_sample);
+
+ wav_hdr[16] = 0x10; // no extra bytes
+ wav_hdr[20] = 0x01; // no compression
+ memcpy((void *) (wav_hdr + 22), (void *) &nchans_f, 2);
+ memcpy((void *) (wav_hdr + 24), (void *) &sample_rate_f, 4);
+ memcpy((void *) (wav_hdr + 28), (void *) &avg_bytes, 4);
+ memcpy((void *) (wav_hdr + 32), (void *) &block_align, 2);
+ memcpy((void *) (wav_hdr + 34), (void *) &bits_per_sample, 2);
+
+ fwrite(&wav_hdr, 1, header_len, fp);
+ if (ferror(fp)) {
+ return false;
+ }
+
+ return true;
}
void
gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample)
{
- void *data_ptr;
- unsigned char buf_8bit;
- int16_t buf_16bit;
-
- if (bytes_per_sample == 1) {
- buf_8bit = (unsigned char) sample;
- data_ptr = (void *) &buf_8bit;
- } else {
- buf_16bit = host_to_wav((int16_t) sample);
- data_ptr = (void *) &buf_16bit;
- }
-
- fwrite(data_ptr, 1, bytes_per_sample, fp);
+ void *data_ptr;
+ unsigned char buf_8bit;
+ int16_t buf_16bit;
+
+ if (bytes_per_sample == 1) {
+ buf_8bit = (unsigned char) sample;
+ data_ptr = (void *) &buf_8bit;
+ } else {
+ buf_16bit = host_to_wav((int16_t) sample);
+ data_ptr = (void *) &buf_16bit;
+ }
+
+ fwrite(data_ptr, 1, bytes_per_sample, fp);
}
bool
gri_wavheader_complete(FILE *fp, unsigned int byte_count)
{
- uint32_t chunk_size = (uint32_t) byte_count;
- chunk_size = host_to_wav(chunk_size);
-
- fseek(fp, 40, SEEK_SET);
- fwrite(&chunk_size, 1, 4, fp);
-
- chunk_size = (uint32_t) byte_count + 36; // fmt chunk and data header
- chunk_size = host_to_wav(chunk_size);
- fseek(fp, 4, SEEK_SET);
-
- fwrite(&chunk_size, 1, 4, fp);
-
- if (ferror(fp)) {
- return false;
- }
-
- return true;
+ uint32_t chunk_size = (uint32_t) byte_count;
+ chunk_size = host_to_wav(chunk_size);
+
+ fseek(fp, 40, SEEK_SET);
+ fwrite(&chunk_size, 1, 4, fp);
+
+ chunk_size = (uint32_t) byte_count + 36; // fmt chunk and data header
+ chunk_size = host_to_wav(chunk_size);
+ fseek(fp, 4, SEEK_SET);
+
+ fwrite(&chunk_size, 1, 4, fp);
+
+ if (ferror(fp)) {
+ return false;
+ }
+
+ return true;
}
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.h
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.h
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/lib/io/gri_wavfile.h
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2004 Free Software Foundation, Inc.
+ * Copyright 2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -79,7 +79,7 @@
/*!
* \brief Write one sample to an open WAV file at the current position.
*
- * Takes care of endiannes.
+ * Takes care of endianness.
*/
void
gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample);
Modified:
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/python/gnuradio/gr/qa_wavefile.py
===================================================================
---
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/python/gnuradio/gr/qa_wavefile.py
2008-02-18 02:26:03 UTC (rev 7726)
+++
gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src/python/gnuradio/gr/qa_wavefile.py
2008-02-18 03:09:07 UTC (rev 7727)
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# Copyright 2004,2007 Free Software Foundation, Inc.
+# Copyright 2008 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
@@ -34,7 +34,7 @@
def test_001_checkwavread (self):
wf = gr.wavfile_source("./test_16bit_1chunk.wav")
- self.assertEqual(wf.get_sample_rate(), 8000)
+ self.assertEqual(wf.sample_rate(), 8000)
def test_002_checkwavcopy (self):
infile = "test_16bit_1chunk.wav"
@@ -42,9 +42,9 @@
wf_in = gr.wavfile_source(infile)
wf_out = gr.wavfile_sink(outfile,
- wf_in.get_n_channels(),
- wf_in.get_sample_rate(),
- wf_in.get_bits_per_sample())
+ wf_in.channels(),
+ wf_in.sample_rate(),
+ wf_in.bits_per_sample())
self.tb.connect(wf_in, wf_out)
self.tb.run()
wf_out.close()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r7727 - in gnuradio/branches/developers/jcorgan/wav/gnuradio-core/src: lib/io python/gnuradio/gr,
jcorgan <=