Jamoma API  0.6.0.a19
CAAudioBufferList.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 //=============================================================================
42 // Includes
43 //=============================================================================
44 
45 #include "CAAudioBufferList.h"
46 #include "CADebugMacros.h"
47 #include "CALogMacros.h"
48 #include <stdlib.h>
49 #include <string.h>
50 
51 //=============================================================================
52 // CAAudioBufferList
53 //=============================================================================
54 
55 AudioBufferList* CAAudioBufferList::Create(UInt32 inNumberBuffers)
56 {
57  UInt32 theSize = CalculateByteSize(inNumberBuffers);
58  AudioBufferList* theAnswer = static_cast<AudioBufferList*>(calloc(1, theSize));
59  if(theAnswer != NULL)
60  {
61  theAnswer->mNumberBuffers = inNumberBuffers;
62  }
63  return theAnswer;
64 }
65 
66 void CAAudioBufferList::Destroy(AudioBufferList* inBufferList)
67 {
68  free(inBufferList);
69 }
70 
71 UInt32 CAAudioBufferList::CalculateByteSize(UInt32 inNumberBuffers)
72 {
73  UInt32 theSize = SizeOf32(AudioBufferList) - SizeOf32(AudioBuffer);
74  theSize += inNumberBuffers * SizeOf32(AudioBuffer);
75  return theSize;
76 }
77 
78 UInt32 CAAudioBufferList::GetTotalNumberChannels(const AudioBufferList& inBufferList)
79 {
80  UInt32 theAnswer = 0;
81 
82  for(UInt32 theIndex = 0; theIndex < inBufferList.mNumberBuffers; ++theIndex)
83  {
84  theAnswer += inBufferList.mBuffers[theIndex].mNumberChannels;
85  }
86 
87  return theAnswer;
88 }
89 
90 bool CAAudioBufferList::GetBufferForChannel(const AudioBufferList& inBufferList, UInt32 inChannel, UInt32& outBufferNumber, UInt32& outBufferChannel)
91 {
92  bool theAnswer = false;
93  UInt32 theIndex = 0;
94 
95  while((theIndex < inBufferList.mNumberBuffers) && (inChannel >= inBufferList.mBuffers[theIndex].mNumberChannels))
96  {
97  inChannel -= inBufferList.mBuffers[theIndex].mNumberChannels;
98  ++theIndex;
99  }
100 
101  if(theIndex < inBufferList.mNumberBuffers)
102  {
103  outBufferNumber = theIndex;
104  outBufferChannel = inChannel;
105  theAnswer = true;
106  }
107 
108  return theAnswer;
109 }
110 
111 void CAAudioBufferList::Clear(AudioBufferList& outBufferList)
112 {
113  // assumes that "0" is actually the 0 value for this stream format
114  for(UInt32 theBufferIndex = 0; theBufferIndex < outBufferList.mNumberBuffers; ++theBufferIndex)
115  {
116  if(outBufferList.mBuffers[theBufferIndex].mData != NULL)
117  {
118  memset(outBufferList.mBuffers[theBufferIndex].mData, 0, outBufferList.mBuffers[theBufferIndex].mDataByteSize);
119  }
120  }
121 }
122 
123 void CAAudioBufferList::Copy(const AudioBufferList& inSource, UInt32 inStartingSourceChannel, AudioBufferList& outDestination, UInt32 inStartingDestinationChannel)
124 {
125  // This is a brute force copy method that can handle ABL's that have different buffer layouts
126  // This means that this method is probably not the fastest way to do this for all cases.
127  // This method also assumes that both the source and destination sample formats are Float32
128 
129  UInt32 theInputChannel = inStartingSourceChannel;
130  UInt32 theNumberInputChannels = GetTotalNumberChannels(inSource);
131  UInt32 theOutputChannel = inStartingDestinationChannel;
132  UInt32 theNumberOutputChannels = GetTotalNumberChannels(outDestination);
133 
134  UInt32 theInputBufferIndex = 0;
135  UInt32 theInputBufferChannel = 0;
136  UInt32 theOutputBufferIndex = 0;
137  UInt32 theOutputBufferChannel = 0;
138  while((theInputChannel < theNumberInputChannels) && (theOutputChannel < theNumberOutputChannels))
139  {
140  GetBufferForChannel(inSource, theInputChannel, theInputBufferIndex, theInputBufferChannel);
141 
142  GetBufferForChannel(inSource, theOutputChannel, theOutputBufferIndex, theOutputBufferChannel);
143 
144  CopyChannel(inSource.mBuffers[theInputBufferIndex], theInputBufferChannel, outDestination.mBuffers[theOutputBufferIndex], theOutputBufferChannel);
145 
146  ++theInputChannel;
147  ++theOutputChannel;
148  }
149 }
150 
151 void CAAudioBufferList::CopyChannel(const AudioBuffer& inSource, UInt32 inSourceChannel, AudioBuffer& outDestination, UInt32 inDestinationChannel)
152 {
153  // set up the stuff for the loop
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);
157 
158  // loop through the data and copy the samples
159  while(theNumberFramesToCopy > 0)
160  {
161  // copy the data
162  theDestination[inDestinationChannel] = theSource[inSourceChannel];
163 
164  // adjust the pointers
165  --theNumberFramesToCopy;
166  theSource += inSource.mNumberChannels;
167  theDestination += outDestination.mNumberChannels;
168  }
169 }
170 
171 void CAAudioBufferList::Sum(const AudioBufferList& inSourceBufferList, AudioBufferList& ioSummedBufferList)
172 {
173  // assumes that the buffers are Float32 samples and the listst have the same layout
174  // this is a lame algorithm, by the way. it could at least be unrolled a couple of times
175  for(UInt32 theBufferIndex = 0; theBufferIndex < ioSummedBufferList.mNumberBuffers; ++theBufferIndex)
176  {
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))
181  {
182  while(theNumberSamplesToMix > 0)
183  {
184  *theSummedBuffer += *theSourceBuffer;
185  ++theSummedBuffer;
186  ++theSourceBuffer;
187  --theNumberSamplesToMix;
188  }
189  }
190  }
191 }
192 
193 bool CAAudioBufferList::HasData(AudioBufferList& inBufferList)
194 {
195  bool hasData = false;
196  for(UInt32 theBufferIndex = 0; !hasData && (theBufferIndex < inBufferList.mNumberBuffers); ++theBufferIndex)
197  {
198  if(inBufferList.mBuffers[theBufferIndex].mData != NULL)
199  {
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)
203  {
204  hasData = theBuffer[theSampleIndex] != 0;
205  }
206  }
207  }
208  return hasData;
209 }
210 
211 #if CoreAudio_Debug
212 void CAAudioBufferList::PrintToLog(const AudioBufferList& inBufferList)
213 {
214  PrintInt(" Number streams: ", inBufferList.mNumberBuffers);
215 
216  for(UInt32 theIndex = 0; theIndex < inBufferList.mNumberBuffers; ++theIndex)
217  {
218  PrintIndexedInt(" Channels in stream", theIndex + 1, inBufferList.mBuffers[theIndex].mNumberChannels);
219  PrintIndexedInt(" Buffer size of stream", theIndex + 1, inBufferList.mBuffers[theIndex].mDataByteSize);
220  }
221 }
222 #endif
223 
224 AudioBufferList CAAudioBufferList::sEmptyBufferList = { 0, { { 0, 0, NULL } } };
225