Added: #795 Synchronous loading of audio file streams

--HG--
branch : sound_dev
This commit is contained in:
kaetemi 2012-04-11 18:07:16 +02:00
parent 3bbf48e699
commit 2f504788f0
2 changed files with 45 additions and 16 deletions

View file

@ -81,6 +81,7 @@ public:
// TODO: getTime // TODO: getTime
private: private:
void prepareDecoder();
void bufferMore(uint bytes); void bufferMore(uint bytes);
private: private:

View file

@ -84,13 +84,33 @@ void CStreamFileSource::play()
// thread may be stopping from stop call // thread may be stopping from stop call
m_Thread->wait(); m_Thread->wait();
} }
nlassert(!_Playing); else
{
nlwarning("Already playing");
}
if (!getStreamFileSound()->getAsync())
prepareDecoder();
// else load audiodecoder in thread
m_WaitingForPlay = true; m_WaitingForPlay = true;
m_Thread->start(); m_Thread->start();
m_Thread->setPriority(NLMISC::ThreadPriorityHighest); m_Thread->setPriority(NLMISC::ThreadPriorityHighest);
if (!getStreamFileSound()->getAsync())
{
// wait until at least one buffer is ready
while (!(m_NextBuffer || !m_FreeBuffers))
NLMISC::nlSleep(10);
CStreamSource::play();
if (!_Playing)
{
nlwarning("Failed to synchronously start playing a file stream source. This happens when all physical tracks are in use. Use a Highest Priority sound");
}
}
else
{
CAudioMixerUser *mixer = CAudioMixerUser::instance(); CAudioMixerUser *mixer = CAudioMixerUser::instance();
mixer->addSourceWaitingForPlay(this); mixer->addSourceWaitingForPlay(this);
} }
}
/*if (!m_WaitingForPlay) /*if (!m_WaitingForPlay)
{ {
@ -162,21 +182,10 @@ bool CStreamFileSource::isEnded()
return (!m_Thread->isRunning() && !_Playing && !m_WaitingForPlay && !m_Paused); return (!m_Thread->isRunning() && !_Playing && !m_WaitingForPlay && !m_Paused);
} }
void CStreamFileSource::bufferMore(uint bytes) // buffer from bytes (minimum) to bytes * 2 (maximum) void CStreamFileSource::prepareDecoder()
{ {
uint8 *buffer = this->lock(bytes * 2); // creates a new decoder or keeps going with the current decoder if the stream was paused
if (buffer)
{
uint32 result = m_AudioDecoder->getNextBytes(buffer, bytes, bytes * 2);
this->unlock(result);
}
}
void CStreamFileSource::run()
{
nldebug("run");
bool looping = _Looping;
if (m_Paused) if (m_Paused)
{ {
// handle paused! // handle paused!
@ -199,6 +208,25 @@ void CStreamFileSource::run()
} }
this->setFormat(m_AudioDecoder->getChannels(), m_AudioDecoder->getBitsPerSample(), (uint32)m_AudioDecoder->getSamplesPerSec()); this->setFormat(m_AudioDecoder->getChannels(), m_AudioDecoder->getBitsPerSample(), (uint32)m_AudioDecoder->getSamplesPerSec());
} }
}
void CStreamFileSource::bufferMore(uint bytes) // buffer from bytes (minimum) to bytes * 2 (maximum)
{
uint8 *buffer = this->lock(bytes * 2);
if (buffer)
{
uint32 result = m_AudioDecoder->getNextBytes(buffer, bytes, bytes * 2);
this->unlock(result);
}
}
void CStreamFileSource::run()
{
nldebug("run");
bool looping = _Looping;
if (getStreamFileSound()->getAsync())
prepareDecoder();
uint samples, bytes; uint samples, bytes;
this->getRecommendedBufferSize(samples, bytes); this->getRecommendedBufferSize(samples, bytes);
bufferMore(bytes); bufferMore(bytes);