From abee116aa267d039d82c2f69a530ea03c2bb4581 Mon Sep 17 00:00:00 2001 From: bootrino Date: Mon, 13 May 2019 01:32:40 +1000 Subject: [PATCH] Added failure messages to ReadWAVInfo, also handled JUNK in WAV (#171) * Modified ReadWAVInfo method to give meaingful messages to Serial when it cannot read a WAV file, and also added code to handle WAV files that contain JUNK data, which is a valid part of the WAV specification. * Added error messages to ReadWAVInfo function using Serial.printf_P(PSTR( Added error messages to begin function using Serial.printf_P(PSTR( Implemented skipping through JUNK (as defined in the WAV specification), if present in the WAV file. * Added new lines to error messages in the "begin" function --- src/AudioGeneratorWAV.cpp | 184 ++++++++++++++++++++++++++++++-------- 1 file changed, 146 insertions(+), 38 deletions(-) diff --git a/src/AudioGeneratorWAV.cpp b/src/AudioGeneratorWAV.cpp index cdd5229..0b82354 100644 --- a/src/AudioGeneratorWAV.cpp +++ b/src/AudioGeneratorWAV.cpp @@ -118,65 +118,150 @@ bool AudioGeneratorWAV::ReadWAVInfo() uint16_t u16; int toSkip; + // WAV specification document: + // https://www.aelius.com/njh/wavemetatools/doc/riffmci.pdf + // Header == "RIFF" - if (!ReadU32(&u32)) return false; - if (u32 != 0x46464952) return false; // "RIFF" + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if (u32 != 0x46464952) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, invalid RIFF header, got: %08X \n"), (uint32_t) u32); + return false; + } + // Skip ChunkSize - if (!ReadU32(&u32)) return false; + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + // Format == "WAVE" - if (!ReadU32(&u32)) return false; - if (u32 != 0x45564157) return false; // "WAVE" + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if (u32 != 0x45564157) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, invalid WAVE header, got: %08X \n"), (uint32_t) u32); + return false; + } + + // there might be JUNK or PAD - ignore it by continuing reading until we get to "fmt " + while (1) { + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if (u32 == 0x20746d66) break; // 'fmt ' + }; - // fmt subchunk has the info on the data format - // id == "fmt " - if (!ReadU32(&u32)) return false; - if (u32 != 0x20746d66) return false; // "fmt " // subchunk size - if (!ReadU32(&u32)) return false; + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; 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 + else { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, appears not to be standard PCM \n")); + return false; + } // we only do standard PCM + // AudioFormat - if (!ReadU16(&u16)) return false; - if (u16 != 1) return false; // we only do standard PCM + if (!ReadU16(&u16)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if (u16 != 1) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, AudioFormat appears not to be standard PCM \n")); + return false; + } // we only do standard PCM + // NumChannels - if (!ReadU16(&channels)) return false; - if ((channels<1) || (channels>2)) return false; // Mono or stereo support only + if (!ReadU16(&channels)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if ((channels<1) || (channels>2)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, only mono and stereo are supported \n")); + return false; + } // Mono or stereo support only + // SampleRate - if (!ReadU32(&sampleRate)) return false; - if (sampleRate < 1) return false; // Weird rate, punt. Will need to check w/DAC to see if supported + if (!ReadU32(&sampleRate)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if (sampleRate < 1) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, unknown sample rate \n")); + return false; + } // Weird rate, punt. Will need to check w/DAC to see if supported + // Ignore byterate and blockalign - if (!ReadU32(&u32)) return false; - if (!ReadU16(&u16)) return false; + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if (!ReadU16(&u16)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + // Bits per sample - if (!ReadU16(&bitsPerSample)) return false; - if ((bitsPerSample!=8) && (bitsPerSample != 16)) return false; // Only 8 or 16 bits + if (!ReadU16(&bitsPerSample)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; + if ((bitsPerSample!=8) && (bitsPerSample != 16)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, only 8 or 16 bits is supported \n")); + return false; + } // Only 8 or 16 bits + // Skip any extra header while (toSkip) { uint8_t ign; - if (!ReadU8(&ign)) return false; + if (!ReadU8(&ign)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; toSkip--; } // look for data subchunk do { // id == "data" - if (!ReadU32(&u32)) return false; + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; if (u32 == 0x61746164) break; // "data" // Skip size, read until end of chunk - if (!ReadU32(&u32)) return false; + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; file->seek(u32, SEEK_CUR); } while (1); - if (!file->isOpen()) return false; + if (!file->isOpen()) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, file is not open \n")); + return false; + }; // Skip size, read until end of file... - if (!ReadU32(&u32)) return false; + if (!ReadU32(&u32)) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: failed to read WAV data\n")); + return false; + }; availBytes = u32; // Now set up the buffer or fail buff = reinterpret_cast(malloc(buffSize)); - if (!buff) return false; + if (!buff) { + Serial.printf_P(PSTR("AudioGeneratorWAV::ReadWAVInfo: cannot read WAV, failed to set up buffer \n")); + return false; + }; buffPtr = 0; buffLen = 0; @@ -185,21 +270,44 @@ bool AudioGeneratorWAV::ReadWAVInfo() bool AudioGeneratorWAV::begin(AudioFileSource *source, AudioOutput *output) { - if (!source) return false; + if (!source) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed: invalid source\n")); + return false; + } file = source; - if (!output) return false; + if (!output) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: invalid output\n")); + return false; + } this->output = output; - if (!file->isOpen()) return false; // Error - - if (!ReadWAVInfo()) return false; + if (!file->isOpen()) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: file not open\n")); + return false; + } // Error - if (!output->SetRate( sampleRate )) return false; - if (!output->SetBitsPerSample( bitsPerSample )) return false; - if (!output->SetChannels( channels )) return false; - if (!output->begin()) return false; + if (!ReadWAVInfo()) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed during ReadWAVInfo\n")); + return false; + } + + if (!output->SetRate( sampleRate )) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed to SetRate in output\n")); + return false; + } + if (!output->SetBitsPerSample( bitsPerSample )) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed to SetBitsPerSample in output\n")); + return false; + } + if (!output->SetChannels( channels )) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: failed to SetChannels in output\n")); + return false; + } + if (!output->begin()) { + Serial.printf_P(PSTR("AudioGeneratorWAV::begin: output's begin did not return true\n")); + return false; + } running = true; - + return true; } -