+ ALint finished = 0;
+
+ alGetSourcei(mSource, AL_BUFFERS_PROCESSED, &finished);
+
+ while (finished-- > 0)
+ {
+ ALuint bufferObj;
+ alSourceUnqueueBuffers(mSource, 1, &bufferObj);
+
+ BufferP buffer = mQueue.front();
+ bool streamed = buffer->stream(bufferObj);
+
+ if (streamed)
+ {
+ alSourceQueueBuffers(mSource, 1, &bufferObj);
+ }
+ else
+ {
+ // the buffer couldn't be streamed, so get rid of it
+ mQueue.pop_front();
+
+ if (!mQueue.empty())
+ {
+ // begin the next buffer in the queue
+ mQueue.front()->rewind();
+ mQueue.front()->stream(bufferObj);
+ alSourceQueueBuffers(mSource, 1, &bufferObj);
+ logInfo("loading new buffer");
+
+ // queue up any unused buffers
+ bufferStream();
+ }
+ else if (mIsLooping)
+ {
+ // reload the same buffer
+ mQueue.push_back(buffer);
+ buffer->rewind();
+ buffer->stream(bufferObj);
+ alSourceQueueBuffers(mSource, 1, &bufferObj);
+ logInfo("looping same buffer");
+ }
+ else
+ {
+ // nothing more to play, stopping...
+ mIsPlaying = false;
+ std::remove(mBuffers.begin(), mBuffers.end(), bufferObj);
+ }
+ }
+ }
+
+ ALenum state;
+ alGetSourcei(mSource, AL_SOURCE_STATE, &state);
+
+ // restart playing if we're stopped but supposed to be playing... this
+ // means we didn't queue enough and the audio skipped :-(
+ if (mIsPlaying && state != AL_PLAYING)
+ {
+ alSourcePlay(mSource);
+ }