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-06-17 01:35:22 +00:00
|
|
|
#define AudioQueueNumAudioBuffers 3
|
|
|
|
#define AudioQueueMaxStreamLength (AudioQueueBufferMaxLength*AudioQueueNumAudioBuffers)
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
enum {
|
2016-02-17 02:47:23 +00:00
|
|
|
AudioQueueCanProceed,
|
|
|
|
AudioQueueWait,
|
|
|
|
AudioQueueIsInvalidated
|
2016-01-18 19:56:20 +00:00
|
|
|
};
|
|
|
|
|
2016-06-15 12:07:25 +00:00
|
|
|
@implementation CSAudioQueue
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
NSUInteger _bufferLength;
|
|
|
|
NSUInteger _streamLength;
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
AudioQueueRef _audioQueue;
|
|
|
|
AudioQueueBufferRef _audioBuffers[AudioQueueNumAudioBuffers];
|
2016-06-17 01:35:22 +00:00
|
|
|
|
2016-01-18 19:50:19 +00:00
|
|
|
unsigned int _audioStreamReadPosition, _audioStreamWritePosition;
|
2016-06-17 01:35:22 +00:00
|
|
|
int16_t _audioStream[AudioQueueMaxStreamLength];
|
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
NSConditionLock *_writeLock;
|
2016-02-17 02:47:23 +00:00
|
|
|
BOOL _isInvalidated;
|
|
|
|
int _dequeuedCount;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark AudioQueue callbacks and setup; for pushing audio out
|
|
|
|
|
|
|
|
- (void)audioQueue:(AudioQueueRef)theAudioQueue didCallbackWithBuffer:(AudioQueueBufferRef)buffer
|
|
|
|
{
|
2016-06-15 11:35:34 +00:00
|
|
|
[self.delegate audioQueueDidCompleteBuffer:self];
|
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
[_writeLock lock];
|
|
|
|
|
|
|
|
const unsigned int writeLead = _audioStreamWritePosition - _audioStreamReadPosition;
|
|
|
|
const size_t audioDataSampleSize = buffer->mAudioDataByteSize / sizeof(int16_t);
|
2016-01-18 21:46:41 +00:00
|
|
|
|
|
|
|
// TODO: if write lead is too great, skip some audio
|
|
|
|
if(writeLead >= audioDataSampleSize)
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
size_t samplesBeforeOverflow = _streamLength - (_audioStreamReadPosition % _streamLength);
|
2016-01-18 19:56:20 +00:00
|
|
|
if(audioDataSampleSize <= samplesBeforeOverflow)
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
memcpy(buffer->mAudioData, &_audioStream[_audioStreamReadPosition % _streamLength], buffer->mAudioDataByteSize);
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-18 19:56:20 +00:00
|
|
|
const size_t bytesRemaining = samplesBeforeOverflow * sizeof(int16_t);
|
2016-06-17 01:35:22 +00:00
|
|
|
memcpy(buffer->mAudioData, &_audioStream[_audioStreamReadPosition % _streamLength], bytesRemaining);
|
2016-01-18 19:56:20 +00:00
|
|
|
memcpy(buffer->mAudioData, &_audioStream[0], buffer->mAudioDataByteSize - bytesRemaining);
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
2016-01-18 19:56:20 +00:00
|
|
|
_audioStreamReadPosition += audioDataSampleSize;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
2016-01-18 19:56:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(buffer->mAudioData, 0, buffer->mAudioDataByteSize);
|
|
|
|
}
|
|
|
|
|
2016-02-17 02:47:23 +00:00
|
|
|
if(!_isInvalidated)
|
|
|
|
{
|
|
|
|
[_writeLock unlockWithCondition:AudioQueueCanProceed];
|
|
|
|
AudioQueueEnqueueBuffer(theAudioQueue, buffer, 0, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dequeuedCount++;
|
|
|
|
if(_dequeuedCount == AudioQueueNumAudioBuffers)
|
|
|
|
[_writeLock unlockWithCondition:AudioQueueIsInvalidated];
|
|
|
|
else
|
|
|
|
[_writeLock unlockWithCondition:AudioQueueCanProceed];
|
|
|
|
}
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audioOutputCallback(
|
|
|
|
void *inUserData,
|
|
|
|
AudioQueueRef inAQ,
|
|
|
|
AudioQueueBufferRef inBuffer)
|
|
|
|
{
|
2016-06-15 12:07:25 +00:00
|
|
|
[(__bridge CSAudioQueue *)inUserData audioQueue:inAQ didCallbackWithBuffer:inBuffer];
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-02-17 02:47:23 +00:00
|
|
|
_writeLock = [[NSConditionLock alloc] initWithCondition:AudioQueueCanProceed];
|
2016-06-01 23:04:07 +00:00
|
|
|
_samplingRate = samplingRate;
|
2016-01-18 19:56:20 +00:00
|
|
|
|
2016-06-17 01:35:22 +00:00
|
|
|
// determine buffer sizes
|
|
|
|
_bufferLength = AudioQueueBufferMaxLength;
|
|
|
|
while((Float64)_bufferLength*50.0 > samplingRate) _bufferLength >>= 1;
|
|
|
|
_streamLength = _bufferLength * AudioQueueNumAudioBuffers;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// create an audio output queue along those lines
|
|
|
|
if(!AudioQueueNewOutput(
|
2016-06-01 23:04:07 +00:00
|
|
|
&outputDescription,
|
|
|
|
audioOutputCallback,
|
|
|
|
(__bridge void *)(self),
|
|
|
|
NULL,
|
|
|
|
kCFRunLoopCommonModes,
|
|
|
|
0,
|
|
|
|
&_audioQueue))
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
UInt32 bufferBytes = (UInt32)(_bufferLength * sizeof(int16_t));
|
2016-01-15 01:33:22 +00:00
|
|
|
|
|
|
|
int c = AudioQueueNumAudioBuffers;
|
|
|
|
while(c--)
|
|
|
|
{
|
|
|
|
AudioQueueAllocateBuffer(_audioQueue, bufferBytes, &_audioBuffers[c]);
|
|
|
|
memset(_audioBuffers[c]->mAudioData, 0, bufferBytes);
|
|
|
|
_audioBuffers[c]->mAudioDataByteSize = bufferBytes;
|
|
|
|
AudioQueueEnqueueBuffer(_audioQueue, _audioBuffers[c], 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2016-02-17 02:47:23 +00:00
|
|
|
[_writeLock lock];
|
|
|
|
_isInvalidated = YES;
|
|
|
|
[_writeLock unlock];
|
|
|
|
|
|
|
|
[_writeLock lockWhenCondition:AudioQueueIsInvalidated];
|
|
|
|
[_writeLock unlock];
|
|
|
|
|
2016-01-18 21:46:41 +00:00
|
|
|
int c = AudioQueueNumAudioBuffers;
|
|
|
|
while(c--)
|
|
|
|
AudioQueueFreeBuffer(_audioQueue, _audioBuffers[c]);
|
|
|
|
|
2016-01-15 01:35:36 +00:00
|
|
|
if(_audioQueue) AudioQueueDispose(_audioQueue, NO);
|
|
|
|
}
|
|
|
|
|
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-01-19 01:57:18 +00:00
|
|
|
while(1)
|
2016-01-18 19:56:20 +00:00
|
|
|
{
|
2016-02-17 02:47:23 +00:00
|
|
|
[_writeLock lockWhenCondition:AudioQueueCanProceed];
|
2016-06-17 01:35:22 +00:00
|
|
|
if((_audioStreamReadPosition + _streamLength) - _audioStreamWritePosition >= lengthInSamples)
|
2016-01-19 01:57:18 +00:00
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
size_t samplesBeforeOverflow = _streamLength - (_audioStreamWritePosition % _streamLength);
|
2016-01-19 01:57:18 +00:00
|
|
|
|
|
|
|
if(samplesBeforeOverflow < lengthInSamples)
|
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
memcpy(&_audioStream[_audioStreamWritePosition % _streamLength], buffer, samplesBeforeOverflow * sizeof(int16_t));
|
2016-01-19 01:57:18 +00:00
|
|
|
memcpy(&_audioStream[0], &buffer[samplesBeforeOverflow], (lengthInSamples - samplesBeforeOverflow) * sizeof(int16_t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
memcpy(&_audioStream[_audioStreamWritePosition % _streamLength], buffer, lengthInSamples * sizeof(int16_t));
|
2016-01-19 01:57:18 +00:00
|
|
|
}
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2016-01-19 01:57:18 +00:00
|
|
|
_audioStreamWritePosition += lengthInSamples;
|
|
|
|
[_writeLock unlockWithCondition:[self writeLockCondition]];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[_writeLock unlockWithCondition:AudioQueueWait];
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 19:56:20 +00:00
|
|
|
}
|
2016-01-18 19:50:19 +00:00
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
- (NSInteger)writeLockCondition
|
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
return ((_audioStreamWritePosition - _audioStreamReadPosition) < (_streamLength - _bufferLength)) ? AudioQueueCanProceed : AudioQueueWait;
|
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-06-17 01:35:22 +00:00
|
|
|
- (NSUInteger)bufferSize
|
2016-06-17 00:51:35 +00:00
|
|
|
{
|
2016-06-17 01:35:22 +00:00
|
|
|
return _bufferLength;
|
2016-06-17 00:51:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
@end
|