Skip to content

Commit 888be7e

Browse files
authored
[BUGFIX] Fix PCM 32-bit (fixed-point) WAVE loading (#22)
1 parent 57ed6bf commit 888be7e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

dsp/wav.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ dsp::wav::LoadReturnCode ReadDataChunk(std::ifstream& wavFile, WaveFileData& wfd
307307
if (audioFormat == AUDIO_FORMAT_IEEE)
308308
{
309309
if (wfd.fmtChunk.bitsPerSample == 32)
310-
dsp::wav::_LoadSamples32(wavFile, wfd.dataChunk.size, audio);
310+
dsp::wav::_LoadSamples32FloatingPoint(wavFile, wfd.dataChunk.size, audio);
311311
else
312312
{
313313
std::cerr << "Error: Unsupported bits per sample for IEEE files: " << wfd.fmtChunk.bitsPerSample << std::endl;
@@ -321,7 +321,7 @@ dsp::wav::LoadReturnCode ReadDataChunk(std::ifstream& wavFile, WaveFileData& wfd
321321
else if (wfd.fmtChunk.bitsPerSample == 24)
322322
dsp::wav::_LoadSamples24(wavFile, wfd.dataChunk.size, audio);
323323
else if (wfd.fmtChunk.bitsPerSample == 32)
324-
dsp::wav::_LoadSamples32(wavFile, wfd.dataChunk.size, audio);
324+
dsp::wav::_LoadSamples32FixedPoint(wavFile, wfd.dataChunk.size, audio);
325325
else
326326
{
327327
std::cerr << "Error: Unsupported bits per sample for PCM files: " << wfd.fmtChunk.bitsPerSample << std::endl;
@@ -456,10 +456,25 @@ int dsp::wav::_ReadSigned24BitInt(std::ifstream& stream)
456456
return value;
457457
}
458458

459-
void dsp::wav::_LoadSamples32(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples)
459+
void dsp::wav::_LoadSamples32FloatingPoint(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples)
460460
{
461461
// NOTE: 32-bit is float.
462462
samples.resize(chunkSize / 4); // 32 bits (4 bytes) per sample
463463
// Read the samples from the file into the array
464464
wavFile.read(reinterpret_cast<char*>(samples.data()), chunkSize);
465465
}
466+
467+
void dsp::wav::_LoadSamples32FixedPoint(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples)
468+
{
469+
// Allocate an array to hold the samples
470+
std::vector<int> tmp(chunkSize / 4); // 32 bits (4 bytes) per sample
471+
472+
// Read the samples from the file into the array
473+
wavFile.read(reinterpret_cast<char*>(tmp.data()), chunkSize);
474+
475+
// Copy into the return array
476+
const float scale = 1.0 / ((double)(1 << 31)); // 2^31 for 32-bit fixed point
477+
samples.resize(tmp.size());
478+
for (auto i = 0; i < samples.size(); i++)
479+
samples[i] = scale * ((float)tmp[i]);
480+
}

dsp/wav.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ void _LoadSamples16(std::ifstream& wavFile, const int chunkSize, std::vector<flo
4545
// Load samples, 24-bit
4646
void _LoadSamples24(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples);
4747
// Load samples, 32-bit
48-
void _LoadSamples32(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples);
48+
void _LoadSamples32FloatingPoint(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples);
49+
// Load samples, 32-bit fixed point
50+
void _LoadSamples32FixedPoint(std::ifstream& wavFile, const int chunkSize, std::vector<float>& samples);
4951

5052
// Read in a 24-bit sample and convert it to an int
5153
int _ReadSigned24BitInt(std::ifstream& stream);

0 commit comments

Comments
 (0)