45 #include "CAAudioBufferList.h"
46 #include "CADebugMacros.h"
47 #include "CALogMacros.h"
55 AudioBufferList* CAAudioBufferList::Create(UInt32 inNumberBuffers)
57 UInt32 theSize = CalculateByteSize(inNumberBuffers);
58 AudioBufferList* theAnswer =
static_cast<AudioBufferList*
>(calloc(1, theSize));
61 theAnswer->mNumberBuffers = inNumberBuffers;
66 void CAAudioBufferList::Destroy(AudioBufferList* inBufferList)
71 UInt32 CAAudioBufferList::CalculateByteSize(UInt32 inNumberBuffers)
73 UInt32 theSize = SizeOf32(AudioBufferList) - SizeOf32(AudioBuffer);
74 theSize += inNumberBuffers * SizeOf32(AudioBuffer);
78 UInt32 CAAudioBufferList::GetTotalNumberChannels(
const AudioBufferList& inBufferList)
82 for(UInt32 theIndex = 0; theIndex < inBufferList.mNumberBuffers; ++theIndex)
84 theAnswer += inBufferList.mBuffers[theIndex].mNumberChannels;
90 bool CAAudioBufferList::GetBufferForChannel(
const AudioBufferList& inBufferList, UInt32 inChannel, UInt32& outBufferNumber, UInt32& outBufferChannel)
92 bool theAnswer =
false;
95 while((theIndex < inBufferList.mNumberBuffers) && (inChannel >= inBufferList.mBuffers[theIndex].mNumberChannels))
97 inChannel -= inBufferList.mBuffers[theIndex].mNumberChannels;
101 if(theIndex < inBufferList.mNumberBuffers)
103 outBufferNumber = theIndex;
104 outBufferChannel = inChannel;
111 void CAAudioBufferList::Clear(AudioBufferList& outBufferList)
114 for(UInt32 theBufferIndex = 0; theBufferIndex < outBufferList.mNumberBuffers; ++theBufferIndex)
116 if(outBufferList.mBuffers[theBufferIndex].mData != NULL)
118 memset(outBufferList.mBuffers[theBufferIndex].mData, 0, outBufferList.mBuffers[theBufferIndex].mDataByteSize);
123 void CAAudioBufferList::Copy(
const AudioBufferList& inSource, UInt32 inStartingSourceChannel, AudioBufferList& outDestination, UInt32 inStartingDestinationChannel)
129 UInt32 theInputChannel = inStartingSourceChannel;
130 UInt32 theNumberInputChannels = GetTotalNumberChannels(inSource);
131 UInt32 theOutputChannel = inStartingDestinationChannel;
132 UInt32 theNumberOutputChannels = GetTotalNumberChannels(outDestination);
134 UInt32 theInputBufferIndex = 0;
135 UInt32 theInputBufferChannel = 0;
136 UInt32 theOutputBufferIndex = 0;
137 UInt32 theOutputBufferChannel = 0;
138 while((theInputChannel < theNumberInputChannels) && (theOutputChannel < theNumberOutputChannels))
140 GetBufferForChannel(inSource, theInputChannel, theInputBufferIndex, theInputBufferChannel);
142 GetBufferForChannel(inSource, theOutputChannel, theOutputBufferIndex, theOutputBufferChannel);
144 CopyChannel(inSource.mBuffers[theInputBufferIndex], theInputBufferChannel, outDestination.mBuffers[theOutputBufferIndex], theOutputBufferChannel);
151 void CAAudioBufferList::CopyChannel(
const AudioBuffer& inSource, UInt32 inSourceChannel, AudioBuffer& outDestination, UInt32 inDestinationChannel)
154 UInt32 theNumberFramesToCopy = outDestination.mDataByteSize / (outDestination.mNumberChannels * SizeOf32(Float32));
155 const Float32* theSource =
static_cast<const Float32*
>(inSource.mData);
156 Float32* theDestination =
static_cast<Float32*
>(outDestination.mData);
159 while(theNumberFramesToCopy > 0)
162 theDestination[inDestinationChannel] = theSource[inSourceChannel];
165 --theNumberFramesToCopy;
166 theSource += inSource.mNumberChannels;
167 theDestination += outDestination.mNumberChannels;
171 void CAAudioBufferList::Sum(
const AudioBufferList& inSourceBufferList, AudioBufferList& ioSummedBufferList)
175 for(UInt32 theBufferIndex = 0; theBufferIndex < ioSummedBufferList.mNumberBuffers; ++theBufferIndex)
177 Float32* theSourceBuffer =
static_cast<Float32*
>(inSourceBufferList.mBuffers[theBufferIndex].mData);
178 Float32* theSummedBuffer =
static_cast<Float32*
>(ioSummedBufferList.mBuffers[theBufferIndex].mData);
179 UInt32 theNumberSamplesToMix = ioSummedBufferList.mBuffers[theBufferIndex].mDataByteSize / SizeOf32(Float32);
180 if((theSourceBuffer != NULL) && (theSummedBuffer != NULL) && (theNumberSamplesToMix > 0))
182 while(theNumberSamplesToMix > 0)
184 *theSummedBuffer += *theSourceBuffer;
187 --theNumberSamplesToMix;
193 bool CAAudioBufferList::HasData(AudioBufferList& inBufferList)
195 bool hasData =
false;
196 for(UInt32 theBufferIndex = 0; !hasData && (theBufferIndex < inBufferList.mNumberBuffers); ++theBufferIndex)
198 if(inBufferList.mBuffers[theBufferIndex].mData != NULL)
200 UInt32* theBuffer = (UInt32*)inBufferList.mBuffers[theBufferIndex].mData;
201 UInt32 theNumberSamples = inBufferList.mBuffers[theBufferIndex].mDataByteSize / SizeOf32(UInt32);
202 for(UInt32 theSampleIndex = 0; !hasData && (theSampleIndex < theNumberSamples); ++theSampleIndex)
204 hasData = theBuffer[theSampleIndex] != 0;
212 void CAAudioBufferList::PrintToLog(
const AudioBufferList& inBufferList)
214 PrintInt(
" Number streams: ", inBufferList.mNumberBuffers);
216 for(UInt32 theIndex = 0; theIndex < inBufferList.mNumberBuffers; ++theIndex)
218 PrintIndexedInt(
" Channels in stream", theIndex + 1, inBufferList.mBuffers[theIndex].mNumberChannels);
219 PrintIndexedInt(
" Buffer size of stream", theIndex + 1, inBufferList.mBuffers[theIndex].mDataByteSize);
224 AudioBufferList CAAudioBufferList::sEmptyBufferList = { 0, { { 0, 0, NULL } } };