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.

Hello

Having a problem, where using several FIIR filters, and processing all of them introduces an expected delay. Now I am trying to call setLatencySamples() to report this delay, but it does not seem to work.

On playback there is enough wait time between each audioBlock that it works fine, but if the audio is bounced offline then the filters’ processing is lost.

Please help.
Andres

Hey Andres!

If I understand correctly, it sounds like the bug you are experiencing is that delay comp is working in your DAW in realtime processing, but not with offline processing, correct? If so, do you mind creating a new topic so that it will be easier for other folks to find when searching? (The original topic is a bit different, where setLatencySamples was specifically not working only when called from prepareToPlay) Happy to respond in your new topic to try and help if I can. (Or better yet, maybe you’ll get some of the JUCE experts in there as well!)

Please include the DAW, DAW version, and OS where you are experiencing the issues. Also, if you are using a DAW that has latency reporting (such as Ableton Live), please let us know whether the DAW is reporting the latency that you expect.
(If you are using Live, here is how you view the reported latency: https://help.ableton.com/hc/en-us/articles/360001820360-Viewing-the-latency-of-a-plugin-or-Live-device#:~:text=Hover%20your%20cursor%20over%20the,the%20latency%20of%20a%20device. )

Best of luck! Feel free to link your new topic in your post to leave a trail of breadcrumbs.

Hey Jack

Thank you for your suggestion. I did create the new post here…

Andres

1 Like