Forums > MaxMSP

How can I create a slider to do smooth gains

February 15, 2012 | 2:08 am

My math skills are unfortunately very rusty. I have a [gain~] which I want to be able to adjust smoothly from a pedal but I have observed that most of the volume increase happens towards the very top of the range.

I realize that one is to supposed to use some kind of log conversion to compensate for this but I don’t know the right function/coefficients to use.

So if I have a pedal that produces MIDI values from 0 to 127, how can I translate that into the correct values to get perceptually smooth volume changes across the entire range?

Thanks in advance,

February 15, 2012 | 7:08 am

Hi, I think that gain~ is the complex object and it’s really easier to implement than you think. Gain uses a dB (log) scaling and the slider is linear so you just have to do the old school Amplitude scaling with it :

— Pasted Max Patch, click to expand. —

hope this helps.
PS : if you really want to deal with the perceived loudness, you’ll have to deal with the frequency, that would be really more complex I guess :
EDIT : I just had alook at your profile and I guess I misunderstood your question sorry, ignore the post above… :)

February 15, 2012 | 8:32 pm

Hmmm, not sure what’s in my profile that would lead you to misunderstand my question !

The amplitude amount changes more rapidly as one gets to the top of possible values, i.e, much of the gain happens in a very small portion of the slider range.

Therefore, I’d like to arrange things so that the rate of change of the gain slider is reduced the higher is the value of the input control (0..127)

February 15, 2012 | 8:49 pm

Sounds like you might be happier with a linear response:

— Pasted Max Patch, click to expand. —
February 15, 2012 | 9:07 pm

this is an abstraction I use for your situation– generally when I want to change the throw of a physical slider in a midi controller– it remaps a straight line into a parabolic segment
it takes arguments in the same way [scale] does, but the curvature coeff is more useful (in my opinion)

— Pasted Max Patch, click to expand. —
February 16, 2012 | 1:54 pm

Terry, this is exactly what I’m looking for except that the behavior is exactly opposite to what I want.

At the bottom, moving source slider a lot only moves the target slider a little.

I want EXACTLY the opposite of that, i.e, at the bottom, moving the source slider a little will move the target slider a lot, and then at the top, moving the source slider a lot will just move the target slider a little as very slight changes in the gain~ are much more noticable at the top end.

February 16, 2012 | 1:57 pm

Hi, did you try setting the curvature to -1?

February 16, 2012 | 6:57 pm

Did you try the linear response thing I posted? Was that closer to the curve you’re looking for?

February 16, 2012 | 8:30 pm

That one didn’t change the gain~ at all. It was the total level that I was trying to change. In the meantime I came up with a simple log function that I’m getting ready to test when I get to my studio but visually it looks close to what I wanted.

— Pasted Max Patch, click to expand. —
February 16, 2012 | 8:40 pm

goodness gracious, dhj did you read the text in the comment box that came with my abstraction? as error_500 says, alter the curvature argument (the 5th arg)– may i suggest about -0.7 is a good feel. It’s got the same types of args as [scale]

February 16, 2012 | 9:37 pm

It’s true that my patch didn’t change the gain~ slider, but it did try point out that a linear change might be close to what you’re after. If so, why are you using gain~ at all? Why not use the line~ *~ combo? The log/antilog behavior seems silly if what you’re after is a sort of linear response.

Also gain~ goes from -∞ to 0db over the range 0-127. 128 to 157 is adding gain, which you probably don’t want.

February 16, 2012 | 10:53 pm

Well, it’s partially my lack of understanding of some of the audio stuff, where I have very little experience.

I have built a mixing console with individual channel strips and a system that allows me to create VSTs on the fly and route them without patching.

The output of each VST or VST FX goes into a channel strip that has a gain~ slider to control volume. Then the final master output strip uses a gain~ for the final level. Everything goes to a MOTU 828 mk3

I found as I adjusted those gain sliders from the GUI (or via incoming MIDI expression pedals) that volume changes were very concentrated towards the top of the range, i.e. very slight changes in slider movement had huge impact on volume and so I wanted to compensate for that.

I’m absolutely open to better ways of doing this. At this time, Max has completely replaced Apple MainStage for me and it’s just working brilliantly.

I appreciate your comments and response.


February 16, 2012 | 11:15 pm

Part of a set of abstractions is a gain that I use always in my own work. It can be downloaded from or directly using this link:
This jx.gain~ has been modeled to be equal to a fader on a mixing desk. Particularly when controlling with midi faders, it gives the feel you describe you’re looking for, in which gain~ is lacking.

February 17, 2012 | 4:10 am

@Terry yes I did..and I tried negative ales but it didn’t seem to do what I needed. The single expr with scaling seemed todo the trick though I’m stil interested in what Chris had to say

@jvkr thanks for telling me about your library. As soon as I’m at my Mac, I will take a look.


February 17, 2012 | 5:33 pm

@jvkr do your abstractions work with Max 6.x?

February 17, 2012 | 5:45 pm

I just started looking at jx.gain2 (I need stero) and have a few questions. First of all, I don’t really understand what exactly is going on, the purpose of the buffer, peek, line~ and so forth.

What exactly is this doing that could not be done by just appropriately scaling the gain~ slider?


February 17, 2012 | 10:07 pm

The peek~ is used to store 128 values, the default range of the slider or dial that will be controlling it. The development of this set of abstractions started in a time when it made sense to calculate all values at launch time and store them in a table (or a buffer, in the case of floats), rather then recalculating them each time. Especially as there are two powers in the calculation.

The whole idea is that a slider reads out values for a multiplication factor. A line~ is used to avoid discontinuities in the signal.

Maybe it is possible to find values for inc and scale to get similar behavior but I can’t find documentation on what the calculation looks like and don’t feel like finding it out.

This abstraction solves for me the issue that you mentioned in the initial post. I use in almost all cases this gain. The live.gain~ is kind of fine with me too, especially as it is stereo.

Below patch gives a little more explanation.

— Pasted Max Patch, click to expand. —
February 17, 2012 | 11:43 pm

Thank you — most illuminating.

February 19, 2012 | 7:25 pm

I use the following settings with a dial and a gain~ to control it via midi CC:

gain~: Number of Step = 300 (158 by default)
dial : Number of Steps/Range = 100 (128 by default)
Minimum Value = 200 (0 by default)

February 20, 2012 | 11:54 am

Are you then scaling midi cc values 0-127 to dial values 200-300?

February 22, 2012 | 7:46 am

Yes, for me that’s fine but I guess you could also scale it to a wider range. Just fiddled that long with it till I got the result I expected.

Viewing 21 posts - 1 through 21 (of 21 total)

Forums > MaxMSP