44 AUBufferList::~AUBufferList()
51 void AUBufferList::Allocate(
const CAStreamBasicDescription &format, UInt32 nFrames)
54 UInt32 channelsPerStream;
55 if (format.IsInterleaved()) {
57 channelsPerStream = format.mChannelsPerFrame;
59 nStreams = format.mChannelsPerFrame;
60 channelsPerStream = 1;
64 if (nStreams > mAllocatedStreams) {
65 mPtrs = (AudioBufferList *)CA_realloc(mPtrs, offsetof(AudioBufferList, mBuffers) + nStreams *
sizeof(AudioBuffer));
66 mAllocatedStreams = nStreams;
68 UInt32 bytesPerStream = (nFrames * format.mBytesPerFrame + 0xF) & ~0xF;
69 UInt32 nBytes = nStreams * bytesPerStream;
70 if (nBytes > mAllocatedBytes) {
71 if (mExternalMemory) {
72 mExternalMemory =
false;
75 mMemory = (Byte *)CA_realloc(mMemory, nBytes);
76 mAllocatedBytes = nBytes;
78 mAllocatedFrames = nFrames;
79 mPtrState = kPtrsInvalid;
82 void AUBufferList::Deallocate()
84 mAllocatedStreams = 0;
95 mExternalMemory =
false;
100 mPtrState = kPtrsInvalid;
103 AudioBufferList & AUBufferList::PrepareBuffer(
const CAStreamBasicDescription &format, UInt32 nFrames)
105 if (nFrames > mAllocatedFrames)
106 COMPONENT_THROW(kAudioUnitErr_TooManyFramesToProcess);
109 UInt32 channelsPerStream;
110 if (format.IsInterleaved()) {
112 channelsPerStream = format.mChannelsPerFrame;
114 nStreams = format.mChannelsPerFrame;
115 channelsPerStream = 1;
116 if (nStreams > mAllocatedStreams)
117 COMPONENT_THROW(kAudioUnitErr_FormatNotSupported);
120 AudioBufferList *abl = mPtrs;
121 abl->mNumberBuffers = nStreams;
122 AudioBuffer *buf = abl->mBuffers;
124 UInt32 streamInterval = (mAllocatedFrames * format.mBytesPerFrame + 0xF) & ~0xF;
125 UInt32 bytesPerBuffer = nFrames * format.mBytesPerFrame;
126 for ( ; nStreams--; ++buf) {
127 buf->mNumberChannels = channelsPerStream;
129 buf->mDataByteSize = bytesPerBuffer;
130 mem += streamInterval;
132 if (UInt32(mem - mMemory) > mAllocatedBytes)
133 COMPONENT_THROW(kAudioUnitErr_TooManyFramesToProcess);
134 mPtrState = kPtrsToMyMemory;
138 AudioBufferList & AUBufferList::PrepareNullBuffer(
const CAStreamBasicDescription &format, UInt32 nFrames)
141 UInt32 channelsPerStream;
142 if (format.IsInterleaved()) {
144 channelsPerStream = format.mChannelsPerFrame;
146 nStreams = format.mChannelsPerFrame;
147 channelsPerStream = 1;
148 if (nStreams > mAllocatedStreams)
149 COMPONENT_THROW(kAudioUnitErr_FormatNotSupported);
151 AudioBufferList *abl = mPtrs;
152 abl->mNumberBuffers = nStreams;
153 AudioBuffer *buf = abl->mBuffers;
154 UInt32 bytesPerBuffer = nFrames * format.mBytesPerFrame;
155 for ( ; nStreams--; ++buf) {
156 buf->mNumberChannels = channelsPerStream;
158 buf->mDataByteSize = bytesPerBuffer;
160 mPtrState = kPtrsToExternalMemory;
165 void AUBufferList::UseExternalBuffer(
const CAStreamBasicDescription &format,
const AudioUnitExternalBuffer &buf)
167 UInt32 alignedSize = buf.size & ~0xF;
168 if (mMemory != NULL && alignedSize >= mAllocatedBytes) {
171 Byte *oldMemory = mMemory;
172 mMemory = buf.buffer;
173 mAllocatedBytes = alignedSize;
176 mAllocatedFrames = mAllocatedBytes / (format.NumberChannelStreams() * format.mBytesPerFrame);
177 mExternalMemory =
true;
183 void AUBufferList::PrintBuffer(
const char *label,
int subscript,
const AudioBufferList &abl, UInt32 nFrames,
bool asFloats)
185 printf(
" %s [%d] 0x%08lX:\n", label, subscript,
long(&abl));
186 const AudioBuffer *buf = abl.mBuffers;
187 for (UInt32 i = 0; i < abl.mNumberBuffers; ++buf, ++i) {
188 printf(
" [%2d] %5dbytes %dch @ %p: ", (
int)i, (
int)buf->mDataByteSize, (
int)buf->mNumberChannels, buf->mData);
189 if (buf->mData != NULL) {
190 UInt32 nSamples = nFrames * buf->mNumberChannels;
191 for (UInt32 j = 0; j < nSamples; ++j) {
192 if (nSamples > 16 && (j % 16) == 0)
195 printf(
" %6.3f", ((
float *)buf->mData)[j]);
197 printf(
" %08X", (
unsigned)((UInt32 *)buf->mData)[j]);