The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

Hey there!

I'm trying to create a volume slider that goes from 0 to 100 but the volume property is in db. Which is the best way to "convert" the values from the slider?

in Engine by (519 points)

4 Answers

+1 vote
Best answer
  1. In script (can't set this low in the inspector) set the slider min
    value and step to 0.0001
  2. Set the slider max value to 1
  3. On the value changed signal from the slider, set the volume of your
    audio player to log(slider_value) * 20
by (519 points)
+11 votes

There's an official GDScript function called linear2db and its counterpart db2linear:

print(linear2db(1)) # 0
print(linear2db(0.1)) # -20
by (4,246 points)
0 votes

The thing is that the slider naturally outputs a linear value (0 .. 8 in my case) that you want to exponentially increase in terms of negative db. Here is the code that I implemented:

func _on_Volume_value_changed(value):
g.settings.volume = value
var db = -pow(8.0 - value, 1.8)
AudioServer.set_bus_volume_db(0, db)

You can adjust the 1.8 exponent value to change the range of attenuation that you may need.

by (202 points)
0 votes

A much better way is to use the lineartodb. You can check out these video if you need more explanation on how to use it.

Make a volume slider in 3 minutes

by (75 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.