fundsp icon indicating copy to clipboard operation
fundsp copied to clipboard

Significant latency in examples

Open bonsairobo opened this issue 6 months ago • 18 comments

I'm not sure if this is representative of fundsp as whole, or if there is some kind of issue with my OS config, but the keys example has an unusable amount of latency (probably like 200ms?) between key presses and notes sounding. The latency is present on both my MacBook and desktop Linux machine. I have already done some linux configuration for real-time audio that was good enough to eliminate most latency from VSTs running in Reaper.

Is this a known issue?

EDIT: See Resolution

bonsairobo avatar Jun 29 '25 22:06 bonsairobo

have you ran it in release? (reverb is known to be too slow in debug) i can't think of anything else, it has no latency for me

fresh clone test

https://github.com/user-attachments/assets/28e06ae4-ed15-4023-b0bf-0cce3f0541a3

tomara-x avatar Jun 30 '25 04:06 tomara-x

Yea I'm running in release.

bonsairobo avatar Jun 30 '25 17:06 bonsairobo

what happens if you go to https://tomara-x.github.io/lapis/ and execute

set_out_device(0, 0);
"keys" = true;
let seq = Sequencer::new(false, 1);
let be = seq.backend();
let filter = (pass() | dc(1729) | dc(1)) >> lowpass();
let verb = (pass() | pass()) & reverb_stereo(40, 3, 0.4);
let limit = limiter(0.1, 0.1);
let g = be >> filter >> limit >> pan(0) >> verb;
g.play();
"a" = "seq.push_relative(0, 0.5, Fade::Smooth, 0.01, 0.01, sine_hz(440));"

and give the a key a press?

tomara-x avatar Jun 30 '25 17:06 tomara-x

Long latency between pressing a and hearing a sound

bonsairobo avatar Jun 30 '25 18:06 bonsairobo

but do you see the text printed immediately? or is there latency between pressing the key and seeing the text?

tomara-x avatar Jun 30 '25 18:06 tomara-x

No latency for seeing the text.

bonsairobo avatar Jun 30 '25 18:06 bonsairobo

https://github.com/user-attachments/assets/6b4de0c6-21ed-4bb8-9028-3127e0663ab4

bonsairobo avatar Jun 30 '25 18:06 bonsairobo

how about this?

set_out_device(0, 0);
"keys" = true;
let seq = Sequencer::new(false, 1);
let be = seq.backend();
let filter = (pass() | dc(1729) | dc(1)) >> lowpass();
let verb = (pass() | pass()) & reverb_stereo(40, 3, 0.4);
let g = be >> filter >> pan(0) >> verb;
g.play();
"a" = "seq.push_relative(0, 0.5, Fade::Smooth, 0, 0, impulse::<U1>() >> pluck(440, 0.5, 1));"

tomara-x avatar Jun 30 '25 19:06 tomara-x

Better, but still there.

https://github.com/user-attachments/assets/45c874e8-46b1-4904-bdad-a2c452aa510b

bonsairobo avatar Jun 30 '25 19:06 bonsairobo

i put your video in audacity and i can measure 100ms between the sound of you pressing the key and the sound of the pluck.

can you do one more test with the same setup? run this and press a

beep.zip

i'm thinking it's an input thing, because there's nothing in fundsp that would cause that much latency

tomara-x avatar Jun 30 '25 20:06 tomara-x

Much better

https://github.com/user-attachments/assets/776fc84b-7657-4cb5-8e18-dfe95c91b9f3

bonsairobo avatar Jun 30 '25 21:06 bonsairobo

that's around 90ms tho? i'm thinking also this being native, while the other tests were in web, might be the reason for that difference oh and there's also an egui version difference, this used 0.31.0, while fundsp's example used 0.29.1

tomara-x avatar Jun 30 '25 21:06 tomara-x

Ah, yea 90ms isn't great. It felt better though, subjectively.

bonsairobo avatar Jun 30 '25 21:06 bonsairobo

hehe without measuring i can't perceive any of that

tomara-x avatar Jun 30 '25 21:06 tomara-x

I also just modified examples/input to just do a passthrough without any effects. I'm playing my synth into it, and there is still a lot of latency.

bonsairobo avatar Jul 02 '25 05:07 bonsairobo

I thought maybe I could reduce the latency by shrinking the buffer size of the cpal devices. My Reaper audio config looks like this:

Image

However, changing the cpal::BufferSize from Default to Fixed(256) seems to totally break the audio; no sound comes out anymore.

bonsairobo avatar Jul 02 '25 05:07 bonsairobo

OK I finally figured it out! Here's what I needed to get low latency:

  1. Upgrade cpal to version 0.16.0.
  2. Enable the audio_thread_priority feature of cpal.
  3. Manually override the cpal::BufferSize to be Fixed(256).

I think it would be a good idea to apply these changes to the fundsp examples.

bonsairobo avatar Jul 02 '25 06:07 bonsairobo

@bonsairobo , thanks, that fixes my latency issue too.

psiphi75 avatar Jul 06 '25 01:07 psiphi75