Analyze this audio manager code for the Godot 4 engine. Suggest refactoring changes. Analyze the code from the point of view of modern programming. Optimize this code, if there is such a possibility. Check the code for errors. Suggest improvements for this code.

extends Node

const MUSIC_DIR = "res://music"

#Audio Player and UI Elements
@onready var audio_player: AudioStreamPlayer = $AudioStreamPlayer
@onready var next_button: Button = $MarginContainer/HBoxContainer/NextButton
@onready var prev_button: Button = $MarginContainer/HBoxContainer/PrevButton
@onready var play_button: Button = $MarginContainer/HBoxContainer/PlayButton
@onready var current_track_label: Label = $MarginContainer/HBoxContainer/Panel/MarginContainer/TrackLabel

#Music-related variables
var music_tracks: Array = []
var current_track_index: int = 0
var fade_duration: float = 1.0
var paused_playback_position: float = 0.0

func _ready() -> void:
	load_music_files()
	update_track_label()
	
func load_music_files():
	var dir = DirAccess.open(MUSIC_DIR)
	
	if dir:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		
		while file_name != "":
			if file_name.ends_with(".mp3"):
				var file_path = MUSIC_DIR + "/" + file_name
				music_tracks.append(file_path)
			file_name = dir.get_next()
		dir.list_dir_end()
		
		if music_tracks.is_empty():
			print("No music files found")
		else:
			print("Music loaded successfully")

func fade(target_volume: float, start_volume: float) -> void:
	if fade_duration <= 0:
		audio_player.volume_db = target_volume
		if target_volume == -80:
			audio_player.stop()
			paused_playback_position = 0
		else:
			audio_player.play()
		return
		
	var timer = 0.0
	audio_player.volume_db = start_volume
	if target_volume == 0:
		audio_player.play()
	while timer < fade_duration:
		timer += get_process_delta_time()
		audio_player.volume_db = lerp(start_volume, target_volume, timer / fade_duration)
		await get_tree().create_timer(0.01).timeout
		
	if target_volume == -80:
		audio_player.stop()
		paused_playback_position = 0

func fade_out() -> void:
	await fade(-80, 0)
	
func fade_in() -> void:
	await fade(0, -80)
	
func play_music() -> void:
	if music_tracks.is_empty():
		return
		
	var track_path = music_tracks[current_track_index]
	var stream = load(track_path) as AudioStream
	
	if stream:
		audio_player.stream = stream
		if paused_playback_position > 0:
			audio_player.play(paused_playback_position)
		else:
			audio_player.play()
			fade_in()
			
func pause_music() -> void:
	if audio_player.playing:
		paused_playback_position = audio_player.get_playback_position()
		audio_player.stop()
		
func stop_music() -> void:
	if audio_player.playing:
		paused_playback_position = 0
		fade_out()
	
func change_track(offset: int) -> void:
	if music_tracks.is_empty():
		return
	
	await fade_out()
	paused_playback_position = 0
	current_track_index = (current_track_index + offset + music_tracks.size()) % music_tracks.size()
	play_music()
	update_track_label()
	
func next_track():
	change_track(1)
	
func prev_track():
	if audio_player.get_playback_position() > 10:
		audio_player.seek(0)
	else:
		change_track(-1)

func update_track_label() -> void:
	if music_tracks.is_empty():
		current_track_label.text = "No music found"
	else:
		var track_name = music_tracks[current_track_index].get_file().get_basename()
		current_track_label.text = track_name
		
func _on_play_button_pressed() -> void:
	if audio_player.playing:
		pause_music()
	else:
		play_music()
	
func _on_next_button_pressed() -> void:
	next_track()

func _on_prev_button_pressed() -> void:
	prev_track()