diff --git a/AudioFile.h b/AudioFile.h index bc71da8..3897949 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -995,9 +995,22 @@ bool AudioFile::encodeWaveFile (std::vector& fileData) int32_t sampleAsInt; if (audioFormat == WavAudioFormat::IEEEFloat) - sampleAsInt = (int32_t) reinterpret_cast (samples[channel][i]); + { + if constexpr (std::is_same_v) + { + sampleAsInt = (int32_t) reinterpret_cast (samples[channel][i]); + } + else if constexpr (std::is_same_v) + { + auto sampleAsFloat = (float) samples[channel][i]; + float& referenceToSample = sampleAsFloat; + sampleAsInt = (int32_t) reinterpret_cast (referenceToSample); + } + } else // assume PCM + { sampleAsInt = AudioSampleConverter::sampleToThirtyTwoBitInt (samples[channel][i]); + } addInt32ToFileData (fileData, sampleAsInt, Endianness::LittleEndian); } diff --git a/tests/FileWritingTests.cpp b/tests/FileWritingTests.cpp index 434d397..33f92a1 100644 --- a/tests/FileWritingTests.cpp +++ b/tests/FileWritingTests.cpp @@ -85,10 +85,9 @@ void writeTestAudioFile (int numChannels, int sampleRate, int bitDepth, AudioFil REQUIRE (OK); //----------------------------------------------------------------- - // for some key bit depths and mono/stereo files, read in the audio file - // we just wrote and do a sample-by-sample comparison to confirm we are - // writing good files - if ((bitDepth == 8 || bitDepth == 16 || bitDepth == 24) && numChannels <= 2) + // read in the audio file we just wrote and do a sample-by-sample + // comparison to confirm we are writing good files + if (numChannels <= 2) { AudioFile audioFileReader; audioFileReader.load (filePath); @@ -136,13 +135,38 @@ TEST_SUITE ("Writing Tests") for (auto& format : audioFormats) { auto fmt_str = format == AudioFileFormat::Wave ? "wav" : "aiff"; - std::cerr << sampleRate << "Hz " << bitDepth << "-bit " << channels << " " << fmt_str << " (floating point)" << std::endl; + std::cerr << sampleRate << "Hz " << bitDepth << "-bit " << channels << " " << fmt_str << " (float)" << std::endl; writeTestAudioFile (channels, sampleRate, bitDepth, format); } } } } } + + //============================================================= + TEST_CASE ("WritingTest::WriteSineToneToManyFormats::DoublePrecision") + { + std::vector sampleRates = {22050, 44100, 48000, 96000}; + std::vector bitDepths = {8, 16, 24, 32}; + std::vector numChannels = {1, 2, 8}; + std::vector audioFormats = {AudioFileFormat::Wave, AudioFileFormat::Aiff}; + + for (auto& sampleRate : sampleRates) + { + for (auto& bitDepth : bitDepths) + { + for (auto& channels : numChannels) + { + for (auto& format : audioFormats) + { + auto fmt_str = format == AudioFileFormat::Wave ? "wav" : "aiff"; + std::cerr << sampleRate << "Hz " << bitDepth << "-bit " << channels << " " << fmt_str << " (double)" << std::endl; + writeTestAudioFile (channels, sampleRate, bitDepth, format); + } + } + } + } + } //============================================================= TEST_CASE ("WritingTest::WriteSineToneToManyFormats::Integer")