-1

I am programming a LUA script so that I can control audio EQ on two audio tracks at same time, such that when one goes up, the other goes down.

In the EQ interface, a user sees 0dB to 36 dB of positive gain, and 0dB down to -inf of negative gain.

The plugin itself internally handles the positive values as floating point numbers from 0.25 - 16.0. This range maps to the 0dB - 36dB range that a user sees in the EQ interface.

Negative values are handled internally as 0.2499999 down to 0.0000002, which covers range in the EQ interface of 0 dB down to -inf dB.

Most people when using an EQ, don't add or reduce more than 16dB or so. So I need a range mapping, such that 0.25 to 1.5773934 (which in the EQ app is 0 dB to 16 dB) in one EQ mirrors/maps to 0.2499999 to .044457 in the other (which is 0 down to -16dB).

This way, when one EQ is 4 dB, the other is -4dB. When one is 12 dB, the other is -12 dB, so on til +-16 dB. Beyond that they will inevitably diverge due to asymmetry of the positive and negative EQ ranges.

After that (beyond the +-16 dB point) I just need a mapping so that the 36dB extreme on one EQ mirrors to -inf on the other (that is, internally, 16.0 on one EQ mirrors to 0.0000002 on the other). I know this mapping won't be linear.

Does this make sense how I explained it?

Initially, I just used a simple linear mapping:

function map(x, in_min, in_max, out_min, out_max)
  return out_min + (x - in_min)*(out_max - out_min)/(in_max - in_min)
end

which I saw at

Mapping a numeric range onto another

and then when I receive an internal EQ gain value on the EQ that is being adjusted, I calculate the value to be mirrored on the other EQ as

if val >= 0.25 then
    mappedval = map(val, nogain, maxgain, nogain, mingain)
else
    mappedval = map(val, nogain, mingain, nogain, maxgain)
end

This doesn't work well at all, because large changes on one EQ can map to very small changes on the other.

So that is why I am trying to get this working in a way where the two EQs are moving opposite to one another in very similar amounts in the +-16dB range.

Ideas?

thanks!

Edit: Looked into this more, and the values are mapped to dB on some sort of log scale, just that it isn't a typical scale. Usually with dB, a decimal value of 1 maps to 0 dB.

In my case, going from decimal to dB has domain [0 1], range [-120 12].

So just trying to work out the equations... This way I can precisely do the mapping across all values

Brian
  • 125
  • 2
  • 15
  • It is a little unclear what `nogain`,`maxgain`, `mingain` are and what the desired output should be. the code seems to work fine with `print(map(1.5773934, 0.25, 1.5773934, 0.2499999, 0.044457))` and `print(map(0.2499999, 0.2499999, 0.044457, 0.25, 1.5773934))` – Nifim Nov 09 '20 at 18:04

2 Answers2

1

Ok, got this working as

function VAL2DB(x)
if x < 0.0000002 then
   x = -120
else
   x = 20*math.log(x/nogain, 10)
end
   return x
end


function DB2VAL(x)
   return (10^(x*0.05))*nogain
end

where nogain is 0.25

Then in main routine I have

mappedval=val
    
if this_track_number ~= track_number then
    
    dBval = VAL2DB(val)
    
    if dBval < -120 then
       mappedval=1.0
    elseif dBval >= 12 then
       mappedval = 0
    else
       mappeddB = -(dBval)
       mappedval = DB2VAL(mappeddB)
    end
    
end

I decided to use 12dB as maximum, and in the EQ plugin noticed that -120 dB is used as -inf.

So this is getting me the behavior I was after.

Brian
  • 125
  • 2
  • 15
-1

You can clamp min/max val if you expect gain to bring your float outside of it's expected range. This should invert your dials though.

if val >= 0.25 then
    local norm = val -0.25  --  normalized to zero
    --  15.75 *2.2857142857143 = 36

    dB = norm *2.2857142857143  --  pos dial
    inverse_dB = -( norm /norm /norm /norm )  --  neg
else
    dB = 16 -( val /val /val /val )  --  neg
    inverse_dB = 144 *val  --  pos dial
end
Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
  • Why `val /val /val /val`? rather than `1 /val /val` or `1/ val^2`? – Nifim Nov 09 '20 at 19:12
  • Thanks - I just edited, as I see that the range of values are a bit different from what I thought. Now it is a question of the x log (y) sorts of equations and their inverse to get my mapping from decimal to dB... – Brian Nov 09 '20 at 21:33