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>
92 lines
3.3 KiB
GDScript
92 lines
3.3 KiB
GDScript
## Renders, levels and encodes every generated sound: the effects in recipes.gd and the
|
|
## score's layers in score.gd. Used by generate.gd to write assets/audio/, and by
|
|
## check.gd and the tests to prove the files there are what the source makes.
|
|
extends RefCounted
|
|
|
|
const Dsp = preload("res://tools/audio/dsp.gd")
|
|
const Recipes = preload("res://tools/audio/recipes.gd")
|
|
const Score = preload("res://tools/audio/score.gd")
|
|
const Wav = preload("res://tools/audio/wav.gd")
|
|
const Loudness = preload("res://tools/audio/loudness.gd")
|
|
|
|
const OUT_DIR: String = "res://assets/audio"
|
|
const MANIFEST: String = "res://assets/audio/manifest.json"
|
|
|
|
|
|
static func names() -> Array[String]:
|
|
var out: Array[String] = []
|
|
for sound: String in Recipes.SOUNDS:
|
|
out.append(sound)
|
|
out.append_array(Score.LAYERS)
|
|
return out
|
|
|
|
|
|
static func category(sound: String) -> String:
|
|
return "music" if Score.LAYERS.has(sound) else String(Recipes.SOUNDS[sound][0])
|
|
|
|
|
|
static func loops(sound: String) -> bool:
|
|
return Score.LAYERS.has(sound) or bool(Recipes.SOUNDS[sound][1])
|
|
|
|
|
|
static func target(sound: String) -> float:
|
|
if Score.TARGETS.has(sound):
|
|
return float(Score.TARGETS[sound])
|
|
return float(Recipes.TARGETS[category(sound)])
|
|
|
|
|
|
static func generator(sound: String) -> String:
|
|
return "tools/audio/score.gd" if Score.LAYERS.has(sound) else "tools/audio/recipes.gd"
|
|
|
|
|
|
static func path(sound: String) -> String:
|
|
return "%s/%s.wav" % [OUT_DIR, sound]
|
|
|
|
|
|
## The levelled samples for a sound.
|
|
static func render(sound: String) -> PackedFloat32Array:
|
|
var raw: PackedFloat32Array = Score.render(sound) if Score.LAYERS.has(sound) else Recipes.render(sound)
|
|
var gain_db: float = target(sound) - Loudness.loudness_db(raw, loops(sound))
|
|
var leveled: PackedFloat32Array = Dsp.scaled(raw, pow(10.0, gain_db / 20.0))
|
|
# Sharp transients would hold a sound under its target at the peak ceiling, so
|
|
# round their peaks off with a soft limiter first, as a mastering limiter would.
|
|
var ceiling: float = pow(10.0, Loudness.PEAK_CEILING_DB / 20.0)
|
|
if Loudness.peak_db(leveled) > Loudness.PEAK_CEILING_DB:
|
|
for i: int in leveled.size():
|
|
leveled[i] = ceiling * tanh(leveled[i] / ceiling)
|
|
return Dsp.scaled(leveled, Loudness.levelling_gain(leveled, loops(sound), target(sound)))
|
|
|
|
|
|
## The file's bytes and its manifest entry.
|
|
static func bake(sound: String) -> Dictionary:
|
|
var samples: PackedFloat32Array = render(sound)
|
|
var bytes: PackedByteArray = Wav.encode(samples)
|
|
# Measure what the file holds, after 16-bit rounding.
|
|
var stored: PackedFloat32Array = Wav.decode(bytes)
|
|
return {
|
|
"bytes": bytes,
|
|
"entry": {
|
|
"category": category(sound),
|
|
"loops": loops(sound),
|
|
"seconds": snappedf(float(stored.size()) / Dsp.RATE, 0.001),
|
|
"sha256": sha256(bytes),
|
|
"peak_db": snappedf(Loudness.peak_db(stored), 0.01),
|
|
"loudness_db": snappedf(Loudness.loudness_db(stored, loops(sound)), 0.01),
|
|
"target_db": target(sound),
|
|
"generator": generator(sound),
|
|
},
|
|
}
|
|
|
|
|
|
static func sha256(bytes: PackedByteArray) -> String:
|
|
var hasher: HashingContext = HashingContext.new()
|
|
hasher.start(HashingContext.HASH_SHA256)
|
|
hasher.update(bytes)
|
|
return hasher.finish().hex_encode()
|
|
|
|
|
|
static func read_manifest() -> Dictionary:
|
|
var text: String = FileAccess.get_file_as_string(MANIFEST)
|
|
var parsed: Variant = JSON.parse_string(text)
|
|
return parsed if parsed is Dictionary else {}
|