extends AudioStreamPlayer

var _fade_tween: Tween = null


func is_sound_playing(sound: AudioStream) -> bool:
	return stream == sound and playing


func load_and_play(sound: AudioStream, fade_in: float = 0.0, priority: bool = false) -> void:
	if playing and not priority:
		return
	if _fade_tween:
		_fade_tween.kill()
	stream = sound
	pitch_scale = 1.0
	play()
	if fade_in > 0.0:
		volume_linear = 0.0
		_fade_tween = get_tree().create_tween()
		_fade_tween.tween_property(self, ^"volume_linear", db_to_linear(-4), fade_in)
	else:
		volume_db = -2


func stop_sound(sound: AudioStream, fade_out: float = 0.0) -> void:
	if sound != stream:
		return

	if _fade_tween:
		_fade_tween.kill()
	if fade_out > 0.0:
		_fade_tween = get_tree().create_tween()
		_fade_tween.tween_property(self, ^"volume_linear", 0, fade_out)
		_fade_tween.tween_callback(stop)
	else:
		stop()
