# Calling setLatencySamples in prepareToPlay

**URL:** <https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131>\
**Category:** Audio Plugins\
**Created:** [October 5, 2021, 7:02pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131 "2021-10-05T19:02:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackwcampbell](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jackwcampbell](https://forum.juce.com/u/jackwcampbell)\
**Post date:** [October 5, 2021, 7:02pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/1 "2021-10-05T19:02:40Z")

</div>

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:

```auto
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)

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [October 5, 2021, 10:06pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/2 "2021-10-05T22:06:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kamedin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kamedin/32/11711_2.png) [@kamedin](https://forum.juce.com/u/kamedin)\
**Post date:** [October 6, 2021, 4:23am UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/3 "2021-10-06T04:23:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [October 6, 2021, 8:32am UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/4 "2021-10-06T08:32:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [October 6, 2021, 8:44am UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/5 "2021-10-06T08:44:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 6, 2021, 12:10pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/6 "2021-10-06T12:10:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jackwcampbell](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jackwcampbell](https://forum.juce.com/u/jackwcampbell)\
**Post date:** [October 6, 2021, 2:31pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/7 "2021-10-06T14:31:36Z")

</div>

> [@reuk](#):
>
> Please could you let us know which host you’re using for testing?

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)

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 7, 2021, 5:03pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/8 "2021-10-07T17:03:02Z")

</div>

We’ve fixed the issue here:

> <https://github.com/juce-framework/JUCE/commit/47dcc31e775edbaef47abacdebc86467bc4c47d1>

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

---

<div class="post-metadata">

**Author:** ![jackwcampbell](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jackwcampbell](https://forum.juce.com/u/jackwcampbell)\
**Post date:** [October 7, 2021, 5:51pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/9 "2021-10-07T17:51:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baKno](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@baKno](https://forum.juce.com/u/baKno)\
**Post date:** [April 19, 2023, 2:17pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/10 "2023-04-19T14:17:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jackwcampbell](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jackwcampbell](https://forum.juce.com/u/jackwcampbell)\
**Post date:** [April 19, 2023, 3:13pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/11 "2023-04-19T15:13:16Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![baKno](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@baKno](https://forum.juce.com/u/baKno)\
**Post date:** [April 19, 2023, 3:18pm UTC](https://forum.juce.com/t/calling-setlatencysamples-in-preparetoplay/48131/12 "2023-04-19T15:18:45Z")

</div>

Hey Jack

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

> [@How to report Plugin latency?](https://forum.juce.com/t/how-to-report-plugin-latency/55869):
>
> 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. In Logic there is no apparent change when calling setLatencySamples(). In Reaper there is an ever weirder behavior, when playing a…

Andres
