How to fade in/out an audio stream?

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

Apparently this can be accomplished using an AudioEffectDelay because it says " Delay effects range from a subtle echo effect to a pronounced blending of previous sounds with new sounds."

Obviously, I need to add the effect using the AudioServer. But how do I set it up so it works like intended?

:bust_in_silhouette: Reply From: Diet Estus

I don’t know anything about AudioEffectDelay or AudioServer, so this may not be what you’re looking for.

In my current projects, I achieve fade outs and fade ins of StreamPlayers using Tweens.

Basically, I have functions which tween up or down the volume.

An example:

onready var tween_out = get_node("Tween")
export var transition_duration = 1.00
export var transition_type = 1 # TRANS_SINE
func fade_out(stream_player):
	# tween music volume down to 0
	tween_out.interpolate_property(stream_player, "volume_db", 0, -80, transition_duration, transition_type, Tween.EASE_IN, 0)
	tween_out.start()
	# when the tween ends, the music will be stopped

func _on_TweenOut_tween_completed(object, key):
	# stop the music -- otherwise it continues to run at silent volume
	object.stop()

Thanks, I guess using Tween works just as well.

paradigmo | 2018-05-01 07:39

Thanks. Was incredibly helpful for me too.

Abhishek Roy | 2019-08-17 16:31

Remember at the function _on_TweenOut_tween_completed it’s important to reset the volume unless you will use a fade in effect

func _on_Tween_tween_completed(object, key):
# stop the music -- otherwise it continues to run at silent volume
object.stop()
object.volume_db = 0 # reset volume

nikito | 2020-07-12 16:56

If you add ‘stream_player.volume_db’ for the original volume, it will sound better if your playing music didn’t start at 100% volume.

oli1980 | 2022-10-13 13:25