41 #include "CABufferList.h"
42 #include "CAByteOrder.h"
44 void CABufferList::AllocateBuffers(UInt32 nBytes)
46 if (nBytes <= GetNumBytes())
return;
48 if (mNumberBuffers > 1)
51 nBytes = ((nBytes + 15) & ~15) | 16;
52 UInt32 memorySize = nBytes * mNumberBuffers;
53 Byte *newMemory =
new Byte[memorySize], *p = newMemory;
54 memset(newMemory, 0, memorySize);
56 AudioBuffer *buf = mBuffers;
57 for (UInt32 i = mNumberBuffers; i--; ++buf) {
58 if (buf->mData != NULL && buf->mDataByteSize > 0)
60 memcpy(p, buf->mData, buf->mDataByteSize);
61 buf->mDataByteSize = nBytes;
65 Byte *oldMemory = mBufferMemory;
66 mBufferMemory = newMemory;
70 void CABufferList::AllocateBuffersAndCopyFrom(UInt32 nBytes, CABufferList *inSrcList, CABufferList *inSetPtrList)
72 if (mNumberBuffers != inSrcList->mNumberBuffers)
return;
73 if (mNumberBuffers != inSetPtrList->mNumberBuffers)
return;
74 if (nBytes <= GetNumBytes()) {
75 CopyAllFrom(inSrcList, inSetPtrList);
78 inSetPtrList->VerifyNotTrashingOwnedBuffer();
79 UInt32 fromByteSize = inSrcList->GetNumBytes();
81 if (mNumberBuffers > 1)
84 nBytes = ((nBytes + 15) & ~15) | 16;
85 UInt32 memorySize = nBytes * mNumberBuffers;
86 Byte *newMemory =
new Byte[memorySize], *p = newMemory;
87 memset(newMemory, 0, memorySize);
89 AudioBuffer *buf = mBuffers;
90 AudioBuffer *ptrBuf = inSetPtrList->mBuffers;
91 AudioBuffer *srcBuf = inSrcList->mBuffers;
92 for (UInt32 i = mNumberBuffers; i--; ++buf, ++ptrBuf, ++srcBuf) {
93 if (srcBuf->mData != NULL && srcBuf->mDataByteSize > 0)
95 memmove(p, srcBuf->mData, srcBuf->mDataByteSize);
96 buf->mDataByteSize = nBytes;
98 ptrBuf->mDataByteSize = srcBuf->mDataByteSize;
102 Byte *oldMemory = mBufferMemory;
103 mBufferMemory = newMemory;
104 if (inSrcList != inSetPtrList)
105 inSrcList->BytesConsumed(fromByteSize);
109 void CABufferList::DeallocateBuffers()
111 AudioBuffer *buf = mBuffers;
112 for (UInt32 i = mNumberBuffers; i--; ++buf) {
114 buf->mDataByteSize = 0;
116 if (mBufferMemory != NULL) {
117 delete[] mBufferMemory;
118 mBufferMemory = NULL;
123 static void show(
const AudioBufferList &abl,
int framesToPrint,
int wordSize,
const char *label,
const char *fmtstr=NULL)
125 printf(
"%s %p (%d fr%s):\n", label ? label :
"AudioBufferList", &abl, framesToPrint, fmtstr ? fmtstr :
"");
126 const AudioBuffer *buf = abl.mBuffers;
127 for (UInt32 i = 0; i < abl.mNumberBuffers; ++i, ++buf) {
128 printf(
" [%2d] %5dbytes %dch @ %p", (
int)i, (
int)buf->mDataByteSize, (
int)buf->mNumberChannels, buf->mData);
129 if (framesToPrint && buf->mData != NULL) {
131 Byte *p = (Byte *)buf->mData;
132 for (
int j = framesToPrint * buf->mNumberChannels; --j >= 0; )
135 printf(
" %6.3f", *(Float32 *)p);
136 p +=
sizeof(Float32);
145 printf(
" %04X", CFSwapInt16BigToHost(*(UInt16 *)p));
149 printf(
" %06X", (p[0] << 16) | (p[1] << 8) | p[2]);
153 printf(
" %08X", (
unsigned int)CFSwapInt32BigToHost(*(UInt32 *)p));
157 printf(
" %6.3f", CASwapFloat32BigToHost(*(Float32 *)p));
158 p +=
sizeof(Float32);
161 printf(
" %04X", CFSwapInt16LittleToHost(*(UInt16 *)p));
165 printf(
" %06X", (p[2] << 16) | (p[1] << 8) | p[0]);
169 printf(
" %08X", (
unsigned int)CFSwapInt32LittleToHost(*(UInt32 *)p));
173 printf(
" %6.3f", CASwapFloat32LittleToHost(*(Float32 *)p));
174 p +=
sizeof(Float32);
182 void CAShowAudioBufferList(
const AudioBufferList &abl,
int framesToPrint,
const AudioStreamBasicDescription &asbd,
const char *label)
184 CAStreamBasicDescription fmt(asbd);
186 char fmtstr[80] = { 0 };
188 if (fmt.mFormatID == kAudioFormatLinearPCM) {
189 if (fmt.mFormatFlags & kLinearPCMFormatFlagIsFloat) {
190 if (fmt.mBitsPerChannel == 32) {
191 if (fmt.mFormatFlags & kLinearPCMFormatFlagIsBigEndian) {
193 strcpy(fmtstr,
", BEF");
196 strcpy(fmtstr,
", LEF");
200 wordSize = fmt.SampleWordSize();
202 #if CA_PREFER_FIXED_POINT
203 int fracbits = (asbd.mFormatFlags & kLinearPCMFormatFlagsSampleFractionMask) >> kLinearPCMFormatFlagsSampleFractionShift;
205 sprintf(fmtstr,
", %d.%d-bit", (
int)asbd.mBitsPerChannel - fracbits, fracbits);
208 sprintf(fmtstr,
", %d-bit", (
int)asbd.mBitsPerChannel);
210 if (!(fmt.mFormatFlags & kLinearPCMFormatFlagIsBigEndian)) {
211 wordSize = -wordSize;
212 strcat(fmtstr,
" LEI");
214 strcat(fmtstr,
" BEI");
219 show(abl, framesToPrint, wordSize, label, fmtstr);
222 void CAShowAudioBufferList(
const AudioBufferList &abl,
int framesToPrint,
int wordSize,
const char *label)
224 show(abl, framesToPrint, wordSize, label);
227 extern "C" void CAShowAudioBufferList(
const AudioBufferList *abl,
int framesToPrint,
int wordSize)
229 show(*abl, framesToPrint, wordSize, NULL);
233 extern "C" int CrashIfClientProvidedBogusAudioBufferList(
const AudioBufferList *abl,
bool nullok)
235 const AudioBuffer *buf = abl->mBuffers, *bufend = buf + abl->mNumberBuffers;
238 for ( ; buf < bufend; ++buf) {
239 const int *p = (
const int *)buf->mData;
242 if (nullok)
continue;
244 unsigned datasize = buf->mDataByteSize;
245 if (datasize >=
sizeof(
int)) {
247 sum += p[datasize /
sizeof(int) - 1];
250 return anyNull | (sum & ~1);