I have a filter that uses a large FFT that is not always needed depending on user settings. I notify the DAW of it’s latency in both OnReset() and OnActivate() but would like to eliminate this latency when the filter is not required (turned off by the user).
I am concerned from past experience that attempting to change latency after a plugin is constructed can cause the DAW to crash - but I see other plugins doing it successfully - even while the DAW transport is running - so there must be a “right way”.
What is the “right way”/safe way to change latency at run time in IPlug?
1 Like
I have tried an approach that seems to work.
When I turn the filter On/Off I set a bool variable in OnParam change I named “ChangeLatency”. At the end of the ProcessBlock - outside the “for” loop - I check ChangeLatency and, if TRUE, I inform the host of the new latency like so:
if (GetLatency() != TotalLatency) SetLatency(TotalLatency);
ChangeLatency = false;
It seems to work OK and sets the correct latency even while the transport is running. It’s not efficient to check that variable on every ProcessBlock but the CPU cost, it seems, is negligible.
Does anyone see any potential problems with this - or does it look like the correct approach?
2 Likes
I’ve been looking into this and I think there is a potential problem. SetLatency() will call IComponentHandler::restartComponent, which has the comment
/** Instructs host to restart the component. This must be called in the UI-Thread context! * @param[in] flags is a combination of RestartFlags * \note [UI-thread & Connected] */
This is for VST3 but I imagine there could be similar issues in other formats. I had also called SetLatency inside ProcessBlock outside any high-frequency loops with no problem. But since this comment says to only call it in the UI thread, I am thinking it would be safer to calculate the latency needed in OnParamChangeUI, call SetLatency, then set a flag that will be checked at the top of ProcessBlock to update things that make the DSP align with what the latency was just set to. Please tell me if you figured out a best practice for this