how to get the volume

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By əkaunt

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.

:bust_in_silhouette: Reply From: spymastermatt

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: AudioEffectSpectrumAnalyzerInstance — Godot Engine (stable) documentation in English )

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
_ready():
    spectrum = AudioServer.get_bus_effect_instance(0, 0)

_process(delta):
    volume = spectrum.get_magnitude_for_frequency_range(0, 10000).length()

Thank you for sharing this!!

JayFi | 2022-11-24 00:42