Skip to content

Commit

Permalink
Fixed inconsistent seek behavior in SoundStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Cobaltergeist committed Oct 11, 2016
1 parent 39208ef commit 7a1797b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
18 changes: 18 additions & 0 deletions include/SFML/Audio/InputSoundFile.hpp
Expand Up @@ -32,6 +32,7 @@
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <string>
#include <algorithm>


namespace sf
Expand Down Expand Up @@ -146,6 +147,22 @@ class SFML_AUDIO_API InputSoundFile : NonCopyable
////////////////////////////////////////////////////////////
Time getDuration() const;

////////////////////////////////////////////////////////////
/// \brief Get the read offset of the file in time
///
/// \return Time position
///
////////////////////////////////////////////////////////////
Time getTimeOffset() const;

////////////////////////////////////////////////////////////
/// \brief Get the read offset of the file in samples
///
/// \return Sample position
///
////////////////////////////////////////////////////////////
Uint64 getSampleOffset() const;

////////////////////////////////////////////////////////////
/// \brief Change the current read position to the given sample offset
///
Expand Down Expand Up @@ -203,6 +220,7 @@ class SFML_AUDIO_API InputSoundFile : NonCopyable
SoundFileReader* m_reader; ///< Reader that handles I/O on the file's format
InputStream* m_stream; ///< Input stream used to access the file's data
bool m_streamOwned; ///< Is the stream internal or external?
Uint64 m_sampleOffset; ///< Sample Read Position
Uint64 m_sampleCount; ///< Total number of samples in the file
unsigned int m_channelCount; ///< Number of channels of the sound
unsigned int m_sampleRate; ///< Number of samples per second
Expand Down
7 changes: 3 additions & 4 deletions include/SFML/Audio/Music.hpp
Expand Up @@ -168,10 +168,9 @@ class SFML_AUDIO_API Music : public SoundStream
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
InputSoundFile m_file; ///< The streamed music file
Time m_duration; ///< Music duration
std::vector<Int16> m_samples; ///< Temporary buffer of samples
Mutex m_mutex; ///< Mutex protecting the data
InputSoundFile m_file; ///< The streamed music file
std::vector<Int16> m_samples; ///< Temporary buffer of samples
Mutex m_mutex; ///< Mutex protecting the data
};

} // namespace sf
Expand Down
3 changes: 2 additions & 1 deletion include/SFML/Audio/SoundStream.hpp
Expand Up @@ -254,11 +254,12 @@ class SFML_AUDIO_API SoundStream : public SoundSource
/// playing queue.
///
/// \param bufferNum Number of the buffer to fill (in [0, BufferCount])
/// \param immediateLoop Treat empty buffers as spent, and act on loops immediately
///
/// \return True if the stream source has requested to stop, false otherwise
///
////////////////////////////////////////////////////////////
bool fillAndPushBuffer(unsigned int bufferNum);
bool fillAndPushBuffer(unsigned int bufferNum, bool immediateLoop = false);

////////////////////////////////////////////////////////////
/// \brief Fill the audio buffers and put them all into the playing queue
Expand Down
43 changes: 39 additions & 4 deletions src/SFML/Audio/InputSoundFile.cpp
Expand Up @@ -41,6 +41,7 @@ InputSoundFile::InputSoundFile() :
m_reader (NULL),
m_stream (NULL),
m_streamOwned (false),
m_sampleOffset (0),
m_sampleCount (0),
m_channelCount(0),
m_sampleRate (0)
Expand Down Expand Up @@ -195,15 +196,42 @@ unsigned int InputSoundFile::getSampleRate() const
////////////////////////////////////////////////////////////
Time InputSoundFile::getDuration() const
{
// Make sure we don't divide by 0
if (m_channelCount == 0 || m_sampleRate == 0)
return Time::Zero;

return seconds(static_cast<float>(m_sampleCount) / m_channelCount / m_sampleRate);
}


////////////////////////////////////////////////////////////
Time InputSoundFile::getTimeOffset() const
{
// Make sure we don't divide by 0
if (m_channelCount == 0 || m_sampleRate == 0)
return Time::Zero;

return seconds(static_cast<float>(m_sampleOffset) / m_channelCount / m_sampleRate);
}


////////////////////////////////////////////////////////////
Uint64 InputSoundFile::getSampleOffset() const
{
return m_sampleOffset;
}


////////////////////////////////////////////////////////////
void InputSoundFile::seek(Uint64 sampleOffset)
{
if (m_reader)
m_reader->seek(sampleOffset);
{
// The reader handles an overrun gracefully, but we
// pre-check to keep our known position consistent
m_sampleOffset = std::min(sampleOffset, m_sampleCount);
m_reader->seek(m_sampleOffset);
}
}


Expand All @@ -217,10 +245,16 @@ void InputSoundFile::seek(Time timeOffset)
////////////////////////////////////////////////////////////
Uint64 InputSoundFile::read(Int16* samples, Uint64 maxCount)
{
Uint64 readSamples = 0;

// Clamp the read count to the expected number of remaining
// samples, so we can force a short-read when EOF is imminent
maxCount = std::min(maxCount, m_sampleCount - m_sampleOffset);

if (m_reader && samples && maxCount)
return m_reader->read(samples, maxCount);
else
return 0;
readSamples = m_reader->read(samples, maxCount);
m_sampleOffset += readSamples;
return readSamples;
}


Expand All @@ -238,6 +272,7 @@ void InputSoundFile::close()
m_streamOwned = false;
}
m_stream = NULL;
m_sampleOffset = 0;

// Reset the sound file attributes
m_sampleCount = 0;
Expand Down
12 changes: 4 additions & 8 deletions src/SFML/Audio/Music.cpp
Expand Up @@ -36,8 +36,7 @@ namespace sf
{
////////////////////////////////////////////////////////////
Music::Music() :
m_file (),
m_duration()
m_file ()
{

}
Expand Down Expand Up @@ -105,7 +104,7 @@ bool Music::openFromStream(InputStream& stream)
////////////////////////////////////////////////////////////
Time Music::getDuration() const
{
return m_duration;
return m_file.getDuration();
}


Expand All @@ -118,8 +117,8 @@ bool Music::onGetData(SoundStream::Chunk& data)
data.samples = &m_samples[0];
data.sampleCount = static_cast<std::size_t>(m_file.read(&m_samples[0], m_samples.size()));

// Check if we have reached the end of the audio file
return data.sampleCount == m_samples.size();
// Check if we have stopped obtaining samples or reached the end of the audio file
return (data.sampleCount != 0) && (m_file.getSampleOffset() < m_file.getSampleCount());
}


Expand All @@ -135,9 +134,6 @@ void Music::onSeek(Time timeOffset)
////////////////////////////////////////////////////////////
void Music::initialize()
{
// Compute the music duration
m_duration = m_file.getDuration();

// Resize the internal buffer so that it can contain 1 second of audio samples
m_samples.resize(m_file.getSampleRate() * m_file.getChannelCount());

Expand Down
21 changes: 14 additions & 7 deletions src/SFML/Audio/SoundStream.cpp
Expand Up @@ -77,7 +77,9 @@ SoundStream::~SoundStream()
void SoundStream::initialize(unsigned int channelCount, unsigned int sampleRate)
{
m_channelCount = channelCount;
m_sampleRate = sampleRate;
m_sampleRate = sampleRate;
m_samplesProcessed = 0;
m_isStreaming = false;

// Deduce the format from the number of channels
m_format = priv::AudioDevice::getFormatFromChannelCount(channelCount);
Expand Down Expand Up @@ -127,11 +129,7 @@ void SoundStream::play()
stop();
}

// Move to the beginning
onSeek(Time::Zero);

// Start updating the stream in a separate thread to avoid blocking the application
m_samplesProcessed = 0;
m_isStreaming = true;
m_threadStartState = Playing;
m_thread.launch();
Expand Down Expand Up @@ -397,7 +395,7 @@ void SoundStream::streamData()


////////////////////////////////////////////////////////////
bool SoundStream::fillAndPushBuffer(unsigned int bufferNum)
bool SoundStream::fillAndPushBuffer(unsigned int bufferNum, bool immediateLoop)
{
bool requestStop = false;

Expand All @@ -417,6 +415,13 @@ bool SoundStream::fillAndPushBuffer(unsigned int bufferNum)
// If we previously had no data, try to fill the buffer once again
if (!data.samples || (data.sampleCount == 0))
{
// If immediateLoop is specified, we have to immediately adjust the sample count
if (immediateLoop)
{
// We just tried to begin preloading at EOF: reset the sample count
m_samplesProcessed = 0;
m_endBuffers[bufferNum] = false;
}
return fillAndPushBuffer(bufferNum);
}
}
Expand Down Expand Up @@ -451,7 +456,9 @@ bool SoundStream::fillQueue()
bool requestStop = false;
for (int i = 0; (i < BufferCount) && !requestStop; ++i)
{
if (fillAndPushBuffer(i))
// Since no sound has been loaded yet, we can't schedule loop seeks preemptively,
// So if we start on EOF or Loop End, we let fillAndPushBuffer() adjust the sample count
if (fillAndPushBuffer(i, (i == 0)))
requestStop = true;
}

Expand Down

0 comments on commit 7a1797b

Please sign in to comment.