extends Node
onready var players: Dictionary = {}


func play_scene_independent(sound_name: String, bus: String = "Master", attenuation: float = 0.0,
 singleton = false, start_time = 0.0, random_pitch = false, pitch = 1.0) -> void:
	if singleton && players.has(sound_name):
		return
	var disposable_player
	disposable_player = AudioStreamPlayer.new()
	add_child(disposable_player)
	disposable_player.connect("finished", self, "dispose_player", [weakref(disposable_player)])
	var stream = GlobalState.sound_library[sound_name]
	if random_pitch:
		stream = AudioStreamRandomPitch.new()
		stream.audio_stream = GlobalState.sound_library[sound_name]
	disposable_player.stream = stream
	disposable_player.volume_db = attenuation
	disposable_player.bus = bus
	disposable_player.pitch_scale = pitch
	disposable_player.play(start_time)
	players[sound_name] = weakref(disposable_player)

func dispose_player(player: WeakRef) -> void:
	if !player.get_ref(): return
	var p = player.get_ref()
	var sound_name
	if("audio_stream" in p.stream):
		sound_name = p.stream.audio_stream.resource_path
	else:
		sound_name = p.stream.resource_path
	if players.has(sound_name):
		players.erase(sound_name)
	p.queue_free()

func pause_scene_independent(sound_name: String):
	if players.has(sound_name) && players[sound_name].get_ref():
		players[sound_name].set_stream_paused(true)

func continue_scene_independent(sound_name: String):
	if players.has(sound_name) && players[sound_name].get_ref():
		players[sound_name].set_stream_paused(false)
	
func stop_scene_independent(sound_name: String):
	if players.has(sound_name) && players[sound_name].get_ref():
		dispose_player(players[sound_name])
		players.erase(sound_name)
