41 #ifndef __AUBuffer_h__
42 #define __AUBuffer_h__
44 #include <TargetConditionals.h>
45 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
46 #include <AudioUnit/AudioUnit.h>
48 #include <AudioUnit.h>
52 #include "CAStreamBasicDescription.h"
53 #include "CAAutoDisposer.h"
54 #include "CADebugMacros.h"
57 #ifndef COMPONENT_THROW
58 #define COMPONENT_THROW(err) \
59 do { DebugMessage(#err); throw static_cast<OSStatus>(err); } while (0)
72 AUBufferList() : mPtrState(kPtrsInvalid), mExternalMemory(false), mPtrs(NULL), mMemory(NULL),
73 mAllocatedStreams(0), mAllocatedFrames(0), mAllocatedBytes(0) { }
78 AudioBufferList & PrepareBuffer(
const CAStreamBasicDescription &format, UInt32 nFrames);
80 AudioBufferList & PrepareNullBuffer(
const CAStreamBasicDescription &format, UInt32 nFrames);
83 AudioBufferList & SetBufferList(
const AudioBufferList &abl) {
84 if (mAllocatedStreams < abl.mNumberBuffers)
86 mPtrState = kPtrsToExternalMemory;
87 memcpy(mPtrs, &abl, (
char *)&abl.mBuffers[abl.mNumberBuffers] - (
char *)&abl);
92 void SetBuffer(UInt32 index,
const AudioBuffer &ab) {
93 if (mPtrState == kPtrsInvalid || index >= mPtrs->mNumberBuffers)
95 mPtrState = kPtrsToExternalMemory;
96 mPtrs->mBuffers[index] = ab;
100 void InvalidateBufferList() { mPtrState = kPtrsInvalid; }
103 AudioBufferList & GetBufferList()
const {
104 if (mPtrState == kPtrsInvalid)
110 void CopyBufferListTo(AudioBufferList &abl)
const {
111 if (mPtrState == kPtrsInvalid)
113 memcpy(&abl, mPtrs, (
char *)&abl.mBuffers[abl.mNumberBuffers] - (
char *)&abl);
117 void CopyBufferContentsTo(AudioBufferList &abl)
const {
118 if (mPtrState == kPtrsInvalid)
120 const AudioBuffer *srcbuf = mPtrs->mBuffers;
121 AudioBuffer *destbuf = abl.mBuffers;
123 for (UInt32 i = 0; i < abl.mNumberBuffers; ++i, ++srcbuf, ++destbuf) {
124 if (i >= mPtrs->mNumberBuffers)
126 if (destbuf->mData != srcbuf->mData)
127 memmove(destbuf->mData, srcbuf->mData, srcbuf->mDataByteSize);
128 destbuf->mDataByteSize = srcbuf->mDataByteSize;
133 void Allocate(
const CAStreamBasicDescription &format, UInt32 nFrames);
138 void UseExternalBuffer(
const CAStreamBasicDescription &format,
const AudioUnitExternalBuffer &buf);
142 static void ZeroBuffer(AudioBufferList &abl) {
143 AudioBuffer *buf = abl.mBuffers;
144 for (UInt32 i = abl.mNumberBuffers ; i--; ++buf)
145 memset(buf->mData, 0, buf->mDataByteSize);
149 static void PrintBuffer(
const char *label,
int subscript,
const AudioBufferList &abl, UInt32 nFrames = 8,
bool asFloats =
true);
153 UInt32 GetAllocatedFrames()
const {
return mAllocatedFrames; }
157 AUBufferList(AUBufferList &) { }
162 bool mExternalMemory;
164 AudioBufferList * mPtrs;
168 UInt32 mAllocatedStreams;
170 UInt32 mAllocatedFrames;
172 UInt32 mAllocatedBytes;
182 kAlignInterval = 0x10,
183 kAlignMask = kAlignInterval - 1
187 TAUBuffer() : mMemObject(NULL), mAlignedBuffer(NULL), mBufferSizeBytes(0)
192 TAUBuffer(UInt32 numElems, UInt32 numChannels) : mMemObject(NULL), mAlignedBuffer(NULL),
195 Allocate(numElems, numChannels);
205 void Allocate(UInt32 numElems)
207 UInt32 reqSize = numElems *
sizeof(T);
209 if (mMemObject != NULL && reqSize == mBufferSizeBytes)
212 mBufferSizeBytes = reqSize;
213 mMemObject = CA_realloc(mMemObject, reqSize);
214 UInt32 misalign = (uintptr_t)mMemObject & kAlignMask;
216 mMemObject = CA_realloc(mMemObject, reqSize + kAlignMask);
217 mAlignedBuffer = (T *)((
char *)mMemObject + kAlignInterval - misalign);
219 mAlignedBuffer = (T *)mMemObject;
225 if (mMemObject == NULL)
return;
230 mAlignedBuffer = NULL;
231 mBufferSizeBytes = 0;
235 void AllocateClear(UInt32 numElems)
244 memset(mAlignedBuffer, 0, mBufferSizeBytes);
250 operator T *() {
return mAlignedBuffer; }
258 UInt32 mBufferSizeBytes;
261 #endif // __AUBuffer_h__