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 #include "AUScopeElement.h"
47 #include "AUInputElement.h"
52 inline bool HasGoodBufferPointers(const AudioBufferList
&abl
, UInt32 nBytes
)
54 const AudioBuffer
*buf
= abl
.mBuffers
;
55 for (UInt32 i
= abl
.mNumberBuffers
; i
--;++buf
) {
56 if (buf
->mData
== NULL
|| buf
->mDataByteSize
< nBytes
)
63 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64 // AUInputElement::AUInputElement
66 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67 AUInputElement::AUInputElement(AUBase
*audioUnit
) :
68 AUIOElement(audioUnit
),
73 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 // AUInputElement::SetConnection
76 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77 void AUInputElement::SetConnection(const AudioUnitConnection
&conn
)
79 if (conn
.sourceAudioUnit
== 0) {
84 mInputType
= kFromConnection
;
86 mConnRenderProc
= NULL
;
90 UInt32 size
= sizeof(AudioUnitRenderProc
);
91 if (AudioUnitGetProperty( conn
.sourceAudioUnit
,
92 kAudioUnitProperty_FastDispatch
,
93 kAudioUnitScope_Global
,
94 kAudioUnitRenderSelect
,
97 mConnRenderProc
= NULL
;
100 mConnInstanceStorage
= GetComponentInstanceStorage(conn
.sourceAudioUnit
);
103 void AUInputElement::Disconnect()
105 mInputType
= kNoInput
;
106 mIOBuffer
.Deallocate();
111 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112 // AUInputElement::SetInputCallback
114 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115 void AUInputElement::SetInputCallback(ProcPtr proc
, void *refCon
)
120 mInputType
= kFromCallback
;
122 mInputProcRefCon
= refCon
;
127 OSStatus
AUInputElement::SetStreamFormat(const CAStreamBasicDescription
&fmt
)
129 OSStatus err
= AUIOElement::SetStreamFormat(fmt
);
135 ComponentResult
AUInputElement::PullInput( AudioUnitRenderActionFlags
& ioActionFlags
,
136 const AudioTimeStamp
& inTimeStamp
,
137 AudioUnitElement inElement
,
140 ComponentResult theResult
= noErr
;
142 if (mInputType
== kNoInput
)
143 return kAudioUnitErr_NoConnection
;
145 if (NeedsBufferSpace() && nFrames
> mIOBuffer
.GetAllocatedFrames())
146 mIOBuffer
.Allocate (mStreamFormat
, nFrames
);
148 AudioBufferList
*pullBuffer
;
150 if (mInputType
== kFromConnection
)
151 pullBuffer
= &mIOBuffer
.PrepareNullBuffer(mStreamFormat
, nFrames
);
153 pullBuffer
= &mIOBuffer
.PrepareBuffer(mStreamFormat
, nFrames
);
155 if (mInputType
== kFromConnection
) {
157 if (mConnRenderProc
!= NULL
)
158 theResult
= reinterpret_cast<AudioUnitRenderProc
>(mConnRenderProc
)(
159 mConnInstanceStorage
, &ioActionFlags
, &inTimeStamp
, mConnection
.sourceOutputNumber
,
160 nFrames
, pullBuffer
);
162 theResult
= AudioUnitRender(
163 mConnection
.sourceAudioUnit
, &ioActionFlags
, &inTimeStamp
, mConnection
.sourceOutputNumber
,
164 nFrames
, pullBuffer
);
169 theResult
= reinterpret_cast<AURenderCallback
>(mInputProc
)(
170 mInputProcRefCon
, &ioActionFlags
, &inTimeStamp
, inElement
,
171 nFrames
, pullBuffer
);
175 if (mInputType
== kNoInput
) // defense: the guy upstream could have disconnected
176 // it's a horrible thing to do, but may happen!
177 return kAudioUnitErr_NoConnection
;
179 // if (theResult == noErr && GetChannelData(0)[0] != 0.)
180 // AUBufferList::PrintBuffer("PullInput", 0, *pullBuffer);