Skip to content

Commit

Permalink
Support 32bit PCM files
Browse files Browse the repository at this point in the history
  • Loading branch information
manongjohn committed Jun 16, 2021
1 parent e2dd56a commit bcc8843
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rhubarb/src/audio/WaveFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include "tools/platformTools.h"
#include "tools/fileTools.h"
#include <climits>

using std::runtime_error;
using fmt::format;
Expand All @@ -18,7 +19,7 @@ using boost::filesystem::path;
#define INT24_MAX 8388607

// Converts an int in the range min..max to a float in the range -1..1
float toNormalizedFloat(int value, int min, int max) {
float toNormalizedFloat(int64_t value, int64_t min, int64_t max) {
return (static_cast<float>(value - min) / (max - min) * 2) - 1;
}

Expand Down Expand Up @@ -97,6 +98,9 @@ WaveFileReader::WaveFileReader(const path& filePath) :
} else if (bitsPerSample <= 24) {
formatInfo.sampleFormat = SampleFormat::Int24;
bytesPerSample = 3;
} else if (bitsPerSample <= 32) {
formatInfo.sampleFormat = SampleFormat::Int32;
bytesPerSample = 4;
} else {
throw runtime_error(
format("Unsupported sample format: {}-bit PCM.", bitsPerSample));
Expand Down Expand Up @@ -172,6 +176,12 @@ inline AudioClip::value_type readSample(
sum += toNormalizedFloat(raw, INT24_MIN, INT24_MAX);
break;
}
case SampleFormat::Int32:
{
int raw = read<int>(file);
sum += toNormalizedFloat(raw, INT_MIN, INT_MAX);
break;
}
case SampleFormat::Float32:
{
sum += read<float>(file);
Expand Down
1 change: 1 addition & 0 deletions rhubarb/src/audio/WaveFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum class SampleFormat {
UInt8,
Int16,
Int24,
Int32,
Float32
};

Expand Down

0 comments on commit bcc8843

Please sign in to comment.