Cabbage Logo
Back to Cabbage Site

Latency reporting: best practices

Hi,

I know the latency can be reported with the latency() identifier.
My plugin introduces a 20ms delay, and I’d like to declare it to hosts.

The number of samples to declare is then: 20ms/1000*44100Hz = 882 samples
What if the host uses a different sample rate?

Capture d’écran 2024-08-15 à 09.51.13

Is there a way to take it into account? From my understanding there is no reserved channel to know the sample rate of the host.

Thanks!

if at 44000 is 20ms/1000 * 44100 the on 48000 is 20ms/1000 * 44100 *( 44100/48000) , and so on

If you leave out the sr assignment in your Csound header section, i.e, sr = 44100, which Cabbage leaves out by default, then sr will always be equal to the host SR. If you change the host SR, sr will also change. In fact, if you change the host SR, Csound get recompiled with the new SR. But you shouldn’t need to concern yourself with that. It should be seamless.

1 Like

Thank you very much! I’ll use sr and change dynamically latency() then.

You won’t be able to dynamically change the latency as it needs to be broadcast to the host before you start playback. If you leave it out, the PDC should be equal to the ksmps, which should be enough to cope with most situations. If you set latency yourself you might be asking for trouble.

Does plug-in performance get throttled so it matches the declared latency?

When you load your Cabbage plugin, the host will register the PDC and run everything accordingly. I don’t think there is any throttling taking place. Throttling plugins is not a great look for a host, but perhaps some do. I’m not aware of any, but I don’t live in that world :wink:

To be fair, I hadn’t even considered that I could calculate latency and communicate that to the DAW. But if I gave it the wrong value it could either incorrectly compensate (more likely, I guess?) or throttle performance so that the defined latency was realized.

I could imagine a plug-in having variable latency depending on what it is doing, but maybe that’s evidence of poor design.

Thank you. Then should I update dynamically ksmps based on sr?

I know the delay in my plugin is 20ms, so based on sr and this value I can convert it in a # of samples and change ksmps, without declaring the latency. I don’t know if changing the sample rate in a DAW will update this value but at least I know it will work when the project loads.

what you can do is predefine max latency from start. Then delay signal within this range to adjust plugin latency.

1 Like

So … throttle it yourself, I guess.

I’m not sure you can dynamically change ksmps on Csound? But if you know the delay and it won’t change, you can just set it from the off?

Thanks for your answers.

In summary, should I set ksmps to sr*20ms/1000 (or a bit higher, let’s say 110% of this value) and remove the latency declaration?

Thanks!

I think that’s best. The latency identifier is really only for special cases.

1 Like

This is very interesting topic. It is not that transparent what and how different parameters influence latency in Csound. A bit of topic but still related, I was experimenting a bit with ksmps, SW and HW buffers and tried to understand how they influence latency but I was only half-way successful (look this topic in Csound forum: https://forum.csound.com/t/demystifying-ksmps-sw-and-hw-buffers/1431 and Steven’s response in discord https://discord.com/channels/784849854270537790/784853359566782484/1172743179049639987).

In the end, it seems to me that the largest value among those three parameters determines the overall latency.

I think Kzz’s reply makes the most sense here. The reported plugin latency will assumedly be used by the host to delay all other tracks to come out phase synchronized with the sound from the track that the effect is on. Since it is difficult (within Csound/Cabbage) to get the host sr before the plugin starts Csound running (and by that time it is too late to modify the latency reported to the host). You could set a reported latency to be the maximum within a reasonable range of sr choices, and then adjust (with an extra delay at the end of your plugin processing) so that the actual latency matches the latency you have reported to the host. This way, your Cabbage plugin audio will come out phase synced to other tracks in the DAW project (assuming the user has enabled PDC - plugin latency compensation - in the DAW)

1 Like