diff --git a/modules/juce_audio_plugin_client/AU/juce_AUv3_Wrapper.mm b/modules/juce_audio_plugin_client/AU/juce_AUv3_Wrapper.mm index 3a6a351dc1..f4486c112c 100644 --- a/modules/juce_audio_plugin_client/AU/juce_AUv3_Wrapper.mm +++ b/modules/juce_audio_plugin_client/AU/juce_AUv3_Wrapper.mm @@ -1064,18 +1064,43 @@ private: for (int i = 0; i < numWrapperBuses; ++i) { - std::unique_ptr audioUnitBus; + using AVAudioFormatPtr = std::unique_ptr; + const auto audioFormat = [&]() -> AVAudioFormatPtr { - const auto channels = numProcessorBuses <= i ? 2 : processor.getChannelCountOfBus (isInput, i); - std::unique_ptr defaultFormat ([[AVAudioFormat alloc] initStandardFormatWithSampleRate: kDefaultSampleRate - channels: static_cast (channels)]); + const auto tag = i < numProcessorBuses ? CoreAudioLayouts::toCoreAudio (processor.getChannelLayoutOfBus (isInput, i)) + : kAudioChannelLayoutTag_Stereo; + const std::unique_ptr layout { [[AVAudioChannelLayout alloc] initWithLayoutTag: tag] }; - audioUnitBus.reset ([[AUAudioUnitBus alloc] initWithFormat: defaultFormat.get() - error: nullptr]); - } + if (auto format = AVAudioFormatPtr { [[AVAudioFormat alloc] initStandardFormatWithSampleRate: kDefaultSampleRate + channelLayout: layout.get()] }) + return format; + + const auto channels = i < numProcessorBuses ? processor.getChannelCountOfBus (isInput, i) + : 2; + + // According to the docs, this will fail if the number of channels is greater than 2. + if (auto format = AVAudioFormatPtr { [[AVAudioFormat alloc] initStandardFormatWithSampleRate: kDefaultSampleRate + channels: static_cast (channels)] }) + return format; + + jassertfalse; + return nullptr; + }(); + + using AUAudioUnitBusPtr = std::unique_ptr; + + const auto audioUnitBus = [&]() -> AUAudioUnitBusPtr + { + if (audioFormat != nullptr) + return AUAudioUnitBusPtr { [[AUAudioUnitBus alloc] initWithFormat: audioFormat.get() error: nullptr] }; + + jassertfalse; + return nullptr; + }(); - [array.get() addObject: audioUnitBus.get()]; + if (audioUnitBus != nullptr) + [array.get() addObject: audioUnitBus.get()]; } (isInput ? inputBusses : outputBusses).reset ([[AUAudioUnitBusArray alloc] initWithAudioUnit: au