Fixed inconsistent seek behavior in SoundStream
This commit is contained in:
parent
2df9abf341
commit
2207af41e4
6 changed files with 75 additions and 24 deletions
|
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -217,10 +245,11 @@ void InputSoundFile::seek(Time timeOffset)
|
|||
////////////////////////////////////////////////////////////
|
||||
Uint64 InputSoundFile::read(Int16* samples, Uint64 maxCount)
|
||||
{
|
||||
Uint64 readSamples = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -238,6 +267,7 @@ void InputSoundFile::close()
|
|||
m_streamOwned = false;
|
||||
}
|
||||
m_stream = NULL;
|
||||
m_sampleOffset = 0;
|
||||
|
||||
// Reset the sound file attributes
|
||||
m_sampleCount = 0;
|
||||
|
|
|
@ -36,8 +36,7 @@ namespace sf
|
|||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
Music::Music() :
|
||||
m_file (),
|
||||
m_duration()
|
||||
m_file ()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -105,7 +104,7 @@ bool Music::openFromStream(InputStream& stream)
|
|||
////////////////////////////////////////////////////////////
|
||||
Time Music::getDuration() const
|
||||
{
|
||||
return m_duration;
|
||||
return m_file.getDuration();
|
||||
}
|
||||
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
|
@ -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());
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
@ -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();
|
||||
|
@ -397,7 +395,7 @@ void SoundStream::streamData()
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool SoundStream::fillAndPushBuffer(unsigned int bufferNum)
|
||||
bool SoundStream::fillAndPushBuffer(unsigned int bufferNum, bool immediateLoop)
|
||||
{
|
||||
bool requestStop = false;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue