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
+91
View File
@@ -0,0 +1,91 @@
## 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 {}
+1
View File
@@ -0,0 +1 @@
uid://rbirp8dh2jcc
+44
View File
@@ -0,0 +1,44 @@
## Checks the generated audio: every sound the source defines has a file and a manifest
## entry, every file's hash matches its entry, and every file passes the loudness rules
## in loudness.gd, measured from the file itself. Exits 1 on any problem.
##
## tools/godot.sh --script res://tools/audio/check.gd
extends SceneTree
const Bake = preload("res://tools/audio/bake.gd")
const Wav = preload("res://tools/audio/wav.gd")
const Loudness = preload("res://tools/audio/loudness.gd")
static func problems() -> Array[String]:
var found: Array[String] = []
var manifest: Dictionary = Bake.read_manifest()
var measured: Dictionary = {}
for sound: String in Bake.names():
if not manifest.has(sound):
found.append("%s has no manifest entry" % sound)
continue
var bytes: PackedByteArray = FileAccess.get_file_as_bytes(Bake.path(sound))
if bytes.is_empty():
found.append("%s has no file at %s" % [sound, Bake.path(sound)])
continue
if Bake.sha256(bytes) != String(manifest[sound]["sha256"]):
found.append("%s's file doesn't match its manifest hash" % sound)
var samples: PackedFloat32Array = Wav.decode(bytes)
var peak: float = Loudness.peak_db(samples)
var loudness: float = Loudness.loudness_db(samples, Bake.loops(sound))
found.append_array(Loudness.problems(sound, peak, loudness, Bake.target(sound)))
measured[sound] = {"category": Bake.category(sound), "loudness": loudness}
for sound: String in manifest:
if not Bake.names().has(sound):
found.append("%s is in the manifest but no recipe makes it" % sound)
found.append_array(Loudness.mix_problems(measured))
return found
func _init() -> void:
var found: Array[String] = problems()
for line: String in found:
printerr(line)
print("audio check: %d sounds, %d problems" % [Bake.names().size(), found.size()])
quit(1 if not found.is_empty() else 0)
+1
View File
@@ -0,0 +1 @@
uid://co7rw4guwnsvp
+279
View File
@@ -0,0 +1,279 @@
## The building blocks every Tidewall sound is made from: seeded noise, wavetable
## oscillators, filters, envelopes, decaying modes and a mixer. Buffers are mono
## PackedFloat32Array at RATE samples a second. Everything here is a pure function of
## its arguments and the seed it is given, so the same engine build renders the same
## bytes every time (see tools/audio/generate.gd and test_audio_assets).
##
## No samples, recordings or third-party audio go in: every sound starts from these
## functions.
extends RefCounted
const RATE: int = 22050
const TABLE_SIZE: int = 2048
static var _tables: Dictionary = {}
## Seconds to samples.
static func n(seconds: float) -> int:
return maxi(1, int(round(seconds * RATE)))
static func silence(seconds: float) -> PackedFloat32Array:
var out: PackedFloat32Array = PackedFloat32Array()
out.resize(n(seconds))
out.fill(0.0)
return out
## White noise from a seeded generator.
static func noise(seconds: float, seed_value: int) -> PackedFloat32Array:
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = seed_value
var out: PackedFloat32Array = PackedFloat32Array()
out.resize(n(seconds))
for i: int in out.size():
out[i] = rng.randf() * 2.0 - 1.0
return out
## Brown-ish noise: white noise through a leaky integrator, for surf and rumble.
static func brown(seconds: float, seed_value: int) -> PackedFloat32Array:
var white: PackedFloat32Array = noise(seconds, seed_value)
var level: float = 0.0
for i: int in white.size():
level = level * 0.985 + white[i] * 0.12
white[i] = level
return white
## One cycle of a band-limited waveform: "sine", "saw", "square", "reed" (a narrow
## pulse, like a free reed) or "bow" (a saw with softened upper partials).
static func table(shape: String) -> PackedFloat32Array:
if _tables.has(shape):
return _tables[shape]
var out: PackedFloat32Array = PackedFloat32Array()
out.resize(TABLE_SIZE)
out.fill(0.0)
var partials: int = 24
for k: int in range(1, partials + 1):
var amp: float = 0.0
match shape:
"sine":
amp = 1.0 if k == 1 else 0.0
"saw":
amp = 1.0 / k
"square":
amp = (1.0 / k) if k % 2 == 1 else 0.0
"reed":
# A pulse of width 1/5: strong odd and even partials with gaps.
amp = absf(sin(PI * k * 0.2)) / k
"bow":
amp = 1.0 / pow(k, 1.35)
if amp == 0.0:
continue
for i: int in TABLE_SIZE:
out[i] += amp * sin(TAU * k * float(i) / TABLE_SIZE)
var peak: float = 0.0
for value: float in out:
peak = maxf(peak, absf(value))
for i: int in TABLE_SIZE:
out[i] /= peak
_tables[shape] = out
return out
## A wavetable oscillator. `pitch` is Hz, either a constant or a curve given as
## [start, end] gliding exponentially; `vibrato` is [rate Hz, depth in semitones].
static func osc(shape: String, seconds: float, pitch: Array, vibrato: Array = [0.0, 0.0], phase: float = 0.0) -> PackedFloat32Array:
var wave: PackedFloat32Array = table(shape)
var count: int = n(seconds)
var out: PackedFloat32Array = PackedFloat32Array()
out.resize(count)
var start: float = float(pitch[0])
var finish: float = float(pitch[pitch.size() - 1])
var position: float = phase * TABLE_SIZE
for i: int in count:
var t: float = float(i) / count
var hz: float = start * pow(finish / start, t)
if vibrato[0] > 0.0:
hz *= pow(2.0, vibrato[1] * sin(TAU * vibrato[0] * float(i) / RATE) / 12.0)
var index: int = int(position)
var frac: float = position - index
var a: float = wave[index % TABLE_SIZE]
var b: float = wave[(index + 1) % TABLE_SIZE]
out[i] = a + (b - a) * frac
position = fmod(position + hz * TABLE_SIZE / RATE, float(TABLE_SIZE))
return out
## One-pole low-pass, cutoff in Hz, either fixed or gliding [start, end].
static func lowpass(input: PackedFloat32Array, cutoff: Array, passes: int = 1) -> PackedFloat32Array:
var out: PackedFloat32Array = input.duplicate()
var start: float = float(cutoff[0])
var finish: float = float(cutoff[cutoff.size() - 1])
for _pass: int in passes:
var state: float = 0.0
for i: int in out.size():
var hz: float = start * pow(finish / start, float(i) / out.size())
var a: float = 1.0 - exp(-TAU * hz / RATE)
state += a * (out[i] - state)
out[i] = state
return out
static func highpass(input: PackedFloat32Array, cutoff: float) -> PackedFloat32Array:
var low: PackedFloat32Array = lowpass(input, [cutoff])
var out: PackedFloat32Array = input.duplicate()
for i: int in out.size():
out[i] -= low[i]
return out
## A resonant band-pass (RBJ biquad), centre in Hz, either fixed or gliding.
static func bandpass(input: PackedFloat32Array, centre: Array, q: float) -> PackedFloat32Array:
var out: PackedFloat32Array = PackedFloat32Array()
out.resize(input.size())
var x1: float = 0.0
var x2: float = 0.0
var y1: float = 0.0
var y2: float = 0.0
var start: float = float(centre[0])
var finish: float = float(centre[centre.size() - 1])
for i: int in input.size():
var hz: float = start * pow(finish / start, float(i) / input.size())
var w: float = TAU * minf(hz, RATE * 0.45) / RATE
var alpha: float = sin(w) / (2.0 * q)
var a0: float = 1.0 + alpha
var b0: float = alpha / a0
var b2: float = -alpha / a0
var a1: float = -2.0 * cos(w) / a0
var a2: float = (1.0 - alpha) / a0
var x: float = input[i]
var y: float = b0 * x + b2 * x2 - a1 * y1 - a2 * y2
x2 = x1
x1 = x
y2 = y1
y1 = y
out[i] = y
return out
## Multiplies by an exponential decay that falls 60 dB over `decay` seconds, after
## a linear attack of `attack` seconds.
static func decay(input: PackedFloat32Array, attack: float, decay_seconds: float) -> PackedFloat32Array:
var out: PackedFloat32Array = input.duplicate()
var attack_n: int = n(attack) if attack > 0.0 else 0
var k: float = log(1000.0) / maxf(decay_seconds * RATE, 1.0)
for i: int in out.size():
var gain: float = 1.0
if i < attack_n:
gain = float(i) / attack_n
else:
gain = exp(-k * (i - attack_n))
out[i] *= gain
return out
## Multiplies by a piecewise-linear envelope: points are [seconds, gain] in order.
static func shape(input: PackedFloat32Array, points: Array) -> PackedFloat32Array:
var out: PackedFloat32Array = input.duplicate()
var p: int = 0
for i: int in out.size():
var t: float = float(i) / RATE
while p < points.size() - 2 and t > float(points[p + 1][0]):
p += 1
var t0: float = float(points[p][0])
var t1: float = float(points[p + 1][0])
var g0: float = float(points[p][1])
var g1: float = float(points[p + 1][1])
var u: float = clampf((t - t0) / maxf(t1 - t0, 0.00001), 0.0, 1.0)
out[i] *= lerpf(g0, g1, u)
return out
## Struck modes: a sum of decaying sines at `base` times each ratio, with gains and
## decays (seconds to -60 dB) per mode. Wood, bells, iron and drums.
static func modes(seconds: float, base: float, ratios: Array, gains: Array, decays: Array, glide: float = 1.0) -> PackedFloat32Array:
var out: PackedFloat32Array = silence(seconds)
for m: int in ratios.size():
var hz: float = base * float(ratios[m])
if hz >= RATE * 0.45:
continue
var tone: PackedFloat32Array = decay(osc("sine", seconds, [hz, hz * glide]), 0.0008, float(decays[m]))
mix_into(out, tone, 0, float(gains[m]))
return out
## Adds `source` times `gain` into `target` from sample `at`. With `wrap`, what runs
## past the end comes round to the start, for seamless loops.
static func mix_into(target: PackedFloat32Array, source: PackedFloat32Array, at: int, gain: float = 1.0, wrap: bool = false) -> void:
var size: int = target.size()
for i: int in source.size():
var j: int = at + i
if j >= size:
if not wrap:
return
j %= size
if j < 0:
continue
target[j] += source[i] * gain
static func scaled(input: PackedFloat32Array, gain: float) -> PackedFloat32Array:
var out: PackedFloat32Array = input.duplicate()
for i: int in out.size():
out[i] *= gain
return out
## Gentle saturation, for powder bangs and heavy hits.
static func saturate(input: PackedFloat32Array, drive: float) -> PackedFloat32Array:
var out: PackedFloat32Array = input.duplicate()
var norm: float = tanh(drive)
for i: int in out.size():
out[i] = tanh(out[i] * drive) / norm
return out
## Crossfades the last `seconds` of a buffer into its start and trims them, so it loops
## without a click.
static func make_loop(input: PackedFloat32Array, seconds: float) -> PackedFloat32Array:
var fade: int = n(seconds)
var length: int = input.size() - fade
var out: PackedFloat32Array = input.slice(0, length)
for i: int in fade:
var u: float = float(i) / fade
out[i] = input[i] * sqrt(u) + input[length + i] * sqrt(1.0 - u)
return out
## Echoes: copies of the buffer at `delay` seconds, each `feedback` times the last,
## low-passed a little more each time. Black-powder reports off cliffs and water.
static func echo(input: PackedFloat32Array, delay_seconds: float, feedback: float, repeats: int) -> PackedFloat32Array:
var step: int = n(delay_seconds)
var out: PackedFloat32Array = input.duplicate()
out.resize(input.size() + step * repeats)
for i: int in range(input.size(), out.size()):
out[i] = 0.0
var tap: PackedFloat32Array = input
var gain: float = 1.0
for r: int in range(1, repeats + 1):
gain *= feedback
tap = lowpass(tap, [2400.0 / r])
mix_into(out, tap, step * r, gain)
return out
## Concatenates buffers with `gap` seconds of silence between them.
static func sequence(parts: Array, gap: float) -> PackedFloat32Array:
var out: PackedFloat32Array = PackedFloat32Array()
for index: int in parts.size():
out.append_array(parts[index])
if index < parts.size() - 1:
out.append_array(silence(gap))
return out
static func midi_hz(note: float) -> float:
return 440.0 * pow(2.0, (note - 69.0) / 12.0)
+1
View File
@@ -0,0 +1 @@
uid://dchkfpfsycsy5
+40
View File
@@ -0,0 +1,40 @@
## Writes every generated sound to assets/audio/ and the manifest beside it.
##
## tools/godot.sh --script res://tools/audio/generate.gd [-- --only=name,name]
##
## Same engine, same bytes: test_audio_assets regenerates the files and compares.
extends SceneTree
const Bake = preload("res://tools/audio/bake.gd")
func _init() -> void:
var only: PackedStringArray = PackedStringArray()
for arg: String in OS.get_cmdline_user_args():
if arg.begins_with("--only="):
only = arg.get_slice("=", 1).split(",", false)
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(Bake.OUT_DIR))
var manifest: Dictionary = Bake.read_manifest() if not only.is_empty() else {}
var started: int = Time.get_ticks_msec()
for sound: String in Bake.names():
if not only.is_empty() and not only.has(sound):
continue
var began: int = Time.get_ticks_msec()
var baked: Dictionary = Bake.bake(sound)
var file: FileAccess = FileAccess.open(Bake.path(sound), FileAccess.WRITE)
file.store_buffer(baked["bytes"])
file.close()
manifest[sound] = baked["entry"]
print("%-24s %6.2f s peak %6.2f loudness %6.2f (target %6.1f) %d ms" % [
sound, baked["entry"]["seconds"], baked["entry"]["peak_db"], baked["entry"]["loudness_db"],
baked["entry"]["target_db"], Time.get_ticks_msec() - began])
var sorted: Dictionary = {}
var keys: Array = manifest.keys()
keys.sort()
for key: Variant in keys:
sorted[key] = manifest[key]
var out: FileAccess = FileAccess.open(Bake.MANIFEST, FileAccess.WRITE)
out.store_string(JSON.stringify(sorted, "\t", false) + "\n")
out.close()
print("generated in %d ms" % (Time.get_ticks_msec() - started))
quit(0)
+1
View File
@@ -0,0 +1 @@
uid://cxbv0gxh0s2ur
+89
View File
@@ -0,0 +1,89 @@
## 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
+1
View File
@@ -0,0 +1 @@
uid://cj5nhe3ftmwu5
+481
View File
@@ -0,0 +1,481 @@
## Every Tidewall sound effect, as a recipe over tools/audio/dsp.gd. Each entry names
## its category, whether it loops, and the loudness it is levelled to. The categories
## and their targets are the mix in docs/STYLE.md: the sea always underneath, the
## tide's bell and foghorn the loudest things in a match, weapons dull thumps rather
## than sharp cracks, carpentry for building.
##
## Loudness is the loudest 400 ms window's RMS in dBFS for one-shots, and the whole
## file's RMS for loops (see tools/audio/loudness.gd).
extends RefCounted
const Dsp = preload("res://tools/audio/dsp.gd")
## Category -> target loudness in dBFS.
const TARGETS: Dictionary = {
"ui": -26.0,
"footstep": -24.0,
"player": -22.0,
"build": -19.0,
"impact": -19.0,
"weapon": -15.0,
"water": -18.0,
"tide": -12.0,
"ambience": -27.0,
"music": -25.0,
}
## Sound name -> [category, loops, recipe method].
const SOUNDS: Dictionary = {
# Footsteps, two of each so steps alternate.
"step_grass_1": ["footstep", false, "_step_grass"],
"step_grass_2": ["footstep", false, "_step_grass"],
"step_sand_1": ["footstep", false, "_step_sand"],
"step_sand_2": ["footstep", false, "_step_sand"],
"step_rock_1": ["footstep", false, "_step_rock"],
"step_rock_2": ["footstep", false, "_step_rock"],
"step_wood_1": ["footstep", false, "_step_wood"],
"step_wood_2": ["footstep", false, "_step_wood"],
"step_shallows_1": ["footstep", false, "_step_shallows"],
"step_shallows_2": ["footstep", false, "_step_shallows"],
"land": ["footstep", false, "_land"],
# Weapons: one report per weapon.
"swing_boathook": ["weapon", false, "_swing"],
"shot_fowling_piece": ["weapon", false, "_shot_fowling"],
"shot_dock_carbine": ["weapon", false, "_shot_carbine"],
"shot_signal_revolver": ["weapon", false, "_shot_revolver"],
"shot_rivet_repeater": ["weapon", false, "_shot_repeater"],
"shot_whalers_longgun": ["weapon", false, "_shot_longgun"],
"shot_line_thrower": ["weapon", false, "_shot_line"],
"shot_harpoon_gun": ["weapon", false, "_shot_harpoon"],
"keg_blast": ["weapon", false, "_keg_blast"],
"reload": ["player", false, "_reload"],
# Where shots and swings land.
"hit_wood": ["impact", false, "_hit_wood"],
"hit_stone": ["impact", false, "_hit_stone"],
"hit_iron": ["impact", false, "_hit_iron"],
"hit_player": ["impact", false, "_hit_player"],
"hit_ground": ["impact", false, "_hit_ground"],
"chop": ["impact", false, "_chop"],
# Building.
"build_driftwood": ["build", false, "_build_wood"],
"build_stone": ["build", false, "_build_stone"],
"build_salvage_iron": ["build", false, "_build_iron"],
"break_driftwood": ["build", false, "_break_wood"],
"break_stone": ["build", false, "_break_stone"],
"break_salvage_iron": ["build", false, "_break_iron"],
# Water and the tide.
"splash": ["water", false, "_splash"],
"swim": ["water", false, "_swim"],
"surge": ["tide", false, "_surge"],
"bell": ["tide", false, "_bell"],
"foghorn": ["tide", false, "_foghorn"],
"lighthouse": ["tide", false, "_lighthouse"],
# The player.
"hurt": ["player", false, "_hurt"],
"pickup": ["player", false, "_pickup"],
"chest_open": ["player", false, "_chest_open"],
"eliminated": ["player", false, "_eliminated"],
# Ambience, looped.
"amb_surf": ["ambience", true, "_amb_surf"],
"amb_wind": ["ambience", true, "_amb_wind"],
"amb_rain": ["ambience", true, "_amb_rain"],
# Menus.
"ui_hover": ["ui", false, "_ui_hover"],
"ui_click": ["ui", false, "_ui_click"],
"ui_back": ["ui", false, "_ui_back"],
"ui_confirm": ["ui", false, "_ui_confirm"],
}
## A stable seed per sound name, so each recipe's noise is its own.
static func seed_of(sound: String) -> int:
return hash(sound) & 0x7fffffff
static func render(sound: String) -> PackedFloat32Array:
var entry: Array = SOUNDS[sound]
var variant: int = 2 if sound.ends_with("_2") else 1
var recipes: Object = load("res://tools/audio/recipes.gd").new()
return recipes.call(String(entry[2]), seed_of(sound), variant)
# --- Footsteps -------------------------------------------------------------------
func _step_grass(seed_value: int, variant: int) -> PackedFloat32Array:
var swish: PackedFloat32Array = Dsp.bandpass(Dsp.noise(0.22, seed_value), [2600.0 + 400.0 * variant, 1500.0], 1.2)
swish = Dsp.shape(swish, [[0.0, 0.0], [0.015, 1.0], [0.08, 0.5], [0.22, 0.0]])
var thud: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.22, [95.0 + 8.0 * variant, 60.0]), 0.004, 0.09)
Dsp.mix_into(swish, thud, 0, 0.7)
return swish
func _step_sand(seed_value: int, variant: int) -> PackedFloat32Array:
var crunch: PackedFloat32Array = Dsp.noise(0.26, seed_value)
# Grains: sparse, uneven clicks under a soft hiss.
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = seed_value + 7
for i: int in crunch.size():
crunch[i] *= 0.25 + (1.6 if rng.randf() < 0.06 else 0.0)
crunch = Dsp.bandpass(crunch, [1700.0 + 300.0 * variant], 0.9)
return Dsp.shape(crunch, [[0.0, 0.0], [0.02, 1.0], [0.1, 0.6], [0.26, 0.0]])
func _step_rock(seed_value: int, variant: int) -> PackedFloat32Array:
var click: PackedFloat32Array = Dsp.decay(Dsp.highpass(Dsp.noise(0.18, seed_value), 1800.0), 0.001, 0.03)
var knock: PackedFloat32Array = Dsp.modes(0.18, 210.0 + 25.0 * variant, [1.0, 2.7, 5.1], [0.8, 0.4, 0.2], [0.06, 0.04, 0.02])
Dsp.mix_into(knock, click, 0, 0.8)
var grit: PackedFloat32Array = Dsp.decay(Dsp.bandpass(Dsp.noise(0.18, seed_value + 3), [3200.0], 1.5), 0.01, 0.1)
Dsp.mix_into(knock, grit, Dsp.n(0.02), 0.3)
return knock
func _step_wood(_seed_value: int, variant: int) -> PackedFloat32Array:
var board: PackedFloat32Array = Dsp.modes(0.3, 150.0 + 18.0 * variant, [1.0, 2.31, 3.9, 6.2], [1.0, 0.55, 0.3, 0.15], [0.16, 0.09, 0.05, 0.03])
var heel: PackedFloat32Array = Dsp.modes(0.2, 190.0 + 18.0 * variant, [1.0, 2.31, 3.9], [0.6, 0.3, 0.15], [0.1, 0.06, 0.04])
Dsp.mix_into(board, heel, Dsp.n(0.05), 0.7)
return board
func _step_shallows(seed_value: int, variant: int) -> PackedFloat32Array:
var slosh: PackedFloat32Array = Dsp.bandpass(Dsp.noise(0.4, seed_value), [900.0 + 150.0 * variant, 2600.0], 0.8)
slosh = Dsp.shape(slosh, [[0.0, 0.0], [0.03, 1.0], [0.12, 0.5], [0.4, 0.0]])
var bubble: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.12, [500.0 + 60.0 * variant, 1100.0]), 0.002, 0.08)
Dsp.mix_into(slosh, bubble, Dsp.n(0.06), 0.25)
return slosh
func _land(seed_value: int, _variant: int) -> PackedFloat32Array:
var thump: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.4, [110.0, 45.0]), 0.003, 0.25)
var dust: PackedFloat32Array = Dsp.shape(Dsp.lowpass(Dsp.noise(0.4, seed_value), [1800.0]), [[0.0, 0.0], [0.01, 1.0], [0.35, 0.0]])
Dsp.mix_into(thump, dust, 0, 0.6)
return thump
# --- Weapons ----------------------------------------------------------------------
## A black-powder report: a low thump, a short burst of filtered noise and a tail.
## `body` is the thump's pitch, `bright` the noise's cutoff, `tail` its decay.
func _powder(seed_value: int, length: float, body: float, bright: float, tail: float, drive: float) -> PackedFloat32Array:
var out: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", length, [body * 1.8, body * 0.7]), 0.001, tail * 0.6)
var blast: PackedFloat32Array = Dsp.decay(Dsp.lowpass(Dsp.noise(length, seed_value), [bright, bright * 0.25], 2), 0.0005, tail)
Dsp.mix_into(out, blast, 0, 1.4)
var crack: PackedFloat32Array = Dsp.decay(Dsp.highpass(Dsp.noise(0.03, seed_value + 1), 1200.0), 0.0, 0.02)
Dsp.mix_into(out, crack, 0, 0.35)
return Dsp.saturate(out, drive)
func _shot_fowling(seed_value: int, _variant: int) -> PackedFloat32Array:
# The scattergun: wide and boomy.
return Dsp.echo(_powder(seed_value, 0.9, 70.0, 2200.0, 0.55, 2.2), 0.19, 0.3, 2)
func _shot_carbine(seed_value: int, _variant: int) -> PackedFloat32Array:
return Dsp.echo(_powder(seed_value, 0.6, 95.0, 3400.0, 0.32, 1.8), 0.16, 0.25, 2)
func _shot_revolver(seed_value: int, _variant: int) -> PackedFloat32Array:
# A flare pistol's pop, and the hiss of the charge.
var out: PackedFloat32Array = _powder(seed_value, 0.5, 130.0, 2800.0, 0.2, 1.5)
var hiss: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.5, seed_value + 9), [4000.0, 2200.0], 1.0), [[0.0, 0.0], [0.04, 0.4], [0.5, 0.0]])
Dsp.mix_into(out, hiss, Dsp.n(0.02), 0.5)
return out
func _shot_repeater(seed_value: int, _variant: int) -> PackedFloat32Array:
# Short pop, then the rivet feed's clack.
var out: PackedFloat32Array = _powder(seed_value, 0.4, 120.0, 3000.0, 0.16, 1.6)
var clack: PackedFloat32Array = Dsp.modes(0.1, 1300.0, [1.0, 2.4, 3.7], [0.6, 0.4, 0.2], [0.03, 0.02, 0.015])
Dsp.mix_into(out, clack, Dsp.n(0.07), 0.5)
return out
func _shot_longgun(seed_value: int, _variant: int) -> PackedFloat32Array:
# The heaviest report, rolling back off the cliffs.
return Dsp.echo(_powder(seed_value, 1.2, 55.0, 2600.0, 0.8, 2.4), 0.33, 0.4, 3)
func _shot_line(seed_value: int, _variant: int) -> PackedFloat32Array:
# A compressed-air whump and the line paying out.
var whump: PackedFloat32Array = Dsp.decay(Dsp.lowpass(Dsp.noise(0.7, seed_value), [900.0, 300.0]), 0.004, 0.2)
var body: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.7, [150.0, 70.0]), 0.002, 0.18)
Dsp.mix_into(whump, body, 0, 0.8)
var rope: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.7, seed_value + 2), [2400.0, 1200.0], 2.5), [[0.0, 0.0], [0.08, 0.0], [0.15, 0.5], [0.7, 0.0]])
Dsp.mix_into(whump, rope, 0, 0.6)
return whump
func _shot_harpoon(seed_value: int, _variant: int) -> PackedFloat32Array:
# A spring's twang and the harpoon's flight.
var twang: PackedFloat32Array = Dsp.decay(Dsp.osc("saw", 0.7, [180.0, 140.0], [9.0, 0.3]), 0.002, 0.35)
twang = Dsp.lowpass(twang, [2500.0, 600.0])
var thunk: PackedFloat32Array = Dsp.modes(0.3, 120.0, [1.0, 2.2], [1.0, 0.4], [0.12, 0.08])
Dsp.mix_into(twang, thunk, 0, 0.9)
var flight: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.7, seed_value), [1800.0, 700.0], 3.0), [[0.0, 0.0], [0.05, 0.6], [0.7, 0.0]])
Dsp.mix_into(twang, flight, 0, 0.5)
return twang
func _keg_blast(seed_value: int, _variant: int) -> PackedFloat32Array:
var out: PackedFloat32Array = _powder(seed_value, 1.8, 40.0, 1600.0, 1.3, 2.8)
var debris: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(1.8, seed_value + 5), [2500.0, 900.0], 0.7), [[0.0, 0.0], [0.15, 0.0], [0.3, 0.35], [1.8, 0.0]])
Dsp.mix_into(out, debris, 0, 0.6)
return Dsp.echo(out, 0.4, 0.3, 2)
func _swing(seed_value: int, _variant: int) -> PackedFloat32Array:
# A boathook through the air.
var whoosh: PackedFloat32Array = Dsp.bandpass(Dsp.noise(0.35, seed_value), [500.0, 1600.0], 2.0)
return Dsp.shape(whoosh, [[0.0, 0.0], [0.12, 1.0], [0.2, 0.8], [0.35, 0.0]])
func _reload(_seed_value: int, _variant: int) -> PackedFloat32Array:
# Ramrod and lock: two dull metal clicks.
var first: PackedFloat32Array = Dsp.modes(0.12, 900.0, [1.0, 2.7, 4.4], [0.7, 0.4, 0.2], [0.04, 0.03, 0.02])
var second: PackedFloat32Array = Dsp.modes(0.18, 640.0, [1.0, 2.7, 4.4], [0.9, 0.4, 0.2], [0.07, 0.04, 0.03])
return Dsp.sequence([first, second], 0.12)
# --- Impacts ----------------------------------------------------------------------
func _hit_wood(seed_value: int, _variant: int) -> PackedFloat32Array:
var knock: PackedFloat32Array = Dsp.modes(0.35, 240.0, [1.0, 2.31, 3.9, 6.2], [1.0, 0.6, 0.35, 0.2], [0.18, 0.1, 0.06, 0.04])
var splinter: PackedFloat32Array = Dsp.decay(Dsp.bandpass(Dsp.noise(0.35, seed_value), [3000.0], 1.0), 0.001, 0.12)
Dsp.mix_into(knock, splinter, 0, 0.5)
return knock
func _hit_stone(seed_value: int, _variant: int) -> PackedFloat32Array:
var chip: PackedFloat32Array = Dsp.decay(Dsp.highpass(Dsp.noise(0.3, seed_value), 2000.0), 0.0005, 0.08)
var body: PackedFloat32Array = Dsp.modes(0.3, 420.0, [1.0, 2.9, 5.3], [0.6, 0.35, 0.2], [0.07, 0.05, 0.03])
Dsp.mix_into(chip, body, 0, 0.8)
var rubble: PackedFloat32Array = Dsp.decay(Dsp.lowpass(Dsp.noise(0.3, seed_value + 4), [1200.0]), 0.02, 0.2)
Dsp.mix_into(chip, rubble, Dsp.n(0.03), 0.4)
return chip
func _hit_iron(_seed_value: int, _variant: int) -> PackedFloat32Array:
# A dull clang: salvage plate, not a bell.
return Dsp.lowpass(Dsp.modes(0.8, 310.0, [1.0, 2.76, 4.07, 5.4, 6.9], [1.0, 0.6, 0.45, 0.3, 0.2], [0.6, 0.4, 0.3, 0.2, 0.15]), [3500.0])
func _hit_player(seed_value: int, _variant: int) -> PackedFloat32Array:
# Oilskin taking a hit: a padded thump.
var thump: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.25, [130.0, 70.0]), 0.002, 0.12)
var cloth: PackedFloat32Array = Dsp.decay(Dsp.bandpass(Dsp.noise(0.25, seed_value), [1100.0], 1.0), 0.002, 0.08)
Dsp.mix_into(thump, cloth, 0, 0.7)
return thump
func _hit_ground(seed_value: int, _variant: int) -> PackedFloat32Array:
var puff: PackedFloat32Array = Dsp.decay(Dsp.lowpass(Dsp.noise(0.3, seed_value), [2400.0, 600.0]), 0.002, 0.15)
var body: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.3, [90.0, 50.0]), 0.002, 0.1)
Dsp.mix_into(puff, body, 0, 0.6)
return puff
func _chop(seed_value: int, _variant: int) -> PackedFloat32Array:
# A hook biting timber.
var bite: PackedFloat32Array = Dsp.modes(0.3, 330.0, [1.0, 2.31, 3.9], [1.0, 0.5, 0.3], [0.12, 0.08, 0.05])
var crack: PackedFloat32Array = Dsp.decay(Dsp.highpass(Dsp.noise(0.3, seed_value), 1500.0), 0.0005, 0.05)
Dsp.mix_into(bite, crack, 0, 0.7)
return bite
# --- Building ---------------------------------------------------------------------
func _build_wood(_seed_value: int, _variant: int) -> PackedFloat32Array:
# Three mallet knocks, the last settling a board.
var parts: Array = []
for k: int in 3:
parts.append(Dsp.modes(0.16, 200.0 + 15.0 * k, [1.0, 2.31, 3.9, 6.2], [1.0, 0.5, 0.3, 0.15], [0.12, 0.08, 0.05, 0.03]))
return Dsp.sequence(parts, 0.07)
func _build_stone(seed_value: int, _variant: int) -> PackedFloat32Array:
# A block set down, and the scrape of it squared.
var set_down: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.7, [85.0, 60.0]), 0.004, 0.25)
var clunk: PackedFloat32Array = Dsp.modes(0.7, 380.0, [1.0, 2.9], [0.4, 0.2], [0.08, 0.05])
Dsp.mix_into(set_down, clunk, 0, 1.0)
var scrape: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.7, seed_value), [1400.0, 900.0], 1.5), [[0.0, 0.0], [0.2, 0.0], [0.3, 0.6], [0.65, 0.0]])
Dsp.mix_into(set_down, scrape, 0, 0.7)
return set_down
func _build_iron(_seed_value: int, _variant: int) -> PackedFloat32Array:
# Rivets driven: four clanks, quick.
var parts: Array = []
for k: int in 4:
parts.append(Dsp.lowpass(Dsp.modes(0.12, 520.0 - 20.0 * k, [1.0, 2.76, 4.07], [1.0, 0.5, 0.3], [0.1, 0.07, 0.05]), [3000.0]))
return Dsp.sequence(parts, 0.05)
func _break_wood(seed_value: int, _variant: int) -> PackedFloat32Array:
var crack: PackedFloat32Array = Dsp.decay(Dsp.bandpass(Dsp.noise(1.0, seed_value), [2200.0, 800.0], 0.8), 0.001, 0.5)
for k: int in 4:
var plank: PackedFloat32Array = Dsp.modes(0.4, 160.0 + 30.0 * k, [1.0, 2.31, 3.9], [0.8, 0.4, 0.2], [0.2, 0.1, 0.06])
Dsp.mix_into(crack, plank, Dsp.n(0.12 + 0.13 * k), 0.6)
return crack
func _break_stone(seed_value: int, _variant: int) -> PackedFloat32Array:
var rumble: PackedFloat32Array = Dsp.decay(Dsp.lowpass(Dsp.brown(1.2, seed_value), [900.0, 300.0]), 0.01, 0.9)
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = seed_value + 11
for k: int in 7:
var chunk: PackedFloat32Array = Dsp.modes(0.25, rng.randf_range(250.0, 600.0), [1.0, 2.9], [0.6, 0.3], [0.06, 0.04])
Dsp.mix_into(rumble, chunk, Dsp.n(rng.randf_range(0.0, 0.8)), 0.5)
return rumble
func _break_iron(seed_value: int, _variant: int) -> PackedFloat32Array:
var out: PackedFloat32Array = Dsp.lowpass(Dsp.modes(1.4, 180.0, [1.0, 2.76, 4.07, 5.4], [1.0, 0.7, 0.5, 0.3], [1.0, 0.7, 0.5, 0.3], 0.97), [2500.0])
var groan: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(1.4, seed_value), [600.0, 350.0], 6.0), [[0.0, 0.0], [0.2, 0.8], [1.4, 0.0]])
Dsp.mix_into(out, groan, 0, 0.8)
return out
# --- Water and the tide -----------------------------------------------------------
func _splash(seed_value: int, _variant: int) -> PackedFloat32Array:
var body: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.9, seed_value), [700.0, 2500.0], 0.7), [[0.0, 0.0], [0.02, 1.0], [0.2, 0.5], [0.9, 0.0]])
var plunge: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.5, [160.0, 60.0]), 0.002, 0.25)
Dsp.mix_into(body, plunge, 0, 0.7)
return body
func _swim(seed_value: int, _variant: int) -> PackedFloat32Array:
return Dsp.shape(Dsp.bandpass(Dsp.noise(0.6, seed_value), [600.0, 1400.0], 1.0), [[0.0, 0.0], [0.15, 1.0], [0.6, 0.0]])
func _surge(seed_value: int, _variant: int) -> PackedFloat32Array:
# A wave breaking over the defences: a swell, the crash and the wash back.
var swell: PackedFloat32Array = Dsp.lowpass(Dsp.brown(3.5, seed_value), [200.0, 1600.0])
swell = Dsp.shape(swell, [[0.0, 0.0], [1.0, 0.6], [1.3, 1.0], [2.0, 0.6], [3.5, 0.0]])
var crash: PackedFloat32Array = Dsp.shape(Dsp.lowpass(Dsp.noise(3.5, seed_value + 1), [3000.0, 900.0]), [[0.0, 0.0], [1.15, 0.0], [1.3, 1.0], [3.5, 0.0]])
Dsp.mix_into(swell, crash, 0, 0.8)
return Dsp.saturate(swell, 1.5)
func _bell(_seed_value: int, _variant: int) -> PackedFloat32Array:
# A ship's bell, struck twice: a phase has turned.
var strike: PackedFloat32Array = Dsp.modes(3.0, 523.0, [0.5, 1.0, 1.19, 1.56, 2.0, 2.51, 2.66, 3.01], [0.6, 1.0, 0.5, 0.45, 0.4, 0.25, 0.2, 0.15], [3.0, 2.4, 1.8, 1.5, 1.2, 0.9, 0.8, 0.6])
var out: PackedFloat32Array = Dsp.silence(3.8)
Dsp.mix_into(out, strike, 0, 1.0)
Dsp.mix_into(out, strike, Dsp.n(0.55), 0.85)
return out
func _foghorn(_seed_value: int, _variant: int) -> PackedFloat32Array:
# The tide is rising: a long, low reed note with a beating second voice.
var low: PackedFloat32Array = Dsp.osc("reed", 3.6, [98.0], [0.0, 0.0])
var beat: PackedFloat32Array = Dsp.osc("reed", 3.6, [98.6], [0.0, 0.0], 0.3)
var fifth: PackedFloat32Array = Dsp.osc("bow", 3.6, [147.0])
Dsp.mix_into(low, beat, 0, 0.8)
Dsp.mix_into(low, fifth, 0, 0.35)
low = Dsp.lowpass(low, [700.0], 2)
return Dsp.shape(low, [[0.0, 0.0], [0.35, 1.0], [2.9, 0.9], [3.6, 0.0]])
func _lighthouse(_seed_value: int, _variant: int) -> PackedFloat32Array:
# The last land: a slow, pure tone that swells like a turning lamp.
var tone: PackedFloat32Array = Dsp.osc("sine", 4.0, [392.0])
var upper: PackedFloat32Array = Dsp.osc("sine", 4.0, [588.0])
Dsp.mix_into(tone, upper, 0, 0.4)
var under: PackedFloat32Array = Dsp.osc("bow", 4.0, [196.0])
Dsp.mix_into(tone, Dsp.lowpass(under, [500.0]), 0, 0.5)
return Dsp.shape(tone, [[0.0, 0.0], [0.8, 0.7], [1.6, 1.0], [2.6, 0.8], [4.0, 0.0]])
# --- The player -------------------------------------------------------------------
func _hurt(seed_value: int, _variant: int) -> PackedFloat32Array:
var thump: PackedFloat32Array = Dsp.decay(Dsp.osc("sine", 0.35, [85.0, 50.0]), 0.002, 0.2)
var breath: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.35, seed_value), [900.0, 600.0], 2.0), [[0.0, 0.0], [0.03, 0.5], [0.35, 0.0]])
Dsp.mix_into(thump, breath, 0, 0.7)
return thump
func _pickup(seed_value: int, _variant: int) -> PackedFloat32Array:
# Rope and canvas: a short rustle and a soft knock.
var rustle: PackedFloat32Array = Dsp.shape(Dsp.bandpass(Dsp.noise(0.3, seed_value), [2000.0, 3200.0], 1.2), [[0.0, 0.0], [0.04, 1.0], [0.3, 0.0]])
var knock: PackedFloat32Array = Dsp.modes(0.2, 420.0, [1.0, 2.31], [0.5, 0.2], [0.06, 0.04])
Dsp.mix_into(rustle, knock, Dsp.n(0.08), 0.8)
return rustle
func _chest_open(_seed_value: int, _variant: int) -> PackedFloat32Array:
# A latch, then the lid's hinge creaking.
var latch: PackedFloat32Array = Dsp.modes(0.1, 1100.0, [1.0, 2.7], [0.7, 0.3], [0.04, 0.03])
var creak: PackedFloat32Array = Dsp.osc("saw", 0.6, [220.0, 310.0], [23.0, 0.6])
creak = Dsp.shape(Dsp.bandpass(creak, [900.0], 4.0), [[0.0, 0.0], [0.1, 0.8], [0.5, 0.6], [0.6, 0.0]])
var lid: PackedFloat32Array = Dsp.modes(0.3, 170.0, [1.0, 2.31, 3.9], [1.0, 0.5, 0.3], [0.18, 0.1, 0.06])
return Dsp.sequence([latch, creak, lid], 0.02)
func _eliminated(_seed_value: int, _variant: int) -> PackedFloat32Array:
# A falling minor third on the bell's low partial: someone is out.
var first: PackedFloat32Array = Dsp.modes(1.4, 293.7, [1.0, 2.0, 2.76], [1.0, 0.3, 0.15], [1.2, 0.8, 0.5])
var second: PackedFloat32Array = Dsp.modes(1.6, 246.9, [1.0, 2.0, 2.76], [1.0, 0.3, 0.15], [1.4, 0.9, 0.6])
var out: PackedFloat32Array = Dsp.silence(2.0)
Dsp.mix_into(out, first, 0, 1.0)
Dsp.mix_into(out, second, Dsp.n(0.35), 1.0)
return out
# --- Ambience ---------------------------------------------------------------------
func _amb_surf(seed_value: int, _variant: int) -> PackedFloat32Array:
# Waves arriving every few seconds over a constant low wash.
var length: float = 8.6
var wash: PackedFloat32Array = Dsp.lowpass(Dsp.brown(length, seed_value), [500.0])
var breaking: PackedFloat32Array = Dsp.lowpass(Dsp.noise(length, seed_value + 1), [1400.0])
var points: Array = [[0.0, 0.15]]
for wave: float in [0.4, 3.1, 5.6]:
points.append([wave + 0.9, 0.9])
points.append([wave + 2.2, 0.15])
points.append([length, 0.15])
breaking = Dsp.shape(breaking, points)
Dsp.mix_into(wash, breaking, 0, 0.45)
return Dsp.make_loop(wash, 0.6)
func _amb_wind(seed_value: int, _variant: int) -> PackedFloat32Array:
var length: float = 7.6
var gust: PackedFloat32Array = Dsp.bandpass(Dsp.noise(length, seed_value), [380.0, 620.0], 1.6)
var whistle: PackedFloat32Array = Dsp.bandpass(Dsp.noise(length, seed_value + 1), [1100.0, 900.0], 8.0)
gust = Dsp.shape(gust, [[0.0, 0.5], [1.8, 1.0], [3.4, 0.4], [5.4, 0.9], [length, 0.5]])
Dsp.mix_into(gust, Dsp.shape(whistle, [[0.0, 0.0], [2.0, 0.6], [4.0, 0.0], [length, 0.0]]), 0, 0.5)
return Dsp.make_loop(gust, 0.6)
func _amb_rain(seed_value: int, _variant: int) -> PackedFloat32Array:
var length: float = 6.6
var hiss: PackedFloat32Array = Dsp.lowpass(Dsp.highpass(Dsp.noise(length, seed_value), 900.0), [6000.0])
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = seed_value + 3
var drop: PackedFloat32Array = Dsp.decay(Dsp.bandpass(Dsp.noise(0.05, seed_value + 4), [3500.0], 3.0), 0.0, 0.02)
for k: int in 260:
Dsp.mix_into(hiss, drop, Dsp.n(rng.randf_range(0.0, length)), rng.randf_range(0.5, 2.0), true)
return Dsp.make_loop(Dsp.scaled(hiss, 0.6), 0.6)
# --- Menus ------------------------------------------------------------------------
func _ui_hover(_seed_value: int, _variant: int) -> PackedFloat32Array:
return Dsp.modes(0.08, 1250.0, [1.0, 2.31], [0.6, 0.2], [0.03, 0.02])
func _ui_click(_seed_value: int, _variant: int) -> PackedFloat32Array:
# A peg knocked home.
return Dsp.modes(0.14, 640.0, [1.0, 2.31, 3.9], [1.0, 0.5, 0.25], [0.07, 0.05, 0.03])
func _ui_back(_seed_value: int, _variant: int) -> PackedFloat32Array:
var first: PackedFloat32Array = Dsp.modes(0.1, 640.0, [1.0, 2.31], [0.8, 0.3], [0.05, 0.03])
var second: PackedFloat32Array = Dsp.modes(0.14, 480.0, [1.0, 2.31], [0.8, 0.3], [0.07, 0.04])
return Dsp.sequence([first, second], 0.03)
func _ui_confirm(_seed_value: int, _variant: int) -> PackedFloat32Array:
# A small bell, once.
return Dsp.modes(0.9, 880.0, [1.0, 2.0, 2.76, 4.1], [1.0, 0.4, 0.25, 0.1], [0.7, 0.5, 0.35, 0.2])
+1
View File
@@ -0,0 +1 @@
uid://dtpx02g1114b1
+146
View File
@@ -0,0 +1,146 @@
## Tidewall's adaptive score: one eight-bar tune in D Dorian, 6/8 at 72 dotted
## crotchets a minute, rendered as four layers that loop together and are faded in
## and out by the client (MusicDirector) as the tide builds:
##
## drone low D and A bowed together, always there
## drum a frame drum's 6/8 pulse, from the first rise
## concertina chords on the bellows, from the middle phases
## fiddle the tune, in menus, results and the last land
##
## The tune and the chords were written for Tidewall (see assets/PROVENANCE.md). They
## follow no existing song: the melody is the note list below, nothing else.
extends RefCounted
const Dsp = preload("res://tools/audio/dsp.gd")
const LAYERS: Array[String] = ["music_drone", "music_drum", "music_concertina", "music_fiddle"]
## Loudness per layer in dBFS RMS, so the full mix sits near -20.
const TARGETS: Dictionary = {
"music_drone": -29.0,
"music_drum": -28.0,
"music_concertina": -29.0,
"music_fiddle": -26.0,
}
const EIGHTH: float = 60.0 / (72.0 * 3.0)
const BAR_EIGHTHS: int = 6
const BARS: int = 8
## The tune: [MIDI note, length in quavers], six quavers to a bar.
const TUNE: Array = [
[69, 3], [67, 1], [65, 2],
[64, 2], [62, 1], [64, 3],
[65, 1], [67, 1], [69, 1], [74, 3],
[72, 2], [69, 1], [64, 3],
[62, 1], [65, 1], [69, 1], [71, 2], [69, 1],
[67, 3], [64, 3],
[71, 2], [67, 1], [74, 3],
[69, 3], [62, 3],
]
## One chord a bar, as MIDI notes: Dm C Dm Am Dm C G Dm.
const CHORDS: Array = [
[50, 53, 57], [48, 52, 55], [50, 53, 57], [45, 52, 57],
[50, 53, 57], [48, 52, 55], [47, 50, 55], [50, 53, 57],
]
## Frame drum strokes per bar, a gain per quaver (0 for a rest); bar 4 and 8 fill.
const DRUM: Array = [1.0, 0.0, 0.45, 0.8, 0.0, 0.45]
const DRUM_FILL: Array = [1.0, 0.35, 0.45, 0.8, 0.5, 0.6]
static func loop_seconds() -> float:
return EIGHTH * BAR_EIGHTHS * BARS
static func render(layer: String) -> PackedFloat32Array:
match layer:
"music_drone":
return _drone()
"music_drum":
return _drum()
"music_concertina":
return _concertina()
"music_fiddle":
return _fiddle()
return PackedFloat32Array()
static func _loop() -> PackedFloat32Array:
var out: PackedFloat32Array = PackedFloat32Array()
out.resize(int(round(loop_seconds() * Dsp.RATE)))
out.fill(0.0)
return out
## A bowed note: soft attack, slow vibrato that grows, a short release.
static func _bowed(note: int, seconds: float, vibrato_depth: float) -> PackedFloat32Array:
var hz: float = Dsp.midi_hz(note)
var tone: PackedFloat32Array = Dsp.osc("bow", seconds + 0.15, [hz], [5.3, vibrato_depth])
tone = Dsp.lowpass(tone, [2600.0])
return Dsp.shape(tone, [[0.0, 0.0], [0.07, 1.0], [seconds * 0.6, 0.85], [seconds, 0.7], [seconds + 0.15, 0.0]])
static func _drone() -> PackedFloat32Array:
var out: PackedFloat32Array = _loop()
var length: float = loop_seconds()
var fade: float = 1.5
for note: int in [38, 45, 50]:
var hz: float = Dsp.midi_hz(note)
var tone: PackedFloat32Array = Dsp.osc("bow", length + fade, [hz], [0.21, 0.04])
tone = Dsp.lowpass(tone, [900.0], 2)
# Swells with the bow, twice a loop.
tone = Dsp.shape(tone, [[0.0, 0.0], [fade, 0.8], [length * 0.25, 1.0], [length * 0.5, 0.75], [length * 0.75, 1.0], [length, 0.8], [length + fade, 0.0]])
Dsp.mix_into(out, tone, 0, 0.5 if note == 50 else 1.0, true)
return out
static func _drum() -> PackedFloat32Array:
var out: PackedFloat32Array = _loop()
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = 6809
for bar: int in BARS:
var pattern: Array = DRUM_FILL if bar % 4 == 3 else DRUM
for q: int in BAR_EIGHTHS:
var gain: float = float(pattern[q])
if gain <= 0.0:
continue
var low: bool = q == 0 or q == 3
var base: float = 82.0 if low else 120.0
var hit: PackedFloat32Array = Dsp.modes(0.5, base, [1.0, 1.59, 2.14], [1.0, 0.4, 0.2], [0.35 if low else 0.18, 0.12, 0.08], 0.85)
var slap: PackedFloat32Array = Dsp.decay(Dsp.bandpass(Dsp.noise(0.06, rng.randi()), [1600.0], 1.0), 0.0, 0.03)
Dsp.mix_into(hit, slap, 0, 0.5 if low else 0.8)
var at: int = int(round((bar * BAR_EIGHTHS + q) * EIGHTH * Dsp.RATE))
Dsp.mix_into(out, hit, at, gain * rng.randf_range(0.9, 1.0), true)
return out
static func _concertina() -> PackedFloat32Array:
var out: PackedFloat32Array = _loop()
for bar: int in BARS:
var chord: Array = CHORDS[bar]
# Two bellows pushes a bar, on each dotted crotchet.
for half: int in 2:
var seconds: float = EIGHTH * 3.0 - 0.06
var press: PackedFloat32Array = Dsp.silence(seconds + 0.1)
for note: int in chord:
var hz: float = Dsp.midi_hz(note)
Dsp.mix_into(press, Dsp.osc("reed", seconds + 0.1, [hz * 0.9977]), 0, 0.5)
Dsp.mix_into(press, Dsp.osc("reed", seconds + 0.1, [hz * 1.0023], [0.0, 0.0], 0.37), 0, 0.5)
press = Dsp.lowpass(press, [1700.0], 2)
var gain: float = 1.0 if half == 0 else 0.7
press = Dsp.shape(press, [[0.0, 0.0], [0.06, gain], [seconds, gain * 0.8], [seconds + 0.1, 0.0]])
var at: int = int(round((bar * BAR_EIGHTHS + half * 3) * EIGHTH * Dsp.RATE))
Dsp.mix_into(out, press, at, 1.0, true)
return out
static func _fiddle() -> PackedFloat32Array:
var out: PackedFloat32Array = _loop()
var quaver: int = 0
for entry: Array in TUNE:
var note: int = int(entry[0])
var length: int = int(entry[1])
var seconds: float = length * EIGHTH - 0.02
var tone: PackedFloat32Array = _bowed(note, seconds, 0.05 + 0.04 * length)
Dsp.mix_into(out, tone, int(round(quaver * EIGHTH * Dsp.RATE)), 1.0, true)
quaver += length
return out
+1
View File
@@ -0,0 +1 @@
uid://dfm7wi1tvyg0c
+44
View File
@@ -0,0 +1,44 @@
## Mono 16-bit PCM WAV files, written and read without the engine's importer, so the
## generator's output is byte-for-byte the same on every run.
extends RefCounted
const Dsp = preload("res://tools/audio/dsp.gd")
static func encode(samples: PackedFloat32Array, rate: int = Dsp.RATE) -> PackedByteArray:
var data: StreamPeerBuffer = StreamPeerBuffer.new()
data.big_endian = false
var bytes: int = samples.size() * 2
data.put_data("RIFF".to_ascii_buffer())
data.put_u32(36 + bytes)
data.put_data("WAVEfmt ".to_ascii_buffer())
data.put_u32(16)
data.put_u16(1)
data.put_u16(1)
data.put_u32(rate)
data.put_u32(rate * 2)
data.put_u16(2)
data.put_u16(16)
data.put_data("data".to_ascii_buffer())
data.put_u32(bytes)
for value: float in samples:
data.put_16(clampi(int(round(clampf(value, -1.0, 1.0) * 32767.0)), -32768, 32767))
return data.data_array
## The samples of a file encode() wrote, or an empty array if it isn't one.
static func decode(bytes: PackedByteArray) -> PackedFloat32Array:
var out: PackedFloat32Array = PackedFloat32Array()
if bytes.size() < 44 or bytes.slice(0, 4).get_string_from_ascii() != "RIFF" or bytes.slice(36, 40).get_string_from_ascii() != "data":
return out
var count: int = bytes.decode_u32(40) / 2
if 44 + count * 2 > bytes.size():
return out
out.resize(count)
for i: int in count:
out[i] = bytes.decode_s16(44 + i * 2) / 32767.0
return out
static func rate_of(bytes: PackedByteArray) -> int:
return bytes.decode_u32(24) if bytes.size() >= 44 else 0
+1
View File
@@ -0,0 +1 @@
uid://35vaxn061683