From b3d6e4811db99d68be1314625c32f4722ff83db0 Mon Sep 17 00:00:00 2001 From: Mario Liebisch Date: Sat, 9 Sep 2017 16:16:17 +0200 Subject: [PATCH] Fixed Wave file writer writing wrong header values Previously when updating the header fields, SFML assumed the number of samples written would be the number of samples per channel, which wasn't the case. Therefore for stereo files the written file length was actually twice the correct value. This fix uses the file size written as a basis, no longer counting the samples written alltogether. This fixes issue #1280. --- src/SFML/Audio/SoundFileWriterWav.cpp | 14 ++++---------- src/SFML/Audio/SoundFileWriterWav.hpp | 2 -- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/SFML/Audio/SoundFileWriterWav.cpp b/src/SFML/Audio/SoundFileWriterWav.cpp index 1b48fe5d5..7d7f60efb 100644 --- a/src/SFML/Audio/SoundFileWriterWav.cpp +++ b/src/SFML/Audio/SoundFileWriterWav.cpp @@ -86,9 +86,7 @@ bool SoundFileWriterWav::check(const std::string& filename) //////////////////////////////////////////////////////////// SoundFileWriterWav::SoundFileWriterWav() : -m_file (), -m_sampleCount (0), -m_channelCount(0) +m_file() { } @@ -118,9 +116,6 @@ bool SoundFileWriterWav::open(const std::string& filename, unsigned int sampleRa return false; } - // Save the channel count - m_channelCount = channelCount; - return true; } @@ -130,8 +125,6 @@ void SoundFileWriterWav::write(const Int16* samples, Uint64 count) { assert(m_file.good()); - m_sampleCount += count; - while (count--) encode(m_file, *samples++); } @@ -191,8 +184,9 @@ void SoundFileWriterWav::close() m_file.flush(); // Update the main chunk size and data sub-chunk size - Uint32 dataChunkSize = static_cast(m_sampleCount * m_channelCount * 2); - Uint32 mainChunkSize = dataChunkSize + 36; + Uint32 fileSize = static_cast(m_file.tellp()); + Uint32 mainChunkSize = fileSize - 8; // 8 bytes RIFF header + Uint32 dataChunkSize = fileSize - 44; // 44 bytes RIFF + WAVE headers m_file.seekp(4); encode(m_file, mainChunkSize); m_file.seekp(40); diff --git a/src/SFML/Audio/SoundFileWriterWav.hpp b/src/SFML/Audio/SoundFileWriterWav.hpp index b37a970cc..98983786a 100644 --- a/src/SFML/Audio/SoundFileWriterWav.hpp +++ b/src/SFML/Audio/SoundFileWriterWav.hpp @@ -113,8 +113,6 @@ private: // Member data //////////////////////////////////////////////////////////// std::ofstream m_file; ///< File stream to write to - Uint64 m_sampleCount; ///< Total number of samples written to the file - unsigned int m_channelCount; ///< Number of channels of the sound }; } // namespace priv