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 =============================================================================*/
49 AUBufferList::~AUBufferList()
56 void AUBufferList::Allocate(const CAStreamBasicDescription
&format
, UInt32 nFrames
)
59 UInt32 channelsPerStream
;
60 if (format
.IsInterleaved()) {
62 channelsPerStream
= format
.mChannelsPerFrame
;
64 nStreams
= format
.mChannelsPerFrame
;
65 channelsPerStream
= 1;
68 // careful -- the I/O thread could be running!
69 if (nStreams
> mAllocatedStreams
) {
70 mPtrs
= (AudioBufferList
*)realloc(mPtrs
, offsetof(AudioBufferList
, mBuffers
) + nStreams
* sizeof(AudioBuffer
));
71 mAllocatedStreams
= nStreams
;
73 UInt32 bytesPerStream
= (nFrames
* format
.mBytesPerFrame
+ 0xF) & ~0xF;
74 UInt32 nBytes
= nStreams
* bytesPerStream
;
75 if (nBytes
> mAllocatedBytes
) {
76 if (mExternalMemory
) {
77 mExternalMemory
= false;
80 mMemory
= (Byte
*)realloc(mMemory
, nBytes
);
81 mAllocatedBytes
= nBytes
;
83 mAllocatedFrames
= nFrames
;
84 mPtrState
= kPtrsInvalid
;
87 void AUBufferList::Deallocate()
89 mAllocatedStreams
= 0;
92 // this causes a world of hurt if someone upstream disconnects during I/O (SysSoundGraph)
94 printf("deallocating bufferlist %08X\n", int(mPtrs));
100 mExternalMemory
= false;
105 mPtrState
= kPtrsInvalid
;
108 AudioBufferList
& AUBufferList::PrepareBuffer(const CAStreamBasicDescription
&format
, UInt32 nFrames
)
111 UInt32 channelsPerStream
;
112 if (format
.IsInterleaved()) {
114 channelsPerStream
= format
.mChannelsPerFrame
;
116 nStreams
= format
.mChannelsPerFrame
;
117 channelsPerStream
= 1;
118 if (nStreams
> mAllocatedStreams
)
119 COMPONENT_THROW(kAudioUnitErr_FormatNotSupported
);
122 AudioBufferList
*abl
= mPtrs
;
123 abl
->mNumberBuffers
= nStreams
;
124 AudioBuffer
*buf
= abl
->mBuffers
;
126 UInt32 streamInterval
= (mAllocatedFrames
* format
.mBytesPerFrame
+ 0xF) & ~0xF;
127 UInt32 bytesPerBuffer
= nFrames
* format
.mBytesPerFrame
;
128 for ( ; nStreams
--; ++buf
) {
129 buf
->mNumberChannels
= channelsPerStream
;
131 buf
->mDataByteSize
= bytesPerBuffer
;
132 mem
+= streamInterval
;
134 if (UInt32(mem
- mMemory
) > mAllocatedBytes
)
135 COMPONENT_THROW(kAudioUnitErr_TooManyFramesToProcess
);
136 mPtrState
= kPtrsToMyMemory
;
140 AudioBufferList
& AUBufferList::PrepareNullBuffer(const CAStreamBasicDescription
&format
, UInt32 nFrames
)
143 UInt32 channelsPerStream
;
144 if (format
.IsInterleaved()) {
146 channelsPerStream
= format
.mChannelsPerFrame
;
148 nStreams
= format
.mChannelsPerFrame
;
149 channelsPerStream
= 1;
150 if (nStreams
> mAllocatedStreams
)
151 COMPONENT_THROW(kAudioUnitErr_FormatNotSupported
);
153 AudioBufferList
*abl
= mPtrs
;
154 abl
->mNumberBuffers
= nStreams
;
155 AudioBuffer
*buf
= abl
->mBuffers
;
156 UInt32 bytesPerBuffer
= nFrames
* format
.mBytesPerFrame
;
157 for ( ; nStreams
--; ++buf
) {
158 buf
->mNumberChannels
= channelsPerStream
;
160 buf
->mDataByteSize
= bytesPerBuffer
;
162 mPtrState
= kPtrsToExternalMemory
;
166 // this should NOT be called while I/O is in process
167 void AUBufferList::UseExternalBuffer(const CAStreamBasicDescription
&format
, const AudioUnitExternalBuffer
&buf
)
169 UInt32 alignedSize
= buf
.size
& ~0xF;
170 if (mMemory
!= NULL
&& alignedSize
>= mAllocatedBytes
) {
171 // don't accept the buffer if we already have one and it's big enough
172 // if we don't already have one, we don't need one
173 Byte
*oldMemory
= mMemory
;
174 mMemory
= buf
.buffer
;
175 mAllocatedBytes
= alignedSize
;
176 // from Allocate(): nBytes = nStreams * nFrames * format.mBytesPerFrame;
177 // thus: nFrames = nBytes / (nStreams * format.mBytesPerFrame)
178 mAllocatedFrames
= mAllocatedBytes
/ (format
.NumberChannelStreams() * format
.mBytesPerFrame
);
179 mExternalMemory
= true;
185 void AUBufferList::PrintBuffer(const char *label
, int subscript
, const AudioBufferList
&abl
, UInt32 nFrames
, bool asFloats
)
187 printf(" %s [%d] 0x%08lX:\n", label
, subscript
, long(&abl
));
188 const AudioBuffer
*buf
= abl
.mBuffers
;
189 for (UInt32 i
= 0; i
< abl
.mNumberBuffers
; ++buf
, ++i
) {
190 printf(" [%2d] %5dbytes %dch @ %p: ", (int)i
, (int)buf
->mDataByteSize
, (int)buf
->mNumberChannels
, buf
->mData
);
191 if (buf
->mData
!= NULL
) {
192 UInt32 nSamples
= nFrames
* buf
->mNumberChannels
;
193 for (UInt32 j
= 0; j
< nSamples
; ++j
) {
194 if (nSamples
> 16 && (j
% 16) == 0)
197 printf(" %6.3f", ((float *)buf
->mData
)[j
]);
199 printf(" %08X", (unsigned)((UInt32
*)buf
->mData
)[j
]);