2016-01-15 01:33:22 +00:00
|
|
|
//
|
|
|
|
// AudioQueue.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/01/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-01-15 01:33:22 +00:00
|
|
|
//
|
|
|
|
|
2016-06-15 12:07:25 +00:00
|
|
|
#import "CSAudioQueue.h"
|
2016-01-15 01:33:22 +00:00
|
|
|
@import AudioToolbox;
|
2022-07-09 17:33:46 +00:00
|
|
|
#include <stdatomic.h>
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2022-07-13 01:43:33 +00:00
|
|
|
#define OSSGuard(x) { \
|
|
|
|
const OSStatus status = x; \
|
|
|
|
assert(!status); \
|
|
|
|
(void)status; \
|
|
|
|
}
|
|
|
|
|
2022-07-14 02:22:19 +00:00
|
|
|
#define IsDry(x) (x) < 2
|
2022-07-13 01:43:33 +00:00
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
#define MaximumBacklog 4
|
|
|
|
#define NumBuffers (MaximumBacklog + 1)
|
|
|
|
|
2018-02-12 01:32:59 +00:00
|
|
|
@implementation CSAudioQueue {
|
2016-01-15 01:33:22 +00:00
|
|
|
AudioQueueRef _audioQueue;
|
2022-07-14 18:44:10 +00:00
|
|
|
|
2022-07-12 20:22:19 +00:00
|
|
|
NSLock *_deallocLock;
|
2022-07-13 01:43:33 +00:00
|
|
|
NSLock *_queueLock;
|
2022-07-14 18:44:10 +00:00
|
|
|
|
2022-07-09 17:33:46 +00:00
|
|
|
atomic_int _enqueuedBuffers;
|
2022-07-14 18:44:10 +00:00
|
|
|
AudioQueueBufferRef _buffers[NumBuffers];
|
|
|
|
int _bufferWritePointer;
|
2022-07-15 01:59:40 +00:00
|
|
|
|
|
|
|
unsigned int _numChannels;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:43:33 +00:00
|
|
|
#pragma mark - Status
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2022-07-09 17:33:46 +00:00
|
|
|
- (BOOL)isRunningDry {
|
2022-07-13 01:43:33 +00:00
|
|
|
return IsDry(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed));
|
2022-07-09 17:33:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:43:33 +00:00
|
|
|
#pragma mark - Object lifecycle
|
2016-10-10 11:30:00 +00:00
|
|
|
|
2020-02-16 19:05:50 +00:00
|
|
|
- (instancetype)initWithSamplingRate:(Float64)samplingRate isStereo:(BOOL)isStereo {
|
2016-01-15 01:33:22 +00:00
|
|
|
self = [super init];
|
|
|
|
|
2018-02-12 01:32:59 +00:00
|
|
|
if(self) {
|
2022-07-12 19:03:35 +00:00
|
|
|
_deallocLock = [[NSLock alloc] init];
|
2022-07-15 19:21:58 +00:00
|
|
|
_deallocLock.name = @"Dealloc lock";
|
|
|
|
|
2022-07-13 01:43:33 +00:00
|
|
|
_queueLock = [[NSLock alloc] init];
|
2022-07-15 19:21:58 +00:00
|
|
|
_queueLock.name = @"Audio queue access lock";
|
|
|
|
|
2022-07-13 01:43:33 +00:00
|
|
|
atomic_store_explicit(&_enqueuedBuffers, 0, memory_order_relaxed);
|
2017-02-23 02:12:59 +00:00
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
_samplingRate = samplingRate;
|
2022-07-15 01:59:40 +00:00
|
|
|
_numChannels = isStereo ? 2 : 1;
|
2016-01-18 19:56:20 +00:00
|
|
|
|
2022-07-13 19:24:43 +00:00
|
|
|
// Determine preferred buffer size as being the first power of two
|
|
|
|
// not less than 1/100th of a second.
|
|
|
|
_preferredBufferSize = 1;
|
|
|
|
const NSUInteger oneHundredthOfRate = (NSUInteger)(samplingRate / 100.0);
|
|
|
|
while(_preferredBufferSize < oneHundredthOfRate) _preferredBufferSize <<= 1;
|
2016-06-17 01:35:22 +00:00
|
|
|
|
2022-07-12 19:58:16 +00:00
|
|
|
// Describe a 16bit stream of the requested sampling rate.
|
2016-01-15 01:33:22 +00:00
|
|
|
AudioStreamBasicDescription outputDescription;
|
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
outputDescription.mSampleRate = samplingRate;
|
2022-07-12 19:58:16 +00:00
|
|
|
outputDescription.mChannelsPerFrame = isStereo ? 2 : 1;
|
2016-01-15 01:33:22 +00:00
|
|
|
|
|
|
|
outputDescription.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
outputDescription.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
|
|
|
|
outputDescription.mFramesPerPacket = 1;
|
2020-02-16 19:14:10 +00:00
|
|
|
outputDescription.mBytesPerFrame = 2 * outputDescription.mChannelsPerFrame;
|
|
|
|
outputDescription.mBytesPerPacket = outputDescription.mBytesPerFrame * outputDescription.mFramesPerPacket;
|
2016-01-15 01:33:22 +00:00
|
|
|
outputDescription.mBitsPerChannel = 16;
|
|
|
|
|
|
|
|
outputDescription.mReserved = 0;
|
|
|
|
|
2022-07-12 19:58:16 +00:00
|
|
|
// Create an audio output queue along those lines.
|
2022-07-12 19:03:35 +00:00
|
|
|
__weak CSAudioQueue *weakSelf = self;
|
|
|
|
if(AudioQueueNewOutputWithDispatchQueue(
|
|
|
|
&_audioQueue,
|
2016-06-01 23:04:07 +00:00
|
|
|
&outputDescription,
|
|
|
|
0,
|
2022-07-12 19:03:35 +00:00
|
|
|
dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
|
|
|
|
^(AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
|
2022-07-14 18:44:10 +00:00
|
|
|
(void)inBuffer;
|
|
|
|
|
2022-07-12 19:03:35 +00:00
|
|
|
CSAudioQueue *queue = weakSelf;
|
|
|
|
if(!queue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if([queue->_deallocLock tryLock]) {
|
2022-07-13 01:43:33 +00:00
|
|
|
const int buffers = atomic_fetch_add(&queue->_enqueuedBuffers, -1) - 1;
|
2022-07-14 02:22:19 +00:00
|
|
|
if(!buffers) {
|
2022-07-14 18:44:10 +00:00
|
|
|
[queue->_queueLock lock];
|
|
|
|
OSSGuard(AudioQueuePause(inAQ));
|
|
|
|
[queue->_queueLock unlock];
|
2022-07-14 02:22:19 +00:00
|
|
|
}
|
2022-07-12 19:03:35 +00:00
|
|
|
|
|
|
|
id<CSAudioQueueDelegate> delegate = queue.delegate;
|
|
|
|
[queue->_deallocLock unlock];
|
|
|
|
|
2022-07-13 01:43:33 +00:00
|
|
|
if(IsDry(buffers)) [delegate audioQueueIsRunningDry:queue];
|
2022-07-12 19:03:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return nil;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-02-12 01:32:59 +00:00
|
|
|
- (void)dealloc {
|
2022-07-12 19:03:35 +00:00
|
|
|
[_deallocLock lock];
|
2022-07-15 19:21:58 +00:00
|
|
|
// Ensure no buffers remain enqueued by stopping the queue.
|
|
|
|
if(_audioQueue) {
|
|
|
|
OSSGuard(AudioQueueStop(_audioQueue, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free all buffers.
|
2022-07-14 18:44:10 +00:00
|
|
|
for(size_t c = 0; c < NumBuffers; c++) {
|
|
|
|
if(_buffers[c]) {
|
|
|
|
OSSGuard(AudioQueueFreeBuffer(_audioQueue, _buffers[c]));
|
|
|
|
_buffers[c] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 19:03:35 +00:00
|
|
|
|
2022-07-15 19:21:58 +00:00
|
|
|
// Dispose of the queue.
|
2022-07-15 19:13:21 +00:00
|
|
|
if(_audioQueue) {
|
|
|
|
OSSGuard(AudioQueueDispose(_audioQueue, true));
|
|
|
|
_audioQueue = NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
// nil out the dealloc lock before entering the critical section such
|
|
|
|
// that it becomes impossible for anyone else to acquire.
|
|
|
|
NSLock *deallocLock = _deallocLock;
|
|
|
|
_deallocLock = nil;
|
2022-07-12 19:03:35 +00:00
|
|
|
[deallocLock unlock];
|
2016-01-15 01:35:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 11:30:00 +00:00
|
|
|
#pragma mark - Audio enqueuer
|
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
- (void)setBufferSize:(NSUInteger)bufferSize {
|
|
|
|
_bufferSize = bufferSize;
|
|
|
|
|
|
|
|
// Allocate future audio buffers.
|
|
|
|
[_queueLock lock];
|
2022-07-15 01:59:40 +00:00
|
|
|
const size_t bufferBytes = self.bufferSize * sizeof(int16_t) * _numChannels;
|
2022-07-14 18:44:10 +00:00
|
|
|
for(size_t c = 0; c < NumBuffers; c++) {
|
|
|
|
if(_buffers[c]) {
|
|
|
|
OSSGuard(AudioQueueFreeBuffer(_audioQueue, _buffers[c]));
|
|
|
|
}
|
|
|
|
|
2022-07-15 01:59:40 +00:00
|
|
|
OSSGuard(AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes, &_buffers[c]));
|
2022-07-14 18:44:10 +00:00
|
|
|
_buffers[c]->mAudioDataByteSize = (UInt32)bufferBytes;
|
|
|
|
}
|
|
|
|
[_queueLock unlock];
|
|
|
|
}
|
|
|
|
|
2022-07-14 18:34:11 +00:00
|
|
|
- (void)enqueueAudioBuffer:(const int16_t *)buffer {
|
2022-07-15 01:59:40 +00:00
|
|
|
const size_t bufferBytes = self.bufferSize * sizeof(int16_t) * _numChannels;
|
2016-01-18 19:50:19 +00:00
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
// Don't enqueue more than the allowed number of future buffers,
|
|
|
|
// to ensure not too much latency accrues.
|
|
|
|
if(atomic_load_explicit(&_enqueuedBuffers, memory_order_relaxed) == MaximumBacklog) {
|
2018-05-16 01:12:43 +00:00
|
|
|
return;
|
2017-02-23 02:42:10 +00:00
|
|
|
}
|
2022-07-13 01:43:33 +00:00
|
|
|
const int enqueuedBuffers = atomic_fetch_add(&_enqueuedBuffers, 1) + 1;
|
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
const int targetBuffer = _bufferWritePointer;
|
|
|
|
_bufferWritePointer = (_bufferWritePointer + 1) % NumBuffers;
|
|
|
|
memcpy(_buffers[targetBuffer]->mAudioData, buffer, bufferBytes);
|
2020-02-09 03:08:27 +00:00
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
[_queueLock lock];
|
|
|
|
OSSGuard(AudioQueueEnqueueBuffer(_audioQueue, _buffers[targetBuffer], 0, NULL));
|
2022-07-13 01:43:33 +00:00
|
|
|
|
2022-07-14 18:44:10 +00:00
|
|
|
// Starting is a no-op if the queue is already playing, but it may not have been started
|
|
|
|
// yet, or may have been paused due to a pipeline failure if the producer is running slowly.
|
|
|
|
if(enqueuedBuffers > 1) {
|
|
|
|
OSSGuard(AudioQueueStart(_audioQueue, NULL));
|
|
|
|
}
|
2022-07-13 01:43:33 +00:00
|
|
|
[_queueLock unlock];
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 11:35:34 +00:00
|
|
|
#pragma mark - Sampling Rate getters
|
|
|
|
|
2018-02-12 01:32:59 +00:00
|
|
|
+ (AudioDeviceID)defaultOutputDevice {
|
2016-06-01 23:04:07 +00:00
|
|
|
AudioObjectPropertyAddress address;
|
|
|
|
address.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
|
|
|
address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
AudioDeviceID deviceID;
|
|
|
|
UInt32 size = sizeof(AudioDeviceID);
|
2016-06-15 11:35:34 +00:00
|
|
|
return AudioObjectGetPropertyData(kAudioObjectSystemObject, &address, sizeof(AudioObjectPropertyAddress), NULL, &size, &deviceID) ? 0 : deviceID;
|
2016-06-01 23:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 01:32:59 +00:00
|
|
|
+ (Float64)preferredSamplingRate {
|
2016-06-01 23:04:07 +00:00
|
|
|
AudioObjectPropertyAddress address;
|
|
|
|
address.mSelector = kAudioDevicePropertyNominalSampleRate;
|
|
|
|
address.mScope = kAudioObjectPropertyScopeGlobal;
|
|
|
|
address.mElement = kAudioObjectPropertyElementMaster;
|
|
|
|
|
|
|
|
Float64 samplingRate;
|
|
|
|
UInt32 size = sizeof(Float64);
|
2016-06-15 11:35:34 +00:00
|
|
|
return AudioObjectGetPropertyData([self defaultOutputDevice], &address, sizeof(AudioObjectPropertyAddress), NULL, &size, &samplingRate) ? 0.0 : samplingRate;
|
2016-06-01 23:04:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
@end
|