class_name AudioLibrary extends Node const AMBIENCE_CONTEXTS := ["exploration", "combat", "danger"] const SFX_EVENTS := ["attack_given", "attack_received", "menu_select", "menu_cancel", "item_use", "death"] const ADMITTED_EXTENSIONS := ["mp3", "ogg", "oga", "wav", "flac", "m4a", "opus"] const DEFAULT_AUDIO_HOME := ".local/share/dcwascii" var current_ambience_context := "" var _audio_home_override := "" var _last_ambience_path := "" var _last_sfx_path := "" var _ambience_player: AudioStreamPlayer var _sfx_player: AudioStreamPlayer func _init(audio_home_override: String = "") -> void: _audio_home_override = audio_home_override func _ready() -> void: _ambience_player = AudioStreamPlayer.new() _sfx_player = AudioStreamPlayer.new() add_child(_ambience_player) add_child(_sfx_player) func set_ambience_context(context: String) -> void: if not context in AMBIENCE_CONTEXTS: return current_ambience_context = context _last_ambience_path = "" var path := _find_first_audio_file("audio", current_ambience_context) if path.is_empty(): _stop_ambience() return _last_ambience_path = path _play_ambience(path) func play_sfx(event: String) -> void: _last_sfx_path = "" if not event in SFX_EVENTS: return var path := _find_first_audio_file("sfx", event) if path.is_empty(): return _last_sfx_path = path _play_sfx(path) func get_last_ambience_path() -> String: return _last_ambience_path func get_last_sfx_path() -> String: return _last_sfx_path func _audio_roots() -> Array[String]: if not _audio_home_override.is_empty(): return [_audio_home_override] var roots: Array[String] = [] var environment_root := OS.get_environment("DCW_AUDIO_HOME") if not environment_root.is_empty(): roots.append(environment_root) roots.append(OS.get_environment("HOME").path_join(DEFAULT_AUDIO_HOME)) return roots func _find_first_audio_file(kind: String, name: String) -> String: for root in _audio_roots(): var directory_path := root.path_join(kind).path_join(name) var directory := DirAccess.open(directory_path) if directory == null: continue directory.list_dir_begin() var entry := directory.get_next() while not entry.is_empty(): if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS: directory.list_dir_end() return directory_path.path_join(entry) entry = directory.get_next() directory.list_dir_end() return "" func _play_ambience(path: String) -> void: var stream := _load_audio_stream(path) if stream == null or _ambience_player == null: return _ambience_player.stream = stream _ambience_player.play() func _play_sfx(path: String) -> void: var stream := _load_audio_stream(path) if stream == null or _sfx_player == null: return _sfx_player.stream = stream _sfx_player.play() func _load_audio_stream(path: String) -> AudioStream: # External roots are not imported Godot resources. Only use Godot-native loaders # that can accept a filesystem path; unavailable codecs fail closed. match path.get_extension().to_lower(): "wav": return AudioStreamWAV.load_from_file(path) "ogg", "oga", "opus": return AudioStreamOggVorbis.load_from_file(path) "mp3": return AudioStreamMP3.load_from_file(path) _: return null func _stop_ambience() -> void: if _ambience_player != null: _ambience_player.stop()