From 6e81dabeda4157361a30b600867371d7d85fbc7c Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Thu, 27 Sep 2012 22:21:29 +0200 Subject: [PATCH] Fixed cracks with ogg music files (fixes #271) --- src/SFML/Audio/SoundFile.cpp | 44 +++++++++++++++++++++--------------- src/SFML/Audio/SoundFile.hpp | 10 ++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/SFML/Audio/SoundFile.cpp b/src/SFML/Audio/SoundFile.cpp index 36591eaa..676d2758 100644 --- a/src/SFML/Audio/SoundFile.cpp +++ b/src/SFML/Audio/SoundFile.cpp @@ -96,18 +96,16 @@ bool SoundFile::openRead(const std::string& filename) sf_close(m_file); // Open the sound file - SF_INFO fileInfos; - m_file = sf_open(filename.c_str(), SFM_READ, &fileInfos); + SF_INFO fileInfo; + m_file = sf_open(filename.c_str(), SFM_READ, &fileInfo); if (!m_file) { err() << "Failed to open sound file \"" << filename << "\" (" << sf_strerror(m_file) << ")" << std::endl; return false; } - // Set the sound parameters - m_channelCount = fileInfos.channels; - m_sampleRate = fileInfos.samplerate; - m_sampleCount = static_cast(fileInfos.frames) * m_channelCount; + // Initialize the internal state from the loaded information + initialize(fileInfo); return true; } @@ -133,18 +131,16 @@ bool SoundFile::openRead(const void* data, std::size_t sizeInBytes) m_memory.TotalSize = sizeInBytes; // Open the sound file - SF_INFO fileInfos; - m_file = sf_open_virtual(&io, SFM_READ, &fileInfos, &m_memory); + SF_INFO fileInfo; + m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &m_memory); if (!m_file) { err() << "Failed to open sound file from memory (" << sf_strerror(m_file) << ")" << std::endl; return false; } - // Set the sound parameters - m_channelCount = fileInfos.channels; - m_sampleRate = fileInfos.samplerate; - m_sampleCount = static_cast(fileInfos.frames) * m_channelCount; + // Initialize the internal state from the loaded information + initialize(fileInfo); return true; } @@ -165,18 +161,16 @@ bool SoundFile::openRead(InputStream& stream) io.tell = &Stream::tell; // Open the sound file - SF_INFO fileInfos; - m_file = sf_open_virtual(&io, SFM_READ, &fileInfos, &stream); + SF_INFO fileInfo; + m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &stream); if (!m_file) { err() << "Failed to open sound file from stream (" << sf_strerror(m_file) << ")" << std::endl; return false; } - // Set the sound parameters - m_channelCount = fileInfos.channels; - m_sampleRate = fileInfos.samplerate; - m_sampleCount = static_cast(fileInfos.frames) * m_channelCount; + // Initialize the internal state from the loaded information + initialize(fileInfo); return true; } @@ -260,6 +254,20 @@ void SoundFile::seek(Time timeOffset) } +//////////////////////////////////////////////////////////// +void SoundFile::initialize(SF_INFO fileInfo) +{ + // Save the sound properties + m_channelCount = fileInfo.channels; + m_sampleRate = fileInfo.samplerate; + m_sampleCount = static_cast(fileInfo.frames) * fileInfo.channels; + + // Enable scaling for Vorbis files (float samples) + if (fileInfo.format & SF_FORMAT_VORBIS) + sf_command(m_file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE); +} + + //////////////////////////////////////////////////////////// int SoundFile::getFormatFromFilename(const std::string& filename) { diff --git a/src/SFML/Audio/SoundFile.hpp b/src/SFML/Audio/SoundFile.hpp index b791fe6e..855dd21e 100644 --- a/src/SFML/Audio/SoundFile.hpp +++ b/src/SFML/Audio/SoundFile.hpp @@ -157,6 +157,16 @@ public : private : + //////////////////////////////////////////////////////////// + /// \brief Initialize the internal state of the sound file + /// + /// This function is called by all the openRead functions. + /// + /// \param fileInfo Information about the loaded sound file + /// + //////////////////////////////////////////////////////////// + void initialize(SF_INFO fileInfo); + //////////////////////////////////////////////////////////// /// \brief Get the internal format of an audio file according to /// its filename extension