# How to calculate attack and release time on a compressor?

Tell me, please, how to calculate the time of attack and release on the compressor? I know that there is an option with the Euler number, but I’m not sure why this is necessary. In my opinion, the time of attack and release is: SampleRate / 1000 * sliderAttackValue.
However, this formula that I found on the Internet confuses me.
double Attack = exp(-1/(0.001 * SampleRate * sliderAttackValue));
Tell me, how right?

Are you trying to calculate the attack time in terms of the number of samples? Is sliderAttackValue in units of milliseconds? Is SampleRate in units of 1/seconds (Hz)?

If yes to all of these:

N = (SampleRate / 1000) * sliderAttackValue

which is what you have.

I have no idea what the second formula is calculating, but would be curious to know where you found it so that maybe I can figure it out.

1. Yes, that’s it. I don’t know any other method how to calculate the attack time, if not by the number of samples.
2. sliderAttackValue - in milliseconds.
3. SampleRate - 44100 (for example).

I found the code for multiband compressor by Amogh Matt on GitHub. There was this code.
// compression : calculates the control voltage
float alphaAttack = exp(-1/(0.001 * cSampleRate * cAttack));
float alphaRelease= exp(-1/(0.001 * cSampleRate * cRelease));

I would give a link to Matt`s GitHub, but for some reason I can’t access the site. Perhaps I do not quite understand what is control voltage and here the attack.

That exponential equation comes from the step response of a first order lowpass filter. Instead of using linear ramps for the balistics (time behavior) of a compressor, you can use a multiplicative or actually relative approach by using a first order lowpass with the alpha values to calculate the coefficients.
That’s way easier to implement, as you don’t have to change the step size of your linear ramp all the time when the input level changes.

2 Likes

From the code notes it sounds like Amogh’s compressor is based on an example from the the book Audio Effects: Theory, Implementation and Application.

I did not know that low frequencies and loudness can influence the compressor attack time. I thought it was only time

The lowpass is not for the audio signal, but for its RMS or peak value. A first order IIR low-pass is the same as a leaky integrator, and it will follow the input signal. The time-constant of that integrator will tell you how fast it can follow the input. So when the detection signal of your compressor is above threshold, the leaky integrator will follow the signal, and will be at 63.2% of the target value after the attack time passed.

If you want to implement ballistics with a ramp with duration `attack * fs` then it’s quite hard to figure out the steepness of your ramp, as it will always change when the signal changes. Example: threshold -10dB, signal jumps to 0db and stays there for half of the attack time and then goes even higher to +10dB. How would you implement the ramp? Und what if the signal always changes?

I guess you would come up with something like calculating the difference and base the steepness of the ramp on that difference. And what you eventually got is a leaky integrator anyhow 1 Like

Thanks, I will try to make attack and release with exponential equation

https://christianfloisand.wordpress.com/2014/06/09/dynamics-processing-compressorlimiter-part-1/ I’d recommend reading through these!

Thank you, David, I will read.