Correct way to force a hosted plugin into Mono or Stereo

Hi all, as the title says if im hosting a vst what’s the correct way to change its layout from stereo to Mono or viceversa? I tried to do it for example to set Mono to Mono using:

vstInstance->getBus(true, 0)->setNumberOfChannels(1);
vstInstance->getBus(false, 0)->setNumberOfChannels(1);

where vstInstance is obviously the pluginInstance, with some plugins works well, others work with a big lag between the change and some crashes, so i guess im doing it wrong considering that the same plugins hosted in the DAW when i set the track from Stereo to Mono and viceversa the change is smooth.
Thx in advance

You need to check if the plugin supports the number of channels, that’s why setNumberOfChannels returns a boolean.

Changing the channel configuration is not a cheap operation in many plugins, oftentimes they’ll allocate/free memory to deal with it.

Thx for answering, the boolean return true and the number of channels change correctly, so i assume it’s something else that crash the plugin and get triggered during the change of the channels, i’ll debug a bit more, so anyway that’s the correct way to do it in case the plugin support it and return true?

Just a shot in the dark, you do call prepareToPlay after changing the channels and before calling processBlock()?

Hi Daniel, thx for answering, after i call the function to change the channels inside prepare to play i call

graph->prepareToPlay(sampleRate, samplesPerBlock);

since the instance is added as a Node in the graph, that should be it right? Even tho i suspect that i call the funcion to sync the channels also in the moment that the plugin is instantiated, and if after that, prepareToPlay doesn’t get triggered there is no call to the graph->prepareToPlay, thx for pointing that