surge-synthesizer / surge

Synthesizer plug-in (previously released as Vember Audio Surge)
https://surge-synthesizer.github.io/
GNU General Public License v3.0
3.1k stars 395 forks source link

S&H LFO -drawing will bug if Latch is on while changing Rate #233

Closed esaruoho closed 3 years ago

esaruoho commented 5 years ago

Steps to replicate:

  1. change LFO to S&H
  2. adjust rate (min-max-min-max) works as expected
  3. Click on Latch
  4. adjust rate (min-max-min-max)

current results: S&H LFO drawing will stutter

s h rate change bugs

got these results with Logic Pro X running newest build (8th January from Kzantow builds) AudioUnit.

kurasu commented 5 years ago

looks like it needs its own private random number generator.

when painting, it will be using the standard c rand() function with a defined seed to draw the same each time, but in this case it will be called from the audio thread as well, causing it to randomly glitch.

baconpaul commented 5 years ago

That will make both the ui and the audio thread glitch right? Although imagine it is hard to hear that audio glitch.

The stdlib has rand_r which is reentrant but requires each thread to squirrel away the state. Guess the solution is to move to that (or some other reentrant rng but that’s a bigger change)

kurasu commented 5 years ago

@baconpaul sure it will affect it, but since its just a random number it doesn't matter. We only want it to be deterministic when we are painting so we always get the same random sequence

baconpaul commented 5 years ago

Sigh OK this one is moderately annoying to fix.

It's easy enough to make a substitute for rand.

if( is_display )
{
 std::default_random_engine engine;
      engine.seed( 17 );
      std::uniform_int_distribution dist( 0, RAND_MAX );
      lfo_rng = std::bind( dist, engine );
}
else
{
   lfo_rng = &rand
}

type thing. Then you have it as a member so the don't interfere. But then you have to plumb this down into correlated_noise_o2mk2 as the rng function to make the call there to rand a function pointer. I'm not 100% sure that it's worth it to fix this bug.

But that's how you would fix it. I'll ponder some more.

mkruselj commented 4 years ago

Actually I'm not sure if I see this issue at all anymore, at least on VST2 and VST3 Win. Maybe it's already fixed?

baconpaul commented 4 years ago

it's still there. can repro it no problem mac au. and it's still hard to fix.

mkruselj commented 4 years ago

Blimey. How come it doesn't happen on Windows, then?

baconpaul commented 4 years ago

i don't know. doesn't happen often on mac. the bug will depend on thread scheduling also so could be that on windows the audio thread isn't let back in while the separate display lfo is being sampled? lots of reasons.