Files
claudeandClaude Opus 5.5 2545337204 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>
2026-11-13 14:28:34 -05:00

90 lines
3.8 KiB
GDScript

## Loudness and peak measurements for the generated audio, and the checks every asset
## must pass (tools/audio/check.gd, test_audio_assets):
##
## - the peak stays at or under PEAK_CEILING_DB, so nothing clips;
## - each sound is within TOLERANCE_DB of its target loudness, or under it only because
## the peak ceiling held it down (by no more than LIMITED_SLACK_DB);
## - the mix order from docs/STYLE.md holds: the tide's sounds are the loudest in a
## match, ambience and music sit under the effects, menus are quietest.
##
## Loudness is the RMS of the loudest WINDOW_SECONDS window for one-shots, and of the
## whole file for loops, in dBFS. That is close enough to a momentary loudness meter for
## levelling sounds against each other without a K-weighting filter.
extends RefCounted
const Dsp = preload("res://tools/audio/dsp.gd")
const WINDOW_SECONDS: float = 0.4
const PEAK_CEILING_DB: float = -1.0
const TOLERANCE_DB: float = 1.0
const LIMITED_SLACK_DB: float = 4.0
static func db(linear: float) -> float:
return 20.0 * log(maxf(linear, 1e-9)) / log(10.0)
static func peak_db(samples: PackedFloat32Array) -> float:
var peak: float = 0.0
for value: float in samples:
peak = maxf(peak, absf(value))
return db(peak)
static func loudness_db(samples: PackedFloat32Array, loops: bool) -> float:
if samples.is_empty():
return -180.0
if loops:
var total: float = 0.0
for value: float in samples:
total += value * value
return db(sqrt(total / samples.size()))
var window: int = mini(Dsp.n(WINDOW_SECONDS), samples.size())
var running: float = 0.0
for i: int in window:
running += samples[i] * samples[i]
var best: float = running
for i: int in range(window, samples.size()):
running += samples[i] * samples[i] - samples[i - window] * samples[i - window]
best = maxf(best, running)
return db(sqrt(maxf(best, 0.0) / window))
## The gain that brings `samples` to `target` dBFS without the peak passing the ceiling.
static func levelling_gain(samples: PackedFloat32Array, loops: bool, target: float) -> float:
var gain_db: float = target - loudness_db(samples, loops)
var headroom_db: float = PEAK_CEILING_DB - 0.05 - peak_db(samples)
return pow(10.0, minf(gain_db, headroom_db) / 20.0)
## Problems with one measured sound, as sentences; empty when it passes.
static func problems(sound: String, peak: float, loudness: float, target: float) -> Array[String]:
var found: Array[String] = []
if peak > PEAK_CEILING_DB + 0.01:
found.append("%s peaks at %.2f dBFS, over the %.1f ceiling" % [sound, peak, PEAK_CEILING_DB])
if loudness > target + TOLERANCE_DB:
found.append("%s is %.1f dBFS, louder than its %.1f target" % [sound, loudness, target])
elif loudness < target - TOLERANCE_DB:
var limited: bool = peak >= PEAK_CEILING_DB - 0.3
if not limited or loudness < target - LIMITED_SLACK_DB:
found.append("%s is %.1f dBFS, quieter than its %.1f target" % [sound, loudness, target])
return found
## Problems with the mix as a whole, from {sound: {category, loudness}}.
static func mix_problems(measured: Dictionary) -> Array[String]:
var loudest: Dictionary = {}
for sound: String in measured:
var category: String = measured[sound]["category"]
loudest[category] = maxf(float(loudest.get(category, -180.0)), float(measured[sound]["loudness"]))
var found: Array[String] = []
var tide: float = float(loudest.get("tide", -180.0))
for category: String in loudest:
if category != "tide" and float(loudest[category]) >= tide:
found.append("%s (%.1f dBFS) is as loud as the tide (%.1f)" % [category, loudest[category], tide])
for under: String in ["ambience", "music", "ui"]:
for over: String in ["weapon", "impact", "build"]:
if loudest.has(under) and loudest.has(over) and float(loudest[under]) >= float(loudest[over]):
found.append("%s should sit under %s" % [under, over])
return found