0

I have 3 audioQueueBufferRef to fill my audioqueue and everyting works nice when data comes fast enough. But sometimes the outputCallback ask to fill a buffer when there is no more data. In this case i don't enqueue something and the callback is no more called. (normal ?)

After a this i run with 2 buffers and later (after another lag) 1 buffer and at the very last no more buffers have callbacks resulting in no sound.

I have try to store the empty audioQueueBufferRef in a array and call them when i have data to fill. But since data don't comes fast enough, the buffer just "eat" my low amount of data that remain and the sound is laggy.

What is the best way to go when running low on data to fill ?

The best i have foun is to let the buffer just dead without callback and let the remaining buffer do the job until every buffer are empty. After this i stop the audioQueue, store some data and play again. Notice that i have to call AudioqueueStop() because when every buffer are empty and i refill them, no sound is comming out. Is this normal ?

In general does i do it the good way or is there a better approach ? And is there a callback that detect that all my audioqueueBufferRef are dead (without callback running) ? 2.

Kamax
  • 212
  • 2
  • 14

2 Answers2

0

I tried this in the playroutine to test a solution for you:

cnt++;
if (cnt > 5) {
    inBuffer->mAudioDataByteSize = 4;

    AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
    cnt = 0;
    return;
}

It simulates that data arrives too late every 5th time AudioQueue asks for a buffer. Note that I sat the mAudioDataByteSize to size 4, which is the SMALLEST sample size for a 2 channel 16 bit wave file. If you set the size to zero, the queue will die and return the error message 'kAudioQueueErr_BufferEmpty', so you do need to feed data, although you don't have any to feed. If you think about it, it makes sense.

Maybe you could even adapt the code to estimate how much longer you need to wait to get data, let us say you will get more in about 2ms. Then you could just set the buffer size to the number of samples a tad less than that time. If you are playing a 2 channel 16 bit 44100 Hz wave song, 2ms would amount to a buffer size of 44100 Hz * 0.002 seconds = 88, 88 * 2 channels * (16 bits / 8) = 353 bytes.

RoyGal
  • 176
  • 7
0

I think the callback is called in a frequency you set , If you can't fill the data in time, the sound is abnormal. Does your data is from on network? I have met the same condition as you and my solution is to record one packet data which is silence and when I do not have audio data to fill in, I use silence data instead.

KudoCC
  • 6,912
  • 1
  • 24
  • 53