My code opens the ogg-file and reads from it whenever a buffer is to be filled.
As a "simple" loop would cost me 100% CPU, I have to sleep a little bit somewhere.
So I set up 4 buffers which will be queued and played one after the other.
After one processed buffer is filled again and queued on the source, I sleep as long as 1 buffer should need to be played. That is (float)PCM_PER_BUFFER/44100*1000 ms.
But it can still happen that the stream stops playing (e.g. when computer is busy I think), which means that all 4 queued buffers were played before new buffers are filled and queued.
But how can that be? I can't imagine that some of my code in the thread takes as long as 3 or 4 buffers.
Does anybody has a tip how I might solve that problem? Is it possible to use callbacks that OpenAL automatically calls as a buffer on a source was processed?
My code looks about like that (pseudo-code):
while(playing){ n = getProcessedBuffers(); // should be 0, 1 or maybe 2. Never 3 or 4... while(n--) // while there are processed buffers to fill { // unqueue processed buffer alSourceUnqueueBuffers(Source, 1, &b); cur = getCurrentBuffer(); // buffer num (0-3) to fill fillBuffer(Buffer[cur]); alSourceQueueBuffers(Source, 1, &(Buffer[cur])); } sleep(sleepTime);}