Auto-gain (gain compensation) post fx
Hi guys : )
I need to balance/auto-gain two audio signals (dry and wet).
I made this basic patch to figure out my problem but I was unable to reach the goal.
Hope you can help me.
Thanks guys.
Why not use peak and/or trough to derive the amplitude of each signal and then multiply (the gain of) the Fx output by the ratio of the two amplitudes? You'll have to consider the frequency at which you do this relative to the frequency of your audio otherwise you might get crazy results!
Hi Andy. Thanks for your reply.
I tried "peakamp~" ("trough" is not an audio processor) but I had a bad gated-distorted sound from the wet signal.
I subtracted the two float values from the two peakamp~ objects (dry and wet) and than I had a negative float number.
I summed that number with a simple 1 to have my amplitude difference to control the multipliyer of the wet signal.
As I said...I got a "gated" distorsion.
I think the reasoning is right but maybe I am missing some pieces.
Yes, point taken - peak and trough only have data versions, not audio ones. Sorry. Although there are the snapshot~ and sig~ objects that do conversions if you need them.
peakamp~ (or mc.peakamp~ for stereo/multichannel signals) should work just the same - just make sure that you spread the analysis across enough samples.
You need to use the ratio (i.e. A/B), not the difference (i.e. A-B) between the amplitudes. As long as you protect against B being zero (trying to divide by 0 will cause a computation error) then you should always get a positive value. However, you need to be clear in your mind as to what the range of values coming out of peakamp~ is so that your ratio (A/B) has a meaningful value. If the amplitude of the dry and wet signals are identical then the ratio should be 1.
Assuming you can scale the output of peakamp~ to give you a range from 0 (silent) to say 100 (maximum) as a float then, if A is the dry signal and B is the wet signal, the gain change to balance the wet signal will be (A / B). For example, if the amplitude of A is twice the amplitude of B then the amount of gain applied to B will be a factor of 2 to balance it up.
To avoid sudden changes in gain causing a gating effect you need to create a "moving averages" algorhythm. By this I mean averaging blocks of samples. Say you get peakamp~ to report every 50 samples, add together 10 values and divide the total by ten. When the next block of 50 samples is reported repeat the calculation on the previous ten values, i.e. dropping the first value and adding the last. bucket is a good object to consider for this.
Here is a very basic patch I created that smooths out a random signal to try to give you some idea of what I mean:

There are other objects that you could consider using for your own patch, such as avg~, average~, normalize~, etc.
dont forget that when analyzing peak or power you are doing that in "windows", i.e. over time.
this causes the analyzed modulation signal to be delayed in relation to the signal to be processed. power 100 ms here requires delay ~50 ms there.
Roman, a certain amount of lag is always going to occur when you are processing live signals. It's a question of getting the balance right between achieving a desirable effect with the shortest possible latency.
of course it is arguable if the center value will always sound the best, but in theory it is the best one.
give that you are using signals, that is. otherwise the values are "wrong" anyway (but of course still might work well)
average/rampsmooth and slide~ are also worth a try, they let you apply different in and out slopes.
Hi guys.
Thanks for your advices but, unfortunately, I was unable to implement those info you provided.
I have little knowledge of Max.
If possible or not too much trouble for you...If you could send me a patch with balance already built in it would be much appreciated.
I believe that with the patch on hand I can associate advice with the practice.
Thank you very much.
I have done! When you see postings with following box added:
what you need to do is to click on the box to expand it and then click on the "Copy To Clipboard" box. Open Max and then under the File menu you select "New From Clipboard" and voila! Your patch appears.
Ok. I've put something together for you to play with. I'm not sure if it's quite what you were looking for but it's a start:

I've taken a drum loop as an audio source to play with. I've ignored any stereo balance here but you could duplicate things if both channels are important. The clean (left) signal goes through the left hand gain control which is fixed at 0dB. The dirty (right) signal goes through another gain control which feeds the gain reduction algorithm. As you increase the right side gain, you will see the gain on the clean side reduce. If you go up to 0dB on the dirty side, you should get a mixed signal that is of roughly similar level. Above 0dB on the dirty side and the output will be saturated with the dirty signal.
The patch is pasted here for you to import into Max via the clipboard:
Thank you so much Mr. Andy, for the patch and for the accurate explanation.
That's exactly what I wanted to achieve.
Frank : )
You're welcome. Thank you for the feedback.