Fade-to-black transition that stays still during continuous input?

Godot Version

Godot 4.4

Question

I am trying to make a project where the game is only visible when a certain microphone volume is reached. With the code I currently have, the screen will continuously fade in and out. I know that this is because it is called under the “Process” function. The transition scene does have a signal that emits when the animation ends, that might be part of the solution. What would be the proper way to call the transition screen?

Here is the code for the main scene:

extends Node2D


var spectrum
var volume
var pitch

func _ready():
	spectrum = AudioServer.get_bus_effect_instance(1, 2)

func _process(delta):
	volume = (spectrum.get_magnitude_for_frequency_range(0, 1000).length())
	if volume >= 0.04:
		AudioServer.set_bus_mute(1, false)
		TransitionScene.transition()
		await TransitionScene.on_transition_finished
	else:
		AudioServer.set_bus_mute(1, true)

Here is the code for the transition scene:

extends CanvasLayer

signal on_transition_finished

@onready var color_rect: ColorRect = $ColorRect
@onready var animation_player: AnimationPlayer = $AnimationPlayer

func _ready() -> void:
	color_rect.visible = false
	animation_player.animation_finished.connect(_on_animation_finished)

func _on_animation_finished(anim_name):
	if anim_name == "fade_to_black":
		animation_player.play("fade_to_transparent")
		emit_signal("on_transition_finished")
	elif anim_name == "fade_to_transparent":
		color_rect.visible
		

func transition():
	color_rect.visible = true
	animation_player.play("fade_to_black")

Some things about the transitionlayer code:
_on_animation_finished detects the end of the fade_to_black animation, then immediately unfades again (animation_player.play(“fade_to_transparent”)). Then, when that animation ends, _on_animation_finished will be called again, at which point it does nothing. Since you put color_rect.visible with no other symbols, that line of code won’t do anything.

Overall, whenever any part of your code tries to fade to black using TransitionScene, it will just fade to transparent directly after.

To complete to “continuously fade in and out” effect, the audio processing script is likely repeatedly trying to fade to black because the threshold is too low.

My recommendations would be to make a script like your first snippet that simply prints the volume being read out of the spectrum effect, to get an idea for how it might be balanced. Then, for simple color effects, like shading a ColorRect for a fade to black, you can use Tweens. Tweens are much harder to put into feedback loops than AnimationPlayers or AnimationTrees in my opinion.

You might also want to make a calibration scene at some point, which asks the player to speak into a microphone and select a range that is sensitive enough for them.