1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-02-16 18:30:32 +00:00

Merge pull request #1066 from TomHarte/AudioAssert

macOS: Fix stereo buffering, various audio asserts.
This commit is contained in:
Thomas Harte 2022-07-15 14:44:55 -04:00 committed by GitHub
commit a7515fe156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 8 deletions

View File

@ -30,6 +30,8 @@
atomic_int _enqueuedBuffers; atomic_int _enqueuedBuffers;
AudioQueueBufferRef _buffers[NumBuffers]; AudioQueueBufferRef _buffers[NumBuffers];
int _bufferWritePointer; int _bufferWritePointer;
unsigned int _numChannels;
} }
#pragma mark - Status #pragma mark - Status
@ -49,6 +51,7 @@
atomic_store_explicit(&_enqueuedBuffers, 0, memory_order_relaxed); atomic_store_explicit(&_enqueuedBuffers, 0, memory_order_relaxed);
_samplingRate = samplingRate; _samplingRate = samplingRate;
_numChannels = isStereo ? 2 : 1;
// Determine preferred buffer size as being the first power of two // Determine preferred buffer size as being the first power of two
// not less than 1/100th of a second. // not less than 1/100th of a second.
@ -138,20 +141,20 @@
// Allocate future audio buffers. // Allocate future audio buffers.
[_queueLock lock]; [_queueLock lock];
const size_t bufferBytes = self.bufferSize * sizeof(int16_t); const size_t bufferBytes = self.bufferSize * sizeof(int16_t) * _numChannels;
for(size_t c = 0; c < NumBuffers; c++) { for(size_t c = 0; c < NumBuffers; c++) {
if(_buffers[c]) { if(_buffers[c]) {
OSSGuard(AudioQueueFreeBuffer(_audioQueue, _buffers[c])); OSSGuard(AudioQueueFreeBuffer(_audioQueue, _buffers[c]));
} }
OSSGuard(AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &_buffers[c])); OSSGuard(AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes, &_buffers[c]));
_buffers[c]->mAudioDataByteSize = (UInt32)bufferBytes; _buffers[c]->mAudioDataByteSize = (UInt32)bufferBytes;
} }
[_queueLock unlock]; [_queueLock unlock];
} }
- (void)enqueueAudioBuffer:(const int16_t *)buffer { - (void)enqueueAudioBuffer:(const int16_t *)buffer {
const size_t bufferBytes = self.bufferSize * sizeof(int16_t); const size_t bufferBytes = self.bufferSize * sizeof(int16_t) * _numChannels;
// Don't enqueue more than the allowed number of future buffers, // Don't enqueue more than the allowed number of future buffers,
// to ensure not too much latency accrues. // to ensure not too much latency accrues.

View File

@ -176,7 +176,7 @@ struct ActivityObserver: public Activity::Observer {
} }
- (void)speaker:(Outputs::Speaker::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length { - (void)speaker:(Outputs::Speaker::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length {
assert(NSUInteger(length) == self.audioQueue.bufferSize); assert(NSUInteger(length) == self.audioQueue.bufferSize*(speaker->get_is_stereo() ? 2 : 1));
[self.audioQueue enqueueAudioBuffer:samples]; [self.audioQueue enqueueAudioBuffer:samples];
} }

View File

@ -239,9 +239,9 @@ template <typename ConcreteT, bool is_stereo> class LowpassBase: public Speaker
} }
protected: protected:
void process(size_t length) { bool process(size_t length) {
const auto delegate = delegate_.load(std::memory_order::memory_order_relaxed); const auto delegate = delegate_.load(std::memory_order::memory_order_relaxed);
if(!delegate) return; if(!delegate) return false;
const int scale = static_cast<ConcreteT *>(this)->get_scale(); const int scale = static_cast<ConcreteT *>(this)->get_scale();
@ -286,6 +286,8 @@ template <typename ConcreteT, bool is_stereo> class LowpassBase: public Speaker
// TODO: input rate is less than output rate. // TODO: input rate is less than output rate.
break; break;
} }
return true;
} }
}; };
@ -334,8 +336,11 @@ template <bool is_stereo> class PushLowpass: public LowpassBase<PushLowpass<is_s
*/ */
void push(const int16_t *buffer, size_t length) { void push(const int16_t *buffer, size_t length) {
buffer_ = buffer; buffer_ = buffer;
process(length); #ifndef NDEBUG
assert(buffer_ == buffer + (length * (1 + is_stereo))); const bool did_process =
#endif
process(length);
assert(!did_process || buffer_ == buffer + (length * (1 + is_stereo)));
} }
}; };