fix: keep external audio discovery diagnostic only

This commit is contained in:
goose
2026-10-01 09:12:16 -04:00
parent 4e356ee7b8
commit c750399128
5 changed files with 84 additions and 126 deletions
+10 -99
View File
@@ -10,39 +10,21 @@ 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)
_last_ambience_path = _find_first_audio_file("audio", current_ambience_context)
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)
_last_sfx_path = _find_first_audio_file("sfx", event)
func get_last_ambience_path() -> String:
return _last_ambience_path
@@ -50,6 +32,12 @@ func get_last_ambience_path() -> String:
func get_last_sfx_path() -> String:
return _last_sfx_path
func has_playable_ambience() -> bool:
return false
func has_playable_sfx() -> bool:
return false
func _audio_roots() -> Array[String]:
if not _audio_home_override.is_empty():
return [_audio_home_override]
@@ -69,86 +57,9 @@ func _find_first_audio_file(kind: String, name: String) -> String:
directory.list_dir_begin()
var entry := directory.get_next()
while not entry.is_empty():
var path := directory_path.path_join(entry)
if not directory.current_is_dir() and _is_loadable_external_audio(path):
if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS:
directory.list_dir_end()
return path
return directory_path.path_join(entry)
entry = directory.get_next()
directory.list_dir_end()
return ""
func _is_loadable_external_audio(path: String) -> bool:
var extension := path.get_extension().to_lower()
if not extension in ADMITTED_EXTENSIONS:
return false
# Do not call Godot's external loaders unless the bytes identify a codec they load.
# Other contract-admitted extensions (flac/m4a) intentionally have no native loader.
match extension:
"wav":
return _has_wav_signature(path)
"ogg", "oga", "opus":
return _has_prefix(path, PackedByteArray([79, 103, 103, 83])) # OggS
"mp3":
return _has_mp3_signature(path)
_:
return false
func _has_wav_signature(path: String) -> bool:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return false
var header := file.get_buffer(12)
file.close()
return header.size() == 12 and header.slice(0, 4) == PackedByteArray([82, 73, 70, 70]) and header.slice(8, 12) == PackedByteArray([87, 65, 86, 69]) # RIFF....WAVE
func _has_prefix(path: String, prefix: PackedByteArray) -> bool:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return false
var header := file.get_buffer(prefix.size())
file.close()
return header == prefix
func _has_mp3_signature(path: String) -> bool:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
return false
var header := file.get_buffer(3)
file.close()
if header.size() < 2:
return false
if header.size() == 3 and header == PackedByteArray([73, 68, 51]): # ID3
return true
# MPEG frame sync: 11 set bits, MPEG version not reserved, layer not reserved.
return header[0] == 255 and (header[1] & 0xE0) == 0xE0 and (header[1] & 0x18) != 0x08 and (header[1] & 0x06) != 0
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()