1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
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.
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.
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.
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.
41 /*=============================================================================
44 =============================================================================*/
46 #ifndef __AUBuffer_h__
47 #define __AUBuffer_h__
49 #include <TargetConditionals.h>
50 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
51 #include <CoreServices/CoreServices.h>
52 #include <AudioUnit/AudioUnit.h>
54 #include <CoreServices.h>
55 #include <AudioUnit.h>
59 #include "CAStreamBasicDescription.h"
60 #include "CADebugMacros.h"
62 // make this usable outside the stricter context of AudiUnits
63 #ifndef COMPONENT_THROW
64 #define COMPONENT_THROW(err) \
65 do { DebugMessage(#err); throw static_cast<OSStatus>(err); } while (0)
69 /*! @class AUBufferList */
77 /*! @ctor AUBufferList */
78 AUBufferList() : mPtrState(kPtrsInvalid
), mExternalMemory(false), mPtrs(NULL
), mMemory(NULL
),
79 mAllocatedStreams(0), mAllocatedFrames(0), mAllocatedBytes(0) { }
80 /*! @dtor ~AUBufferList */
83 /*! @method PrepareBuffer */
84 AudioBufferList
& PrepareBuffer(const CAStreamBasicDescription
&format
, UInt32 nFrames
);
85 /*! @method PrepareNullBuffer */
86 AudioBufferList
& PrepareNullBuffer(const CAStreamBasicDescription
&format
, UInt32 nFrames
);
88 /*! @method SetBufferList */
89 AudioBufferList
& SetBufferList(const AudioBufferList
&abl
) {
90 if (mAllocatedStreams
< abl
.mNumberBuffers
)
92 mPtrState
= kPtrsToExternalMemory
;
93 memcpy(mPtrs
, &abl
, (char *)&abl
.mBuffers
[abl
.mNumberBuffers
] - (char *)&abl
);
97 /*! @method SetBuffer */
98 void SetBuffer(UInt32 index
, const AudioBuffer
&ab
) {
99 if (mPtrState
== kPtrsInvalid
|| index
>= mPtrs
->mNumberBuffers
)
101 mPtrState
= kPtrsToExternalMemory
;
102 mPtrs
->mBuffers
[index
] = ab
;
105 /*! @method InvalidateBufferList */
106 void InvalidateBufferList() { mPtrState
= kPtrsInvalid
; }
108 /*! @method GetBufferList */
109 AudioBufferList
& GetBufferList() const {
110 if (mPtrState
== kPtrsInvalid
)
115 /*! @method CopyBufferListTo */
116 void CopyBufferListTo(AudioBufferList
&abl
) const {
117 if (mPtrState
== kPtrsInvalid
)
119 memcpy(&abl
, mPtrs
, (char *)&abl
.mBuffers
[abl
.mNumberBuffers
] - (char *)&abl
);
122 /*! @method CopyBufferContentsTo */
123 void CopyBufferContentsTo(AudioBufferList
&abl
) const {
124 if (mPtrState
== kPtrsInvalid
)
126 const AudioBuffer
*srcbuf
= mPtrs
->mBuffers
;
127 AudioBuffer
*destbuf
= abl
.mBuffers
;
129 for (UInt32 i
= 0; i
< abl
.mNumberBuffers
; ++i
, ++srcbuf
, ++destbuf
) {
130 if (i
>= mPtrs
->mNumberBuffers
) // duplicate last source to additional outputs [4341137]
132 if (destbuf
->mData
!= srcbuf
->mData
)
133 memmove(destbuf
->mData
, srcbuf
->mData
, srcbuf
->mDataByteSize
);
134 destbuf
->mDataByteSize
= srcbuf
->mDataByteSize
;
138 /*! @method Allocate */
139 void Allocate(const CAStreamBasicDescription
&format
, UInt32 nFrames
);
140 /*! @method Deallocate */
143 /*! @method UseExternalBuffer */
144 void UseExternalBuffer(const CAStreamBasicDescription
&format
, const AudioUnitExternalBuffer
&buf
);
146 // AudioBufferList utilities
147 /*! @method ZeroBuffer */
148 static void ZeroBuffer(AudioBufferList
&abl
) {
149 AudioBuffer
*buf
= abl
.mBuffers
;
150 for (UInt32 i
= abl
.mNumberBuffers
; i
--; ++buf
)
151 memset(buf
->mData
, 0, buf
->mDataByteSize
);
154 /*! @method PrintBuffer */
155 static void PrintBuffer(const char *label
, int subscript
, const AudioBufferList
&abl
, UInt32 nFrames
= 8, bool asFloats
= true);
158 /*! @method GetAllocatedFrames */
159 UInt32
GetAllocatedFrames() const { return mAllocatedFrames
; }
162 /*! @ctor AUBufferList */
163 AUBufferList(AUBufferList
&) { } // prohibit copy constructor
165 /*! @var mPtrState */
167 /*! @var mExternalMemory */
168 bool mExternalMemory
;
170 AudioBufferList
* mPtrs
;
173 /*! @var mAllocatedStreams */
174 UInt32 mAllocatedStreams
;
175 /*! @var mAllocatedFrames */
176 UInt32 mAllocatedFrames
;
177 /*! @var mAllocatedBytes */
178 UInt32 mAllocatedBytes
;
182 // Allocates an array of samples (type T), to be optimally aligned for the processor
183 /*! @class TAUBuffer */
188 kAlignInterval
= 0x10,
189 kAlignMask
= kAlignInterval
- 1
192 /*! @ctor TAUBuffer.0 */
193 TAUBuffer() : mMemObject(NULL
), mAlignedBuffer(NULL
), mBufferSizeBytes(0)
197 /*! @ctor TAUBuffer.1 */
198 TAUBuffer(UInt32 numElems
, UInt32 numChannels
) : mMemObject(NULL
), mAlignedBuffer(NULL
),
201 Allocate(numElems
, numChannels
);
204 /*! @dtor ~TAUBuffer */
210 /*! @method Allocate */
211 void Allocate(UInt32 numElems
) // can also re-allocate
213 UInt32 reqSize
= numElems
* sizeof(T
);
215 if (mMemObject
!= NULL
&& reqSize
== mBufferSizeBytes
)
216 return; // already allocated
218 mBufferSizeBytes
= reqSize
;
219 mMemObject
= realloc(mMemObject
, reqSize
);
220 UInt32 misalign
= (uintptr_t)mMemObject
& kAlignMask
;
222 mMemObject
= realloc(mMemObject
, reqSize
+ kAlignMask
);
223 mAlignedBuffer
= (T
*)((char *)mMemObject
+ kAlignInterval
- misalign
);
225 mAlignedBuffer
= (T
*)mMemObject
;
228 /*! @method Deallocate */
231 if (mMemObject
== NULL
) return; // so this method has no effect if we're using
232 // an external buffer
236 mAlignedBuffer
= NULL
;
237 mBufferSizeBytes
= 0;
240 /*! @method AllocateClear */
241 void AllocateClear(UInt32 numElems
) // can also re-allocate
250 memset(mAlignedBuffer
, 0, mBufferSizeBytes
);
255 /*! @method operator T *()@ */
256 operator T
*() { return mAlignedBuffer
; }
259 /*! @var mMemObject */
260 void * mMemObject
; // null when using an external buffer
261 /*! @var mAlignedBuffer */
262 T
* mAlignedBuffer
; // always valid once allocated
263 /*! @var mBufferSizeBytes */
264 UInt32 mBufferSizeBytes
;
267 #endif // __AUBuffer_h__