Use exponential for dB in linear bar-width?

0 favourites
  • 13 posts
From the Asset Store
Health, Weapon & Rage Bars for your Games. Easy to use & Customize.
  • The player can set the master volume by clicking on full bar (looks like a healthbar). Wherever they click in the bar I subtract that X from the bar's origin (origin left).

    i.e. full bar width is 109, if they click in the middle I get the value 54.5

    Then I subtract 109 from 54.5 and get -54.5 then I divide that by 5 (why? trial and error) to get -10.9 (which I then set the volume to to make it half as loud)

    Now I need to save the -10.9 to Localdata and init the volume to that the next time they load the game - and that part is easy. But when they look at the master volume bar I need it to reflect the current volume. I know the / 5 part of the calculation isn't exact, so I figure I should ask how to get the REAL value so I can reverse it for the correct bar width. Any ideas?

  • Here’s a post with the formula to convert a percentage 0-100 to decibels. I never understood why this was the default since percentages are way more intuitive and actually is what webaudio uses behind the scenes.

    construct.net/en/forum/construct-3/how-do-i-8/user-change-volume-percentage-162705

    If I understand how you’re selecting the volume with a click you should be able to get a percentage with:

    100*(mouse.x-bar.bboxLeft)/bar.width

    Then just convert that to decibels with the formula in the link above.

  • I never understood why this was the default since percentages are way more intuitive and actually is what webaudio uses behind the scenes.

    Decibels actually match human hearing better. Actual human volume perception works on a logarithmic scale too. If you make a linear slider, you'll find the top 50% doesn't seem to change the volume much at all, but the bottom 10% has huge changes to the volume. Using a decibel scale will actually sound more linear to a human.

  • Still whenever making a volume slider it’s still needed to convert a percentage to decibels. 0db is full volume and -infinity is silent.

    Db = 20*log10(percent/100)

    Does that. At least it’s correct at those two points.

    Guess you could do the volume slider like this if one wants to stay in decibels. The -1000 is arbitrary though. Technically should be -infinity to be silent but at some point it’s probably quiet enough but that would have to be found by trial and error.

    Db = lerp(-1000, 0, percent/100)

    Anyways my opinion still stands about decibels and percentages. Percentages are more intuitive to me. Decibels have their use for sure but I haven’t found them useful for my purposes.

  • R0J0hound I think its because of how sound is doubled (similar to what ashley said). the human ear can tell the difference in volume if you were to drop 1 tennis ball vs 2 tennis balls on the floor. but it would not be able to tell the difference from 50 balls vs 51 balls. And if it were linear you could be making volume changes and hear no difference at certain points.

    percentages is an interesting idea but I think the percent is still based on a logarithmic scale right? 1-2 vs 50-51 are two different (human hearing) ranges but are mathematically the same - we just can't hear it as the same!

  • I read a bit more on it.

    With decibels, 0 is full volume and about -80 is sufficiently quiet to be considered silent. Subtracting/adding 6db will half/double the volume.

    So far so good. But using 20*log10(percentage) you get the same result:

    Full volume (1) will give 0.

    Half volume (0.5) will give -6.

    Quarter volume (0.25) will give -12.

    Either seems viable to setting volume and you can convert between the two and get expected results. The underling webaudio api uses percentages for volume.

    Anyways, it’s merely a preference. People who have done a lot of audio processing seem to prefer decibels and a lay person like myself likes percentages.

  • TIL dBs can kill (which I seriously did not know!)

  • R0J0hound

    Full volume (1) will give 0.

    Half volume (0.5) will give -6.

    Quarter volume (0.25) will give -12.

    I feel half as loud sounds like a 10dB cut? Is this just me? I have read about using -6, it just doesn't seem half as loud to me.. could be because of my aging ears!

    (I also commented this on the link you posted above, feel free to ignore it.)

  • You can adjust the formula to give -10 for half. My researching have shown -6db for half in other apps but maybe construct is using a slightly different curve. Godot docs for example uses -6db for half and construct docs use -10db for half.

    Anyways no matter. We can adjust the formula to match construct’s db scale.

    -10/log10(0.5) =~ 33

    So you can use

    Decibel = 33*log10(percent/100)

  • R0J0hound so to go backwards I somehow need antilog right?

    if I start the game out only knowing the masterVol is -7 I need to init the volume bar width to a little less than half.

    what I don't understand is how to reverse the 20*log10(percent) to get the width of the bar.

    EDIT: if the only information I have is: vol(-7) and maxWidth(109) of the bar how do I calculate the actual width?

    in the first scenario the player is setting the volume bar manually so its not an issue but once its set and I save to localstorage the next time the game is loaded I need to start the bar at the width that reflects the stored volume.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • others are suggesting I save the percent of the bar instead of the actual dB attenuation. I suppose that is more logical!

  • Here is the reverse conversion:

    Percent=100*10^(decibel/33)

    So with -7db you’d get the x position on the slider with:

    X=Bar.bboxLeft+ Bar.width*10^(-7/33)

  • R0J0hound thanks! you're the best!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)