EDIT: Smoothing the response of a sample peak meter

Hi everyone,

I have instantiated a sample peak meter in my plugin, which takes the maximum value of each sample within the buffer per channel, and uses that value to paint a section of the GUI green. Right now, the meter works, and is pretty jumpy. I am trying to smooth out the response. I have tried everything I know from LinearInterpolation on the processor side, to using a SmoothedValue on both the processor and the editor, to changing the frame rate of repaint() using Timer. I just can’t seem to find something that works.

My thought is that if I could build a decay function for the meter that interpolates the value of what the editor is to paint, rather than showing the values all in instantaneous, real time. Do you all know how to accomplish something like that?

Thanks,
Andy

You can hold the value in a variable, then each callback check to see if the new sample is larger. If it is, update it. If it isn’t subtract the desired amount from the held variable to create the decay effect. This will give you an instant rise, with a decaying fall.

2 Likes

If you’re metering an actual, unsmoothed audio signal, max per buffer is pretty rough indeed. You need an envelope follower (running in the audio thread). A basic peak follower would be something like Fandusss suggested:

x = std::abs (x);
if (x > yPrevious)
    y = x;
else
    y = x + k * (yPrevious - x);
yPrevious = y;
return y;

where k = exp(-1/n) (fairly approximated by n/(n+1)) and n is your decay time in samples. Then in your UI Timer you use the last output of this filter for your meter.

There’s a BallisticsFilter in Juce DSP classes that would work for this too.

2 Likes

Thank you all! I got it to work!