Skip to content

Commit

Permalink
#1041 in progress: decode first sample batch right in OpenAL_SoundStr…
Browse files Browse the repository at this point in the history
…eam::play()
  • Loading branch information
nikolaykasyanov committed Feb 16, 2014
1 parent 5e8cb2e commit 51fb9f6
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions apps/openmw/mwsound/openal_output.cpp
Expand Up @@ -172,7 +172,6 @@ class OpenAL_SoundStream : public Sound
DecoderPtr mDecoder;

volatile bool mIsFinished;
volatile bool mIsInitialBatchEnqueued;

void updateAll(bool local);

Expand Down Expand Up @@ -265,7 +264,7 @@ struct OpenAL_Output::StreamThread {

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)
, mOutput(output), mSource(src), mSamplesQueued(0), mDecoder(decoder), mIsFinished(true), mIsInitialBatchEnqueued(false)
, mOutput(output), mSource(src), mSamplesQueued(0), mDecoder(decoder), mIsFinished(true)
{
throwALerror();

Expand Down Expand Up @@ -316,16 +315,33 @@ void OpenAL_SoundStream::play()
alSourcei(mSource, AL_BUFFER, 0);
throwALerror();
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);
}

void OpenAL_SoundStream::stop()
{
mOutput.mStreamThread->remove(this);
mIsFinished = true;
mIsInitialBatchEnqueued = false;

alSourceStop(mSource);
alSourcei(mSource, AL_BUFFER, 0);
Expand Down Expand Up @@ -438,24 +454,6 @@ bool OpenAL_SoundStream::process()
} while(processed > 0);
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)
{
Expand All @@ -473,7 +471,6 @@ bool OpenAL_SoundStream::process()
std::cout<< "Error updating stream \""<<mDecoder->getName()<<"\"" <<std::endl;
mSamplesQueued = 0;
mIsFinished = true;
mIsInitialBatchEnqueued = false;
}
return !mIsFinished;
}
Expand Down

0 comments on commit 51fb9f6

Please sign in to comment.