From 955b1d29b2c8d98528b83954ed28fd3a0ed8d734 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 3 Aug 2018 12:02:52 -0700 Subject: [PATCH] Fix WAV header decode, add WAV host test Fixes #114 Adobe Audition WAV files have a slightly different but legal header format. Enable that format (only for PCM, not a/ulaw or anything else) to be decoedd properly. Add in a WAV object test that can run on hosts and valgrind. --- src/AudioGeneratorWAV.cpp | 12 +++++++++++- tests/host/Makefile | 10 ++++++++-- tests/host/wav.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/host/wav.cpp diff --git a/src/AudioGeneratorWAV.cpp b/src/AudioGeneratorWAV.cpp index aff91da..b32240a 100644 --- a/src/AudioGeneratorWAV.cpp +++ b/src/AudioGeneratorWAV.cpp @@ -115,6 +115,7 @@ bool AudioGeneratorWAV::ReadWAVInfo() { uint32_t u32; uint16_t u16; + int toSkip; // Header == "RIFF" if (!ReadU32(&u32)) return false; @@ -131,7 +132,10 @@ bool AudioGeneratorWAV::ReadWAVInfo() if (u32 != 0x20746d66) return false; // "fmt " // subchunk size if (!ReadU32(&u32)) return false; - if (u32 != 16) return false; // we only do standard PCM + if (u32 == 16) { toSkip = 0; } + else if (u32 == 18) { toSkip = 18 - 16; } + else if (u32 == 40) { toSkip = 40 - 16; } + else { return false; } // we only do standard PCM // AudioFormat if (!ReadU16(&u16)) return false; if (u16 != 1) return false; // we only do standard PCM @@ -147,6 +151,12 @@ bool AudioGeneratorWAV::ReadWAVInfo() // Bits per sample if (!ReadU16(&bitsPerSample)) return false; if ((bitsPerSample!=8) && (bitsPerSample != 16)) return false; // Only 8 or 16 bits + // Skip any extra header + while (toSkip) { + uint8_t ign; + if (!ReadU8(&ign)) return false; + toSkip--; + } // look for data subchunk do { diff --git a/tests/host/Makefile b/tests/host/Makefile index 79cef61..81fa74c 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -1,4 +1,4 @@ -all: mp3 aac +all: mp3 aac wav libmad=../../src/libmad/decoder.c ../../src/libmad/frame.c ../../src/libmad/bit.c ../../src/libmad/stream.c ../../src/libmad/fixed.c \ ../../src/libmad/timer.c ../../src/libmad/layer3.c ../../src/libmad/synth.c ../../src/libmad/huffman.c ../../src/libmad/version.c @@ -45,7 +45,13 @@ aac: FORCE rm -f *.o echo valgrind --leak-check=full --track-origins=yes -v --error-limit=no --show-leak-kinds=all ./aac +wav: FORCE + rm -f *.o + g++ $(CPPOPTS) -o wav wav.cpp Serial.cpp ../../src/AudioFileSourceSTDIO.cpp ../../src/AudioOutputSTDIO.cpp ../../src/AudioGeneratorWAV.cpp -I ../../src/ -I. + rm -f *.o + echo valgrind --leak-check=full --track-origins=yes -v --error-limit=no --show-leak-kinds=all ./wav + clean: - rm -f mp3 aac *.o + rm -f mp3 aac wav *.o FORCE: diff --git a/tests/host/wav.cpp b/tests/host/wav.cpp new file mode 100644 index 0000000..f428fd2 --- /dev/null +++ b/tests/host/wav.cpp @@ -0,0 +1,24 @@ +#include +#include "AudioFileSourceSTDIO.h" +#include "AudioOutputSTDIO.h" +#include "AudioGeneratorWAV.h" + + +int main(int argc, char **argv) +{ + (void) argc; + (void) argv; + AudioFileSourceSTDIO *in = new AudioFileSourceSTDIO("test_8u_16.wav"); + AudioOutputSTDIO *out = new AudioOutputSTDIO(); + out->SetFilename("pcm.wav"); + AudioGeneratorWAV *wav = new AudioGeneratorWAV(); + + wav->begin(in, out); + while (wav->loop()) { /*noop*/ } + wav->stop(); + out->stop(); + + delete wav; + delete out; + delete in; +}