diff options
author | japm48 <japm48@users.noreply.github.com> | 2020-04-01 03:26:52 +0200 |
---|---|---|
committer | Michael Dickens <michael.dickens@ettus.com> | 2020-04-14 15:47:29 -0400 |
commit | d0a351a6fc2f018f36f6e89395f346707a059099 (patch) | |
tree | b5a2389eeee92a5441a31597ecd6b45fcee9baff | |
parent | c1a91d140919518de18eda1fd5b598b2c11ef0cd (diff) |
blocks: fix wavfile test
The example test_16bit_1chunk.wav was invalid even if the tests passed.
This file caused errors in ffprobe (from ffmpeg) and wave (from python
standard lib).
This is what changed:
- LIST chunk corrected, according to spec. Previously `ffprobe` complained:
[wav @ 0x5653f92d6dc0] too short LIST tag
- File size corrected. Previously the test assumed that the resulting
headers to be identical, this is now taken into account.
Because of this, python's `wave` did not work. This is added to the test.
Parser is updated to check file size.
- Tests that did not close opened files were corrected.
References:
[1] https://sites.google.com/site/musicgapi/technical-documents/wav-file-format
[2] http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf
-rw-r--r-- | gr-blocks/lib/wavfile.cc | 18 | ||||
-rw-r--r-- | gr-blocks/lib/wavfile_source_impl.cc | 2 | ||||
-rw-r--r-- | gr-blocks/python/blocks/qa_wavfile.py | 32 | ||||
-rw-r--r-- | gr-blocks/python/blocks/test_16bit_1chunk.wav | bin | 70 -> 74 bytes |
4 files changed, 41 insertions, 11 deletions
diff --git a/gr-blocks/lib/wavfile.cc b/gr-blocks/lib/wavfile.cc index d3015f75b8..846de48177 100644 --- a/gr-blocks/lib/wavfile.cc +++ b/gr-blocks/lib/wavfile.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2004,2008,2012-2013 Free Software Foundation, Inc. + * Copyright 2004,2008,2012-2013,2020 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -13,6 +13,7 @@ #endif #include <gnuradio/blocks/wavfile.h> +#include <gnuradio/logger.h> #include <stdint.h> #include <cstring> @@ -73,15 +74,30 @@ bool wavheader_parse(FILE* fp, uint16_t block_align; uint16_t bits_per_sample; uint32_t chunk_size; + long real_file_size; size_t fresult; + fseek(fp, 0L, SEEK_END); + real_file_size = ftell(fp); + rewind(fp); + 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); + file_size = wav_to_host(file_size); + if (fresult != 4 || file_size != real_file_size - 8L) { + // FIXME use predefined loggers + gr::logger_ptr logger, debug_logger; + gr::configure_default_loggers(logger, debug_logger, "wavfile"); + GR_LOG_ERROR(logger, + boost::format("invalid file size (expected: %d; actual: %d)") % + (file_size + 8L) % real_file_size); + return false; + } fresult = fread(str_buf, 1, 8, fp); if (fresult != 8 || strncmp(str_buf, "WAVEfmt ", 8) || feof(fp)) { diff --git a/gr-blocks/lib/wavfile_source_impl.cc b/gr-blocks/lib/wavfile_source_impl.cc index 0bd1209757..8c17724393 100644 --- a/gr-blocks/lib/wavfile_source_impl.cc +++ b/gr-blocks/lib/wavfile_source_impl.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2004,2008,2010,2013 Free Software Foundation, Inc. + * Copyright 2004,2008,2010,2013,2020 Free Software Foundation, Inc. * * This file is part of GNU Radio * diff --git a/gr-blocks/python/blocks/qa_wavfile.py b/gr-blocks/python/blocks/qa_wavfile.py index ecfa3a4c27..ba2d80dc66 100644 --- a/gr-blocks/python/blocks/qa_wavfile.py +++ b/gr-blocks/python/blocks/qa_wavfile.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright 2008,2010,2013 Free Software Foundation, Inc. +# Copyright 2008,2010,2013,2020 Free Software Foundation, Inc. # # This file is part of GNU Radio # @@ -16,7 +16,7 @@ from os.path import getsize g_in_file = os.path.join(os.getenv("srcdir"), "test_16bit_1chunk.wav") g_extra_header_offset = 36 -g_extra_header_len = 18 +g_extra_header_len = 22 class test_wavefile(gr_unittest.TestCase): @@ -43,19 +43,33 @@ class test_wavefile(gr_unittest.TestCase): self.tb.run() wf_out.close() + # Test file validity. + import wave + try: + with wave.open(infile, 'rb') as f: + pass + with wave.open(outfile, 'rb') as f: + pass + except: + raise AssertionError('Invalid WAV file') + # we're losing all extra header chunks self.assertEqual(getsize(infile) - g_extra_header_len, getsize(outfile)) - in_f = open(infile, 'rb') - out_f = open(outfile, 'rb') + with open(infile, 'rb') as f: + in_data = bytearray(f.read()) + with open(outfile, 'rb') as f: + out_data = bytearray(f.read()) - in_data = in_f.read() - out_data = out_f.read() - out_f.close() os.remove(outfile) - # cut extra header chunks input file + + # Ignore size field: + in_data[4:8] = b'\x00\x00\x00\x00' + out_data[4:8] = b'\x00\x00\x00\x00' + + # cut extra header chunks from input file self.assertEqual(in_data[:g_extra_header_offset] + \ in_data[g_extra_header_offset + g_extra_header_len:], out_data) if __name__ == '__main__': - gr_unittest.run(test_wavefile, "test_wavefile.xml") + gr_unittest.run(test_wavefile) diff --git a/gr-blocks/python/blocks/test_16bit_1chunk.wav b/gr-blocks/python/blocks/test_16bit_1chunk.wav Binary files differindex d1b5ba91bd..3d028a9582 100644 --- a/gr-blocks/python/blocks/test_16bit_1chunk.wav +++ b/gr-blocks/python/blocks/test_16bit_1chunk.wav |