2

I have audioMixerGroup.audioMixer.GetFloat("AllVolume", out tmp); tmp will have volume in db. I vant to convert db (-80, 0) to slider's value (0, 1).

in short, I need to do How to set a Mixer's volume to a slider's volume in Unity? just the other way around

grouptout
  • 46
  • 1
  • 8
  • The generic problem you are trying to solve is converting between a logarithmic (audio) scale and a percentage. As for Unity specific did you come across this? https://gamedevbeginner.com/the-right-way-to-make-a-volume-slider-in-unity-using-logarithmic-conversion/ – shox Aug 18 '20 at 18:28

3 Answers3

3

You can create a Remap function:

float Remap(float value, float min1, float max1, float min2, float max2) 
{
    return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}

Implementation:

audioMixerGroup.audioMixer.GetFloat("AllVolume", out tmp);
slider.value = Remap(tmp, -80f, 0f, 0f, 1f);
MysticalUser
  • 404
  • 5
  • 13
0

When setting the volume you will utilize the following code:

Mathf.Log10(value) *20;

What you need is the inverse:

Mathf.Pow(10, (value/20);
0

I would do the below when you need to use the tmp variable:

(tmp + 80f) * 0.01f
Rob
  • 93
  • 3