41 #include "AUOutputBL.h"
42 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
43 #include <AudioUnit/AUComponent.h>
45 #include <AUComponent.h>
61 AUOutputBL::AUOutputBL (
const CAStreamBasicDescription &inDesc, UInt32 inDefaultNumFrames)
67 mFrames(inDefaultNumFrames)
69 mNumberBuffers = mFormat.IsInterleaved() ? 1 : mFormat.NumberChannels();
70 mBufferList =
reinterpret_cast<AudioBufferList*
>(
new Byte[offsetof(AudioBufferList, mBuffers) + (mNumberBuffers *
sizeof(AudioBuffer))]);
73 AUOutputBL::~AUOutputBL()
76 delete[] mBufferMemory;
79 delete [] (Byte *)mBufferList;
82 void AUOutputBL::Prepare (UInt32 inNumFrames,
bool inWantNullBufferIfAllocated)
84 UInt32 channelsPerBuffer = mFormat.IsInterleaved() ? mFormat.NumberChannels() : 1;
86 if (mBufferMemory == NULL || inWantNullBufferIfAllocated)
88 mBufferList->mNumberBuffers = mNumberBuffers;
89 AudioBuffer *buf = &mBufferList->mBuffers[0];
90 for (UInt32 i = 0; i < mNumberBuffers; ++i, ++buf) {
91 buf->mNumberChannels = channelsPerBuffer;
92 buf->mDataByteSize = mFormat.FramesToBytes (inNumFrames);
98 UInt32 nBytes = mFormat.FramesToBytes (inNumFrames);
99 if ((nBytes * mNumberBuffers) > AllocatedBytes())
100 throw OSStatus(kAudioUnitErr_TooManyFramesToProcess);
102 mBufferList->mNumberBuffers = mNumberBuffers;
103 AudioBuffer *buf = &mBufferList->mBuffers[0];
104 Byte* p = mBufferMemory;
105 for (UInt32 i = 0; i < mNumberBuffers; ++i, ++buf) {
106 buf->mNumberChannels = channelsPerBuffer;
107 buf->mDataByteSize = nBytes;
115 void AUOutputBL::Allocate (UInt32 inNumFrames)
119 UInt32 nBytes = mFormat.FramesToBytes (inNumFrames);
121 if (nBytes <= AllocatedBytes())
126 if (mNumberBuffers > 1)
127 nBytes = (nBytes + (0x10 - (nBytes & 0xF))) | 0x10;
129 mBufferSize = nBytes;
131 UInt32 memorySize = mBufferSize * mNumberBuffers;
132 Byte *newMemory =
new Byte[memorySize];
133 memset(newMemory, 0, memorySize);
135 Byte *oldMemory = mBufferMemory;
136 mBufferMemory = newMemory;
139 mFrames = inNumFrames;
144 delete [] mBufferMemory;
145 mBufferMemory = NULL;
153 void AUOutputBL::Print()
155 printf (
"AUOutputBL::Print\n");
157 printf (
"Num Buffers:%d, mFrames:%d, allocatedMemory:%c\n", (
int)mBufferList->mNumberBuffers, (
int)mFrames, (mBufferMemory != NULL ?
'T' :
'F'));
158 AudioBuffer *buf = &mBufferList->mBuffers[0];
159 for (UInt32 i = 0; i < mBufferList->mNumberBuffers; ++i, ++buf)
160 printf (
"\tBuffer:%d, Size:%d, Chans:%d, Buffer:%p\n", (
int)i, (
int)buf->mDataByteSize, (
int)buf->mNumberChannels, buf->mData);