Skip to content

Commit

Permalink
Fixed cracks with ogg music files (fixes #271)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGomila committed Sep 27, 2012
1 parent 79df414 commit 6e81dab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/SFML/Audio/SoundFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(fileInfos.frames) * m_channelCount;
// Initialize the internal state from the loaded information
initialize(fileInfo);

return true;
}
Expand All @@ -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<std::size_t>(fileInfos.frames) * m_channelCount;
// Initialize the internal state from the loaded information
initialize(fileInfo);

return true;
}
Expand All @@ -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<std::size_t>(fileInfos.frames) * m_channelCount;
// Initialize the internal state from the loaded information
initialize(fileInfo);

return true;
}
Expand Down Expand Up @@ -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<std::size_t>(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)
{
Expand Down
10 changes: 10 additions & 0 deletions src/SFML/Audio/SoundFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6e81dab

Please sign in to comment.