TPCircularBuffer+AudioBufferList.h
Go to the documentation of this file.
1 //
2 // TPCircularBuffer+AudioBufferList.h
3 // Circular/Ring buffer implementation
4 //
5 // https://github.com/michaeltyson/TPCircularBuffer
6 //
7 // Created by Michael Tyson on 20/03/2012.
8 //
9 // Copyright (C) 2012-2013 A Tasty Pixel
10 //
11 // This software is provided 'as-is', without any express or implied
12 // warranty. In no event will the authors be held liable for any damages
13 // arising from the use of this software.
14 //
15 // Permission is granted to anyone to use this software for any purpose,
16 // including commercial applications, and to alter it and redistribute it
17 // freely, subject to the following restrictions:
18 //
19 // 1. The origin of this software must not be misrepresented; you must not
20 // claim that you wrote the original software. If you use this software
21 // in a product, an acknowledgment in the product documentation would be
22 // appreciated but is not required.
23 //
24 // 2. Altered source versions must be plainly marked as such, and must not be
25 // misrepresented as being the original software.
26 //
27 // 3. This notice may not be removed or altered from any source distribution.
28 //
29 
30 #ifndef TPCircularBuffer_AudioBufferList_h
31 #define TPCircularBuffer_AudioBufferList_h
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "TPCircularBuffer.h"
38 #include <AudioToolbox/AudioToolbox.h>
39 
40 #define kTPCircularBufferCopyAll UINT32_MAX
41 
42 typedef struct {
43  AudioTimeStamp timestamp;
44  UInt32 totalLength;
45  AudioBufferList bufferList;
47 
48 
58 AudioBufferList *TPCircularBufferPrepareEmptyAudioBufferList(TPCircularBuffer *buffer, int numberOfBuffers, int bytesPerBuffer, const AudioTimeStamp *timestamp);
59 
69 AudioBufferList *TPCircularBufferPrepareEmptyAudioBufferListWithAudioFormat(TPCircularBuffer *buffer, const AudioStreamBasicDescription *audioFormat, UInt32 frameCount, const AudioTimeStamp *timestamp);
70 
81 void TPCircularBufferProduceAudioBufferList(TPCircularBuffer *buffer, const AudioTimeStamp *inTimestamp);
82 
93 bool TPCircularBufferCopyAudioBufferList(TPCircularBuffer *buffer, const AudioBufferList *bufferList, const AudioTimeStamp *timestamp, UInt32 frames, const AudioStreamBasicDescription *audioFormat);
94 
102 static __inline__ __attribute__((always_inline)) AudioBufferList *TPCircularBufferNextBufferList(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp) {
103  int32_t dontcare; // Length of segment is contained within buffer list, so we can ignore this
104  TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferTail(buffer, &dontcare);
105  if ( !block ) {
106  if ( outTimestamp ) {
107  memset(outTimestamp, 0, sizeof(AudioTimeStamp));
108  }
109  return NULL;
110  }
111  if ( outTimestamp ) {
112  memcpy(outTimestamp, &block->timestamp, sizeof(AudioTimeStamp));
113  }
114  return &block->bufferList;
115 }
116 
125 AudioBufferList *TPCircularBufferNextBufferListAfter(TPCircularBuffer *buffer, const AudioBufferList *bufferList, AudioTimeStamp *outTimestamp);
126 
132 static __inline__ __attribute__((always_inline)) void TPCircularBufferConsumeNextBufferList(TPCircularBuffer *buffer) {
133  int32_t dontcare;
134  TPCircularBufferABLBlockHeader *block = (TPCircularBufferABLBlockHeader*)TPCircularBufferTail(buffer, &dontcare);
135  if ( !block ) return;
136  TPCircularBufferConsume(buffer, block->totalLength);
137 }
138 
149 void TPCircularBufferConsumeNextBufferListPartial(TPCircularBuffer *buffer, int framesToConsume, const AudioStreamBasicDescription *audioFormat);
150 
165 void TPCircularBufferDequeueBufferListFrames(TPCircularBuffer *buffer, UInt32 *ioLengthInFrames, const AudioBufferList *outputBufferList, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat);
166 
179 UInt32 TPCircularBufferPeek(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat);
180 
195 UInt32 TPCircularBufferPeekContiguous(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat, UInt32 contiguousToleranceSampleTime);
196 
213 UInt32 TPCircularBufferPeekContiguousWrapped(TPCircularBuffer *buffer, AudioTimeStamp *outTimestamp, const AudioStreamBasicDescription *audioFormat, UInt32 contiguousToleranceSampleTime, UInt32 wrapPoint);
214 
226 UInt32 TPCircularBufferGetAvailableSpace(TPCircularBuffer *buffer, const AudioStreamBasicDescription *audioFormat);
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 
232 #endif