OSStatus AUEffectBase::Initialize()

in Source/AUEffectBase.cpp [42:103]


OSStatus AUEffectBase::Initialize()
{
	// get our current numChannels for input and output
	const auto auNumInputs = static_cast<SInt16>(Input(0).GetStreamFormat().mChannelsPerFrame);
	const auto auNumOutputs = static_cast<SInt16>(Output(0).GetStreamFormat().mChannelsPerFrame);

	// does the unit publish specific information about channel configurations?
	const AUChannelInfo* auChannelConfigs = nullptr;
	const UInt32 numIOconfigs = SupportedNumChannels(&auChannelConfigs);

	if ((numIOconfigs > 0) && (auChannelConfigs != nullptr)) {
		bool foundMatch = false;
		for (UInt32 i = 0; (i < numIOconfigs) && !foundMatch; ++i) {
			const SInt16 configNumInputs = auChannelConfigs[i].inChannels;   // NOLINT
			const SInt16 configNumOutputs = auChannelConfigs[i].outChannels; // NOLINT
			if ((configNumInputs < 0) && (configNumOutputs < 0)) {
				// unit accepts any number of channels on input and output
				if (((configNumInputs == -1) && (configNumOutputs == -2)) ||
					((configNumInputs == -2) &&
						(configNumOutputs == -1))) { // NOLINT repeated branch below
					foundMatch = true;
					// unit accepts any number of channels on input and output IFF they are the same
					// number on both scopes
				} else if (((configNumInputs == -1) && (configNumOutputs == -1)) &&
						   (auNumInputs == auNumOutputs)) {
					foundMatch = true;
					// unit has specified a particular number of channels on both scopes
				} else {
					continue;
				}
			} else {
				// the -1 case on either scope is saying that the unit doesn't care about the
				// number of channels on that scope
				const bool inputMatch = (auNumInputs == configNumInputs) || (configNumInputs == -1);
				const bool outputMatch =
					(auNumOutputs == configNumOutputs) || (configNumOutputs == -1);
				if (inputMatch && outputMatch) {
					foundMatch = true;
				}
			}
		}
		if (!foundMatch) {
			return kAudioUnitErr_FormatNotSupported;
		}
	} else {
		// there is no specifically published channel info
		// so for those kinds of effects, the assumption is that the channels (whatever their
		// number) should match on both scopes
		if ((auNumOutputs != auNumInputs) || (auNumOutputs == 0)) {
			return kAudioUnitErr_FormatNotSupported;
		}
	}
	MaintainKernels();

	mMainOutput = &Output(0);
	mMainInput = &Input(0);

	const AudioStreamBasicDescription format = GetStreamFormat(kAudioUnitScope_Output, 0);
	mBytesPerFrame = format.mBytesPerFrame;

	return noErr;
}