[Audio] tweening AudioStreamPlayer volume_db from -inf db to 0.0db

Godot Version

4.3

Question

In the context of a fade in through Tween for the AudioStreamPlayer’s volume_db:

linear_to_db(0.0) is actually -inf (or at least that’s what’s printed, which can cause NaN errors), -inf would certainly silence audio if applied to volume_db I guess, and when I do assign it:

_my_audio_stream_player.set_volume_db(linear_to_db(0.0))

It actually works fine, the volume for my AudioStreamPlayer is completely silent. Good, but weird, maybe it’s applying a closer negative number that actually is supported? idk.

Real problem comes when attempting to tween volume_db from -inf to 0.0, the latter being the normal volume for the AudioStream in .ogg format (for higher overriding values from game volume config I would change the actual bus volume, or that’s what I had in mind), for a fade in I just do:

var fade_in_music = create_tween()
fade_in_music.tween_property(_my_audio_stream_player, "volume_db", linear_to_db(1.0), 1.0)
E 0:00:01:0559   set_volume_db: Volume can't be set to NaN.
  <C++ Error>    Condition "Math::is_nan(p_volume)" is true.
  <C++ Source>   scene/audio/audio_stream_player.cpp:62 @ set_volume_db()

Note: I also tried tween_method, same error.
Now that I’m actually tweening, it does indeed complain. But not if volume_db
is set a single time.

It’s quite posible I just don’t know how to handle db’s, db conversions from linear, I’m just not doing the tween correctly, or something else.

Alternatively I thought of having min/max volume in db’s values, but I tested with the min db’s and couldn’t find something that was truly silent, down to even -90db I could still hear with volume at 100% (I mean, maybe I could try going lower, but I just don’t trust it, and I think converting from 0 to 1 linear is likely best).

Might be able to use .from on your tween to assert that the volume_db is not tween from -inf, which sounds like your error as you assessed.

var fade_in_music = create_tween()
fade_in_music.tween_property(_my_audio_stream_player, "volume_db", linear_to_db(1.0), 1.0).from(linear_to_db(0.1))
1 Like

Pretty good solution (I mean it performs the desired effect), This is probably going to be the equivalent to deciding some low db value (just in linear space though).

I had to set .from(linear_to_db(0.0001)) Though, which causes the Tween to clip from -inf to whatever 0.0001 is in db space, avoiding NaN errors. (The music was starting too high with 0.1).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.