Best way to process animation in getNextAudioBlock()?

I am currently working on a little animation project with a 2D grid of spring forces. If you grab a bob of a spring with the mouse it is moving each spring and therefore the bob position updates. Now I wanted to translate each position displacement to an individual level for some sound (in my case just a bunch of sinewave wavetable osc from the juce tutorial).
I got the following code in my processing block:

The code works “fine” but causes crackling because it’s not properly ramping because the GUI thread and audio thread aren’t synced right? I tried to buffer the levels in each call but it didn’t work either.
I thought about using the applygainramp but didn’t get it to work because I got individual levels for each oscillator.
Does anybody know how I could get this to work or give me any good tips & references on how to properly process GUI animation?

Do not do GUI stuff on the audio thread. You’ll have to move your animation code to the editor thread.

See e.g. this article for best practices.

1 Like

Thanks for your reply, but I am just retrieving values from the components - actually isn’t it the same like automating a slider callback really quickly and updating the level value?
Also thanks for the reference!


Hard to say what’s going wrong exactly with this code snippet, but if you share stuff between your GUI thread and audio thread, you’ll definitely need some kind of synchronisation to ensure thread safety. Does not mean that your exact problem comes from this, but if you’re not familiar with these topics, this ADC talk is a really good place to start:

associated tools presented in the talk are available here:

Another thing that can cause the crackling you’re talking about is that the level you retrieve from your GUI is probably updated at the GUI thread rate, but you need to smooth this level at audio rate. Maybe have a look at:

Hope this helps.

1 Like

Thank you very much, the SmoothedValue class was exactly what I needed. This is the updated code for anyone who is curious:

And thanks for the ADC talk ref, will have a look at it!