Skip to content

77 game-dev skills for AI coding agents — Godot, Unity, Unreal, Phaser, PixiJS, three.js, Bevy, pygame, LÖVE, Roblox. Portable SKILL.md Agent Skills (the format Anthropic launched as Claude Skills), with a router that loads the right skill for your engine and task. Runs in Claude Code, Cursor, Kiro, Codex, Copilot, Gemini CLI and more.

fix(audio-design): correct loudness targets and make the Godot snippets run

Kiro Crewf6ce608a13d6
diff --git a/skills/disciplines/audio-design/SKILL.md b/skills/disciplines/audio-design/SKILL.md
index 84fc078..3799699 100644
--- a/skills/disciplines/audio-design/SKILL.md
+++ b/skills/disciplines/audio-design/SKILL.md
@@ -35,8 +35,11 @@ engine concerns. For UI sliders that drive bus volume, see the engine UI skill.
sliders map to bus volumes. Never set hundreds of clip volumes by hand.
2. **Work in decibels, not linear.** Perceived loudness is logarithmic. Volume
controls and automation should operate in dB; convert only at the edges.
-3. **Leave headroom.** Mix so the Master peaks below 0 dBFS (aim for a target
- loudness, e.g. around -14 to -16 LUFS for many games) to avoid clipping.
+3. **Leave headroom and hit a loudness target.** Keep true peaks at or below -1 dBTP and
+ measure integrated loudness over a representative play session with an ITU-R BS.1770 meter.
+ Sony's ASWG-R001 recommendation, the usual reference, is -24 LUFS (±2) for console/PC
+ played on TVs and speakers and -18 LUFS (±2) for handheld/mobile. Music-streaming targets
+ such as -14 LUFS are not game targets.
4. **Duck competing sources** with a sidechain compressor (or volume automation):
when voice/important SFX plays, the music bus dips, then recovers.
5. **Make music adaptive** via *vertical* layering (stems faded in/out) and/or
@@ -50,87 +53,129 @@ engine concerns. For UI sliders that drive bus volume, see the engine UI skill.
## Patterns
-### 1. Bus routing and dB gain
+Snippets are Godot 4.7 GDScript; the Unity 6.3 LTS and middleware equivalents are noted
+under each one.
+
+### 1. Bus routing and slider-to-gain mapping
```gdscript
# Route sounds to named buses; control GROUPS, not individual clips.
-sfx_player.bus = "SFX"
-music_player.bus = "Music"
+sfx_player.bus = &"SFX"
+music_player.bus = &"Music"
-# Map a 0..1 settings slider to decibels (linear_to_db), the perceptual unit.
-func set_bus_volume(bus_name: String, slider01: float) -> void:
+# Map a 0..1 settings slider to bus gain. Squaring the slider first spreads the
+# audible change over the whole travel; a raw linear-amplitude slider puts most of
+# the change in the bottom fifth.
+func set_bus_volume(bus_name: StringName, slider01: float) -> void:
var idx := AudioServer.get_bus_index(bus_name)
- var db := linear_to_db(clamp(slider01, 0.0001, 1.0)) # 0 -> silence, 1 -> 0 dB
- AudioServer.set_bus_volume_db(idx, db)
-# RIGHT: slider -> dB via linear_to_db. WRONG: assigning slider01 straight as dB
-# (a "0.5" would be only +0.5 dB — almost no change — and 0 would be 0 dB, full).
+ var gain := clampf(slider01, 0.0, 1.0)
+ AudioServer.set_bus_mute(idx, gain <= 0.0) # true silence at 0
+ AudioServer.set_bus_volume_linear(idx, gain * gain) # = set_bus_volume_db(idx, linear_to_db(gain * gain))
+# WRONG: AudioServer.set_bus_volume_db(idx, slider01): "0.5" is +0.5 dB (no audible
+# change) and 0 is 0 dB (full volume).
```
+Unity 6.3 LTS: expose the group's Volume as a mixer parameter, then
+`mixer.SetFloat("MusicVolume", 20f * Mathf.Log10(Mathf.Max(gain * gain, 0.0001f)))` (the mixer
+floor is -80 dB). An `AudioSource` whose Output group is empty plays outside your mixer groups,
+so no category slider or snapshot affects it: route every source.
+
### 2. Ducking via sidechain (music dips under voice)
+A compressor on the music bus, keyed by the voice bus, lowers the music while dialogue plays and
+releases afterwards. Starting values: threshold about -30 dB on the voice signal, ratio 4:1 to
+8:1, attack as fast as the effect allows (up to ~10 ms), release 300-500 ms.
+
```gdscript
-# A compressor on the MUSIC bus, keyed by the VOICE bus, lowers music while
-# dialogue plays, then releases. This is "sidechain ducking".
-# Setup (engine-specific): add a compressor effect to the Music bus and set its
-# sidechain to the Voice bus. Then tune:
-# threshold: level on Voice that triggers ducking (e.g. -30 dB)
-# ratio: how hard to duck (e.g. 8:1 for a clear dip)
-# attack: fast (~10 ms) so music gets out of the way promptly
-# release: slow (~300-500 ms) so it recovers smoothly, not pumping
-# No-middleware alternative: tween the Music bus volume down on voice start and
-# back up on voice end.
+# Godot 4.7: AudioEffectCompressor reads its sidechain bus for threshold detection.
+func _ready() -> void:
+ var duck := AudioEffectCompressor.new()
+ duck.sidechain = &"Voice" # the bus that triggers the dip
+ duck.threshold = -30.0 # dB, range -60..0
+ duck.ratio = 8.0 # range 1..48
+ duck.attack_us = 2000.0 # MICROseconds, range 20..2000 (2 ms is the maximum)
+ duck.release_ms = 400.0 # milliseconds, range 20..2000
+ AudioServer.add_bus_effect(AudioServer.get_bus_index(&"Music"), duck)
+
+# No-compressor alternative: tween a dedicated "MusicDuck" bus that sits between
+# Music and Master, so ducking never overwrites the player's Music slider.
func duck_music(active: bool) -> void:
- var target_db := -12.0 if active else 0.0
+ var idx := AudioServer.get_bus_index(&"MusicDuck")
+ var from := AudioServer.get_bus_volume_db(idx)
+ var to := -12.0 if active else 0.0
create_tween().tween_method(
- func(v): set_bus_volume_db("Music", v), current_music_db, target_db, 0.25)
+ func(db: float) -> void: AudioServer.set_bus_volume_db(idx, db), from, to, 0.25)
```
+Unity 6.3 LTS: add a **Duck Volume** effect to the Music group and a **Send** effect on the Voice
+group targeting it. FMOD Studio and Wwise have sidechain/auto-ducking on buses; prefer those over
+script tweens when the project already uses middleware.
+
### 3. SFX variation (kill the "machine gun" repeat)
```gdscript
-# Randomize pitch slightly and pick from a sample pool so repeats feel organic.
-func play_varied(samples: Array, bus := "SFX") -> void:
- var p := AudioStreamPlayer.new()
- p.stream = samples[randi() % samples.size()] # rotate through several takes
- p.bus = bus
- p.pitch_scale = randf_range(0.94, 1.06) # +/- ~6% pitch wobble
- add_child(p); p.play()
- p.finished.connect(p.queue_free) # clean up one-shots
+# Godot 4.7: AudioStreamRandomizer picks a take (no immediate repeats by default)
+# and randomizes pitch and volume on every play. No per-shot node allocation.
+func make_footsteps(samples: Array[AudioStream]) -> void:
+ var rnd := AudioStreamRandomizer.new()
+ for s in samples:
+ rnd.add_stream(-1, s) # -1 appends; optional third arg = weight
+ rnd.random_pitch_semitones = 1.0 # up to +/-1 semitone (about 6%)
+ rnd.random_volume_offset_db = 2.0 # up to +/-2 dB
+ $Footsteps.stream = rnd
+ $Footsteps.max_polyphony = 4 # overlapping steps without new players
```
+Unity 6.3 LTS has no built-in randomizer: keep an array of clips, pick one that differs from the
+last, set `pitch` in a small range, and call `PlayOneShot` on a pooled `AudioSource`.
+
### 4. Beat-synced events (quantize to the music grid)
```gdscript
# Schedule gameplay/visuals on musical time, not frame time, so they land on beat.
const BPM := 120.0
-var seconds_per_beat := 60.0 / BPM
+const SECONDS_PER_BEAT := 60.0 / BPM
+
+# What the player HEARS now: playback position, plus time since the last mix
+# (the position only updates once per mixed chunk), minus output latency.
+func song_time() -> float:
+ return ($Music.get_playback_position() + AudioServer.get_time_since_last_mix()
+ - AudioServer.get_output_latency())
-func current_beat(playback_position_sec: float) -> int:
- return int(playback_position_sec / seconds_per_beat)
+func current_beat() -> int:
+ return int(song_time() / SECONDS_PER_BEAT)
# Quantize an action to the NEXT beat boundary instead of firing immediately.
-func time_until_next_beat(pos: float) -> float:
- return seconds_per_beat - fmod(pos, seconds_per_beat)
-# Drive timing from the audio playback clock, which is steadier than frame delta.
+func time_until_next_beat() -> float:
+ return SECONDS_PER_BEAT - fmod(song_time(), SECONDS_PER_BEAT)
```
+Unity 6.3 LTS: read `AudioSettings.dspTime` and schedule with `AudioSource.PlayScheduled(dspTime)`
+for sample-accurate starts.
+
## Pitfalls
-- **Treating slider values as dB.** Volume is logarithmic; map `0..1` through
- `linear_to_db` (and back with `db_to_linear`). A linear slider on raw amplitude
- feels like it does nothing until the very bottom.
+- **Treating slider values as dB.** Map `0..1` to gain (`linear_to_db`, or
+ `set_bus_volume_linear`) and back with `db_to_linear`. A raw linear-amplitude
+ slider sounds almost unchanged over most of its travel and drops off at the
+ bottom; squaring (or cubing) the slider before conversion evens it out.
- **Per-clip volume instead of buses** makes a global balance pass impossible and
bloats save/settings. Mix on buses.
+- **Sources that bypass the mix.** A Unity `AudioSource` with no Output group, or a
+ Godot player left on `Master`, ignores the category sliders. Route everything.
- **Clipping the master.** Summed sounds exceed 0 dBFS and distort. Leave
headroom; put a limiter on Master as a safety net, not as the mixer.
- **Pumping ducking**: too-fast release or too-high ratio makes music audibly
breathe. Lengthen release; lower ratio.
+- **Ducking that fights the volume slider.** Tweening the Music bus back to 0 dB
+ overwrites the player's setting. Duck a separate bus, or use the compressor.
- **Looping a single music track** for the whole game feels flat. Use layers or
segments that respond to state (see the reference).
- **Beat sync off frame time.** `delta` drifts; read the **audio playback
- position** for musical timing, and account for output latency.
+ position**, add the time since the last mix, and subtract output latency.
- **Unbounded one-shot players**: spawning AudioStreamPlayers without freeing
- them leaks. Free on `finished`, or use a small pool.
+ them leaks. Use `max_polyphony`/`AudioStreamRandomizer`, free on `finished`, or
+ pool them.
## References
diff --git a/skills/disciplines/audio-design/references/adaptive-music.md b/skills/disciplines/audio-design/references/adaptive-music.md
index 0c4dba7..ddc194b 100644
--- a/skills/disciplines/audio-design/references/adaptive-music.md
+++ b/skills/disciplines/audio-design/references/adaptive-music.md
@@ -37,6 +37,11 @@ Tips: author stems at the same BPM/length and bounce them aligned. Fades of
~0.5–1.5 s feel musical; instant cuts feel mechanical. Because layers never
restart, intensity can change any time without losing sync.
+Godot 4.7 has this built in: an `AudioStreamSynchronized` holds up to 32 sub-streams
+that start at exactly the same time in one player, and
+`set_sync_stream_volume(index, volume_db)` fades a layer. Prefer it over several
+players when the stems must stay sample-aligned.
+
## Horizontal re-sequencing
Switch segments at safe musical points so transitions don't sound abrupt.
@@ -44,19 +49,30 @@ Switch segments at safe musical points so transitions don't sound abrupt.
```gdscript
# Request a section change; apply it only at the next bar boundary.
var pending_section := ""
+var _last_bar := -1
const BPM := 120.0
const BEATS_PER_BAR := 4
-var sec_per_bar := 60.0 / BPM * BEATS_PER_BAR
-
-func request_section(name: String) -> void:
- pending_section = name # don't switch mid-bar; queue it
-
-func on_bar_boundary(pos: float) -> void:
- if pending_section != "":
- crossfade_to(pending_section, 0.2) # short crossfade across the seam
- pending_section = ""
+const SEC_PER_BAR := 60.0 / BPM * BEATS_PER_BAR
+
+func request_section(section: String) -> void:
+ pending_section = section # don't switch mid-bar; queue it
+
+func _process(_dt: float) -> void:
+ var bar := int(song_time() / SEC_PER_BAR) # song_time(): see the beat-sync pattern in SKILL.md
+ if bar != _last_bar:
+ _last_bar = bar
+ if pending_section != "":
+ crossfade_to(pending_section, 0.2) # short crossfade across the seam
+ pending_section = ""
```
+`_process` only sees the boundary on the first frame after it, so this switch lands up to one
+frame late. Godot 4.7's `AudioStreamInteractive` does the switch in the audio thread instead:
+add clips, set `bpm`/`bar_beats` on each clip's Ogg Vorbis or MP3 stream (`AudioStreamWAV` has no
+beat data), define
+transitions with `add_transition(from, to, TRANSITION_FROM_TIME_NEXT_BAR, ...)`, then call
+`get_stream_playback().switch_to_clip_by_name(&"combat")` on the player.
+
Transition strategies, roughly increasing in polish:
- **Immediate crossfade** — quick volume blend; fine for low-stakes changes.
@@ -72,9 +88,13 @@ Drive the music from a small, smoothed intensity value rather than raw events:
```gdscript
# Combine signals into 0..1, smooth it, then map to layers/sections with hysteresis.
-func intensity_from_state(enemies_near: int, player_hp01: float) -> float:
- var raw = clamp(enemies_near / 5.0, 0.0, 1.0) * (1.0 - 0.4 * player_hp01)
- intensity = lerp(intensity, raw, 0.05) # smooth so it doesn't flicker
+var intensity := 0.0
+
+func intensity_from_state(enemies_near: int, player_hp01: float, dt: float) -> float:
+ var raw := clampf(enemies_near / 5.0, 0.0, 1.0) * (1.0 - 0.4 * player_hp01)
+ # Frame-rate-independent smoothing; a fixed lerp(…, 0.05) per frame would settle
+ # twice as fast at 120 FPS as at 60.
+ intensity = lerpf(intensity, raw, 1.0 - exp(-3.0 * dt))
return intensity
# Hysteresis: require crossing different thresholds up vs down so the music