summaryrefslogtreecommitdiff
path: root/libs/appleutility/CoreAudio105/CAAudioUnit.cpp
blob: f0b0890c51505ee9089cfaadd128f7705ab77238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
/*	Copyright: 	© Copyright 2005 Apple Computer, Inc. All rights reserved.

	Disclaimer:	IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
			("Apple") in consideration of your agreement to the following terms, and your
			use, installation, modification or redistribution of this Apple software
			constitutes acceptance of these terms.  If you do not agree with these terms,
			please do not use, install, modify or redistribute this Apple software.

			In consideration of your agreement to abide by the following terms, and subject
			to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
			copyrights in this original Apple software (the "Apple Software"), to use,
			reproduce, modify and redistribute the Apple Software, with or without
			modifications, in source and/or binary forms; provided that if you redistribute
			the Apple Software in its entirety and without modifications, you must retain
			this notice and the following text and disclaimers in all such redistributions of
			the Apple Software.  Neither the name, trademarks, service marks or logos of
			Apple Computer, Inc. may be used to endorse or promote products derived from the
			Apple Software without specific prior written permission from Apple.  Except as
			expressly stated in this notice, no other rights or licenses, express or implied,
			are granted by Apple herein, including but not limited to any patent rights that
			may be infringed by your derivative works or by other works in which the Apple
			Software may be incorporated.

			The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
			WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
			WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
			PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
			COMBINATION WITH YOUR PRODUCTS.

			IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
			CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
			GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
			ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
			OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
			(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
			ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*=============================================================================
	CAAudioUnit.cpp

=============================================================================*/

#include "CAAudioUnit.h"

#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
	#include <AudioUnit/MusicDevice.h>
#else
	#include <MusicDevice.h>
#endif

#include "CAReferenceCounted.h"
#include "AUOutputBL.h" //this is for the Preroll only


struct StackAUChannelInfo {
		StackAUChannelInfo (UInt32 inSize) : mChanInfo ((AUChannelInfo*)malloc (inSize)) {}
		~StackAUChannelInfo() { free (mChanInfo); }

	AUChannelInfo* mChanInfo;
};



class CAAudioUnit::AUState : public CAReferenceCounted  {
public:
	AUState (Component inComp)
						: mUnit(0), mNode (0)
						{
							OSStatus result = ::OpenAComponent (inComp, &mUnit);
							if (result)
								throw result;
							Init();
						}

	AUState (const AUNode &inNode, const AudioUnit& inUnit)
						: mUnit (inUnit), mNode (inNode)
						{
							Init();
						}

	~AUState();

	AudioUnit			mUnit;
	AUNode				mNode;

	OSStatus			GetParameter(AudioUnitParameterID inID, AudioUnitScope scope, AudioUnitElement element,
											Float32 &outValue) const
	{
			if (mGetParamProc != NULL) {
				return reinterpret_cast<AudioUnitGetParameterProc>(mGetParamProc) (mConnInstanceStorage,
										inID, scope, element, &outValue);
			}
		return AudioUnitGetParameter(mUnit, inID, scope, element, &outValue);
	}

	OSStatus			SetParameter(AudioUnitParameterID inID, AudioUnitScope scope, AudioUnitElement element,
											Float32 value, UInt32 bufferOffsetFrames)
	{
			if (mSetParamProc != NULL) {
				return reinterpret_cast<AudioUnitSetParameterProc>(mSetParamProc) (mConnInstanceStorage,
										inID, scope, element, value, bufferOffsetFrames);
			}
			return AudioUnitSetParameter(mUnit, inID, scope, element, value, bufferOffsetFrames);
	}

	OSStatus			Render (AudioUnitRenderActionFlags *  ioActionFlags,
								const AudioTimeStamp *        inTimeStamp,
								UInt32                        inOutputBusNumber,
								UInt32                        inNumberFrames,
								AudioBufferList *             ioData)
	{
		if (mRenderProc != NULL) {
			return reinterpret_cast<AudioUnitRenderProc>(mRenderProc) (mConnInstanceStorage,
									ioActionFlags, inTimeStamp, inOutputBusNumber, inNumberFrames, ioData);
		}
		return AudioUnitRender(mUnit, ioActionFlags, inTimeStamp, inOutputBusNumber, inNumberFrames, ioData);
	}

	OSStatus		MIDIEvent (UInt32					inStatus,
								UInt32					inData1,
								UInt32					inData2,
								UInt32					inOffsetSampleFrame)
	{
#if !TARGET_OS_WIN32
		if (mMIDIEventProc != NULL) {
			return reinterpret_cast<MusicDeviceMIDIEventProc>(mMIDIEventProc) (mConnInstanceStorage,
									inStatus, inData1, inData2, inOffsetSampleFrame);
		}
		return MusicDeviceMIDIEvent (mUnit, inStatus, inData1, inData2, inOffsetSampleFrame);
#else
		return paramErr;
#endif
	}

	OSStatus				StartNote (MusicDeviceInstrumentID	inInstrument,
									MusicDeviceGroupID			inGroupID,
									NoteInstanceID *			outNoteInstanceID,
									UInt32						inOffsetSampleFrame,
									const MusicDeviceNoteParams * inParams)
	{
#if !TARGET_OS_WIN32
		return MusicDeviceStartNote (mUnit, inInstrument, inGroupID, outNoteInstanceID, inOffsetSampleFrame, inParams);
#else
		return paramErr;
#endif
	}
	OSStatus				StopNote (MusicDeviceGroupID		inGroupID,
									NoteInstanceID				inNoteInstanceID,
									UInt32						inOffsetSampleFrame)
	{
#if !TARGET_OS_WIN32
		return MusicDeviceStopNote (mUnit, inGroupID, inNoteInstanceID, inOffsetSampleFrame);
#else
		return paramErr;
#endif
	}

private:
	// get the fast dispatch pointers
	void Init()
	{
		UInt32 size = sizeof(AudioUnitRenderProc);
		if (AudioUnitGetProperty(mUnit, kAudioUnitProperty_FastDispatch,
								kAudioUnitScope_Global, kAudioUnitRenderSelect,
								&mRenderProc, &size) != noErr)
			mRenderProc = NULL;
		if (AudioUnitGetProperty(mUnit, kAudioUnitProperty_FastDispatch,
								kAudioUnitScope_Global, kAudioUnitGetParameterSelect,
								&mGetParamProc, &size) != noErr)
			mGetParamProc = NULL;
		if (AudioUnitGetProperty(mUnit, kAudioUnitProperty_FastDispatch,
								kAudioUnitScope_Global, kAudioUnitSetParameterSelect,
								&mSetParamProc, &size) != noErr)
			mSetParamProc = NULL;

		if (AudioUnitGetProperty(mUnit, kAudioUnitProperty_FastDispatch,
								kAudioUnitScope_Global, kMusicDeviceMIDIEventSelect,
								&mMIDIEventProc, &size) != noErr)
			mMIDIEventProc = NULL;

		if (mRenderProc || mGetParamProc || mSetParamProc || mMIDIEventProc)
			mConnInstanceStorage = GetComponentInstanceStorage(mUnit);
		else
			mConnInstanceStorage = NULL;
	}

	ProcPtr						mRenderProc, mGetParamProc, mSetParamProc, mMIDIEventProc;

	void *						mConnInstanceStorage;

private:
		// get the compiler to tell us when we do a bad thing!!!
	AUState () {}
        AUState (const AUState& other) : CAReferenceCounted (other) {}
	AUState& operator= (const AUState&) { return *this; }
};


CAAudioUnit::AUState::~AUState ()
{
	if (mUnit && (mNode == 0)) {
		::CloseComponent (mUnit);
	}
	mNode = 0;
	mUnit = 0;
}

OSStatus		CAAudioUnit::Open (const CAComponent& inComp, CAAudioUnit &outUnit)
{
	try {
		outUnit = inComp;
		return noErr;
	} catch (OSStatus res) {
		return res;
	} catch (...) {
		return -1;
	}
}

CAAudioUnit::CAAudioUnit (const AudioUnit& inUnit)
	: mComp (inUnit), mDataPtr (new AUState (-1, inUnit))
{
}

CAAudioUnit::CAAudioUnit (const CAComponent& inComp)
	: mComp (inComp), mDataPtr (0)
{
	mDataPtr = new AUState (mComp.Comp());
}

CAAudioUnit::CAAudioUnit (const AUNode &inNode, const AudioUnit& inUnit)
	: mComp (inUnit), mDataPtr(new AUState (inNode, inUnit))
{
}

CAAudioUnit::~CAAudioUnit ()
{
	if (mDataPtr) {
		mDataPtr->release();
		mDataPtr = NULL;
	}
}

CAAudioUnit&	CAAudioUnit::operator= (const CAAudioUnit &a)
{
	if (mDataPtr != a.mDataPtr) {
		if (mDataPtr)
			mDataPtr->release();

		if ((mDataPtr = a.mDataPtr) != NULL)
			mDataPtr->retain();

		mComp = a.mComp;
	}

	return *this;
}

bool			CAAudioUnit::operator== (const CAAudioUnit& y) const
{
	if (mDataPtr == y.mDataPtr) return true;
	AudioUnit au1 = mDataPtr ? mDataPtr->mUnit : 0;
	AudioUnit au2 = y.mDataPtr ? y.mDataPtr->mUnit : 0;
	return au1 == au2;
}

bool			CAAudioUnit::operator== (const AudioUnit& y) const
{
	if (!mDataPtr) return false;
	return mDataPtr->mUnit == y;
}

#pragma mark __State Management

bool			CAAudioUnit::IsValid () const
{
	return mDataPtr ? mDataPtr->mUnit != 0 : false;
}

AudioUnit		CAAudioUnit::AU() const
{
	return mDataPtr ? mDataPtr->mUnit : 0;
}

AUNode			CAAudioUnit::GetAUNode () const
{
	return mDataPtr ? mDataPtr->mNode : 0;
}

#pragma mark __Format Handling

bool		CAAudioUnit::CanDo (	int 				inChannelsIn,
									int 				inChannelsOut) const
{
	// this is the default assumption of an audio effect unit
	Boolean* isWritable = 0;
	UInt32	dataSize = 0;
		// lets see if the unit has any channel restrictions
	OSStatus result = AudioUnitGetPropertyInfo (AU(),
									kAudioUnitProperty_SupportedNumChannels,
									kAudioUnitScope_Global, 0,
									&dataSize, isWritable); //don't care if this is writable

		// if this property is NOT implemented an FX unit
		// is expected to deal with same channel valance in and out
	if (result)
	{
		if ((Comp().Desc().IsEffect() && (inChannelsIn == inChannelsOut))
			|| (Comp().Desc().IsOffline() && (inChannelsIn == inChannelsOut)))
		{
			return true;
		}
		else
		{
			// the au should either really tell us about this
			// or we will assume the worst
			return false;
		}
	}

	StackAUChannelInfo info (dataSize);

	result = GetProperty (kAudioUnitProperty_SupportedNumChannels,
							kAudioUnitScope_Global, 0,
							info.mChanInfo, &dataSize);
	if (result) { return false; }

	return ValidateChannelPair (inChannelsIn, inChannelsOut, info.mChanInfo, (dataSize / sizeof (AUChannelInfo)));
}

int    CAAudioUnit::GetChannelInfo (AUChannelInfo** chaninfo, UInt32& cnt)
{
	// this is the default assumption of an audio effect unit
	Boolean* isWritable = 0;
	UInt32	dataSize = 0;
		// lets see if the unit has any channel restrictions
	OSStatus result = AudioUnitGetPropertyInfo (AU(),
						    kAudioUnitProperty_SupportedNumChannels,
						    kAudioUnitScope_Global, 0,
						    &dataSize, isWritable); //don't care if this is writable

	// if this property is NOT implemented an FX unit
	// is expected to deal with same channel valance in and out

	if (result)
	{
		if (Comp().Desc().IsEffect())
		{
			return 1;
		}
		else if (Comp().Desc().IsGenerator() || Comp().Desc().IsMusicDevice()) {
			// directly query Bus Formats
			// Note that that these may refer to different subBusses
			// (eg. Kick, Snare,.. on a Drummachine)
			// eventually the Bus-Name for each configuration should be exposed
			// for the User to select..

			UInt32 elCountIn, elCountOut;

			if (GetElementCount (kAudioUnitScope_Input, elCountIn)) return -1;
			if (GetElementCount (kAudioUnitScope_Output, elCountOut)) return -1;

			cnt = std::max(elCountIn, elCountOut);

			*chaninfo = (AUChannelInfo*) malloc (sizeof (AUChannelInfo) * cnt);

			for (unsigned int i = 0; i < elCountIn; ++i) {
				UInt32 numChans;
				if (NumberChannels (kAudioUnitScope_Input, i, numChans)) return -1;
				(*chaninfo)[i].inChannels = numChans;
			}
			for (unsigned int i = elCountIn; i < cnt; ++i) {
				(*chaninfo)[i].inChannels = 0;
			}

			for (unsigned int i = 0; i < elCountOut; ++i) {
				UInt32 numChans;
				if (NumberChannels (kAudioUnitScope_Output, i, numChans)) return -1;
				(*chaninfo)[i].outChannels = numChans;
			}
			for (unsigned int i = elCountOut; i < cnt; ++i) {
				(*chaninfo)[i].outChannels = 0;
			}
			return 0;
		}
		else
		{
			// the au should either really tell us about this
			// or we will assume the worst
			return -1;
		}
	}

	*chaninfo = (AUChannelInfo*) malloc (dataSize);
	cnt = dataSize / sizeof (AUChannelInfo);

	result = GetProperty (kAudioUnitProperty_SupportedNumChannels,
			      kAudioUnitScope_Global, 0,
			      *chaninfo, &dataSize);

	if (result) { return -1; }
	return 0;
}


bool	CAAudioUnit::ValidateChannelPair (int 				inChannelsIn,
										int 				inChannelsOut,
										const AUChannelInfo * info,
										UInt32				numChanInfo) const
{
// we've the following cases (some combinations) to test here:
/*
>0		An explicit number of channels on either side
0		that side (generally input!) has no elements
-1		wild card:
-1,-1	any num channels as long as same channels on in and out
-1,-2	any num channels channels on in and out - special meaning
-2+ 	indicates total num channs AU can handle
			- elements configurable to any num channels,
			- element count in scope must be writable
*/

	//now chan layout can contain -1 for either scope (ie. doesn't care)
	for (unsigned int i = 0; i < numChanInfo; ++i)
	{
			//less than zero on both sides - check for special attributes
		if ((info[i].inChannels < 0) && (info[i].outChannels < 0))
		{
				// these are our wild card matches
			if (info[i].inChannels == -1 && info[i].outChannels == -1) {
				if (inChannelsOut == inChannelsIn) {
					return true;
				}
			}
			else if ((info[i].inChannels == -1 && info[i].outChannels == -2)
					|| (info[i].inChannels == -2 && info[i].outChannels == -1))
			{
				return true;
			}
				// these are our total num channels matches
				// element count MUST be writable
			else {
				bool outWrite = false; bool inWrite = false;
				IsElementCountWritable (kAudioUnitScope_Output, outWrite);
				IsElementCountWritable (kAudioUnitScope_Input, inWrite);
				if (inWrite && outWrite) {
					if ((inChannelsOut <= abs(info[i].outChannels))
						&& (inChannelsIn <= abs(info[i].inChannels)))
					{
						return true;
					}
				}
			}
		}

			// special meaning on input, specific num on output
		else if (info[i].inChannels < 0) {
			if (info[i].outChannels == inChannelsOut)
			{
					// can do any in channels
				if (info[i].inChannels == -1) {
					return true;
				}
					// total chans on input
				else {
					bool inWrite = false;
					IsElementCountWritable (kAudioUnitScope_Input, inWrite);
					if (inWrite && (inChannelsIn <= abs(info[i].inChannels))) {
						return true;
					}
				}
			}
		}

			// special meaning on output, specific num on input
		else if (info[i].outChannels < 0) {
			if (info[i].inChannels == inChannelsIn)
			{
					// can do any out channels
				if (info[i].outChannels == -1) {
					return true;
				}
					// total chans on output
				else {
					bool outWrite = false;
					IsElementCountWritable (kAudioUnitScope_Output, outWrite);
					if (outWrite && (inChannelsOut <= abs(info[i].outChannels))) {
						return true;
					}
				}
			}
		}

			// both chans in struct >= 0 - thus has to explicitly match
		else if ((info[i].inChannels == inChannelsIn) && (info[i].outChannels == inChannelsOut)) {
			return true;
		}

			// now check to see if a wild card on the args (inChannelsIn or inChannelsOut chans is zero) is found
			// tells us to match just one side of the scopes
		else if (inChannelsIn == 0) {
			if (info[i].outChannels == inChannelsOut) {
				return true;
			}
		}
		else if (inChannelsOut == 0) {
			if (info[i].inChannels == inChannelsIn) {
				return true;
			}
		}
	}

	return false;
}

bool CheckDynCount (SInt32 inTotalChans, const CAAUChanHelper &inHelper)
{
	int totalChans = 0;
	for (unsigned int i = 0; i < inHelper.mNumEls; ++i)
		totalChans += inHelper.mChans[i];
	return (totalChans <= inTotalChans);
}

bool	CAAudioUnit::CheckOneSide (const CAAUChanHelper		&inHelper,
									bool					checkOutput,
									const AUChannelInfo		*info,
									UInt32					numInfo) const
{
		// now we can use the wildcard option (see above impl) to see if this matches
	for (unsigned int el = 0; el < inHelper.mNumEls; ++el) {
		bool testAlready = false;
		for (unsigned int i = 0; i < el; ++i) {
			if (inHelper.mChans[i] == inHelper.mChans[el]) {
				testAlready = true;
				break;
			}
		}
		if (!testAlready) {
			if (checkOutput) {
				if (!ValidateChannelPair (0, inHelper.mChans[el], info, numInfo)) return false;
			} else {
				if (!ValidateChannelPair (inHelper.mChans[el], 0, info, numInfo)) return false;
			}
		}
	}
	return true;
}

bool		CAAudioUnit::CanDo (const CAAUChanHelper		&inputs,
								const CAAUChanHelper		&outputs) const

{
// first check our state
		// huh!
	if (inputs.mNumEls == 0 && outputs.mNumEls == 0) return false;

	UInt32 elCount;
	if (GetElementCount (kAudioUnitScope_Input, elCount)) { return false; }
	if (elCount != inputs.mNumEls) return false;

	if (GetElementCount (kAudioUnitScope_Output, elCount)) { return false; }
	if (elCount != outputs.mNumEls) return false;

// (1) special cases (effects and sources (generators and instruments) only)
	UInt32	dataSize = 0;
	if (GetPropertyInfo (kAudioUnitProperty_SupportedNumChannels,
									kAudioUnitScope_Global, 0, &dataSize, NULL) != noErr)
	{
		if (Comp().Desc().IsEffect() || Comp().Desc().IsOffline()) {
			UInt32 numChan = outputs.mNumEls > 0 ? outputs.mChans[0] : inputs.mChans[0];
			for (unsigned int in = 0; in < inputs.mNumEls; ++in)
				if (numChan != inputs.mChans[in]) return false;
			for (unsigned int out = 0; out < outputs.mNumEls; ++out)
				if (numChan != outputs.mChans[out]) return false;
			return true;
		}

			// in this case, all the channels have to match the current config
		if (Comp().Desc().IsGenerator() || Comp().Desc().IsMusicDevice()) {
			for (unsigned int in = 0; in < inputs.mNumEls; ++in) {
				UInt32 chan;
				if (NumberChannels (kAudioUnitScope_Input, in, chan)) return false;
				if (chan != UInt32(inputs.mChans[in])) return false;
			}
			for (unsigned int out = 0; out < outputs.mNumEls; ++out) {
				UInt32 chan;
				if (NumberChannels (kAudioUnitScope_Output, out, chan)) return false;
				if (chan != UInt32(outputs.mChans[out])) return false;
			}
			return true;
		}

			// if we get here we can't determine anything about channel capabilities
		return false;
	}

	StackAUChannelInfo info (dataSize);

	if (GetProperty (kAudioUnitProperty_SupportedNumChannels,
							kAudioUnitScope_Global, 0,
							info.mChanInfo, &dataSize) != noErr)
	{
		return false;
	}

	int numInfo = dataSize / sizeof(AUChannelInfo);

// (2) Test for dynamic capability (or no elements on that scope)
	SInt32 dynInChans = 0;
	if (ValidateDynamicScope (kAudioUnitScope_Input, dynInChans, info.mChanInfo, numInfo)) {
		if (CheckDynCount (dynInChans, inputs) == false) return false;
	}

	SInt32 dynOutChans = 0;
	if (ValidateDynamicScope (kAudioUnitScope_Output, dynOutChans, info.mChanInfo, numInfo)) {
		if (CheckDynCount (dynOutChans, outputs) == false) return false;
	}

	if (dynOutChans && dynInChans) { return true; }

// (3)	Just need to test one side
	if (dynInChans || (inputs.mNumEls == 0)) {
		return CheckOneSide (outputs, true, info.mChanInfo, numInfo);
	}

	if (dynOutChans || (outputs.mNumEls == 0)) {
		return CheckOneSide (inputs, false, info.mChanInfo, numInfo);
	}

// (4) - not a dynamic AU, has ins and outs, and has channel constraints so we test every possible pairing
	for (unsigned int in = 0; in < inputs.mNumEls; ++in)
	{
		bool testInAlready = false;
		for (unsigned int i = 0; i < in; ++i) {
			if (inputs.mChans[i] == inputs.mChans[in]) {
				testInAlready = true;
				break;
			}
		}
		if (!testInAlready) {
			for (unsigned int out = 0; out < outputs.mNumEls; ++out) {
					// try to save a little bit and not test the same pairing multiple times...
				bool testOutAlready = false;
				for (unsigned int i = 0; i < out; ++i) {
					if (outputs.mChans[i] == outputs.mChans[out]) {
						testOutAlready = true;
						break;
					}
				}
				if (!testOutAlready) {
					if (!ValidateChannelPair (inputs.mChans[in], outputs.mChans[out],info.mChanInfo, numInfo)) {
						return false;
					}
				}
			}
		}
	}

	return true;
}

bool		CAAudioUnit::SupportsNumChannels () const
{
	// this is the default assumption of an audio effect unit
	Boolean* isWritable = 0;
	UInt32	dataSize = 0;
		// lets see if the unit has any channel restrictions
	OSStatus result = AudioUnitGetPropertyInfo (AU(),
									kAudioUnitProperty_SupportedNumChannels,
									kAudioUnitScope_Global, 0,
									&dataSize, isWritable); //don't care if this is writable

		// if this property is NOT implemented an FX unit
		// is expected to deal with same channel valance in and out
	if (result) {
		if (Comp().Desc().IsEffect() || Comp().Desc().IsOffline())
			return true;
	}
	return result == noErr;
}

bool		CAAudioUnit::GetChannelLayouts (AudioUnitScope 			inScope,
										AudioUnitElement 			inEl,
										ChannelTagVector			&outChannelVector) const
{
	if (HasChannelLayouts (inScope, inEl) == false) return false;

	UInt32 dataSize;
	OSStatus result = AudioUnitGetPropertyInfo (AU(),
								kAudioUnitProperty_SupportedChannelLayoutTags,
								inScope, inEl,
								&dataSize, NULL);

	if (result == kAudioUnitErr_InvalidProperty) {
		// if we get here we can do layouts but we've got the speaker config property
		outChannelVector.erase (outChannelVector.begin(), outChannelVector.end());
		outChannelVector.push_back (kAudioChannelLayoutTag_Stereo);
		outChannelVector.push_back (kAudioChannelLayoutTag_StereoHeadphones);
		outChannelVector.push_back (kAudioChannelLayoutTag_Quadraphonic);
		outChannelVector.push_back (kAudioChannelLayoutTag_AudioUnit_5_0);
		return true;
	}

	if (result) return false;

	bool canDo = false;
		// OK lets get our channel layouts and see if the one we want is present
	AudioChannelLayoutTag* info = (AudioChannelLayoutTag*)malloc (dataSize);
	result = AudioUnitGetProperty (AU(),
							kAudioUnitProperty_SupportedChannelLayoutTags,
							inScope, inEl,
							info, &dataSize);
	if (result) goto home;

	outChannelVector.erase (outChannelVector.begin(), outChannelVector.end());
	for (unsigned int i = 0; i < (dataSize / sizeof (AudioChannelLayoutTag)); ++i)
		outChannelVector.push_back (info[i]);

home:
	free (info);
	return canDo;
}

bool		CAAudioUnit::HasChannelLayouts (AudioUnitScope 		inScope,
										AudioUnitElement 		inEl) const
{
	OSStatus result = AudioUnitGetPropertyInfo (AU(),
									kAudioUnitProperty_SupportedChannelLayoutTags,
									inScope, inEl,
									NULL, NULL);
	return !result;
}

OSStatus	CAAudioUnit::GetChannelLayout (AudioUnitScope 		inScope,
										AudioUnitElement 		inEl,
										CAAudioChannelLayout	&outLayout) const
{
	UInt32 size;
	OSStatus result = AudioUnitGetPropertyInfo (AU(), kAudioUnitProperty_AudioChannelLayout,
									inScope, inEl, &size, NULL);
	if (result) return result;

	AudioChannelLayout *layout = (AudioChannelLayout*)malloc (size);

	require_noerr (result = AudioUnitGetProperty (AU(), kAudioUnitProperty_AudioChannelLayout,
									inScope, inEl, layout, &size), home);

	outLayout = CAAudioChannelLayout (layout);

home:
	free (layout);
	return result;
}

OSStatus	CAAudioUnit::SetChannelLayout (AudioUnitScope 		inScope,
									AudioUnitElement 			inEl,
									CAAudioChannelLayout 		&inLayout)
{
	OSStatus result = AudioUnitSetProperty (AU(),
									kAudioUnitProperty_AudioChannelLayout,
									inScope, inEl,
									inLayout, inLayout.Size());
	return result;
}

OSStatus	CAAudioUnit::SetChannelLayout (AudioUnitScope 			inScope,
											AudioUnitElement 		inEl,
											AudioChannelLayout		&inLayout,
											UInt32					inSize)
{
	OSStatus result = AudioUnitSetProperty (AU(),
									kAudioUnitProperty_AudioChannelLayout,
									inScope, inEl,
									&inLayout, inSize);
	return result;
}

OSStatus		CAAudioUnit::ClearChannelLayout (AudioUnitScope	inScope,
											AudioUnitElement	inEl)
{
	return AudioUnitSetProperty (AU(),
							kAudioUnitProperty_AudioChannelLayout,
							inScope, inEl, NULL, 0);
}

OSStatus	CAAudioUnit::GetFormat (AudioUnitScope				inScope,
									AudioUnitElement			inEl,
									AudioStreamBasicDescription	&outFormat) const
{
	UInt32 dataSize = sizeof (AudioStreamBasicDescription);
	return AudioUnitGetProperty (AU(), kAudioUnitProperty_StreamFormat,
								inScope, inEl,
								&outFormat, &dataSize);
}

OSStatus	CAAudioUnit::SetFormat (AudioUnitScope						inScope,
									AudioUnitElement					inEl,
									const AudioStreamBasicDescription	&inFormat)
{
	return AudioUnitSetProperty (AU(), kAudioUnitProperty_StreamFormat,
								inScope, inEl,
								const_cast<AudioStreamBasicDescription*>(&inFormat),
								sizeof (AudioStreamBasicDescription));
}

OSStatus	CAAudioUnit::GetSampleRate (AudioUnitScope		inScope,
										AudioUnitElement	inEl,
										Float64				&outRate) const
{
	UInt32 dataSize = sizeof (Float64);
	return AudioUnitGetProperty (AU(), kAudioUnitProperty_SampleRate,
								inScope, inEl,
								&outRate, &dataSize);
}

OSStatus	CAAudioUnit::SetSampleRate (AudioUnitScope		inScope,
										AudioUnitElement	inEl,
										Float64				inRate)
{
	AudioStreamBasicDescription desc;
	OSStatus result = GetFormat (inScope, inEl, desc);
	if (result) return result;
	desc.mSampleRate = inRate;
	return SetFormat (inScope, inEl, desc);
}

OSStatus	CAAudioUnit::SetSampleRate (Float64			inSampleRate)
{
	OSStatus result;

	UInt32 elCount;
	require_noerr (result = GetElementCount(kAudioUnitScope_Input, elCount), home);
	if (elCount) {
		for (unsigned int i = 0; i < elCount; ++i) {
			require_noerr (result = SetSampleRate (kAudioUnitScope_Input, i, inSampleRate), home);
		}
	}

	require_noerr (result = GetElementCount(kAudioUnitScope_Output, elCount), home);
	if (elCount) {
		for (unsigned int i = 0; i < elCount; ++i) {
			require_noerr (result = SetSampleRate (kAudioUnitScope_Output, i, inSampleRate), home);
		}
	}

home:
	return result;
}

OSStatus	CAAudioUnit::NumberChannels (AudioUnitScope		inScope,
										AudioUnitElement	inEl,
										UInt32				&outChans) const
{
	AudioStreamBasicDescription desc;
	OSStatus result = GetFormat (inScope, inEl, desc);
	if (!result)
		outChans = desc.mChannelsPerFrame;
	return result;
}

OSStatus	CAAudioUnit::SetNumberChannels (AudioUnitScope	inScope,
										AudioUnitElement	inEl,
										UInt32				inChans)
{
			// set this as the output of the AU
	CAStreamBasicDescription desc;
	OSStatus result = GetFormat (inScope, inEl, desc);
		if (result) return result;
	desc.SetCanonical (inChans, desc.IsInterleaved());
	result = SetFormat (inScope, inEl, desc);
	return result;
}

OSStatus		CAAudioUnit::IsElementCountWritable (AudioUnitScope inScope, bool &outWritable) const
{
	Boolean isWritable;
	UInt32 outDataSize;
	OSStatus result = GetPropertyInfo (kAudioUnitProperty_ElementCount, inScope, 0, &outDataSize, &isWritable);
	if (result)
		return result;
	outWritable = isWritable ? true : false;
	return noErr;
}

OSStatus		CAAudioUnit::GetElementCount (AudioUnitScope inScope, UInt32 &outCount) const
{
	UInt32 propSize = sizeof(outCount);
	return GetProperty (kAudioUnitProperty_ElementCount, inScope, 0, &outCount, &propSize);
}

OSStatus		CAAudioUnit::SetElementCount (AudioUnitScope inScope, UInt32 inCount)
{
	return SetProperty (kAudioUnitProperty_ElementCount, inScope, 0, &inCount, sizeof(inCount));
}

bool			CAAudioUnit::HasDynamicScope (AudioUnitScope inScope, SInt32 &outTotalNumChannels) const
{
	// ok - now we need to check the AU's capability here.
	// this is the default assumption of an audio effect unit
	Boolean* isWritable = 0;
	UInt32	dataSize = 0;
	OSStatus result = GetPropertyInfo (kAudioUnitProperty_SupportedNumChannels,
								kAudioUnitScope_Global, 0,
								&dataSize, isWritable); //don't care if this is writable

		// AU has to explicitly tell us about this.
	if (result) return false;

	StackAUChannelInfo info (dataSize);

	result = GetProperty (kAudioUnitProperty_SupportedNumChannels,
							kAudioUnitScope_Global, 0,
							info.mChanInfo, &dataSize);
	if (result) return false;

	return ValidateDynamicScope (inScope, outTotalNumChannels, info.mChanInfo, (dataSize / sizeof(AUChannelInfo)));
}

// as we've already checked that the element count is writable
// the following conditions will match this..
/*
-1, -2 ->	signifies no restrictions
-2, -1 ->	signifies no restrictions -> in this case outTotalNumChannels == -1 (any num channels)

-N 	(where N is less than -2), signifies the total channel count on the scope side (in or out)
*/
bool	CAAudioUnit::ValidateDynamicScope (AudioUnitScope		inScope,
											SInt32				&outTotalNumChannels,
											const AUChannelInfo *info,
											UInt32				numInfo) const
{
	bool writable = false;
	OSStatus result = IsElementCountWritable (inScope, writable);
	if (result || (writable == false))
		return false;

	//now chan layout can contain -1 for either scope (ie. doesn't care)
	for (unsigned int i = 0; i < numInfo; ++i)
	{
		// lets test the special wild card case first...
		// this says the AU can do any num channels on input or output - for eg. Matrix Mixer
		if (((info[i].inChannels == -1) && (info[i].outChannels == -2))
			|| ((info[i].inChannels == -2) && (info[i].outChannels == -1)))
		{
			outTotalNumChannels = -1;
			return true;
		}

		// ok lets now test our special case....
		if (inScope == kAudioUnitScope_Input) {
				// isn't dynamic on this side at least
			if (info[i].inChannels >= 0)
				continue;

			if (info[i].inChannels < -2) {
				outTotalNumChannels = abs (info[i].inChannels);
				return true;
			}
		}

		else if (inScope == kAudioUnitScope_Output) {
				// isn't dynamic on this side at least
			if (info[i].outChannels >= 0)
				continue;

			if (info[i].outChannels < -2) {
				outTotalNumChannels = abs (info[i].outChannels);
				return true;
			}
		}

		else {
			break; // wrong scope was specified
		}
	}

	return false;
}

OSStatus	CAAudioUnit::ConfigureDynamicScope (AudioUnitScope 		inScope,
											UInt32 					inNumElements,
											UInt32 					*inChannelsPerElement,
											Float64 				inSampleRate)
{
	SInt32 numChannels = 0;
	bool isDyamic = HasDynamicScope (inScope, numChannels);
	if (isDyamic == false)
		return kAudioUnitErr_InvalidProperty;

	//lets to a sanity check...
	// if numChannels == -1, then it can do "any"...
	if (numChannels > 0) {
		SInt32 count = 0;
		for (unsigned int i = 0; i < inNumElements; ++i)
			count += inChannelsPerElement[i];
		if (count > numChannels)
			return kAudioUnitErr_InvalidPropertyValue;
	}

	OSStatus result = SetElementCount (inScope, inNumElements);
	if (result)
		return result;

	CAStreamBasicDescription desc;
	desc.mSampleRate = inSampleRate;
	for (unsigned int i = 0; i < inNumElements; ++i) {
		desc.SetCanonical (inChannelsPerElement[i], false);
		result = SetFormat (inScope, i, desc);
		if (result)
			return result;
	}
	return noErr;
}

#pragma mark __Properties

bool		CAAudioUnit::CanBypass () const
{
	Boolean outWritable;
	OSStatus result = AudioUnitGetPropertyInfo (AU(), kAudioUnitProperty_BypassEffect,
									kAudioUnitScope_Global, 0,
									NULL, &outWritable);
	return (!result && outWritable);
}

bool		CAAudioUnit::GetBypass 		() const
{
	UInt32 dataSize = sizeof (UInt32);
	UInt32 outBypass;
	OSStatus result = AudioUnitGetProperty (AU(), kAudioUnitProperty_BypassEffect,
								kAudioUnitScope_Global, 0,
								&outBypass, &dataSize);
	return (result ? false : outBypass);
}

OSStatus	CAAudioUnit::SetBypass 		(bool	inBypass) const
{
	UInt32 bypass = inBypass ? 1 : 0;
	return AudioUnitSetProperty (AU(), kAudioUnitProperty_BypassEffect,
								kAudioUnitScope_Global, 0,
								&bypass, sizeof (UInt32));
}

Float64		CAAudioUnit::Latency () const
{
	Float64 secs;
	UInt32 size = sizeof(secs);
	if (GetProperty (kAudioUnitProperty_Latency, kAudioUnitScope_Global, 0, &secs, &size))
		return 0;
	return secs;
}

OSStatus	CAAudioUnit::GetAUPreset (CFPropertyListRef &outData) const
{
	UInt32 dataSize = sizeof(outData);
	return AudioUnitGetProperty (AU(), kAudioUnitProperty_ClassInfo,
								kAudioUnitScope_Global, 0,
								&outData, &dataSize);
}

OSStatus	CAAudioUnit::SetAUPreset (CFPropertyListRef &inData)
{
	return AudioUnitSetProperty (AU(), kAudioUnitProperty_ClassInfo,
								kAudioUnitScope_Global, 0,
								&inData, sizeof (CFPropertyListRef));
}

OSStatus	CAAudioUnit::GetPresentPreset (AUPreset &outData) const
{
	UInt32 dataSize = sizeof(outData);
	OSStatus result = AudioUnitGetProperty (AU(), kAudioUnitProperty_PresentPreset,
								kAudioUnitScope_Global, 0,
								&outData, &dataSize);
	if (result == kAudioUnitErr_InvalidProperty) {
		dataSize = sizeof(outData);
		result = AudioUnitGetProperty (AU(), kAudioUnitProperty_CurrentPreset,
									kAudioUnitScope_Global, 0,
									&outData, &dataSize);
		if (result == noErr) {
			// we now retain the CFString in the preset so for the client of this API
			// it is consistent (ie. the string should be released when done)
			if (outData.presetName)
				CFRetain (outData.presetName);
		}
	}
	return result;
}

OSStatus	CAAudioUnit::SetPresentPreset (AUPreset &inData)
{
	OSStatus result = AudioUnitSetProperty (AU(), kAudioUnitProperty_PresentPreset,
								kAudioUnitScope_Global, 0,
								&inData, sizeof (AUPreset));
	if (result == kAudioUnitErr_InvalidProperty) {
		result = AudioUnitSetProperty (AU(), kAudioUnitProperty_CurrentPreset,
								kAudioUnitScope_Global, 0,
								&inData, sizeof (AUPreset));
	}
	return result;
}

bool		CAAudioUnit::HasCustomView () const
{
	UInt32 dataSize = 0;
	OSStatus result = GetPropertyInfo(kAudioUnitProperty_GetUIComponentList,
                                        kAudioUnitScope_Global, 0,
                                        &dataSize, NULL);
	if (result || !dataSize) {
		dataSize = 0;
		result = GetPropertyInfo(kAudioUnitProperty_CocoaUI,
                                        kAudioUnitScope_Global, 0,
                                        &dataSize, NULL);
		if (result || !dataSize)
			return false;
	}
	return true;
}

OSStatus		CAAudioUnit::GetParameter(AudioUnitParameterID inID, AudioUnitScope scope, AudioUnitElement element,
											Float32 &outValue) const
{
	return mDataPtr ? (OSStatus) mDataPtr->GetParameter (inID, scope, element, outValue) : paramErr;
}

OSStatus		CAAudioUnit::SetParameter(AudioUnitParameterID inID, AudioUnitScope scope, AudioUnitElement element,
											Float32 value, UInt32 bufferOffsetFrames)
{
	return mDataPtr ? (OSStatus) mDataPtr->SetParameter (inID, scope, element, value, bufferOffsetFrames) : paramErr;
}

OSStatus		CAAudioUnit::MIDIEvent (UInt32			inStatus,
								UInt32					inData1,
								UInt32					inData2,
								UInt32					inOffsetSampleFrame)
{
	return mDataPtr ? (OSStatus) mDataPtr->MIDIEvent (inStatus, inData1, inData2, inOffsetSampleFrame) : paramErr;
}

OSStatus	CAAudioUnit::StartNote (MusicDeviceInstrumentID		inInstrument,
									MusicDeviceGroupID			inGroupID,
									NoteInstanceID *			outNoteInstanceID,
									UInt32						inOffsetSampleFrame,
									const MusicDeviceNoteParams * inParams)
{
	return mDataPtr ? (OSStatus) mDataPtr->StartNote (inInstrument, inGroupID, outNoteInstanceID, inOffsetSampleFrame, inParams)
		: paramErr;
}

OSStatus	CAAudioUnit::StopNote (MusicDeviceGroupID		inGroupID,
									NoteInstanceID				inNoteInstanceID,
									UInt32						inOffsetSampleFrame)
{
	return mDataPtr ? (OSStatus) mDataPtr->StopNote (inGroupID, inNoteInstanceID, inOffsetSampleFrame) : paramErr;
}

#pragma mark __Render

OSStatus		CAAudioUnit::Render (AudioUnitRenderActionFlags 			* ioActionFlags,
												const AudioTimeStamp 		* inTimeStamp,
												UInt32						inOutputBusNumber,
												UInt32						inNumberFrames,
												AudioBufferList				* ioData)
{
	return mDataPtr ? (OSStatus) mDataPtr->Render (ioActionFlags, inTimeStamp, inOutputBusNumber, inNumberFrames, ioData) : paramErr;
}

static AURenderCallbackStruct sRenderCallback;
static OSStatus PrerollRenderProc (	void 						* /*inRefCon*/,
								AudioUnitRenderActionFlags		* /*inActionFlags*/,
								const AudioTimeStamp 			* /*inTimeStamp*/,
								UInt32 							/*inBusNumber*/,
								UInt32							/*inNumFrames*/,
								AudioBufferList 				*ioData)
{
	AudioBuffer *buf = ioData->mBuffers;
	for (UInt32 i = ioData->mNumberBuffers; i--; ++buf)
		memset((Byte *)buf->mData, 0, buf->mDataByteSize);

	return noErr;
}

OSStatus 	CAAudioUnit::Preroll (UInt32 inFrameSize)
{
	CAStreamBasicDescription desc;
	OSStatus result = GetFormat (kAudioUnitScope_Input, 0, desc);
	bool hasInput = false;
			//we have input
	if (result == noErr)
	{
		sRenderCallback.inputProc = PrerollRenderProc;
		sRenderCallback.inputProcRefCon = 0;

		result = SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
								0, &sRenderCallback, sizeof(sRenderCallback));
		if (result) return result;
		hasInput = true;
	}

	AudioUnitRenderActionFlags flags = 0;
	AudioTimeStamp time;
	memset (&time, 0, sizeof(time));
	time.mFlags = kAudioTimeStampSampleTimeValid;

	CAStreamBasicDescription outputFormat;
	require_noerr (result = GetFormat (kAudioUnitScope_Output, 0, outputFormat), home);
	{
		AUOutputBL list (outputFormat, inFrameSize);
		list.Prepare ();

		require_noerr (result = Render (&flags, &time, 0, inFrameSize, list.ABL()), home);
		require_noerr (result = GlobalReset(), home);
	}

home:
	if (hasInput) {
            // remove our installed callback
		sRenderCallback.inputProc = 0;
		sRenderCallback.inputProcRefCon = 0;

		SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
								0, &sRenderCallback, sizeof(sRenderCallback));
	}
	return result;
}

#pragma mark __CAAUChanHelper

CAAUChanHelper::CAAUChanHelper(const CAAudioUnit &inAU, AudioUnitScope inScope)
	:mChans(NULL), mNumEls(0), mDidAllocate(false)
{
	UInt32 elCount;
	if (inAU.GetElementCount (inScope, elCount)) return;
	if (elCount > 8) {
		mChans = new UInt32[elCount];
		mDidAllocate = true;
		memset (mChans, 0, sizeof(int) * elCount);
	} else {
		mChans = mStaticChans;
		memset (mChans, 0, sizeof(int) * 8);
	}
	for (unsigned int i = 0; i < elCount; ++i) {
		UInt32 numChans;
		if (inAU.NumberChannels (inScope, i, numChans)) return;
		mChans[i] = numChans;
	}
	mNumEls = elCount;
}

CAAUChanHelper::~CAAUChanHelper()
{
	if (mDidAllocate) delete [] mChans;
}

CAAUChanHelper&		CAAUChanHelper::operator= (const CAAUChanHelper &c)
{
	if (mDidAllocate) delete [] mChans;
	if (c.mDidAllocate) {
		mChans = new UInt32[c.mNumEls];
		mDidAllocate = true;
	} else {
		mDidAllocate = false;
		mChans = mStaticChans;
	}
	memcpy (mChans, c.mChans, c.mNumEls * sizeof(int));

	return *this;
}

#pragma mark __Print Utilities

void		CAAudioUnit::Print (FILE* file) const
{
	fprintf (file, "AudioUnit:%p\n", AU());
	if (IsValid()) {
		fprintf (file, "\tnode=%ld\t", (long)GetAUNode()); Comp().Print (file);
	}
}