Skip to content

Commit

Permalink
* Ignore bext chunk
Browse files Browse the repository at this point in the history
* formatting script

Squashed commit of the following:

commit 1c8e40a7a7d137718abff4dc292b9b0b8f2a3134
Author: Steven Atkinson <steven@atkinson.mn>
Date:   Sat Mar 25 18:52:42 2023 -0700

    Format

commit 2cf8cdffca3382c9a19e69e1a6af27f5ed1c5508
Author: Steven Atkinson <steven@atkinson.mn>
Date:   Sat Mar 25 18:52:14 2023 -0700

    format.bash

commit 198abd6a08683720829e53c8769e2d8e97bff5ad
Author: Steven Atkinson <steven@atkinson.mn>
Date:   Sat Mar 25 18:51:39 2023 -0700

    WAV: ignore bext chunk
  • Loading branch information
sdatkinson committed Mar 26, 2023
1 parent 573065e commit 070811a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dsp/ImpulseResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
85 changes: 44 additions & 41 deletions dsp/wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <vector>

#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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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 "
Expand All @@ -87,15 +88,19 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
int subchunk1Size;
wavFile.read(reinterpret_cast<char *>(&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<char *>(&audioFormat), 2);
const short AUDIO_FORMAT_PCM = 1;
const short AUDIO_FORMAT_IEEE = 3;
std::unordered_set<short> supportedFormats{ AUDIO_FORMAT_PCM, AUDIO_FORMAT_IEEE };
std::unordered_set<short> supportedFormats{AUDIO_FORMAT_PCM,
AUDIO_FORMAT_IEEE};
if (supportedFormats.find(audioFormat) == supportedFormats.end()) {
std::cerr << "Error: Unsupported WAV format detected. ";
switch (audioFormat) {
Expand Down Expand Up @@ -136,22 +141,21 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
short bitsPerSample;
wavFile.read(reinterpret_cast<char *>(&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<char*>(&byteRate), remainder);
const int extraBytes = subchunk1Size - 16;
const int skipChars = extraBytes / 4;
wavFile.ignore(skipChars);
const int remainder = extraBytes % 4;
wavFile.read(reinterpret_cast<char *>(&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;
Expand All @@ -163,26 +167,25 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
wavFile.read(reinterpret_cast<char *>(&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
Expand Down
15 changes: 15 additions & 0 deletions format.bash
Original file line number Diff line number Diff line change
@@ -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 ""

0 comments on commit 070811a

Please sign in to comment.