From f6b6ec7009a6498207f3068a06108961f7fd0ca8 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 17 Oct 2016 08:18:32 -0400 Subject: [PATCH] Cemented new meaningof the audio queue delegate callout. --- OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.h | 3 +-- OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m | 6 ++++-- OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.h b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.h index 106454f99..5bdf71d4a 100644 --- a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.h +++ b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.h @@ -11,8 +11,7 @@ @class CSAudioQueue; @protocol CSAudioQueueDelegate -- (void)audioQueueDidCompleteBuffer:(nonnull CSAudioQueue *)audioQueue; // TODO: rename this to audioQueueNeedsData or something, to indicate that - // it means something more along the lines of 'may run out soon' +- (void)audioQueueIsRunningDry:(nonnull CSAudioQueue *)audioQueue; @end /*! diff --git a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m index 4771ae381..2e1ef29e1 100644 --- a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m +++ b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m @@ -21,8 +21,10 @@ - (void)audioQueue:(AudioQueueRef)theAudioQueue didCallbackWithBuffer:(AudioQueueBufferRef)buffer { - _queuedSamples -= (size_t)(buffer->mAudioDataByteSize / sizeof(int16_t)); - if(_queuedSamples < 128) [self.delegate audioQueueDidCompleteBuffer:self]; + size_t samplesInBuffer = (size_t)(buffer->mAudioDataByteSize / sizeof(int16_t)); + if(_queuedSamples >= 128 && _queuedSamples - samplesInBuffer < 128) [self.delegate audioQueueIsRunningDry:self]; + _queuedSamples -= samplesInBuffer; + AudioQueueFreeBuffer(_audioQueue, buffer); } diff --git a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift index 028372e2e..8f54d6884 100644 --- a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift @@ -148,7 +148,7 @@ class MachineDocument: } // MARK: CSAudioQueueDelegate - final func audioQueueDidCompleteBuffer(_ audioQueue: CSAudioQueue) { + final func audioQueueIsRunningDry(_ audioQueue: CSAudioQueue) { bestEffortUpdater.update() }