Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I find a percentage in between two numbers?

Discussion in 'Scripting' started by AntAptive, May 13, 2023.

  1. AntAptive

    AntAptive

    Joined:
    Nov 8, 2020
    Posts:
    7
    Hi.

    So what I'm trying to do is make a volume slider for audio using the audio mixer. I want the max (100%) to be -10db and the minimum (0%) to be -80db. My thought process if -10db is 100% and -80 is 0%, what would, for instance, 75% be? This is what I'm trying to do.
    The reason I'm doing this is because I want the slider to display the percentage instead of the decibel level.

    There might be an easier way to go about this, but any help would be much appreciated! Thank you!
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    See Mathf.Lerp() and Mathf.InverseLerp(). The math of it is pretty simple.

    Mathf.Lerp(-80f, -10f, 0.75)
    gives the 75% value between -80 and -10, or -27.5.

    But beware, that may or may not be what you want on a logarithmic scale like decibels.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Decibels are definitely NOT linear, but the biggest issue with them is that they are relativistic. Moreover there are two kinds of decibel uses. One of them is attenuating, where the zero is the supposed maximum, and then it attenuates to -inf, and the other is the absolute scale which goes from some convenient positive minimum and then scales up, theoretically to +inf. Obviously, because it's relativistic, this is all just a convention. I believe mixers use the first kind, because you kind of want to normalize the amplitude information across a range of devices (so they usually track from -inf to +3 dB to avoid hard clipping).

    Anyway, it's doable if you can accept that you can only ever approach the 0%, not get there. And you need to know and understand exp and log functions and use them accordingly. From my recent experience with log-polar grids I can tell that this tends to be much more easier in code than by looking at math. Math notation is intimidating as hell.
     
    Ryiah, seejayjames and koirat like this.
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    The percentage might be visually intuitive, but it might not "look right" when comparing with the loudness, given the log scaling that the audio mixer uses.

    If you convert to 0-1 range, it can be easier to do some of the exponential/log operations, then you can scale back to 0-100% for visual representation. I'm sure you can do it without scaling, but don't have the formula off the top of my head, been too long since math class hehe.

    The log base for decibels is ~1.2589, so that to the 10th power gives you 10, to the 20th power gives you 100, etc. Might be useful for scaling the percent display if you're using decibels in the calculation somewhere.
     
    Last edited: May 15, 2023
    orionsyndrome likes this.