1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-02-06 13:31:55 +00:00

Do a better job of keeping the queue populated.

This commit is contained in:
Thomas Harte 2022-07-11 20:50:02 -04:00
parent f2fb9cf596
commit a1544f3033

View File

@ -42,10 +42,10 @@ static NSLock *CSAudioQueueDeallocLock;
[_storedBuffersLock lock]; [_storedBuffersLock lock];
const int buffers = atomic_fetch_add(&_enqueuedBuffers, -1); const int buffers = atomic_fetch_add(&_enqueuedBuffers, -1);
// If that leaves nothing in the queue, re-enqueue whatever just came back in order to keep the // If that suggests the queue may be exhausted soon, re-enqueue whatever just came back in order to
// queue going. AudioQueues seem to stop playing and never restart no matter how much encouragement // keep the queue going. AudioQueues seem to stop playing and never restart no matter how much
// if exhausted. // encouragement if exhausted.
if(!buffers) { if(buffers == 1) {
AudioQueueEnqueueBuffer(theAudioQueue, buffer, 0, NULL); AudioQueueEnqueueBuffer(theAudioQueue, buffer, 0, NULL);
atomic_fetch_add(&_enqueuedBuffers, 1); atomic_fetch_add(&_enqueuedBuffers, 1);
} else { } else {
@ -73,7 +73,7 @@ static void audioOutputCallback(
} }
- (BOOL)isRunningDry { - (BOOL)isRunningDry {
return atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) < 2; return atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) < 3;
} }
#pragma mark - Standard object lifecycle #pragma mark - Standard object lifecycle
@ -167,7 +167,7 @@ static void audioOutputCallback(
[_storedBuffersLock unlock]; [_storedBuffersLock unlock];
return; return;
} }
atomic_fetch_add(&_enqueuedBuffers, 1); const int enqueuedBuffers = atomic_fetch_add(&_enqueuedBuffers, 1);
AudioQueueBufferRef newBuffer; AudioQueueBufferRef newBuffer;
AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer); AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer);
@ -179,7 +179,9 @@ static void audioOutputCallback(
// 'Start' the queue. This is documented to be a no-op if the queue is already started, // 'Start' the queue. This is documented to be a no-op if the queue is already started,
// and it's better to defer starting it until at least some data is available. // and it's better to defer starting it until at least some data is available.
AudioQueueStart(_audioQueue, NULL); if(enqueuedBuffers > 3) {
AudioQueueStart(_audioQueue, NULL);
}
} }
#pragma mark - Sampling Rate getters #pragma mark - Sampling Rate getters