From e7fda5b546fb804a735b7880530b76018519faa6 Mon Sep 17 00:00:00 2001 From: jcorgan Date: Mon, 18 Feb 2008 06:27:50 +0000 Subject: Merged -r7723:7729 from jcorgan/wav into trunk, with added example program. Adds Martin Braun's gr.wavfile_source and gr.wavfile_sink blocks. git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@7730 221aa14e-8319-0410-a670-987f0aec2ac5 --- gnuradio-core/src/lib/io/gri_wavfile.cc | 312 ++++++++++++++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100644 gnuradio-core/src/lib/io/gri_wavfile.cc (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc new file mode 100644 index 000000000..4a9db61d8 --- /dev/null +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -0,0 +1,312 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2008 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +# 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 +#include +#else +#warning Using non-portable code (likely wrong other than ILP32). + +static inline short int +bswap_16 (unsigned short int x) +{ + return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)); +} + +static inline unsigned int +bswap_32 (unsigned int x) +{ + return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \ + | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)); +} +#endif // HAVE_BYTESWAP_H + +static inline uint32_t +host_to_wav(uint32_t x) +{ + return bswap_32(x); +} + +static inline uint16_t +host_to_wav(uint16_t x) +{ + return bswap_16(x); +} + +static inline int16_t +host_to_wav(int16_t x) +{ + return bswap_32(x); +} + +static inline uint32_t +wav_to_host(uint32_t x) +{ + return bswap_32(x); +} + +static inline uint16_t +wav_to_host(uint16_t x) +{ + return bswap_16(x); +} + +static inline int16_t +wav_to_host(int16_t x) +{ + return bswap_32(x); +} + +#else + +static inline uint32_t +host_to_wav(uint32_t x) +{ + return x; +} + +static inline uint16_t +host_to_wav(uint16_t x) +{ + return x; +} + +static inline int16_t +host_to_wav(int16_t x) +{ + return x; +} + +static inline uint32_t +wav_to_host(uint32_t x) +{ + return x; +} + +static inline uint16_t +wav_to_host(uint16_t x) +{ + return x; +} + +static inline int16_t +wav_to_host(int16_t x) +{ + return x; +} + +#endif // WORDS_BIGENDIAN + + +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) +{ + // _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; + + 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 + 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); +} + + +bool +gri_wavheader_write(FILE *fp, + 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; +} + + +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); +} + + +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; +} -- cgit From 873a61258e6e8456b5d48a264e9a28d1246bf149 Mon Sep 17 00:00:00 2001 From: jcorgan Date: Wed, 9 Apr 2008 15:26:29 +0000 Subject: Fixes ticket:238. (Tim Meehan) git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@8163 221aa14e-8319-0410-a670-987f0aec2ac5 --- gnuradio-core/src/lib/io/gri_wavfile.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index 4a9db61d8..c1a2b7c73 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -71,7 +71,7 @@ host_to_wav(uint16_t x) static inline int16_t host_to_wav(int16_t x) { - return bswap_32(x); + return bswap_16(x); } static inline uint32_t @@ -89,7 +89,7 @@ wav_to_host(uint16_t x) static inline int16_t wav_to_host(int16_t x) { - return bswap_32(x); + return bswap_16(x); } #else -- cgit From c276a4ffee9314d2528166547abfd2c09d29713f Mon Sep 17 00:00:00 2001 From: jcorgan Date: Thu, 9 Jul 2009 02:55:51 +0000 Subject: Merged r11377:11390 from jcorgan/usrp-headers in to trunk. * Public USRP(1) header files are now in their own source directory and install into $(includedir)/usrp. This was done to avoid name clashes in the top-level include directory. Only users who are developing directly to libusrp in C++ are affected; the GNU Radio C++ and Python APIs are unchanged. The simple change required by this update is to change: #include to #include ...in your source code. * Removed usrp-inband code from tree (put into limbo directory.) This code has become unmaintained and has started to suffer from bitrot. A checkpoint tag has been made for anyone still needing to use it: http://gnuradio.org/svn/gnuradio/tags/checkpoints/trunk-20090708-pre-usrp-reorg The plan during the 3.2->3.3 development cycle is to replace the functions done by the in-band code with extensions to the existing gr-usrp blocks using the new message passing architecture. The USRP hardware FPGA code that provided the inband interface has not been removed; however, it too has become unmaintained and will likely be rewritten/replaced during the 3.3 timeframe. The trunk passes distcheck. git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@11394 221aa14e-8319-0410-a670-987f0aec2ac5 --- gnuradio-core/src/lib/io/gri_wavfile.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index c1a2b7c73..b8375edc2 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -32,8 +32,7 @@ // 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 +// FIXME: Use libgruel versions #ifdef WORDS_BIGENDIAN -- cgit From b623dff43ad12b7539184950074ba5b0ecbf34fb Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Tue, 26 Jul 2011 21:59:27 -0400 Subject: Fixed return warnings. --- gnuradio-core/src/lib/io/gri_wavfile.cc | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index b8375edc2..8f1c6a2bb 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -160,25 +160,25 @@ gri_wavheader_parse(FILE *fp, return false; } - fread(&file_size, 1, 4, fp); + fresult = 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); + fresult = fread(&fmt_hdr_skip, 1, 4, fp); - fread(&compression_type, 1, 2, fp); + fresult = 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); + fresult = fread(&nchans, 1, 2, fp); + fresult = fread(&sample_rate, 1, 4, fp); + fresult = fread(&avg_bytes_per_sec, 1, 4, fp); + fresult = fread(&block_align, 1, 2, fp); + fresult = fread(&bits_per_sample, 1, 2, fp); if (ferror(fp)) { return false; @@ -204,7 +204,7 @@ gri_wavheader_parse(FILE *fp, return false; } - fread(&chunk_size, 1, 4, fp); + fresult = fread(&chunk_size, 1, 4, fp); if (ferror(fp)) { return false; } @@ -226,7 +226,9 @@ short int gri_wav_read_sample(FILE *fp, int bytes_per_sample) { int16_t buf = 0; - fread(&buf, bytes_per_sample, 1, fp); + size_t fresult; + + fresult = fread(&buf, bytes_per_sample, 1, fp); return (short) wav_to_host(buf); } -- cgit From f919f9dcbb54a08e6e26d6c229ce92fb784fa1b2 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Fri, 13 Apr 2012 18:36:53 -0400 Subject: Removed whitespace and added dtools/bin/remove-whitespace as a tool to do this in the future. The sed script was provided by Moritz Fischer. --- gnuradio-core/src/lib/io/gri_wavfile.cc | 52 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index 8f1c6a2bb..e316a0825 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -142,7 +142,7 @@ gri_wavheader_parse(FILE *fp, { // _o variables take return values char str_buf[8] = {0}; - + uint32_t file_size; uint32_t fmt_hdr_skip; uint16_t compression_type; @@ -152,52 +152,52 @@ gri_wavheader_parse(FILE *fp, uint16_t block_align; uint16_t bits_per_sample; uint32_t chunk_size; - + size_t fresult; fresult = fread(str_buf, 1, 4, fp); if (fresult != 4 || strncmp(str_buf, "RIFF", 4) || feof(fp)) { return false; } - + fresult = 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; } - + fresult = fread(&fmt_hdr_skip, 1, 4, fp); - + fresult = fread(&compression_type, 1, 2, fp); if (wav_to_host(compression_type) != VALID_COMPRESSION_TYPE) { return false; } - + fresult = fread(&nchans, 1, 2, fp); fresult = fread(&sample_rate, 1, 4, fp); fresult = fread(&avg_bytes_per_sec, 1, 4, fp); fresult = fread(&block_align, 1, 2, fp); fresult = 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)) { @@ -208,10 +208,10 @@ gri_wavheader_parse(FILE *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; @@ -229,7 +229,7 @@ gri_wav_read_sample(FILE *fp, int bytes_per_sample) size_t fresult; fresult = fread(&buf, bytes_per_sample, 1, fp); - + return (short) wav_to_host(buf); } @@ -247,13 +247,13 @@ gri_wavheader_write(FILE *fp, 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); @@ -261,12 +261,12 @@ gri_wavheader_write(FILE *fp, 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; } @@ -277,7 +277,7 @@ 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; @@ -285,7 +285,7 @@ gri_wav_write_sample(FILE *fp, short int sample, int bytes_per_sample) buf_16bit = host_to_wav((int16_t) sample); data_ptr = (void *) &buf_16bit; } - + fwrite(data_ptr, 1, bytes_per_sample, fp); } @@ -295,19 +295,19 @@ 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; } -- cgit From db360e476e62d2c412d1654065d18047a25490b8 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 21 Jun 2012 22:44:16 -0400 Subject: core: fixes a very minor bug in gri_wavfile.cc and also makes a FIXME obsolete --- gnuradio-core/src/lib/io/gri_wavfile.cc | 118 ++++++-------------------------- 1 file changed, 22 insertions(+), 96 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index e316a0825..3bc9d8fad 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -27,107 +27,30 @@ #include #include #include +#include # define VALID_COMPRESSION_TYPE 0x0001 // WAV files are always little-endian, so we need some byte switching macros -// FIXME: Use libgruel versions - +// Basically, this is the opposite of htonx() and ntohx() #ifdef WORDS_BIGENDIAN -#ifdef HAVE_BYTESWAP_H -#include -#else -#warning Using non-portable code (likely wrong other than ILP32). - -static inline short int -bswap_16 (unsigned short int x) -{ - return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)); -} - -static inline unsigned int -bswap_32 (unsigned int x) -{ - return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \ - | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)); -} -#endif // HAVE_BYTESWAP_H - -static inline uint32_t -host_to_wav(uint32_t x) -{ - return bswap_32(x); -} - -static inline uint16_t -host_to_wav(uint16_t x) -{ - return bswap_16(x); -} - -static inline int16_t -host_to_wav(int16_t x) -{ - return bswap_16(x); -} - -static inline uint32_t -wav_to_host(uint32_t x) -{ - return bswap_32(x); -} - -static inline uint16_t -wav_to_host(uint16_t x) -{ - return bswap_16(x); -} - -static inline int16_t -wav_to_host(int16_t x) -{ - return bswap_16(x); -} +static inline uint32_t host_to_wav(uint32_t x) { return bswap_32(x); } +static inline uint16_t host_to_wav(uint16_t x) { return bswap_16(x); } +static inline int16_t host_to_wav(int16_t x) { return bswap_16(x); } +static inline uint32_t wav_to_host(uint32_t x) { return bswap_32(x); } +static inline uint16_t wav_to_host(uint16_t x) { return bswap_16(x); } +static inline int16_t wav_to_host(int16_t x) { return bswap_16(x); } #else -static inline uint32_t -host_to_wav(uint32_t x) -{ - return x; -} - -static inline uint16_t -host_to_wav(uint16_t x) -{ - return x; -} - -static inline int16_t -host_to_wav(int16_t x) -{ - return x; -} - -static inline uint32_t -wav_to_host(uint32_t x) -{ - return x; -} - -static inline uint16_t -wav_to_host(uint16_t x) -{ - return x; -} - -static inline int16_t -wav_to_host(int16_t x) -{ - return x; -} +static inline uint32_t host_to_wav(uint32_t x) { return x; } +static inline uint16_t host_to_wav(uint16_t x) { return x; } +static inline int16_t host_to_wav(int16_t x) { return x; } +static inline uint32_t wav_to_host(uint32_t x) { return x; } +static inline uint16_t wav_to_host(uint16_t x) { return x; } +static inline int16_t wav_to_host(int16_t x) { return x; } #endif // WORDS_BIGENDIAN @@ -225,12 +148,15 @@ gri_wavheader_parse(FILE *fp, short int gri_wav_read_sample(FILE *fp, int bytes_per_sample) { - int16_t buf = 0; - size_t fresult; - - fresult = fread(&buf, bytes_per_sample, 1, fp); + int16_t buf_16bit; - return (short) wav_to_host(buf); + if(!fread(&buf_16bit, bytes_per_sample, 1, fp)) { + return 0; + } + if(bytes_per_sample == 1) { + return (short) buf_16bit; + } + return (short)wav_to_host(buf_16bit); } -- cgit From 056d3006d07c11c4bfba9ac5c2f82111be55dd35 Mon Sep 17 00:00:00 2001 From: Nicholas Corgan Date: Fri, 22 Jun 2012 11:19:58 -0700 Subject: gruel: got rid of inet.h and adjusted only file that depended on it --- gnuradio-core/src/lib/io/gri_wavfile.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index 3bc9d8fad..780bd1b98 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2004,2008 Free Software Foundation, Inc. + * Copyright 2004,2008,2012 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -27,7 +27,7 @@ #include #include #include -#include +#include # define VALID_COMPRESSION_TYPE 0x0001 @@ -36,12 +36,12 @@ // Basically, this is the opposite of htonx() and ntohx() #ifdef WORDS_BIGENDIAN -static inline uint32_t host_to_wav(uint32_t x) { return bswap_32(x); } -static inline uint16_t host_to_wav(uint16_t x) { return bswap_16(x); } -static inline int16_t host_to_wav(int16_t x) { return bswap_16(x); } -static inline uint32_t wav_to_host(uint32_t x) { return bswap_32(x); } -static inline uint16_t wav_to_host(uint16_t x) { return bswap_16(x); } -static inline int16_t wav_to_host(int16_t x) { return bswap_16(x); } +static inline uint32_t host_to_wav(uint32_t x) { return htonl(x); } +static inline uint16_t host_to_wav(uint16_t x) { return htons(x); } +static inline int16_t host_to_wav(int16_t x) { return htons(x); } +static inline uint32_t wav_to_host(uint32_t x) { return ntohl(x); } +static inline uint16_t wav_to_host(uint16_t x) { return ntohs(x); } +static inline int16_t wav_to_host(int16_t x) { return ntohs(x); } #else -- cgit From 75fac1e1476578cd354a2cc586af23da88ce4b12 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 28 Jun 2012 17:55:34 -0700 Subject: core: fix for gri wave byteswapping Sorry guys, I misunderstood the comments. Wave data is specifically in little endian format. Therefore we should only be swapping on big endian machines. Since systems only provide network endian macros: The following commit provides a cross platform byteswap and macros for host to/from worknet short and long. --- gnuradio-core/src/lib/io/gri_wavfile.cc | 46 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'gnuradio-core/src/lib/io/gri_wavfile.cc') diff --git a/gnuradio-core/src/lib/io/gri_wavfile.cc b/gnuradio-core/src/lib/io/gri_wavfile.cc index 780bd1b98..277e6b7b0 100644 --- a/gnuradio-core/src/lib/io/gri_wavfile.cc +++ b/gnuradio-core/src/lib/io/gri_wavfile.cc @@ -27,33 +27,45 @@ #include #include #include -#include +#include //BOOST_BIG_ENDIAN # define VALID_COMPRESSION_TYPE 0x0001 -// WAV files are always little-endian, so we need some byte switching macros - // Basically, this is the opposite of htonx() and ntohx() -#ifdef WORDS_BIGENDIAN +// Define host to/from worknet (little endian) short and long +#ifdef BOOST_BIG_ENDIAN + + static inline uint16_t __gri_wav_bs16(uint16_t x) + { + return (x>>8) | (x<<8); + } -static inline uint32_t host_to_wav(uint32_t x) { return htonl(x); } -static inline uint16_t host_to_wav(uint16_t x) { return htons(x); } -static inline int16_t host_to_wav(int16_t x) { return htons(x); } -static inline uint32_t wav_to_host(uint32_t x) { return ntohl(x); } -static inline uint16_t wav_to_host(uint16_t x) { return ntohs(x); } -static inline int16_t wav_to_host(int16_t x) { return ntohs(x); } + static inline uint32_t __gri_wav_bs32(uint32_t x) + { + return (uint32_t(__gri_wav_bs16(uint16_t(x&0xfffful)))<<16) | (__gri_wav_bs16(uint16_t(x>>16))); + } + + #define htowl(x) __gri_wav_bs32(x) + #define wtohl(x) __gri_wav_bs32(x) + #define htows(x) __gri_wav_bs16(x) + #define wtohs(x) __gri_wav_bs16(x) #else -static inline uint32_t host_to_wav(uint32_t x) { return x; } -static inline uint16_t host_to_wav(uint16_t x) { return x; } -static inline int16_t host_to_wav(int16_t x) { return x; } -static inline uint32_t wav_to_host(uint32_t x) { return x; } -static inline uint16_t wav_to_host(uint16_t x) { return x; } -static inline int16_t wav_to_host(int16_t x) { return x; } + #define htowl(x) uint32_t(x) + #define wtohl(x) uint32_t(x) + #define htows(x) uint16_t(x) + #define wtohs(x) uint16_t(x) -#endif // WORDS_BIGENDIAN +#endif // BOOST_BIG_ENDIAN +// WAV files are always little-endian, so we need some byte switching macros +static inline uint32_t host_to_wav(uint32_t x) { return htowl(x); } +static inline uint16_t host_to_wav(uint16_t x) { return htows(x); } +static inline int16_t host_to_wav(int16_t x) { return htows(x); } +static inline uint32_t wav_to_host(uint32_t x) { return wtohl(x); } +static inline uint16_t wav_to_host(uint16_t x) { return wtohs(x); } +static inline int16_t wav_to_host(int16_t x) { return wtohs(x); } bool gri_wavheader_parse(FILE *fp, -- cgit