Calling setLatencySamples in prepareToPlay

I’ve seen topics around about setLatencySamples but not quite the one I’m running into exactly… There is a clause in the JUCE VST3 wrapper that I don’t quite understand the significance of, and it is causing a bug in my plugin where latency is not reported correctly when the sample rate is changed.

A summary–
In prepareToPlay, I am using the sample rate to calculate my plugin’s latency and report it to the host using setLatencySamples. I followed this call through to the VST3 wrapper and found this snippet in audioProcessorChanged:

if (! inSetupProcessing)
    componentRestarter.restart (flags);

inSetupProcessing seems to be in place to avoid calling the restarter during prepareToPlay, which means my setLatencySamples gets ignored. (thus the bug on sample rate changes) But since my latency changes are directly dependent on sample rate changes, where else should I be calling setLatencySamples such that the change goes through? (note that the AAX and AU wrappers do not seem to behave this way)

I believe you can call setLatencySamples() from the processBlock() function, so you could calculate it in the prepareToPlay(), store it as a member, then later set it in the audio callback.

1 Like

Good find. This should be documented, it’s pretty not self-evident.

Yeah, the docs say "The processor should call this as soon as it can during initialisation, and can call it later if the value changes." but it doesn’t explicitly say whether or not it’s safe to call on the audio thread!

It’s not if it’s unsafe… if you look into the wrapper code, you’ll find it’s set asynchronously – I found it unreliable setting it synchronously from the audio thread in some hosts (Ableton Live)… but never experienced any crashes or anything.

Rail

Please could you let us know which host you’re using for testing? I tried modifying the DSPModulePluginDemo to set its latency to a fraction of the current sample rate inside its prepareToPlay. When I load the VST3 in REAPER, the latency is correctly reported after changing the host sample rate. It’s possible that the behaviour you’re seeing only affects certain DAWs.

Testing in Ableton, I can reproduce the buggy behaviour. It would still be useful to know which host is causing problems for you, so that I can verify that the fix for Ableton also works for you.

2 Likes

Ah yes, I should have noted – I am only seeing this bug so far in VST3 in Ableton, not in Cubase VST3, Reaper VST3, or Ableton AU. (All on macOS)

1 Like

We’ve fixed the issue here:

Please try out the change and let us know if you encounter any new problems.

That change fixed the issue in Ableton on my end, thanks! Will keep my eye out for any related issues if they pop up.