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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "AudioQueue.h"
|
|
|
|
@import AudioToolbox;
|
|
|
|
|
|
|
|
#define AudioQueueNumAudioBuffers 3
|
|
|
|
#define AudioQueueStreamLength 2048
|
|
|
|
#define AudioQueueBufferLength 512
|
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
enum {
|
|
|
|
AudioQueueCanWrite,
|
|
|
|
AudioQueueWait
|
|
|
|
};
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
@implementation AudioQueue
|
|
|
|
{
|
|
|
|
AudioQueueRef _audioQueue;
|
|
|
|
AudioQueueBufferRef _audioBuffers[AudioQueueNumAudioBuffers];
|
2016-01-18 19:50:19 +00:00
|
|
|
unsigned int _audioStreamReadPosition, _audioStreamWritePosition;
|
2016-01-15 01:33:22 +00:00
|
|
|
int16_t _audioStream[AudioQueueStreamLength];
|
2016-01-18 19:56:20 +00:00
|
|
|
NSConditionLock *_writeLock;
|
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-01-18 19:56:20 +00:00
|
|
|
[_writeLock lock];
|
|
|
|
|
|
|
|
const unsigned int writeLead = _audioStreamWritePosition - _audioStreamReadPosition;
|
|
|
|
const size_t audioDataSampleSize = buffer->mAudioDataByteSize / sizeof(int16_t);
|
|
|
|
if(writeLead >= audioDataSampleSize*2)
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-01-18 19:56:20 +00:00
|
|
|
size_t samplesBeforeOverflow = AudioQueueStreamLength - (_audioStreamReadPosition % AudioQueueStreamLength);
|
|
|
|
if(audioDataSampleSize <= samplesBeforeOverflow)
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-01-18 19:56:20 +00:00
|
|
|
memcpy(buffer->mAudioData, &_audioStream[_audioStreamReadPosition % AudioQueueStreamLength], 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);
|
|
|
|
memcpy(buffer->mAudioData, &_audioStream[_audioStreamReadPosition % AudioQueueStreamLength], bytesRemaining);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
AudioQueueEnqueueBuffer(theAudioQueue, buffer, 0, NULL);
|
|
|
|
|
|
|
|
[_writeLock unlockWithCondition:AudioQueueCanWrite];
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audioOutputCallback(
|
|
|
|
void *inUserData,
|
|
|
|
AudioQueueRef inAQ,
|
|
|
|
AudioQueueBufferRef inBuffer)
|
|
|
|
{
|
|
|
|
[(__bridge AudioQueue *)inUserData audioQueue:inAQ didCallbackWithBuffer:inBuffer];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if(self)
|
|
|
|
{
|
2016-01-18 19:56:20 +00:00
|
|
|
_writeLock = [[NSConditionLock alloc] initWithCondition:AudioQueueCanWrite];
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
/*
|
|
|
|
Describe a mono, 16bit, 44.1Khz audio format
|
|
|
|
*/
|
|
|
|
AudioStreamBasicDescription outputDescription;
|
|
|
|
|
|
|
|
outputDescription.mSampleRate = 44100;
|
|
|
|
|
|
|
|
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(
|
|
|
|
&outputDescription,
|
|
|
|
audioOutputCallback,
|
|
|
|
(__bridge void *)(self),
|
|
|
|
NULL,
|
|
|
|
kCFRunLoopCommonModes,
|
|
|
|
0,
|
|
|
|
&_audioQueue))
|
|
|
|
{
|
|
|
|
UInt32 bufferBytes = AudioQueueBufferLength * sizeof(int16_t);
|
|
|
|
|
|
|
|
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-01-15 01:35:36 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
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-18 19:56:20 +00:00
|
|
|
[_writeLock lockWhenCondition:AudioQueueCanWrite];
|
|
|
|
size_t samplesBeforeOverflow = AudioQueueStreamLength - (_audioStreamWritePosition % AudioQueueStreamLength);
|
|
|
|
|
|
|
|
if(samplesBeforeOverflow < lengthInSamples)
|
2016-01-15 01:33:22 +00:00
|
|
|
{
|
2016-01-18 19:56:20 +00:00
|
|
|
memcpy(&_audioStream[_audioStreamWritePosition % AudioQueueStreamLength], buffer, samplesBeforeOverflow * sizeof(int16_t));
|
|
|
|
memcpy(&_audioStream[0], &buffer[samplesBeforeOverflow], (lengthInSamples - samplesBeforeOverflow) * sizeof(int16_t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&_audioStream[_audioStreamWritePosition % AudioQueueStreamLength], buffer, lengthInSamples * sizeof(int16_t));
|
|
|
|
}
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
_audioStreamWritePosition += lengthInSamples;
|
|
|
|
[_writeLock unlockWithCondition:[self writeLockCondition]];
|
|
|
|
}
|
2016-01-18 19:50:19 +00:00
|
|
|
|
2016-01-18 19:56:20 +00:00
|
|
|
- (NSInteger)writeLockCondition
|
|
|
|
{
|
|
|
|
return ((_audioStreamWritePosition - _audioStreamReadPosition) < AudioQueueStreamLength) ? AudioQueueCanWrite : AudioQueueWait;
|
2016-01-15 01:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|