AEAudioBufferListUtilities.h
Go to the documentation of this file.
1 //
2 // AEAudioBufferListUtilities.h
3 // TheAmazingAudioEngine
4 //
5 // Created by Michael Tyson on 24/03/2016.
6 // Copyright © 2016 A Tasty Pixel. All rights reserved.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source distribution.
25 //
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #import <Foundation/Foundation.h>
32 #import <AudioToolbox/AudioToolbox.h>
33 #import "AETypes.h"
34 
44 AudioBufferList *AEAudioBufferListCreate(int frameCount);
45 
56 AudioBufferList *AEAudioBufferListCreateWithFormat(AudioStreamBasicDescription audioFormat, int frameCount);
57 
71 #define AEAudioBufferListCreateOnStack(name) \
72  AEAudioBufferListCreateOnStackWithFormat(name, AEAudioDescription)
73 
88 #define AEAudioBufferListCreateOnStackWithFormat(name, audioFormat) \
89  int name ## _numberBuffers = audioFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved \
90  ? audioFormat.mChannelsPerFrame : 1; \
91  char name ## _bytes[sizeof(AudioBufferList)+(sizeof(AudioBuffer)*(name ## _numberBuffers-1))]; \
92  memset(&name ## _bytes, 0, sizeof(name ## _bytes)); \
93  AudioBufferList * name = (AudioBufferList*)name ## _bytes; \
94  name->mNumberBuffers = name ## _numberBuffers; \
95  for ( int i=0; i<name->mNumberBuffers; i++ ) { \
96  name->mBuffers[i].mNumberChannels \
97  = audioFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved ? 1 : audioFormat.mChannelsPerFrame; \
98  }
99 
115 #define AEAudioBufferListCopyOnStack(name, sourceBufferList, offsetFrames) \
116  AEAudioBufferListCopyOnStackWithByteOffset(name, sourceBufferList, offsetFrames * AEAudioDescription.mBytesPerFrame)
117 
133 #define AEAudioBufferListCopyOnStackWithByteOffset(name, sourceBufferList, offsetBytes) \
134  char name ## _bytes[sizeof(AudioBufferList)+(sizeof(AudioBuffer)*(sourceBufferList->mNumberBuffers-1))]; \
135  memcpy(name ## _bytes, sourceBufferList, sizeof(name ## _bytes)); \
136  AudioBufferList * name = (AudioBufferList*)name ## _bytes; \
137  for ( int i=0; i<name->mNumberBuffers; i++ ) { \
138  name->mBuffers[i].mData = (char*)name->mBuffers[i].mData + offsetBytes; \
139  name->mBuffers[i].mDataByteSize -= offsetBytes; \
140  }
141 
149 #define AEAudioBufferListCopyOnStackWithChannelSubset(name, sourceBufferList, channelSet) \
150  int name ## _bufferCount = MIN(sourceBufferList->mNumberBuffers-1, channelSet.lastChannel) - \
151  MIN(sourceBufferList->mNumberBuffers-1, channelSet.firstChannel) + 1; \
152  char name ## _bytes[sizeof(AudioBufferList)+(sizeof(AudioBuffer)*(name ## _bufferCount-1))]; \
153  AudioBufferList * name = (AudioBufferList*)name ## _bytes; \
154  name->mNumberBuffers = name ## _bufferCount; \
155  memcpy(name->mBuffers, &sourceBufferList->mBuffers[MIN(sourceBufferList->mNumberBuffers-1, channelSet.firstChannel)], \
156  sizeof(AudioBuffer) * name ## _bufferCount);
157 
167 AudioBufferList *AEAudioBufferListCopy(const AudioBufferList *original);
168 
175 void AEAudioBufferListFree(AudioBufferList *bufferList);
176 
187 UInt32 AEAudioBufferListGetLength(const AudioBufferList *bufferList, int *oNumberOfChannels);
188 
200 UInt32 AEAudioBufferListGetLengthWithFormat(const AudioBufferList *bufferList,
201  AudioStreamBasicDescription audioFormat,
202  int *oNumberOfChannels);
203 
213 void AEAudioBufferListSetLength(AudioBufferList *bufferList, UInt32 frames);
214 
226  AudioStreamBasicDescription audioFormat,
227  UInt32 frames);
228 
238 void AEAudioBufferListOffset(AudioBufferList *bufferList, UInt32 frames);
239 
250 void AEAudioBufferListOffsetWithFormat(AudioBufferList *bufferList,
251  AudioStreamBasicDescription audioFormat,
252  UInt32 frames);
253 
265 void AEAudioBufferListAssign(AudioBufferList * target, const AudioBufferList * source, UInt32 offset, UInt32 length);
266 
279 void AEAudioBufferListAssignWithFormat(AudioBufferList * target, const AudioBufferList * source,
280  AudioStreamBasicDescription audioFormat, UInt32 offset, UInt32 length);
281 
289 void AEAudioBufferListSilence(const AudioBufferList *bufferList, UInt32 offset, UInt32 length);
290 
299 void AEAudioBufferListSilenceWithFormat(const AudioBufferList *bufferList,
300  AudioStreamBasicDescription audioFormat,
301  UInt32 offset,
302  UInt32 length);
303 
313 void AEAudioBufferListCopyContents(const AudioBufferList * target,
314  const AudioBufferList * source,
315  UInt32 targetOffset,
316  UInt32 sourceOffset,
317  UInt32 length);
318 
329 void AEAudioBufferListCopyContentsWithFormat(const AudioBufferList * target,
330  const AudioBufferList * source,
331  AudioStreamBasicDescription audioFormat,
332  UInt32 targetOffset,
333  UInt32 sourceOffset,
334  UInt32 length);
335 
347 static inline size_t AEAudioBufferListGetStructSize(const AudioBufferList *bufferList) {
348  return sizeof(AudioBufferList) + (bufferList->mNumberBuffers-1) * sizeof(AudioBuffer);
349 }
350 
351 #ifdef __cplusplus
352 }
353 #endif