From 070811a60f9c9f72e27bbb38dae03f122799abac Mon Sep 17 00:00:00 2001 From: Steven Atkinson Date: Sat, 25 Mar 2023 18:53:37 -0700 Subject: [PATCH] * Ignore bext chunk * formatting script Squashed commit of the following: commit 1c8e40a7a7d137718abff4dc292b9b0b8f2a3134 Author: Steven Atkinson Date: Sat Mar 25 18:52:42 2023 -0700 Format commit 2cf8cdffca3382c9a19e69e1a6af27f5ed1c5508 Author: Steven Atkinson Date: Sat Mar 25 18:52:14 2023 -0700 format.bash commit 198abd6a08683720829e53c8769e2d8e97bff5ad Author: Steven Atkinson Date: Sat Mar 25 18:51:39 2023 -0700 WAV: ignore bext chunk --- dsp/ImpulseResponse.cpp | 2 +- dsp/wav.cpp | 85 +++++++++++++++++++++-------------------- format.bash | 15 ++++++++ 3 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 format.bash diff --git a/dsp/ImpulseResponse.cpp b/dsp/ImpulseResponse.cpp index ad9fe1e..4ecd5d9 100644 --- a/dsp/ImpulseResponse.cpp +++ b/dsp/ImpulseResponse.cpp @@ -66,7 +66,7 @@ void dsp::ImpulseResponse::_SetWeights(const double sampleRate) { // Gain reduction. // https://github.com/sdatkinson/NeuralAmpModelerPlugin/issues/100#issuecomment-1455273839 // Add sample rate-dependence - const float gain = pow(10, -18 * 0.05) * 48000/sampleRate; + const float gain = pow(10, -18 * 0.05) * 48000 / sampleRate; for (size_t i = 0, j = irLength - 1; i < irLength; i++, j--) this->mWeight[j] = gain * this->mResampled[i]; this->mHistoryRequired = irLength - 1; diff --git a/dsp/wav.cpp b/dsp/wav.cpp index c1673e5..89dcbe5 100644 --- a/dsp/wav.cpp +++ b/dsp/wav.cpp @@ -9,13 +9,15 @@ #include #include #include -#include #include +#include #include "wav.h" bool idIsJunk(char *id) { - return strncmp(id, "junk", 4) == 0 || strncmp(id, "JUNK", 4) == 0 || strncmp(id, "smpl", 4) == 0 || strncmp(id, "LIST", 4) == 0; + return strncmp(id, "junk", 4) == 0 || strncmp(id, "JUNK", 4) == 0 || + strncmp(id, "smpl", 4) == 0 || strncmp(id, "LIST", 4) == 0 || + strncmp(id, "bext", 4) == 0; } bool ReadChunkAndSkipJunk(std::ifstream &file, char *chunkID) { @@ -50,11 +52,10 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName, // Read the WAV file header char chunkId[4]; if (!ReadChunkAndSkipJunk(wavFile, chunkId)) { - std::cerr << "Error while reading for next chunk." << std::endl; - return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; + std::cerr << "Error while reading for next chunk." << std::endl; + return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; } - - + if (strncmp(chunkId, "RIFF", 4) != 0) { std::cerr << "Error: File does not start with expected RIFF chunk. Got" << chunkId << " instead." << std::endl; @@ -75,8 +76,8 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName, // Read the format chunk char subchunk1Id[4]; if (!ReadChunkAndSkipJunk(wavFile, subchunk1Id)) { - std::cerr << "Error while reading for next chunk." << std::endl; - return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; + std::cerr << "Error while reading for next chunk." << std::endl; + return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; } if (strncmp(subchunk1Id, "fmt ", 4) != 0) { std::cerr << "Error: Invalid WAV file missing expected fmt section; got " @@ -87,15 +88,19 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName, int subchunk1Size; wavFile.read(reinterpret_cast(&subchunk1Size), 4); if (subchunk1Size < 16) { - std::cerr << "WAV chunk 1 size is " << subchunk1Size << ", which is smaller than the requried 16 to fit the expected information." << std::endl; - return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; + std::cerr << "WAV chunk 1 size is " << subchunk1Size + << ", which is smaller than the requried 16 to fit the expected " + "information." + << std::endl; + return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; } unsigned short audioFormat; wavFile.read(reinterpret_cast(&audioFormat), 2); const short AUDIO_FORMAT_PCM = 1; const short AUDIO_FORMAT_IEEE = 3; - std::unordered_set supportedFormats{ AUDIO_FORMAT_PCM, AUDIO_FORMAT_IEEE }; + std::unordered_set supportedFormats{AUDIO_FORMAT_PCM, + AUDIO_FORMAT_IEEE}; if (supportedFormats.find(audioFormat) == supportedFormats.end()) { std::cerr << "Error: Unsupported WAV format detected. "; switch (audioFormat) { @@ -136,22 +141,21 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName, short bitsPerSample; wavFile.read(reinterpret_cast(&bitsPerSample), 2); - // The default is for there to be 16 bytes in the fmt chunk, but sometimes it's different. + // The default is for there to be 16 bytes in the fmt chunk, but sometimes + // it's different. if (subchunk1Size > 16) { - const int extraBytes = subchunk1Size - 16; - const int skipChars = extraBytes / 4; - wavFile.ignore(skipChars); - const int remainder = extraBytes % 4; - int junk; - wavFile.read(reinterpret_cast(&byteRate), remainder); + const int extraBytes = subchunk1Size - 16; + const int skipChars = extraBytes / 4; + wavFile.ignore(skipChars); + const int remainder = extraBytes % 4; + wavFile.read(reinterpret_cast(&byteRate), remainder); } - // Read the data chunk char subchunk2Id[4]; if (!ReadChunkAndSkipJunk(wavFile, subchunk2Id)) { - std::cerr << "Error while reading for next chunk." << std::endl; - return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; + std::cerr << "Error while reading for next chunk." << std::endl; + return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE; } if (strncmp(subchunk2Id, "data", 4) != 0) { std::cerr << "Error: Invalid WAV file" << std::endl; @@ -163,26 +167,25 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName, wavFile.read(reinterpret_cast(&subchunk2Size), 4); if (audioFormat == AUDIO_FORMAT_IEEE) { - if (bitsPerSample == 32) - dsp::wav::_LoadSamples32(wavFile, subchunk2Size, audio); - else { - std::cerr << "Error: Unsupported bits per sample for IEEE files: " << bitsPerSample - << std::endl; - return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE; - } - } - else if (audioFormat == AUDIO_FORMAT_PCM) { - if (bitsPerSample == 16) - dsp::wav::_LoadSamples16(wavFile, subchunk2Size, audio); - else if (bitsPerSample == 24) - dsp::wav::_LoadSamples24(wavFile, subchunk2Size, audio); - else if (bitsPerSample == 32) - dsp::wav::_LoadSamples32(wavFile, subchunk2Size, audio); - else { - std::cerr << "Error: Unsupported bits per sample for PCM files: " << bitsPerSample - << std::endl; - return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE; - } + if (bitsPerSample == 32) + dsp::wav::_LoadSamples32(wavFile, subchunk2Size, audio); + else { + std::cerr << "Error: Unsupported bits per sample for IEEE files: " + << bitsPerSample << std::endl; + return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE; + } + } else if (audioFormat == AUDIO_FORMAT_PCM) { + if (bitsPerSample == 16) + dsp::wav::_LoadSamples16(wavFile, subchunk2Size, audio); + else if (bitsPerSample == 24) + dsp::wav::_LoadSamples24(wavFile, subchunk2Size, audio); + else if (bitsPerSample == 32) + dsp::wav::_LoadSamples32(wavFile, subchunk2Size, audio); + else { + std::cerr << "Error: Unsupported bits per sample for PCM files: " + << bitsPerSample << std::endl; + return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE; + } } // Close the WAV file diff --git a/format.bash b/format.bash new file mode 100644 index 0000000..8e0d8ae --- /dev/null +++ b/format.bash @@ -0,0 +1,15 @@ +#!/bin/bash +# Apply project formatting (i.e. clang-format with LLVM style) +# +# Usage: +# $ bash format.bash + +echo "Formatting..." + +git ls-files "*.h" "*.cpp" | xargs clang-format --style=llvm -i + +echo "Formatting complete!" +echo "You can stage all of the files using:" +echo "" +echo ' git ls-files "*.h" "*.cpp" | xargs git add' +echo "" \ No newline at end of file