+2 votes

is there a way to get the current volume of AudioStreamPlayer? volume_db only gets the volume that was set to begin with, not how loud it actually currently is.
AudioServer.get_bus_volume_db(AudioServer.get_bus_index('Master')) also only gets the originally set value. i know that godot knows how loud it actually is cuz the audio tab shows the colors jumping up and down with the music.

in Engine by (16 points)

1 Answer

+2 votes

Old question I know, but in case anyone else is looking.

You can use the AudioSpectrumAnalyzer for this.
In the Audio tab at the bottom of the editor, under the Master bus, click Add Effect and then select SpectrumAnalyzer.

In your code you can get the analyzer using AudioServer.get_bus_effect_instance(a, b), where a is the bus index (0 for Master) and b is the effect index (also 0, assuming you only have the SpectrumAnalyzer on your Master bus)

You can then use get_magnitude_for_frequency_range function to get the current volume of the Master bus (See here: https://docs.godotengine.org/en/stable/classes/class_audioeffectspectrumanalyzerinstance.html )

Assuming you just want the volume and are not worried about limiting to a specific frequency range, just use 0 and 10000 as your parameters. This gives you a Vector2 ( I believe for each channel, though it's not documented), which you can convert to amplitude using length()

An example code which does the above:

var spectrum
var volume
    spectrum = AudioServer.get_bus_effect_instance(0, 0)

    volume = spectrum.get_magnitude_for_frequency_range(0, 10000).length()
by (20 points)
edited by

Thank you for sharing this!!

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.