feat: add safe optional audio playback

This commit is contained in:
goose
2026-10-03 23:55:34 -04:00
parent d3c417fb91
commit efcaef181b
11 changed files with 339 additions and 54 deletions
+4 -8
View File
@@ -29,18 +29,14 @@ func test_unknown_requests_are_silent() -> void:
assert_false(library.has_playable_ambience())
assert_false(library.has_playable_sfx())
func test_nominal_and_invalid_wavs_are_discovered_but_never_playable() -> void:
func test_legacy_external_directory_override_is_ignored_and_invalid_wavs_stay_silent() -> void:
var root := ProjectSettings.globalize_path("user://s4-external-audio-library")
var nominal_path := root.path_join("audio/exploration/silence.wav")
var invalid_path := root.path_join("sfx/attack_given/not-a-wave.wav")
DirAccess.make_dir_recursive_absolute(nominal_path.get_base_dir())
DirAccess.make_dir_recursive_absolute(invalid_path.get_base_dir())
var nominal_file := FileAccess.open(nominal_path, FileAccess.WRITE)
nominal_file.store_buffer(PackedByteArray([
82, 73, 70, 70, 36, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32,
16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0,
2, 0, 16, 0, 100, 97, 116, 97, 0, 0, 0, 0,
]))
nominal_file.store_string("this must not be loaded by the audio library")
nominal_file.close()
var invalid_file := FileAccess.open(invalid_path, FileAccess.WRITE)
invalid_file.store_string("this is not a WAV file")
@@ -51,7 +47,7 @@ func test_nominal_and_invalid_wavs_are_discovered_but_never_playable() -> void:
library.set_ambience_context("exploration")
library.play_sfx("attack_given")
assert_eq(library.get_last_ambience_path(), nominal_path)
assert_eq(library.get_last_sfx_path(), invalid_path)
assert_eq(library.get_last_ambience_path(), "")
assert_eq(library.get_last_sfx_path(), "")
assert_false(library.has_playable_ambience())
assert_false(library.has_playable_sfx())
+60
View File
@@ -0,0 +1,60 @@
extends GutTest
const AudioLibrary = preload("res://scripts/infrastructure/audio_library.gd")
const AudioImportManifest = preload("res://scripts/infrastructure/audio_import_manifest.gd")
func test_imported_manifest_maps_only_known_ambience_and_sfx_to_godot_streams() -> void:
var ambience_stream := _silent_stream()
var sfx_stream := _silent_stream()
var manifest := AudioImportManifest.new()
assert_true(manifest.register_ambience(&"combat", ambience_stream))
assert_true(manifest.register_sfx(&"attack_given", sfx_stream))
assert_false(manifest.register_ambience(&"unknown", ambience_stream))
assert_false(manifest.register_sfx(&"unknown", sfx_stream))
var library := AudioLibrary.new(manifest)
add_child_autofree(library)
library.set_ambience_context("combat")
library.play_sfx("attack_given")
assert_eq(library.get_ambience_stream(), ambience_stream)
assert_eq(library.get_sfx_stream(), sfx_stream)
assert_true(library.has_playable_ambience())
assert_true(library.has_playable_sfx())
assert_eq(library.get_ambience_player().bus, AudioLibrary.AMBIENCE_BUS)
assert_eq(library.get_sfx_player().bus, AudioLibrary.SFX_BUS)
func test_setup_creates_the_private_user_audio_directory_for_manifest_assets() -> void:
assert_eq(AudioImportManifest.ensure_user_audio_directory(), OK)
assert_true(DirAccess.dir_exists_absolute(ProjectSettings.globalize_path(AudioImportManifest.USER_AUDIO_DIRECTORY)))
var manifest := AudioImportManifest.new()
assert_true(manifest.register_sfx(&"menu_select", _silent_stream()))
assert_eq(manifest.sfx_for(&"menu_select").get_class(), "AudioStreamWAV")
assert_eq(manifest.ambience_for(&"combat"), null)
func test_unregistered_external_audio_file_is_never_decoded_or_playable() -> void:
var root := ProjectSettings.globalize_path("user://g3-unregistered-audio-%s" % Time.get_ticks_usec())
var wav_path := root.path_join("audio/combat/external.wav")
DirAccess.make_dir_recursive_absolute(wav_path.get_base_dir())
var file := FileAccess.open(wav_path, FileAccess.WRITE)
file.store_string("this must never be decoded by AudioLibrary")
file.close()
var library := AudioLibrary.new()
add_child_autofree(library)
library.set_ambience_context("combat")
library.play_sfx("attack_given")
assert_eq(library.get_ambience_stream(), null)
assert_eq(library.get_sfx_stream(), null)
assert_false(library.has_playable_ambience())
assert_false(library.has_playable_sfx())
func _silent_stream() -> AudioStreamWAV:
var stream := AudioStreamWAV.new()
stream.format = AudioStreamWAV.FORMAT_16_BITS
stream.mix_rate = 44100
stream.stereo = false
stream.data = PackedByteArray([0, 0, 0, 0])
return stream
+1
View File
@@ -0,0 +1 @@
uid://b7ftt2slt52b0