summaryrefslogtreecommitdiff
path: root/libs/appleutility/CoreAudio/AudioUnits/AUPublic/OtherBases/AUPannerBase.cpp
blob: 91e7d9cc7e810fe26a5668d0eeec7b235d0a8e90 (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
/*
     File: AUPannerBase.cpp
 Abstract: AUPannerBase.h
  Version: 1.1

 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
 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 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.

 Copyright (C) 2014 Apple Inc. All Rights Reserved.

*/
#include "AUPannerBase.h"
#include "CABundleLocker.h"
#include <AudioToolbox/AudioToolbox.h>
#include <Accelerate/Accelerate.h>

static bool sLocalized = false;

static CFStringRef kPanner_Azimuth_Name = CFSTR("azimuth");
static CFStringRef kPanner_Elevation_Name = CFSTR("elevation");
static CFStringRef kPanner_Distance_Name = CFSTR("distance");

static CFStringRef kPanner_CoordScale_Name = CFSTR("coordinate scale");
static CFStringRef kPanner_RefDistance_Name = CFSTR("reference distance");
static CFStringRef kPanner_Gain_Name = CFSTR("gain");

static Float32 kPannerParamDefault_Azimuth = 0.;
static Float32 kPannerParamDefault_Elevation = 0.;
static Float32 kPannerParamDefault_Distance = 1.;

static Float32 kPannerParamDefault_CoordScale = 10.;
static Float32 kPannerParamDefault_RefDistance = 1.;
static Float32 kPannerParamDefault_Gain = 1.;

//_____________________________________________________________________________
//
AUPannerBase::AUPannerBase(AudioComponentInstance inAudioUnit)
    : AUBase(inAudioUnit, 1, 1), mBypassEffect(false)
{
	{
		CABundleLocker lock;
		if (!sLocalized)
		{
			CFBundleRef bundle = CFBundleGetBundleWithIdentifier( CFSTR("com.apple.audio.units.Components") );
			if (bundle != NULL)
			{
				kPanner_Azimuth_Name 	= CFCopyLocalizedStringFromTableInBundle(kPanner_Azimuth_Name,    CFSTR("AudioUnits"), bundle, CFSTR(""));
				kPanner_Elevation_Name 	= CFCopyLocalizedStringFromTableInBundle(kPanner_Elevation_Name,  CFSTR("AudioUnits"), bundle, CFSTR(""));
				kPanner_Distance_Name 	= CFCopyLocalizedStringFromTableInBundle(kPanner_Distance_Name,   CFSTR("AudioUnits"), bundle, CFSTR(""));

				kPanner_CoordScale_Name  = CFCopyLocalizedStringFromTableInBundle(kPanner_CoordScale_Name,  CFSTR("AudioUnits"), bundle, CFSTR(""));
				kPanner_RefDistance_Name = CFCopyLocalizedStringFromTableInBundle(kPanner_RefDistance_Name, CFSTR("AudioUnits"), bundle, CFSTR(""));
				kPanner_Gain_Name 	     = CFCopyLocalizedStringFromTableInBundle(kPanner_Gain_Name,        CFSTR("AudioUnits"), bundle, CFSTR(""));

			}

			sLocalized = true;
		}
	}

	CreateElements();

    SetParameter(kPannerParam_Azimuth, kPannerParamDefault_Azimuth);
    SetParameter(kPannerParam_Elevation, kPannerParamDefault_Elevation);
    SetParameter(kPannerParam_Distance, kPannerParamDefault_Distance);

    SetParameter(kPannerParam_CoordScale, kPannerParamDefault_CoordScale);
    SetParameter(kPannerParam_RefDistance, kPannerParamDefault_RefDistance);
    SetParameter(kPannerParam_Gain, kPannerParamDefault_Gain);
}

//_____________________________________________________________________________
//
AUPannerBase::~AUPannerBase()
{
    Cleanup();
}

//_____________________________________________________________________________
//
/*! @method Initialize */
OSStatus			AUPannerBase::Initialize()
{
	OSStatus err = noErr;
	AllocBypassMatrix();
	err = UpdateBypassMatrix();
	return err;
}

//_____________________________________________________________________________
//
/*! @method AllocBypassMatrix */
void		AUPannerBase::AllocBypassMatrix()
{
	UInt32 inChannels = GetNumberOfInputChannels();
	UInt32 outChannels = GetNumberOfOutputChannels();
	mBypassMatrix.alloc(inChannels * outChannels, true);
}

static AudioChannelLayoutTag DefaultTagForNumberOfChannels(UInt32 inNumberChannels)
{
	switch (inNumberChannels) {
		case 1: return kAudioChannelLayoutTag_Mono;
		case 2: return kAudioChannelLayoutTag_Stereo;
		case 4: return kAudioChannelLayoutTag_Quadraphonic;
		case 5: return kAudioChannelLayoutTag_AudioUnit_5_0;
		case 6: return kAudioChannelLayoutTag_AudioUnit_6_0;
		case 7: return kAudioChannelLayoutTag_AudioUnit_7_0;
		case 8: return kAudioChannelLayoutTag_AudioUnit_8;

		default: return 0xFFFF0000 | inNumberChannels;
	}
}

//_____________________________________________________________________________
//
/*! @method UpdateBypassMatrix */
OSStatus			AUPannerBase::SetDefaultChannelLayoutsIfNone()
{
	OSStatus err = noErr;

	// if layout has not been set, then guess layout from number of channels
	UInt32 inChannels = GetNumberOfInputChannels();
	AudioChannelLayout inputLayoutSubstitute;
	const AudioChannelLayout* inputLayout = &GetInputLayout();
	if (inputLayout == NULL || inputLayout->mChannelLayoutTag == 0) {
		inputLayout = &inputLayoutSubstitute;
		inputLayoutSubstitute.mNumberChannelDescriptions = 0;
		inputLayoutSubstitute.mChannelBitmap = 0;
		inputLayoutSubstitute.mChannelLayoutTag = DefaultTagForNumberOfChannels(inChannels);

		mInputLayout = &inputLayoutSubstitute;
		err = SetAudioChannelLayout(kAudioUnitScope_Input, 0, &GetInputLayout());
		if (err) return err;
	}

	// if layout has not been set, then guess layout from number of channels
	UInt32 outChannels = GetNumberOfOutputChannels();
	AudioChannelLayout outputLayoutSubstitute;
	const AudioChannelLayout* outputLayout = &GetOutputLayout();
	if (outputLayout == NULL || outputLayout->mChannelLayoutTag == 0) {
		outputLayout = &outputLayoutSubstitute;
		outputLayoutSubstitute.mNumberChannelDescriptions = 0;
		outputLayoutSubstitute.mChannelBitmap = 0;
		outputLayoutSubstitute.mChannelLayoutTag = DefaultTagForNumberOfChannels(outChannels);

		mOutputLayout = &outputLayoutSubstitute;
		err = SetAudioChannelLayout(kAudioUnitScope_Output, 0, &GetOutputLayout());
		if (err) return err;
	}

	return err;
}



OSStatus			AUPannerBase::UpdateBypassMatrix()
{
	OSStatus err = SetDefaultChannelLayoutsIfNone();
	if (err) return err;

	UInt32 inChannels = GetNumberOfInputChannels();
	UInt32 outChannels = GetNumberOfOutputChannels();

	const AudioChannelLayout* inoutACL[2];

	inoutACL[0] = &GetInputLayout();
	inoutACL[1] = &GetOutputLayout();

	mBypassMatrix.alloc(inChannels * outChannels, true);

	UInt32 propSize = inChannels * outChannels * sizeof(Float32);

	err = AudioFormatGetProperty(kAudioFormatProperty_MatrixMixMap, sizeof(inoutACL), inoutACL, &propSize, mBypassMatrix());
	if (err) {
		// if there is an error, use a diagonal matrix.
		Float32* bypass = mBypassMatrix();
		for (UInt32 chan = 0; chan < std::min(inChannels, outChannels); ++chan)
		{
			float *amp = bypass + (chan * outChannels + chan);
			*amp = 1.;
		}
	}

    return noErr;
}

//_____________________________________________________________________________
//
/*! @method Cleanup */
void				AUPannerBase::Cleanup()
{

}


//_____________________________________________________________________________
//
/*! @method Reset */
OSStatus			AUPannerBase::Reset(		AudioUnitScope 				inScope,
										AudioUnitElement 			inElement)
{
    return AUBase::Reset(inScope, inElement);
}

//_____________________________________________________________________________
//
/*! @method GetParameterInfo */
OSStatus			AUPannerBase::GetParameterInfo(	AudioUnitScope			inScope,
												AudioUnitParameterID	inParameterID,
												AudioUnitParameterInfo	&outParameterInfo )
{
	OSStatus result = noErr;

	outParameterInfo.flags = 	kAudioUnitParameterFlag_IsWritable
						+		kAudioUnitParameterFlag_IsReadable;

	if (inScope == kAudioUnitScope_Global) {

		switch(inParameterID)
		{
			case kPannerParam_Azimuth:
				AUBase::FillInParameterName (outParameterInfo, kPanner_Azimuth_Name, false);
				outParameterInfo.unit = kAudioUnitParameterUnit_Degrees;
				outParameterInfo.minValue = -180.;
				outParameterInfo.maxValue = 180;
				outParameterInfo.defaultValue = kPannerParamDefault_Azimuth;
			break;

			case kPannerParam_Elevation:
				AUBase::FillInParameterName (outParameterInfo, kPanner_Elevation_Name, false);
				outParameterInfo.unit = kAudioUnitParameterUnit_Degrees;
				outParameterInfo.minValue = -90.;
				outParameterInfo.maxValue = 90;
				outParameterInfo.defaultValue = kPannerParamDefault_Elevation;
			break;

			case kPannerParam_Distance:
				AUBase::FillInParameterName (outParameterInfo, kPanner_Distance_Name, false);
				outParameterInfo.unit = kAudioUnitParameterUnit_Generic;
				outParameterInfo.minValue = 0.0;
				outParameterInfo.maxValue = 1.;
				outParameterInfo.defaultValue = kPannerParamDefault_Distance;
				outParameterInfo.flags += kAudioUnitParameterFlag_IsHighResolution;
				//outParameterInfo.flags += kAudioUnitParameterFlag_DisplayLogarithmic;
			break;

 			case kPannerParam_CoordScale:
				AUBase::FillInParameterName (outParameterInfo, kPanner_CoordScale_Name, false);
				outParameterInfo.unit = kAudioUnitParameterUnit_Meters;
				outParameterInfo.minValue = 0.01;
				outParameterInfo.maxValue = 1000.;
				outParameterInfo.defaultValue = kPannerParamDefault_CoordScale;
				outParameterInfo.flags += kAudioUnitParameterFlag_DisplayLogarithmic;
			break;

 			case kPannerParam_RefDistance:
				AUBase::FillInParameterName (outParameterInfo, kPanner_RefDistance_Name, false);
				outParameterInfo.unit = kAudioUnitParameterUnit_Meters;
				outParameterInfo.minValue = 0.01;
				outParameterInfo.maxValue = 1000.;
				outParameterInfo.defaultValue = kPannerParamDefault_RefDistance;
				outParameterInfo.flags += kAudioUnitParameterFlag_DisplayLogarithmic;
			break;

			case kPannerParam_Gain:
				AUBase::FillInParameterName (outParameterInfo, kPanner_Gain_Name, false);
				outParameterInfo.unit = kAudioUnitParameterUnit_Generic;
				outParameterInfo.minValue = 0.;
				outParameterInfo.maxValue = 1.;
				outParameterInfo.defaultValue = kPannerParamDefault_Gain;
			break;

			default:
				result = kAudioUnitErr_InvalidParameter;
				break;
		}
	} else {
		result = kAudioUnitErr_InvalidParameter;
	}

	return result;
}

//_____________________________________________________________________________
//
OSStatus 	AUPannerBase::GetParameter(		AudioUnitParameterID			inParamID,
													AudioUnitScope 					inScope,
													AudioUnitElement 				inElement,
													Float32 &						outValue)
{
	if (inScope != kAudioUnitScope_Global)
		return kAudioUnitErr_InvalidScope;

	outValue = Globals()->GetParameter(inParamID);

	return noErr;
}


//_____________________________________________________________________________
//
OSStatus 	AUPannerBase::SetParameter(		AudioUnitParameterID			inParamID,
													AudioUnitScope 					inScope,
													AudioUnitElement 				inElement,
													Float32							inValue,
													UInt32							inBufferOffsetInFrames)
{
	if (inScope != kAudioUnitScope_Global)
		return kAudioUnitErr_InvalidScope;

	Globals()->SetParameter(inParamID, inValue);

	return noErr;
}



//_____________________________________________________________________________
//
/*! @method GetPropertyInfo */
OSStatus			AUPannerBase::GetPropertyInfo (AudioUnitPropertyID	inID,
										AudioUnitScope				inScope,
										AudioUnitElement			inElement,
										UInt32 &					outDataSize,
										Boolean &					outWritable)
{
	OSStatus err = noErr;
	switch (inID) {
		case kAudioUnitProperty_BypassEffect:
			if (inScope != kAudioUnitScope_Global)
				return kAudioUnitErr_InvalidScope;

			outWritable = true;
			outDataSize = sizeof (UInt32);
			break;
		default:
			err = AUBase::GetPropertyInfo(inID, inScope, inElement, outDataSize, outWritable);
	}
	return err;
}

//_____________________________________________________________________________
//
/*! @method GetProperty */
OSStatus			AUPannerBase::GetProperty (AudioUnitPropertyID 		inID,
										AudioUnitScope 				inScope,
										AudioUnitElement	 		inElement,
										void *						outData)
{
	OSStatus err = noErr;
	switch (inID)
	{
		case kAudioUnitProperty_BypassEffect:
			if (inScope != kAudioUnitScope_Global)
				return kAudioUnitErr_InvalidScope;

			*((UInt32*)outData) = (IsBypassEffect() ? 1 : 0);
			break;
		default:
			err = AUBase::GetProperty(inID, inScope, inElement, outData);
	}
	return err;
}

//_____________________________________________________________________________
//
/*! @method SetProperty */
OSStatus			AUPannerBase::SetProperty(AudioUnitPropertyID 		inID,
										AudioUnitScope 				inScope,
										AudioUnitElement 			inElement,
										const void *				inData,
										UInt32 						inDataSize)
{
	switch (inID)
	{
		case kAudioUnitProperty_BypassEffect:
				if (inDataSize < sizeof(UInt32))
					return kAudioUnitErr_InvalidPropertyValue;
				bool tempNewSetting = *((UInt32*)inData) != 0;
					// we're changing the state of bypass
				if (tempNewSetting != IsBypassEffect())
				{
					if (!tempNewSetting && IsBypassEffect() && IsInitialized()) // turning bypass off and we're initialized
						Reset(0, 0);
					SetBypassEffect (tempNewSetting);
				}
				return noErr;
	}
    return AUBase::SetProperty(inID, inScope, inElement, inData, inDataSize);
}


//_____________________________________________________________________________
//
/*! @method StreamFormatWritable */
bool				AUPannerBase::StreamFormatWritable (AudioUnitScope	scope,
										AudioUnitElement			element)
{
    return true;
}

//_____________________________________________________________________________
//
/*! @method ChangeStreamFormat */
OSStatus			AUPannerBase::ChangeStreamFormat (
									AudioUnitScope						inScope,
									AudioUnitElement					inElement,
									const CAStreamBasicDescription & 	inPrevFormat,
									const CAStreamBasicDescription &	inNewFormat)
{
	if (inScope == kAudioUnitScope_Input && !InputChannelConfigIsSupported(inNewFormat.NumberChannels()))
		return kAudioUnitErr_FormatNotSupported;

	if (inScope == kAudioUnitScope_Output && !OutputChannelConfigIsSupported(inNewFormat.NumberChannels()))
		return kAudioUnitErr_FormatNotSupported;

	if (inNewFormat.NumberChannels() != inPrevFormat.NumberChannels())
		RemoveAudioChannelLayout(inScope, inElement);

	return AUBase::ChangeStreamFormat(inScope, inElement, inPrevFormat, inNewFormat);
}

//_____________________________________________________________________________
//
/*! @method Render */
OSStatus 	AUPannerBase::Render(AudioUnitRenderActionFlags &		ioActionFlags,
									const AudioTimeStamp &			inTimeStamp,
									UInt32							inNumberFrames)
{
	if (IsBypassEffect())
		return BypassRender(ioActionFlags, inTimeStamp, inNumberFrames);
	else
		return PannerRender(ioActionFlags, inTimeStamp, inNumberFrames);
}

//_____________________________________________________________________________
//
/*! @method Render */
OSStatus 	AUPannerBase::BypassRender(AudioUnitRenderActionFlags &		ioActionFlags,
									const AudioTimeStamp &			inTimeStamp,
									UInt32							inNumberFrames)
{
	AudioUnitRenderActionFlags xflags = 0;
	OSStatus result = PullInput(0, xflags, inTimeStamp, inNumberFrames);
	if (result) return false;
	bool isSilent = xflags & kAudioUnitRenderAction_OutputIsSilence;

	AudioBufferList& outputBufferList = GetOutput(0)->GetBufferList();
	AUBufferList::ZeroBuffer(outputBufferList);

	if (!isSilent)
	{
		UInt32 inChannels = GetNumberOfInputChannels();
		UInt32 outChannels = GetNumberOfOutputChannels();
		Float32* bypass = mBypassMatrix();
		for (UInt32 outChan = 0; outChan < outChannels; ++outChan)
		{
			float* outData = GetOutput(0)->GetChannelData(outChan);

			for (UInt32 inChan = 0; inChan < inChannels; ++inChan)
			{
				float* inData = GetInput(0)->GetChannelData(inChan);
				float *amp = bypass + (inChan * outChannels + outChan);
				vDSP_vsma(inData, 1, amp, outData, 1, outData, 1, inNumberFrames);
			}
		}
	}
    return noErr;
}

//_____________________________________________________________________________
//
UInt32		AUPannerBase::GetAudioChannelLayout(	AudioUnitScope				inScope,
													AudioUnitElement 			inElement,
													AudioChannelLayout *		outLayoutPtr,
													Boolean &					outWritable)
{
	SetDefaultChannelLayoutsIfNone();

	outWritable = true;

	CAAudioChannelLayout* caacl = NULL;
	switch (inScope)
	{
		case kAudioUnitScope_Input:
			caacl = &mInputLayout;
			break;
		case kAudioUnitScope_Output:
			caacl = &mOutputLayout;
			break;
		default:
			COMPONENT_THROW(kAudioUnitErr_InvalidScope);
	}

	if (inElement != 0)
		COMPONENT_THROW(kAudioUnitErr_InvalidElement);

	UInt32 size = caacl->IsValid() ? caacl->Size() : 0;
	if (size > 0 && outLayoutPtr)
		memcpy(outLayoutPtr, &caacl->Layout(), size);

	return size;
}

//_____________________________________________________________________________
//
OSStatus	AUPannerBase::RemoveAudioChannelLayout(		AudioUnitScope				inScope,
														AudioUnitElement			inElement)
{
	CAAudioChannelLayout* caacl = NULL;
	switch (inScope)
	{
		case kAudioUnitScope_Input:
			caacl = &mInputLayout;
			break;
		case kAudioUnitScope_Output:
			caacl = &mOutputLayout;
			break;
		default:
			return kAudioUnitErr_InvalidScope;
	}

	if (inElement != 0)
		return kAudioUnitErr_InvalidElement;

	*caacl = NULL;
	return noErr;
}

//_____________________________________________________________________________
//
OSStatus 	AUPannerBase::SetAudioChannelLayout(		AudioUnitScope 				inScope,
														AudioUnitElement 			inElement,
														const AudioChannelLayout *	inLayout)
{
	if (!inLayout)
		return RemoveAudioChannelLayout(inScope, inElement);

	if (!ChannelLayoutTagIsSupported(inScope, inElement, inLayout->mChannelLayoutTag))
		return kAudioUnitErr_FormatNotSupported;

	CAAudioChannelLayout* caacl = NULL;
	UInt32 currentChannels;
	switch (inScope)
	{
		case kAudioUnitScope_Input:
			caacl = &mInputLayout;
			currentChannels = GetNumberOfInputChannels();
			break;
		case kAudioUnitScope_Output:
			caacl = &mOutputLayout;
			currentChannels = GetNumberOfOutputChannels();
			break;
		default:
			return kAudioUnitErr_InvalidScope;
	}

	if (inElement != 0)
		return kAudioUnitErr_InvalidElement;

	UInt32 numChannelsInLayout = CAAudioChannelLayout::NumberChannels(*inLayout);
	if (currentChannels != numChannelsInLayout)
		return kAudioUnitErr_InvalidPropertyValue;

	*caacl = inLayout;
	if (IsInitialized())
		UpdateBypassMatrix();

	return noErr;
}

//_____________________________________________________________________________
//
UInt32 AUPannerBase::GetChannelLayoutTags(		AudioUnitScope				inScope,
												AudioUnitElement 			inElement,
												AudioChannelLayoutTag*		outTags)
{
	switch (inScope)
	{
		case kAudioUnitScope_Input:
			if (outTags) {
				outTags[0] = kAudioChannelLayoutTag_Mono;
				outTags[1] = kAudioChannelLayoutTag_Stereo;
				outTags[2] = kAudioChannelLayoutTag_Ambisonic_B_Format;
			}
			return 3;
		case kAudioUnitScope_Output:
			if (outTags) {
				outTags[0] = kAudioChannelLayoutTag_Stereo;
				outTags[1] = kAudioChannelLayoutTag_Quadraphonic;
				outTags[2] = kAudioChannelLayoutTag_AudioUnit_5_0;
				outTags[3] = kAudioChannelLayoutTag_AudioUnit_6_0;
				outTags[4] = kAudioChannelLayoutTag_AudioUnit_7_0;
				outTags[5] = kAudioChannelLayoutTag_AudioUnit_7_0_Front;
				outTags[6] = kAudioChannelLayoutTag_AudioUnit_8;
			}
			return 7;
		default: {
			OSStatus err = kAudioUnitErr_InvalidScope;
			throw err;
		}
	}
}



//_____________________________________________________________________________
//
bool 	AUPannerBase::ChannelConfigIsSupported()
{
	UInt32 inChannels = GetNumberOfInputChannels();
	UInt32 outChannels = GetNumberOfOutputChannels();
	const AUChannelInfo* cinfo = NULL;
	UInt32 numConfigs = SupportedNumChannels(&cinfo);
	for (UInt32 i = 0; i < numConfigs; ++i)
	{
		if (cinfo[i].inChannels == (SInt16)inChannels && cinfo[i].outChannels == (SInt16)outChannels)
			return true;
	}
	return false;
}


//_____________________________________________________________________________
//
bool 	AUPannerBase::InputChannelConfigIsSupported(UInt32 inNumberChannels)
{
	const AUChannelInfo* cinfo = NULL;
	UInt32 numConfigs = SupportedNumChannels(&cinfo);
	for (UInt32 i = 0; i < numConfigs; ++i)
	{
		if (cinfo[i].inChannels == (SInt16)inNumberChannels)
			return true;
	}
	return false;
}

//_____________________________________________________________________________
//
bool 	AUPannerBase::OutputChannelConfigIsSupported(UInt32 inNumberChannels)
{
	const AUChannelInfo* cinfo = NULL;
	UInt32 numConfigs = SupportedNumChannels(&cinfo);
	for (UInt32 i = 0; i < numConfigs; ++i)
	{
		if (cinfo[i].outChannels == (SInt16)inNumberChannels)
			return true;
	}
	return false;
}

//_____________________________________________________________________________
//
bool 	AUPannerBase::ChannelLayoutTagIsSupported(		AudioUnitScope			inScope,
														AudioUnitElement		inElement,
														AudioChannelLayoutTag	inTag)
{
	UInt32 numTags = GetChannelLayoutTags(inScope, inElement, NULL);
	CAAutoFree<AudioChannelLayoutTag> tags(numTags);
	GetChannelLayoutTags(inScope, inElement, tags());

	for (UInt32 i = 0; i < numTags; ++i)
	{
		if (tags[i] == inTag)
			return true;
	}

	return false;
}