1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-03 20:45:33 +00:00

#1041 in progress: decode first sample batch right in OpenAL_SoundStream::play()

This commit is contained in:
Nikolay Kasyanov 2014-02-17 02:59:23 +04:00
parent 5e8cb2e466
commit 51fb9f65ea

View file

@ -172,7 +172,6 @@ class OpenAL_SoundStream : public Sound
DecoderPtr mDecoder; DecoderPtr mDecoder;
volatile bool mIsFinished; volatile bool mIsFinished;
volatile bool mIsInitialBatchEnqueued;
void updateAll(bool local); void updateAll(bool local);
@ -265,7 +264,7 @@ private:
OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, float basevol, float pitch, int flags) OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, float basevol, float pitch, int flags)
: Sound(Ogre::Vector3(0.0f), 1.0f, basevol, pitch, 1.0f, 1000.0f, flags) : Sound(Ogre::Vector3(0.0f), 1.0f, basevol, pitch, 1.0f, 1000.0f, flags)
, mOutput(output), mSource(src), mSamplesQueued(0), mDecoder(decoder), mIsFinished(true), mIsInitialBatchEnqueued(false) , mOutput(output), mSource(src), mSamplesQueued(0), mDecoder(decoder), mIsFinished(true)
{ {
throwALerror(); throwALerror();
@ -316,8 +315,26 @@ void OpenAL_SoundStream::play()
alSourcei(mSource, AL_BUFFER, 0); alSourcei(mSource, AL_BUFFER, 0);
throwALerror(); throwALerror();
mSamplesQueued = 0; mSamplesQueued = 0;
mIsFinished = false;
mIsInitialBatchEnqueued = false; std::vector<char> data(mBufferSize);
bool finished = false;
for(ALuint i = 0;i < sNumBuffers && !finished;i++)
{
size_t got = mDecoder->read(&data[0], data.size());
finished = (got < data.size());
if(got > 0)
{
ALuint bufid = mBuffers[i];
alBufferData(bufid, mFormat, &data[0], got, mSampleRate);
alSourceQueueBuffers(mSource, 1, &bufid);
throwALerror();
mSamplesQueued += getBufferSampleCount(bufid);
}
}
mIsFinished = finished;
alSourcePlay(mSource);
mOutput.mStreamThread->add(this); mOutput.mStreamThread->add(this);
} }
@ -325,7 +342,6 @@ void OpenAL_SoundStream::stop()
{ {
mOutput.mStreamThread->remove(this); mOutput.mStreamThread->remove(this);
mIsFinished = true; mIsFinished = true;
mIsInitialBatchEnqueued = false;
alSourceStop(mSource); alSourceStop(mSource);
alSourcei(mSource, AL_BUFFER, 0); alSourcei(mSource, AL_BUFFER, 0);
@ -438,24 +454,6 @@ bool OpenAL_SoundStream::process()
} while(processed > 0); } while(processed > 0);
throwALerror(); throwALerror();
} }
else if (!mIsInitialBatchEnqueued) { // nothing enqueued yet
std::vector<char> data(mBufferSize);
for(ALuint i = 0;i < sNumBuffers && !finished;i++)
{
size_t got = mDecoder->read(&data[0], data.size());
finished = (got < data.size());
if(got > 0)
{
ALuint bufid = mBuffers[i];
alBufferData(bufid, mFormat, &data[0], got, mSampleRate);
alSourceQueueBuffers(mSource, 1, &bufid);
throwALerror();
mSamplesQueued += getBufferSampleCount(bufid);
}
}
mIsInitialBatchEnqueued = true;
}
if(state != AL_PLAYING && state != AL_PAUSED) if(state != AL_PLAYING && state != AL_PAUSED)
{ {
@ -473,7 +471,6 @@ bool OpenAL_SoundStream::process()
std::cout<< "Error updating stream \""<<mDecoder->getName()<<"\"" <<std::endl; std::cout<< "Error updating stream \""<<mDecoder->getName()<<"\"" <<std::endl;
mSamplesQueued = 0; mSamplesQueued = 0;
mIsFinished = true; mIsFinished = true;
mIsInitialBatchEnqueued = false;
} }
return !mIsFinished; return !mIsFinished;
} }