Skip to content

Commit 99e4ce6

Browse files
authored
Merge pull request #97 from gitelope/bugfix-saving_AudioFile-double_to_32-bit_WAV
bugfix: AudioFile<double> not properly saved to 32-bit (IEEE floating-point) WAV
2 parents 9458a09 + ee36289 commit 99e4ce6

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

AudioFile.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,9 +995,22 @@ bool AudioFile<T>::encodeWaveFile (std::vector<uint8_t>& fileData)
995995
int32_t sampleAsInt;
996996

997997
if (audioFormat == WavAudioFormat::IEEEFloat)
998-
sampleAsInt = (int32_t) reinterpret_cast<int32_t&> (samples[channel][i]);
998+
{
999+
if constexpr (std::is_same_v<T, float>)
1000+
{
1001+
sampleAsInt = (int32_t) reinterpret_cast<int32_t&> (samples[channel][i]);
1002+
}
1003+
else if constexpr (std::is_same_v<T, double>)
1004+
{
1005+
auto sampleAsFloat = (float) samples[channel][i];
1006+
float& referenceToSample = sampleAsFloat;
1007+
sampleAsInt = (int32_t) reinterpret_cast<int32_t&> (referenceToSample);
1008+
}
1009+
}
9991010
else // assume PCM
1011+
{
10001012
sampleAsInt = AudioSampleConverter<T>::sampleToThirtyTwoBitInt (samples[channel][i]);
1013+
}
10011014

10021015
addInt32ToFileData (fileData, sampleAsInt, Endianness::LittleEndian);
10031016
}

tests/FileWritingTests.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ void writeTestAudioFile (int numChannels, int sampleRate, int bitDepth, AudioFil
8585
REQUIRE (OK);
8686

8787
//-----------------------------------------------------------------
88-
// for some key bit depths and mono/stereo files, read in the audio file
89-
// we just wrote and do a sample-by-sample comparison to confirm we are
90-
// writing good files
91-
if ((bitDepth == 8 || bitDepth == 16 || bitDepth == 24) && numChannels <= 2)
88+
// read in the audio file we just wrote and do a sample-by-sample
89+
// comparison to confirm we are writing good files
90+
if (numChannels <= 2)
9291
{
9392
AudioFile<T> audioFileReader;
9493
audioFileReader.load (filePath);
@@ -136,13 +135,38 @@ TEST_SUITE ("Writing Tests")
136135
for (auto& format : audioFormats)
137136
{
138137
auto fmt_str = format == AudioFileFormat::Wave ? "wav" : "aiff";
139-
std::cerr << sampleRate << "Hz " << bitDepth << "-bit " << channels << " " << fmt_str << " (floating point)" << std::endl;
138+
std::cerr << sampleRate << "Hz " << bitDepth << "-bit " << channels << " " << fmt_str << " (float)" << std::endl;
140139
writeTestAudioFile<float> (channels, sampleRate, bitDepth, format);
141140
}
142141
}
143142
}
144143
}
145144
}
145+
146+
//=============================================================
147+
TEST_CASE ("WritingTest::WriteSineToneToManyFormats::DoublePrecision")
148+
{
149+
std::vector<int> sampleRates = {22050, 44100, 48000, 96000};
150+
std::vector<int> bitDepths = {8, 16, 24, 32};
151+
std::vector<int> numChannels = {1, 2, 8};
152+
std::vector<AudioFileFormat> audioFormats = {AudioFileFormat::Wave, AudioFileFormat::Aiff};
153+
154+
for (auto& sampleRate : sampleRates)
155+
{
156+
for (auto& bitDepth : bitDepths)
157+
{
158+
for (auto& channels : numChannels)
159+
{
160+
for (auto& format : audioFormats)
161+
{
162+
auto fmt_str = format == AudioFileFormat::Wave ? "wav" : "aiff";
163+
std::cerr << sampleRate << "Hz " << bitDepth << "-bit " << channels << " " << fmt_str << " (double)" << std::endl;
164+
writeTestAudioFile<double> (channels, sampleRate, bitDepth, format);
165+
}
166+
}
167+
}
168+
}
169+
}
146170

147171
//=============================================================
148172
TEST_CASE ("WritingTest::WriteSineToneToManyFormats::Integer")

0 commit comments

Comments
 (0)