Load a plugin and match number of inputs to audioDeviceManager

How can one load a plugin and match its number of inputs to the configuration of the audioDeviceManager?

At startup I’m initializing the audioDeviceManager as follows:

    ScopedPointer<XmlElement> savedAudioState (getAppProperties().getUserSettings()
                                               ->getXmlValue ("audioDeviceState"));
    audioDeviceManager.initialise (0, 2, savedAudioState, true);

I have the following procedure to load a plugin:

void loadPlugin(const PluginDescription* pluginDescription)
{
    std::cout << "Loading plugin "<<pluginDescription->descriptiveName <<"\n";
    const double sampRate = audioDeviceManager.getCurrentAudioDevice()->getCurrentSampleRate();
    const double bufSz = audioDeviceManager.getCurrentAudioDevice()->getCurrentBufferSizeSamples();
    String errorMsg;
    if (thePlugin)
    {
        thePlugin->suspendProcessing(true);
        thePlayer.setProcessor(nullptr);
        thePlugin = nullptr;
    }
    thePlugin = formatManager.createPluginInstance(*pluginDescription, sampRate,bufSz,errorMsg);
    if (thePlugin)
        std::cout << "Loaded plugin \n";
    else
        std::cout << "Plugin error "<<errorMsg<<"\n";
    juce::AudioProcessor::BusesLayout bl = juce::AudioProcessor::BusesLayout();
    bl = thePlugin->getBusesLayout();
    AudioChannelSet cs;
    cs.addChannel(AudioChannelSet::ChannelType::left);
    cs.addChannel(AudioChannelSet::ChannelType::right);
    if (thePlugin->getBusesLayout().inputBuses.size()>0)
        //????;
    thePlayer.setProcessor(thePlugin);
    audioDeviceManager.addAudioCallback (&thePlayer);
    thePlugin->suspendProcessing(false);
    
    MidiMessageCollector &mmc = thePlayer.getMidiMessageCollector();
    processor->pluginMessageCollector = &mmc;
    thePlugin->setPlayHead(processor);
    processor->synthMessageCollectorReset(sampRate);
    thePlayer.getMidiMessageCollector().reset(sampRate);
}
AudioPluginFormatManager formatManager;
KnownPluginList knownPluginList;
KnownPluginList::SortMethod pluginSortMethod;
AudioDeviceManager audioDeviceManager;
AudioProcessorPlayer thePlayer;
ScopedPointer<AudioPluginInstance> thePlugin;

All this works fine as long as I load plugins with zero input buses. However I get an assert in AudioProcessor::setPlayConfigDetails if I choose a plugin with inputs.

If I initialize with:

audioDeviceManager.initialise (2, 2, savedAudioState, true);

Now I can load plugins with one input bus but get asserts for plugins with zero inputs.

What do I need to insert at “???” in my above loadPlugin() procedure to match the plugin to the device manager?

I’ve tried deleting buses on the plugin but that’s not allowed. I’m not clear on how to dynamically add inputs to the audio processor.

[Edit] I just watched Fabian’s presentation at ADC 2016 on the new bus classes but I don’t think it helps in this situation. I want to be able to load plugins (for example) like Pianoteq that always has zero inputs and FM8 that always has 2 inputs. However each only loads if audioDeviceManager matches its number of inputs. So I can’t solve this problem by configuring the plugin as discussed in Fabian’s the presentation. I apparently need to reconfigure the device manager on the fly and so far I haven’t found a way to do that.

Can someone help?

It’s probably best you try to come up with some channel count adapter/routing matrix scheme that allows the plugins to work with any sensible audio device configuration. It’s not a good idea to expect either the plugins or the audio device can be forced into some particular configurations.