A8: Synthesized sound effects and an adaptive score

Every sound is made by scripts in tools/audio/ and committed as a WAV in
assets/audio/ with a manifest of hashes and levels:

- dsp.gd: seeded noise, wavetable oscillators, filters, envelopes and
  struck modes. recipes.gd: 50 effects (footsteps on five surfaces,
  a report per weapon, impacts, carpentry building and breaking, the
  tide's bell, foghorn, surge and lighthouse, player, ambience, menus).
- score.gd: an original eight-bar tune and chords in D Dorian, 6/8,
  rendered as four looping layers (drone, frame drum, concertina,
  fiddle).
- loudness.gd and check.gd: per-category loudness targets, a -1 dBFS
  peak ceiling and the mix order (the tide loudest). make audio and
  make audio-check.

In the client, SoundDirector turns FX records, footsteps, building,
the tide and the player's own hurts into sounds, placed on 3D voices
at the event so gunfire and footsteps come from their direction.
MusicDirector fades the score's layers with the tide on an
AudioStreamSynchronized, full in menus. MenuSounds knocks for buttons.

Closes #48

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 14:28:34 -05:00
co-authored by Claude Opus 5.5
parent b3c59d66ac
commit 2545337204
149 changed files with 4143 additions and 8 deletions
+86
View File
@@ -0,0 +1,86 @@
extends TestCase
## A8: every sound is generated by the scripts in tools/audio/, the files in
## assets/audio/ are exactly what those scripts make, and they pass the loudness rules.
const Bake = preload("res://tools/audio/bake.gd")
const Check = preload("res://tools/audio/check.gd")
const Loudness = preload("res://tools/audio/loudness.gd")
const Wav = preload("res://tools/audio/wav.gd")
func test_the_files_pass_the_loudness_check() -> void:
var found: Array[String] = Check.problems()
assert_eq(found, [] as Array[String], "\n".join(found))
func test_every_file_regenerates_byte_for_byte() -> void:
var manifest: Dictionary = Bake.read_manifest()
for sound: String in Bake.names():
var baked: Dictionary = Bake.bake(sound)
assert_eq(baked["entry"]["sha256"], manifest.get(sound, {}).get("sha256", ""), "%s regenerates to the committed file" % sound)
func test_nothing_in_the_folder_comes_from_elsewhere() -> void:
var names: Array[String] = Bake.names()
for file: String in DirAccess.get_files_at(Bake.OUT_DIR):
if file.ends_with(".import") or file == "manifest.json":
continue
assert_true(file.ends_with(".wav"), "%s is a generated WAV" % file)
assert_true(names.has(file.get_basename()), "%s is made by a recipe" % file)
func test_provenance_names_the_generators() -> void:
var provenance: String = FileAccess.get_file_as_string("res://assets/PROVENANCE.md")
for script: String in ["tools/audio/recipes.gd", "tools/audio/score.gd", "tools/audio/generate.gd"]:
assert_true(provenance.contains(script), "PROVENANCE.md names %s" % script)
func test_every_sound_loads_and_loops_only_where_it_should() -> void:
for sound: String in Bake.names():
var stream: AudioStreamWAV = SoundBank.stream(sound) as AudioStreamWAV
assert_not_null(stream, "%s loads" % sound)
if stream == null:
continue
assert_eq(stream.loop_mode != AudioStreamWAV.LOOP_DISABLED, Bake.loops(sound), "%s loops only if it should" % sound)
assert_near(stream.get_length(), float(Bake.read_manifest()[sound]["seconds"]), 0.01, "%s's length" % sound)
SoundBank.clear()
func test_the_mix_puts_the_tide_on_top() -> void:
var manifest: Dictionary = Bake.read_manifest()
var loudest_other: float = -180.0
var quietest_tide: float = 0.0
for sound: String in manifest:
var level: float = float(manifest[sound]["loudness_db"])
if manifest[sound]["category"] == "tide":
quietest_tide = minf(quietest_tide, level)
else:
loudest_other = maxf(loudest_other, level)
assert_true(quietest_tide > loudest_other, "the foghorn, bell, surge and lighthouse are the loudest sounds")
func test_the_loudness_rules_catch_bad_levels() -> void:
assert_eq(Loudness.problems("x", -0.2, -12.0, -12.0).size(), 1, "a peak over the ceiling")
assert_eq(Loudness.problems("x", -6.0, -9.0, -12.0).size(), 1, "too loud")
assert_eq(Loudness.problems("x", -6.0, -16.0, -12.0).size(), 1, "too quiet without being held by the peak")
assert_eq(Loudness.problems("x", -1.05, -14.0, -12.0).size(), 0, "held down a little by the peak ceiling")
assert_eq(Loudness.problems("x", -3.0, -12.4, -12.0).size(), 0, "on target")
var mix: Array[String] = Loudness.mix_problems({
"horn": {"category": "tide", "loudness": -14.0},
"gun": {"category": "weapon", "loudness": -13.0},
"wind": {"category": "ambience", "loudness": -20.0},
})
assert_eq(mix.size(), 1, "a gun as loud as the tide is caught: %s" % [mix])
func test_wav_round_trip() -> void:
var samples: PackedFloat32Array = PackedFloat32Array([0.0, 0.5, -0.5, 1.0, -1.0])
var bytes: PackedByteArray = Wav.encode(samples)
assert_eq(bytes.size(), 44 + 10)
assert_eq(Wav.rate_of(bytes), 22050)
var back: PackedFloat32Array = Wav.decode(bytes)
assert_eq(back.size(), 5)
for i: int in 5:
assert_near(back[i], samples[i], 0.0001)
assert_eq(Wav.decode(PackedByteArray([1, 2, 3])).size(), 0, "not a WAV")
+1
View File
@@ -0,0 +1 @@
uid://cugkr67rsrrtd
+60
View File
@@ -0,0 +1,60 @@
extends TestCase
## A8: the score builds with the tide, quiet in play and full in menus and results.
var tree: SceneTree
func before_each() -> void:
super.before_each()
tree = Engine.get_main_loop() as SceneTree
func _game(phase: int, tide_tick: int = -1) -> ClientGame:
var game: ClientGame = ClientGame.new()
game.world = Simulation.new(3, SimGround.flat(0.0))
game.match_phase = phase
if tide_tick >= 0:
game.world.tide = Tide.new(0)
game.world.tick = tide_tick
return game
func test_intensity_by_moment() -> void:
assert_eq(MusicDirector.intensity_for(null), 3, "menus are fullest")
assert_eq(MusicDirector.intensity_for(_game(Match.LOBBY)), 1)
assert_eq(MusicDirector.intensity_for(_game(Match.RESULTS)), 3)
var tide: Tide = Tide.new(0)
assert_eq(MusicDirector.intensity_for(_game(Match.PLAY, 10)), 0, "a holding tide: the drone alone")
var rising: int = tide.ticks_until_rise(0) + 5
assert_eq(MusicDirector.intensity_for(_game(Match.PLAY, rising)), 1, "the drum comes in as the sea rises")
var levels: Array[int] = []
var tick: int = 0
while tide.stage_at(tick) != Tide.SUDDEN_DEATH:
levels.append(MusicDirector.intensity_for(_game(Match.PLAY, tick)))
tick += Tuning.TICK_RATE_HZ * 5
levels.append(MusicDirector.intensity_for(_game(Match.PLAY, tick)))
for index: int in range(1, levels.size()):
assert_true(levels[index] >= levels[index - 1], "never falls back as the tide goes on: %s" % [levels])
assert_eq(levels[levels.size() - 1], 3, "the whole tune for the last land")
assert_true(levels.has(2), "the concertina in the middle phases")
func test_layers_fade_toward_the_intensity_and_play_on_the_music_bus() -> void:
var music: MusicDirector = MusicDirector.new()
tree.root.add_child(music)
assert_eq(String(music.player.bus), SoundBank.MUSIC_BUS)
assert_true(music.player.stream is AudioStreamSynchronized)
var game: ClientGame = _game(Match.PLAY, 10)
music.update(game, 1.0)
assert_near(music.gains[0], 1.0 / MusicDirector.FADE_SECONDS, 0.001, "fading, not switching")
for frame: int in 10:
music.update(game, 1.0)
assert_eq(Array(music.gains), [1.0, 0.0, 0.0, 0.0])
assert_true(music.in_match)
for frame: int in 10:
music.update(null, 1.0)
assert_eq(Array(music.gains), [1.0, 1.0, 1.0, 1.0], "menus have everything")
assert_false(music.in_match)
music.free()
SoundBank.clear()
+1
View File
@@ -0,0 +1 @@
uid://cpmcgkhsonldp
+231
View File
@@ -0,0 +1,231 @@
extends TestCase
## A8: what the match sounds like, from the client's world. Sounds are placed where they
## happen (so they are heard from that direction), and each event picks its own sound.
const FRAME: float = 1.0 / 60.0
var tree: SceneTree
var director: SoundDirector
var game: ClientGame
var world: Simulation
func before_each() -> void:
super.before_each()
tree = Engine.get_main_loop() as SceneTree
director = SoundDirector.new()
tree.root.add_child(director)
game = ClientGame.new()
world = Simulation.new(5, SimGround.flat(0.0))
world.sea_level = -5.0
game.world = world
game.match_phase = Match.PLAY
director.update(game, 0, Vector3.ZERO, FRAME)
func after_each() -> void:
director.free()
SoundBank.clear()
super.after_each()
func _sounds() -> Array[String]:
var out: Array[String] = []
for cue: Dictionary in director.cues:
out.append(String(cue["sound"]))
return out
func _player(at: Vector3) -> SimEntity:
var player: SimEntity = world.entities.find(world.add_player())
player.position = at
player.on_ground = true
player.drop_state = Drop.NONE
player.health = 100
return player
func _fx(record: Dictionary) -> void:
game.fx.append(record)
game.fx_received += 1
func test_a_shot_is_placed_where_it_was_fired_with_that_weapons_report() -> void:
var at: Vector3 = Vector3(30.0, 2.0, -12.0)
_fx({"kind": NetFx.SHOT, "tick": 1, "player": 1, "item": ItemCatalog.code_of("whalers_longgun"), "position": at})
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), ["shot_whalers_longgun"] as Array[String])
assert_true(bool(director.cues[0]["spatial"]), "positional")
var voice: AudioStreamPlayer3D = director.voice_playing("shot_whalers_longgun")
assert_not_null(voice, "played on a 3D voice")
assert_eq(voice.global_position, at, "at the muzzle, so the engine pans it from there")
assert_true(voice.max_distance >= 400.0, "gunfire carries")
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(director.cues.size(), 0, "each FX record is heard once")
func test_each_weapon_has_its_own_report() -> void:
var seen: Dictionary = {}
for id: String in ["fowling_piece", "dock_carbine", "signal_revolver", "rivet_repeater", "whalers_longgun", "line_thrower", "harpoon_gun"]:
var sound: String = SoundDirector.shot_sound(ItemCatalog.code_of(id))
assert_eq(sound, "shot_" + id)
assert_true(SoundBank.has(sound), "%s exists" % sound)
seen[sound] = true
assert_eq(SoundDirector.shot_sound(-1), "swing_boathook", "the boathook, which has no item code")
assert_eq(SoundDirector.shot_sound(250), "shot_dock_carbine", "an unknown code still makes a sound")
func test_impacts_sound_like_what_was_hit() -> void:
var stone: StructureSet.Piece = world.structures.add(StructureSet.WALL, Materials.STONE, Vector4i(10, 0, 10, StructureSet.SLOT_WALL_X), 0, 0, 0, world.ground)
director.update(game, 0, Vector3.ZERO, FRAME)
var cases: Array = [
[Weapons.HIT_PIECE, stone.id, "hit_stone"], [Weapons.HIT_PLAYER, 3, "hit_player"],
[Weapons.HIT_PROP, 4, "hit_wood"], [Weapons.HIT_TERRAIN, 0, "hit_ground"],
]
for entry: Array in cases:
_fx({"kind": NetFx.IMPACT, "tick": 2, "player": 1, "item": ItemCatalog.code_of("dock_carbine"), "position": Vector3(1, 1, 1), "hit": entry[0], "target": entry[1]})
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), [entry[2]] as Array[String])
_fx({"kind": NetFx.HARVEST, "tick": 3, "player": 1, "item": -1, "position": Vector3.ZERO})
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), ["chop"] as Array[String])
func _walk(player: SimEntity, velocity: Vector3, seconds: float, listener: Vector3 = Vector3.ZERO) -> Array[String]:
var heard: Array[String] = []
for frame: int in int(seconds / FRAME):
player.position += velocity * FRAME
director.update(game, player.id, listener, FRAME)
heard.append_array(_sounds())
return heard
func test_footsteps_follow_the_stride_and_alternate_feet() -> void:
var player: SimEntity = _player(Vector3(5.0, 0.0, 5.0))
director.update(game, player.id, Vector3.ZERO, FRAME)
var heard: Array[String] = _walk(player, Vector3(5.0, 0.0, 0.0), 2.0)
# 10 m at a 1.6 m stride.
assert_eq(heard.size(), 6, "%s" % [heard])
assert_eq(heard[0], "step_grass_2")
assert_eq(heard[1], "step_grass_1")
for cue: Dictionary in director.cues:
assert_true(bool(cue["spatial"]))
var standing: Array[String] = _walk(player, Vector3.ZERO, 1.0)
assert_eq(standing.size(), 0, "standing still is silent")
func test_footsteps_know_the_surface() -> void:
var player: SimEntity = _player(Vector3(5.0, 2.0, 5.0))
director.update(game, player.id, Vector3.ZERO, FRAME)
var on_planks: Array[String] = _walk(player, Vector3(4.0, 0.0, 0.0), 1.0)
assert_true(on_planks.size() > 0 and on_planks[0].begins_with("step_wood"), "above the ground means on a built piece: %s" % [on_planks])
player.position.y = 0.0
world.sea_level = 0.4
director.update(game, player.id, Vector3.ZERO, FRAME)
var wading: Array[String] = _walk(player, Vector3(4.0, 0.0, 0.0), 1.0)
assert_true(wading.size() > 0 and wading[0].begins_with("step_shallows"), "%s" % [wading])
func test_footsteps_on_a_real_island_by_band() -> void:
var island_world: Simulation = Simulation.new(2)
var found: Dictionary = {}
var size: float = Tuning.WORLD_SIZE_METRES
for gz: int in 60:
for gx: int in 60:
var x: float = size * (gx + 0.5) / 60.0
var z: float = size * (gz + 0.5) / 60.0
var feet: Vector3 = Vector3(x, island_world.ground.terrain_height(x, z), z)
if feet.y <= island_world.sea_level + 0.1:
continue
found[SoundDirector.surface_at(island_world, feet)] = true
for surface: String in ["sand", "grass", "rock"]:
assert_true(found.has(surface), "the island has %s to walk on: %s" % [surface, found.keys()])
func test_far_footsteps_are_not_heard_but_near_ones_are() -> void:
var far: SimEntity = _player(Vector3(200.0, 0.0, 200.0))
var near: SimEntity = _player(Vector3(10.0, 0.0, 0.0))
director.update(game, 0, Vector3.ZERO, FRAME)
var heard: int = 0
for frame: int in 120:
far.position.x += 5.0 * FRAME
near.position.z += 5.0 * FRAME
director.update(game, 0, Vector3.ZERO, FRAME)
for cue: Dictionary in director.cues:
heard += 1
assert_true(Vector3(cue["at"]).distance_to(near.position) < 1.0, "only the near player's steps")
assert_eq(heard, 6)
func test_landing_and_splashing() -> void:
var player: SimEntity = _player(Vector3(0.0, 10.0, 0.0))
player.on_ground = false
director.update(game, player.id, Vector3.ZERO, FRAME)
_walk(player, Vector3(0.0, -12.0, 0.0), 0.5)
player.on_ground = true
assert_eq(_walk(player, Vector3.ZERO, FRAME), ["land"] as Array[String])
player.swimming = true
assert_eq(_walk(player, Vector3.ZERO, FRAME), ["splash"] as Array[String])
func test_building_and_breaking_by_material() -> void:
var piece: StructureSet.Piece = world.structures.add(StructureSet.FLOOR, Materials.SALVAGE_IRON, Vector4i(20, 0, 20, StructureSet.SLOT_FLOOR), 0, 0, 0, world.ground)
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), ["build_salvage_iron"] as Array[String])
assert_true(Vector3(director.cues[0]["at"]).distance_to(piece.boxes[0].get_center()) < 0.01, "at the piece")
world.structures.remove(piece.id, world.ground)
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), ["break_salvage_iron"] as Array[String])
func test_the_tide_rings_and_sounds_its_horn() -> void:
world.tide = Tide.new(0)
world.tick = 1
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), [] as Array[String], "nothing on the first look")
world.tick = world.tide.ticks_until_rise(1) + 2
director.update(game, 0, Vector3.ZERO, FRAME)
assert_true(_sounds().has("foghorn"), "the sea starts to rise: %s" % [_sounds()])
var heard: Dictionary = {}
while world.tide.stage_at(world.tick) != Tide.SUDDEN_DEATH:
world.tick += 20
director.update(game, 0, Vector3.ZERO, FRAME)
for sound: String in _sounds():
heard[sound] = int(heard.get(sound, 0)) + 1
assert_eq(int(heard.get("foghorn", 0)), Tide.phase_count() - 1, "a horn for each later rise: %s" % [heard])
assert_eq(int(heard.get("bell", 0)), Tide.phase_count() - 1, "a bell as each new phase holds")
assert_eq(int(heard.get("lighthouse", 0)), 1, "the lighthouse for the last land")
for cue: Dictionary in director.cues:
assert_false(bool(cue["spatial"]), "the tide is everywhere")
func test_own_hurt_and_the_feed() -> void:
var me: SimEntity = _player(Vector3.ZERO)
director.update(game, me.id, Vector3.ZERO, FRAME)
me.health = 70
director.update(game, me.id, Vector3.ZERO, FRAME)
assert_eq(_sounds(), ["hurt"] as Array[String])
game.feed.append({"victim": 9, "by": 3, "place": 12})
director.update(game, me.id, Vector3.ZERO, FRAME)
assert_eq(_sounds(), ["eliminated"] as Array[String])
func test_a_new_world_starts_quietly() -> void:
world.structures.add(StructureSet.FLOOR, Materials.STONE, Vector4i(20, 0, 20, StructureSet.SLOT_FLOOR), 0, 0, 0, world.ground)
_fx({"kind": NetFx.SHOT, "tick": 1, "player": 1, "item": 0, "position": Vector3.ZERO})
var next: Simulation = Simulation.new(6, SimGround.flat(0.0))
next.structures.add(StructureSet.FLOOR, Materials.STONE, Vector4i(20, 0, 20, StructureSet.SLOT_FLOOR), 0, 0, 0, next.ground)
game.world = next
director.update(game, 0, Vector3.ZERO, FRAME)
assert_eq(_sounds(), [] as Array[String], "what was there before the client looked isn't news")
func test_ambience_surf_near_the_water_and_wind_up_high() -> void:
director.update(game, 0, Vector3(0.0, world.sea_level + 2.0, 0.0), FRAME)
var low_surf: float = director.ambience_level("amb_surf")
var low_wind: float = director.ambience_level("amb_wind")
director.update(game, 0, Vector3(0.0, world.sea_level + 90.0, 0.0), FRAME)
assert_true(low_surf > director.ambience_level("amb_surf") * 2.0, "the surf fades with height")
assert_true(director.ambience_level("amb_wind") > low_wind * 2.0, "the wind grows with height")
assert_near(director.ambience_level("amb_rain"), 0.0, 0.001, "no rain until weather asks for it")
+1
View File
@@ -0,0 +1 @@
uid://cekbfmiu1xq3s
+18
View File
@@ -263,3 +263,21 @@ func _press_in(on: Menus, screen: String, text: String) -> void:
assert_true(buttons.has(text), "%s has a '%s' button (has %s)" % [screen, text, buttons.keys()])
if buttons.has(text):
(buttons[text] as Button).pressed.emit()
func test_menu_buttons_knock_when_pointed_at_and_pressed() -> void:
var sounds: MenuSounds = MenuSounds.new()
tree.root.add_child(sounds)
sounds.attach(menus)
var main: Dictionary = menus.buttons_of("main")
var first: Button = main.values()[0]
first.mouse_entered.emit()
assert_eq(sounds.heard.back(), "ui_hover")
var later: Button = Button.new()
later.text = "Back"
menus.add_child(later)
later.pressed.emit()
assert_eq(sounds.heard.back(), "ui_back", "a button made later is hooked too")
sounds.free()
later.free()
SoundBank.clear()