Round sample offset when seeking InputSoundFile

This fixes a bug that caused `sf::Music` to cease looping after certain
seeks due to the sample offset misaligning with the sound file's channel
count.
This commit is contained in:
Marukyu 2020-10-01 13:07:00 +02:00 committed by Lukas Dürrenberger
parent 7b84e46fac
commit c11ea7792f

View File

@ -225,11 +225,11 @@ Uint64 InputSoundFile::getSampleOffset() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void InputSoundFile::seek(Uint64 sampleOffset) void InputSoundFile::seek(Uint64 sampleOffset)
{ {
if (m_reader) if (m_reader && m_channelCount != 0)
{ {
// The reader handles an overrun gracefully, but we // The reader handles an overrun gracefully, but we
// pre-check to keep our known position consistent // pre-check to keep our known position consistent
m_sampleOffset = std::min(sampleOffset, m_sampleCount); m_sampleOffset = std::min(sampleOffset / m_channelCount * m_channelCount, m_sampleCount);
m_reader->seek(m_sampleOffset); m_reader->seek(m_sampleOffset);
} }
} }
@ -238,7 +238,7 @@ void InputSoundFile::seek(Uint64 sampleOffset)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void InputSoundFile::seek(Time timeOffset) void InputSoundFile::seek(Time timeOffset)
{ {
seek(static_cast<Uint64>(timeOffset.asSeconds() * m_sampleRate * m_channelCount)); seek(static_cast<Uint64>(timeOffset.asSeconds() * m_sampleRate) * m_channelCount);
} }