2016-01-15 01:33:22 +00:00
|
|
|
//
|
|
|
|
// AudioQueue.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-06-15 12:07:25 +00:00
|
|
|
#import "CSAudioQueue.h"
|
2016-01-15 01:33:22 +00:00
|
|
|
@import AudioToolbox;
|
|
|
|
|
2016-06-17 01:41:17 +00:00
|
|
|
#define AudioQueueBufferMaxLength 8192
|
2016-10-24 01:17:00 +00:00
|
|
|
#define NumberOfStoredAudioQueueBuffer 16
|
2016-01-18 19:56:20 +00:00
|
|
|
|
2017-02-23 02:12:59 +00:00
|
|
|
static NSLock *CSAudioQueueDeallocLock;
|
|
|
|
|
2017-03-27 00:28:04 +00:00
|
|
|
/*!
|
|
|
|
Holds a weak reference to a CSAudioQueue. Used to work around an apparent AudioQueue bug.
|
|
|
|
See -[CSAudioQueue dealloc].
|
|
|
|
*/
|
|
|
|
@interface CSWeakAudioQueuePointer: NSObject
|
|
|
|
@property(nonatomic, weak) CSAudioQueue *queue;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CSWeakAudioQueuePointer
|
|
|
|
@end
|
|
|
|
|
2016-06-15 12:07:25 +00:00
|
|
|
@implementation CSAudioQueue
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
|
|
|
AudioQueueRef _audioQueue;
|
2016-10-24 01:17:00 +00:00
|
|
|
AudioQueueBufferRef _storedBuffers[NumberOfStoredAudioQueueBuffer];
|
2017-02-23 02:42:10 +00:00
|
|
|
NSLock *_storedBuffersLock;
|
2017-03-27 00:28:04 +00:00
|
|
|
CSWeakAudioQueuePointer *_weakPointer;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 11:30:00 +00:00
|
|
|
#pragma mark - AudioQueue callbacks
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2017-03-11 22:44:02 +00:00
|
|
|
/*!
|
|
|
|
@returns @c YES if the queue is running dry; @c NO otherwise.
|
|
|
|
*/
|
|
|
|
- (BOOL)audioQueue:(AudioQueueRef)theAudioQueue didCallbackWithBuffer:(AudioQueueBufferRef)buffer
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2017-02-23 02:42:10 +00:00
|
|
|
[_storedBuffersLock lock];
|
|
|
|
for(int c = 0; c < NumberOfStoredAudioQueueBuffer; c++)
|
2016-10-24 01:17:00 +00:00
|
|
|
{
|
2017-02-23 02:42:10 +00:00
|
|
|
if(!_storedBuffers[c] || buffer->mAudioDataBytesCapacity > _storedBuffers[c]->mAudioDataBytesCapacity)
|
2016-10-24 01:17:00 +00:00
|
|
|
{
|
2017-02-23 02:42:10 +00:00
|
|
|
if(_storedBuffers[c]) AudioQueueFreeBuffer(_audioQueue, _storedBuffers[c]);
|
|
|
|
_storedBuffers[c] = buffer;
|
|
|
|
[_storedBuffersLock unlock];
|
2017-03-11 22:44:02 +00:00
|
|
|
return YES;
|
2016-10-24 01:17:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 02:42:10 +00:00
|
|
|
[_storedBuffersLock unlock];
|
2016-10-10 11:30:00 +00:00
|
|
|
AudioQueueFreeBuffer(_audioQueue, buffer);
|
2017-03-11 22:44:02 +00:00
|
|
|
return YES;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audioOutputCallback(
|
|
|
|
void *inUserData,
|
|
|
|
AudioQueueRef inAQ,
|
|
|
|
AudioQueueBufferRef inBuffer)
|
|
|
|
{
|
2017-03-11 22:44:02 +00:00
|
|
|
// Pull the delegate call for audio queue running dry outside of the locked region, to allow non-deadlocking
|
|
|
|
// lifecycle -dealloc events to result from it.
|
2017-02-23 02:12:59 +00:00
|
|
|
if([CSAudioQueueDeallocLock tryLock])
|
|
|
|
{
|
2017-03-27 00:28:04 +00:00
|
|
|
CSAudioQueue *queue = ((__bridge CSWeakAudioQueuePointer *)inUserData).queue;
|
2017-03-11 23:17:09 +00:00
|
|
|
BOOL isRunningDry = NO;
|
2017-03-11 22:44:02 +00:00
|
|
|
isRunningDry = [queue audioQueue:inAQ didCallbackWithBuffer:inBuffer];
|
2017-03-11 23:17:09 +00:00
|
|
|
id<CSAudioQueueDelegate> delegate = queue.delegate;
|
2017-02-23 02:12:59 +00:00
|
|
|
[CSAudioQueueDeallocLock unlock];
|
2017-03-11 23:17:09 +00:00
|
|
|
if(isRunningDry) [delegate audioQueueIsRunningDry:queue];
|
2017-02-23 02:12:59 +00:00
|
|
|
}
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 11:30:00 +00:00
|
|
|
#pragma mark - Standard object lifecycle
|
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
- (instancetype)initWithSamplingRate:(Float64)samplingRate
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if(self)
|
|
|
|
{
|
2017-02-23 02:12:59 +00:00
|
|
|
if(!CSAudioQueueDeallocLock)
|
|
|
|
{
|
|
|
|
CSAudioQueueDeallocLock = [[NSLock alloc] init];
|
|
|
|
}
|
2017-02-23 02:42:10 +00:00
|
|
|
_storedBuffersLock = [[NSLock alloc] init];
|
2017-02-23 02:12:59 +00:00
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
_samplingRate = samplingRate;
|
2016-01-18 19:56:20 +00:00
|
|
|
|
2016-10-10 11:30:00 +00:00
|
|
|
// determine preferred buffer sizes
|
|
|
|
_preferredBufferSize = AudioQueueBufferMaxLength;
|
2016-10-10 11:42:24 +00:00
|
|
|
while((Float64)_preferredBufferSize*100.0 > samplingRate) _preferredBufferSize >>= 1;
|
2016-06-17 01:35:22 +00:00
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
/*
|
2016-06-15 11:35:34 +00:00
|
|
|
Describe a mono 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;
|
2016-01-15 01:33:22 +00:00
|
|
|
|
|
|
|
outputDescription.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
outputDescription.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
|
|
|
|
outputDescription.mBytesPerPacket = 2;
|
|
|
|
outputDescription.mFramesPerPacket = 1;
|
|
|
|
outputDescription.mBytesPerFrame = 2;
|
|
|
|
outputDescription.mChannelsPerFrame = 1;
|
|
|
|
outputDescription.mBitsPerChannel = 16;
|
|
|
|
|
|
|
|
outputDescription.mReserved = 0;
|
|
|
|
|
2017-03-27 00:28:04 +00:00
|
|
|
// create an audio output queue along those lines; see -dealloc re: the CSWeakAudioQueuePointer
|
|
|
|
_weakPointer = [[CSWeakAudioQueuePointer alloc] init];
|
|
|
|
_weakPointer.queue = self;
|
2016-01-15 01:33:22 +00:00
|
|
|
if(!AudioQueueNewOutput(
|
2016-06-01 23:04:07 +00:00
|
|
|
&outputDescription,
|
|
|
|
audioOutputCallback,
|
2017-03-27 00:28:04 +00:00
|
|
|
(__bridge void *)(_weakPointer),
|
2016-06-01 23:04:07 +00:00
|
|
|
NULL,
|
|
|
|
kCFRunLoopCommonModes,
|
|
|
|
0,
|
|
|
|
&_audioQueue))
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
|
|
|
AudioQueueStart(_audioQueue, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
return [self initWithSamplingRate:[[self class] preferredSamplingRate]];
|
|
|
|
}
|
|
|
|
|
2016-01-15 01:35:36 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2017-02-23 02:12:59 +00:00
|
|
|
[CSAudioQueueDeallocLock lock];
|
2017-03-27 00:28:04 +00:00
|
|
|
if(_audioQueue)
|
|
|
|
{
|
|
|
|
AudioQueueDispose(_audioQueue, true);
|
|
|
|
_audioQueue = NULL;
|
|
|
|
}
|
2017-02-23 02:12:59 +00:00
|
|
|
[CSAudioQueueDeallocLock unlock];
|
2017-03-27 00:28:04 +00:00
|
|
|
|
|
|
|
// Yuck. Horrid hack happening here. At least under macOS v10.12, I am frequently seeing calls to
|
|
|
|
// my registered audio callback (audioOutputCallback in this case) that occur **after** the call
|
|
|
|
// to AudioQueueDispose above, even though the second parameter there asks for a synchronous shutdown.
|
|
|
|
// So this appears to be a bug on Apple's side.
|
|
|
|
//
|
|
|
|
// Since the audio callback receives a void * pointer that identifies the class it should branch into,
|
|
|
|
// it's therefore unsafe to pass 'self'. Instead I pass a CSWeakAudioQueuePointer which points to the actual
|
|
|
|
// queue. The lifetime of that class is the lifetime of this instance plus 1 second, as effected by the
|
|
|
|
// artificial dispatch_after below — it serves only to keep pointerSaviour alive for an extra second.
|
|
|
|
//
|
|
|
|
// Why a second? That's definitely quite a lot longer than any amount of audio that may be queued. So
|
|
|
|
// probably safe. As and where Apple's audio queue works properly, CSAudioQueueDeallocLock should provide
|
|
|
|
// absolute safety; elsewhere the CSWeakAudioQueuePointer provides probabilistic.
|
|
|
|
CSWeakAudioQueuePointer *pointerSaviour = _weakPointer;
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
|
|
[pointerSaviour hash];
|
|
|
|
});
|
2016-01-15 01:35:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 11:30:00 +00:00
|
|
|
#pragma mark - Audio enqueuer
|
|
|
|
|
2016-01-18 19:50:19 +00:00
|
|
|
- (void)enqueueAudioBuffer:(const int16_t *)buffer numberOfSamples:(size_t)lengthInSamples
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-10-10 11:30:00 +00:00
|
|
|
size_t bufferBytes = lengthInSamples * sizeof(int16_t);
|
2016-01-18 19:50:19 +00:00
|
|
|
|
2017-02-23 02:42:10 +00:00
|
|
|
[_storedBuffersLock lock];
|
|
|
|
for(int c = 0; c < NumberOfStoredAudioQueueBuffer; c++)
|
2016-10-24 01:17:00 +00:00
|
|
|
{
|
2017-02-23 02:42:10 +00:00
|
|
|
if(_storedBuffers[c] && _storedBuffers[c]->mAudioDataBytesCapacity >= bufferBytes)
|
2016-10-24 01:17:00 +00:00
|
|
|
{
|
2017-02-23 02:42:10 +00:00
|
|
|
memcpy(_storedBuffers[c]->mAudioData, buffer, bufferBytes);
|
|
|
|
_storedBuffers[c]->mAudioDataByteSize = (UInt32)bufferBytes;
|
|
|
|
|
|
|
|
AudioQueueEnqueueBuffer(_audioQueue, _storedBuffers[c], 0, NULL);
|
|
|
|
_storedBuffers[c] = NULL;
|
|
|
|
[_storedBuffersLock unlock];
|
|
|
|
return;
|
2016-10-24 01:17:00 +00:00
|
|
|
}
|
2017-02-23 02:42:10 +00:00
|
|
|
}
|
|
|
|
[_storedBuffersLock unlock];
|
2016-10-10 11:30:00 +00:00
|
|
|
|
2017-02-23 02:42:10 +00:00
|
|
|
AudioQueueBufferRef newBuffer;
|
|
|
|
AudioQueueAllocateBuffer(_audioQueue, (UInt32)bufferBytes * 2, &newBuffer);
|
|
|
|
memcpy(newBuffer->mAudioData, buffer, bufferBytes);
|
|
|
|
newBuffer->mAudioDataByteSize = (UInt32)bufferBytes;
|
2016-10-24 01:17:00 +00:00
|
|
|
|
2017-02-23 02:42:10 +00:00
|
|
|
AudioQueueEnqueueBuffer(_audioQueue, newBuffer, 0, NULL);
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 11:35:34 +00:00
|
|
|
#pragma mark - Sampling Rate getters
|
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
+ (AudioDeviceID)defaultOutputDevice
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
+ (Float64)preferredSamplingRate
|
|
|
|
{
|
|
|
|
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
|