-
Notifications
You must be signed in to change notification settings - Fork 140
/
TPCircularBuffer+AudioBufferList.c
318 lines (246 loc) · 14 KB
/
TPCircularBuffer+AudioBufferList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// TPCircularBuffer+AudioBufferList.c
// Circular/Ring buffer implementation
//
// https://github.com/michaeltyson/TPCircularBuffer
//
// Created by Michael Tyson on 20/03/2012.
//
// Copyright (C) 2012-2013 A Tasty Pixel
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#include "TPCircularBuffer+AudioBufferList.h"
#import <mach/mach_time.h>
static double __secondsToHostTicks = 0.0;
static inline unsigned long align16byte(unsigned long val) {
if ( val & (16-1) ) {
return val + (16 - (val & (16-1)));
}
return val;
}
static inline unsigned long min(unsigned long a, unsigned long b) {
return a > b ? b : a;
}
AudioBufferList *TPCircularBufferPrepareEmptyAudioBufferList(TPCircularBuffer *buffer, UInt32 numberOfBuffers, UInt32 bytesPerBuffer, const AudioTimeStamp *inTimestamp) {
uint32_t availableBytes;
TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferHead(buffer, &availableBytes);
if ( !block || availableBytes < sizeof(TPCircularBufferABLBlockHeader)+((numberOfBuffers-1)*sizeof(AudioBuffer))+(numberOfBuffers*bytesPerBuffer) ) return NULL;
#ifdef DEBUG
assert(!((unsigned long)block & 0xF) /* Beware unaligned accesses */);
#endif
if ( inTimestamp ) {
memcpy(&block->timestamp, inTimestamp, sizeof(AudioTimeStamp));
} else {
memset(&block->timestamp, 0, sizeof(AudioTimeStamp));
}
memset(&block->bufferList, 0, sizeof(AudioBufferList)+((numberOfBuffers-1)*sizeof(AudioBuffer)));
block->bufferList.mNumberBuffers = numberOfBuffers;
char *dataPtr = (char*)&block->bufferList + sizeof(AudioBufferList)+((numberOfBuffers-1)*sizeof(AudioBuffer));
for ( UInt32 i=0; i<numberOfBuffers; i++ ) {
// Find the next 16-byte aligned memory area
dataPtr = (char*)align16byte((unsigned long)dataPtr);
if ( (UInt32)((dataPtr + bytesPerBuffer) - (char*)block) > availableBytes ) {
return NULL;
}
block->bufferList.mBuffers[i].mData = dataPtr;
block->bufferList.mBuffers[i].mDataByteSize = bytesPerBuffer;
block->bufferList.mBuffers[i].mNumberChannels = 1;
dataPtr += bytesPerBuffer;
}
// Make sure whole buffer (including timestamp and length value) is 16-byte aligned in length
block->totalLength = (UInt32)align16byte((unsigned long)(dataPtr - (char*)block));
if ( block->totalLength > availableBytes ) {
return NULL;
}
return &block->bufferList;
}
AudioBufferList *TPCircularBufferPrepareEmptyAudioBufferListWithAudioFormat(TPCircularBuffer *buffer, const AudioStreamBasicDescription *audioFormat, UInt32 frameCount, const AudioTimeStamp *timestamp) {
return TPCircularBufferPrepareEmptyAudioBufferList(buffer,
(audioFormat->mFormatFlags & kAudioFormatFlagIsNonInterleaved) ? audioFormat->mChannelsPerFrame : 1,
audioFormat->mBytesPerFrame * frameCount,
timestamp);
}
void TPCircularBufferProduceAudioBufferList(TPCircularBuffer *buffer, const AudioTimeStamp *inTimestamp) {
uint32_t availableBytes;
TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferHead(buffer, &availableBytes);
assert(block);
#ifdef DEBUG
assert(!((unsigned long)block & 0xF) /* Beware unaligned accesses */);
#endif
assert(block->bufferList.mBuffers[0].mDataByteSize > 0);
if ( inTimestamp ) {
memcpy(&block->timestamp, inTimestamp, sizeof(AudioTimeStamp));
}
UInt32 calculatedLength = (UInt32)(((char*)block->bufferList.mBuffers[block->bufferList.mNumberBuffers-1].mData + block->bufferList.mBuffers[block->bufferList.mNumberBuffers-1].mDataByteSize) - (char*)block);
// Make sure whole buffer (including timestamp and length value) is 16-byte aligned in length
calculatedLength = (UInt32)align16byte(calculatedLength);
assert(calculatedLength <= block->totalLength && calculatedLength <= availableBytes);
block->totalLength = calculatedLength;
TPCircularBufferProduce(buffer, block->totalLength);
}
bool TPCircularBufferCopyAudioBufferList(TPCircularBuffer *buffer, const AudioBufferList *inBufferList, const AudioTimeStamp *inTimestamp, UInt32 frames, const AudioStreamBasicDescription *audioDescription) {
if ( frames == 0 ) return true;
UInt32 byteCount = inBufferList->mBuffers[0].mDataByteSize;
if ( frames != kTPCircularBufferCopyAll ) {
byteCount = frames * audioDescription->mBytesPerFrame;
assert(byteCount <= inBufferList->mBuffers[0].mDataByteSize);
}
if ( byteCount == 0 ) return true;
AudioBufferList *bufferList = TPCircularBufferPrepareEmptyAudioBufferList(buffer, inBufferList->mNumberBuffers, byteCount, inTimestamp);
if ( !bufferList ) return false;
for ( UInt32 i=0; i<bufferList->mNumberBuffers; i++ ) {
memcpy(bufferList->mBuffers[i].mData, inBufferList->mBuffers[i].mData, byteCount);
}
TPCircularBufferProduceAudioBufferList(buffer, NULL);
return true;
}
AudioBufferList *TPCircularBufferNextBufferListAfter(TPCircularBuffer *buffer, const AudioBufferList *bufferList, AudioTimeStamp *outTimestamp) {
uint32_t availableBytes;
void *tail = TPCircularBufferTail(buffer, &availableBytes);
void *end = (char*)tail + availableBytes;
assert((void*)bufferList > (void*)tail && (void*)bufferList < end);
TPCircularBufferABLBlockHeader *originalBlock = (TPCircularBufferABLBlockHeader*)((char*)bufferList - offsetof(TPCircularBufferABLBlockHeader, bufferList));
#ifdef DEBUG
assert(!((unsigned long)originalBlock & 0xF) /* Beware unaligned accesses */);
#endif
TPCircularBufferABLBlockHeader *nextBlock = (TPCircularBufferABLBlockHeader*)((char*)originalBlock + originalBlock->totalLength);
if ( (void*)nextBlock >= end ) return NULL;
#ifdef DEBUG
assert(!((unsigned long)nextBlock & 0xF) /* Beware unaligned accesses */);
#endif
if ( outTimestamp ) {
memcpy(outTimestamp, &nextBlock->timestamp, sizeof(AudioTimeStamp));
}
return &nextBlock->bufferList;
}
void TPCircularBufferConsumeNextBufferListPartial(TPCircularBuffer *buffer, UInt32 framesToConsume, const AudioStreamBasicDescription *audioFormat) {
uint32_t dontcare;
TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferTail(buffer, &dontcare);
if ( !block ) return;
#ifdef DEBUG
assert(!((unsigned long)block & 0xF)); // Beware unaligned accesses
#endif
UInt32 bytesToConsume = (UInt32)min(framesToConsume * audioFormat->mBytesPerFrame, block->bufferList.mBuffers[0].mDataByteSize);
if ( bytesToConsume == block->bufferList.mBuffers[0].mDataByteSize ) {
TPCircularBufferConsumeNextBufferList(buffer);
return;
}
for ( UInt32 i=0; i<block->bufferList.mNumberBuffers; i++ ) {
assert(bytesToConsume <= block->bufferList.mBuffers[i].mDataByteSize);
block->bufferList.mBuffers[i].mData = (char*)block->bufferList.mBuffers[i].mData + bytesToConsume;
block->bufferList.mBuffers[i].mDataByteSize -= bytesToConsume;
}
if ( block->timestamp.mFlags & kAudioTimeStampSampleTimeValid ) {
block->timestamp.mSampleTime += framesToConsume;
}
if ( block->timestamp.mFlags & kAudioTimeStampHostTimeValid ) {
if ( __secondsToHostTicks == 0.0 ) {
mach_timebase_info_data_t tinfo;
mach_timebase_info(&tinfo);
__secondsToHostTicks = 1.0 / (((double)tinfo.numer / tinfo.denom) * 1.0e-9);
}
block->timestamp.mHostTime += (UInt64)(((double)framesToConsume / audioFormat->mSampleRate) * __secondsToHostTicks);
}
// Reposition block forward, just before the audio data, ensuring 16-byte alignment
TPCircularBufferABLBlockHeader *newBlock = (TPCircularBufferABLBlockHeader*)(((unsigned long)block + bytesToConsume) & ~0xFul);
memmove(newBlock, block, sizeof(TPCircularBufferABLBlockHeader) + (block->bufferList.mNumberBuffers-1)*sizeof(AudioBuffer));
UInt32 bytesFreed = (UInt32)((intptr_t)newBlock - (intptr_t)block);
newBlock->totalLength -= bytesFreed;
TPCircularBufferConsume(buffer, bytesFreed);
}
void TPCircularBufferDequeueBufferListFrames(TPCircularBuffer *buffer, UInt32 *ioLengthInFrames, const AudioBufferList *outputBufferList, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat) {
bool hasTimestamp = false;
UInt32 bytesToGo = *ioLengthInFrames * audioFormat->mBytesPerFrame;
UInt32 bytesCopied = 0;
while ( bytesToGo > 0 ) {
AudioBufferList *bufferList = TPCircularBufferNextBufferList(buffer, !hasTimestamp ? outTimestamp : NULL);
if ( !bufferList ) break;
hasTimestamp = true;
UInt32 bytesToCopy = (UInt32)min(bytesToGo, bufferList->mBuffers[0].mDataByteSize);
if ( outputBufferList ) {
for ( UInt32 i=0; i<outputBufferList->mNumberBuffers; i++ ) {
assert(bytesCopied + bytesToCopy <= outputBufferList->mBuffers[i].mDataByteSize);
memcpy((char*)outputBufferList->mBuffers[i].mData + bytesCopied, bufferList->mBuffers[i].mData, bytesToCopy);
}
}
TPCircularBufferConsumeNextBufferListPartial(buffer, bytesToCopy/audioFormat->mBytesPerFrame, audioFormat);
bytesToGo -= bytesToCopy;
bytesCopied += bytesToCopy;
}
*ioLengthInFrames -= bytesToGo / audioFormat->mBytesPerFrame;
}
UInt32 TPCircularBufferPeekContiguousWrapped(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat, UInt32 contiguousToleranceSampleTime, UInt32 wrapPoint) {
uint32_t availableBytes;
TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferTail(buffer, &availableBytes);
if ( !block ) return 0;
#ifdef DEBUG
assert(!((unsigned long)block & 0xF) /* Beware unaligned accesses */);
#endif
if ( outTimestamp ) {
memcpy(outTimestamp, &block->timestamp, sizeof(AudioTimeStamp));
}
void *end = (char*)block + availableBytes;
UInt32 byteCount = 0;
while ( 1 ) {
byteCount += block->bufferList.mBuffers[0].mDataByteSize;
TPCircularBufferABLBlockHeader *nextBlock = (TPCircularBufferABLBlockHeader*)((char*)block + block->totalLength);
if ( (void*)nextBlock >= end ) {
break;
}
if ( contiguousToleranceSampleTime != UINT32_MAX ) {
UInt32 frames = block->bufferList.mBuffers[0].mDataByteSize / audioFormat->mBytesPerFrame;
Float64 nextTime = block->timestamp.mSampleTime + frames;
if ( wrapPoint && nextTime > wrapPoint ) nextTime = fmod(nextTime, wrapPoint);
Float64 diff = fabs(nextBlock->timestamp.mSampleTime - nextTime);
if ( diff > contiguousToleranceSampleTime && (!wrapPoint || fabs(diff-wrapPoint) > contiguousToleranceSampleTime) ) {
break;
}
}
#ifdef DEBUG
assert(!((unsigned long)nextBlock & 0xF) /* Beware unaligned accesses */);
#endif
block = nextBlock;
}
return byteCount / audioFormat->mBytesPerFrame;
}
UInt32 TPCircularBufferPeek(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat) {
return TPCircularBufferPeekContiguousWrapped(buffer, outTimestamp, audioFormat, UINT32_MAX, 0);
}
UInt32 TPCircularBufferPeekContiguous(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat, UInt32 contiguousToleranceSampleTime) {
return TPCircularBufferPeekContiguousWrapped(buffer, outTimestamp, audioFormat, contiguousToleranceSampleTime, 0);
}
UInt32 TPCircularBufferGetAvailableSpace(TPCircularBuffer *buffer, const AudioStreamBasicDescription *audioFormat) {
// Look at buffer head; make sure there's space for the block metadata
uint32_t availableBytes;
TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferHead(buffer, &availableBytes);
if ( !block ) return 0;
#ifdef DEBUG
assert(!((unsigned long)block & 0xF) /* Beware unaligned accesses */);
#endif
// Now find out how much 16-byte aligned audio we can store in the space available
UInt32 numberOfBuffers = audioFormat->mFormatFlags & kAudioFormatFlagIsNonInterleaved ? audioFormat->mChannelsPerFrame : 1;
char * endOfBuffer = (char*)block + availableBytes;
char * dataPtr = (char*)align16byte((unsigned long)(&block->bufferList + sizeof(AudioBufferList)+((numberOfBuffers-1)*sizeof(AudioBuffer))));
if ( dataPtr >= endOfBuffer ) return 0;
UInt32 availableAudioBytes = (UInt32)(endOfBuffer - dataPtr);
UInt32 availableAudioBytesPerBuffer = availableAudioBytes / numberOfBuffers;
availableAudioBytesPerBuffer -= (availableAudioBytesPerBuffer % (16-1));
return availableAudioBytesPerBuffer > 0 ? availableAudioBytesPerBuffer / audioFormat->mBytesPerFrame : 0;
}