A22 × Permadice Soundtrack Engine
Original, deterministic, loudness-mastered music and sound effects, rendered offline by two small
native programs that the skill builds from its own sources (engine/vendor/) on first use. It needs
no DAW, no samples and no network.
Paths below are relative to this skill's folder (the folder holding this SKILL.md). Run scripts
as python3 <skill>/scripts/..., or uv run --with numpy python3 <skill>/scripts/... when numpy
isn't installed. Preview videos also need --with pillow.
First use on a machine
python3 <skill>/scripts/setup.py
This checks Python 3.9+, numpy, ffmpeg (with the filters used) and a C/C++ compiler. It builds the
engine (5–20 s, once) into engine/bin/, or, when the skill folder is read-only, into
~/.cache/a22-soundtrack/<platform>/bin/ ($A22_ENGINE_BIN overrides both), and renders a smoke test. If a check fails, it prints the install
command (Homebrew / apt / dnf). Tested on macOS (Apple Silicon, and Intel binaries under Rosetta) with Python 3.9 and 3.14 and ffmpeg 9. Linux should work but hasn't been tested yet; on Windows use WSL. Older ffmpeg builds without alimiter latency or amix normalize get measured fallbacks. Importing
score_engine also builds the engine automatically if needed.
Pick the workflow
| The user wants… | Workflow | Start from |
|---|---|---|
| Music + sounds that hit on-screen moments | A: cue-synced | scripts/render_cue_demo.py |
| A seamless looping bed for a web hero / looping video | C: loop | scripts/render_loop_demo.py |
| Effects only, on a video that already has music (keep the music) | effects over music | scripts/sfx_over_music.py |
| A music bed (+ effects) under the video's own voiceover | A, voiceover mode | render_cue_demo.py cues.json out/ video.mp4 --voiceover |
| Just a background track | B: track only | scripts/render_10_demos.py |
Workflow A: Scoring a Video to Its Cues
Read references/cue_sheets.md first.
- Find the moments and write
cues.json. For a finished video, runuv run --with numpy --with pillow python3 <skill>/scripts/find_cues.py video.mp4 --out cues_draft.json --sheet moments.png(Pillow draws the contact sheet). It lists every moment where something appears, moves, ticks, fades or cuts, with frame-exact times (typing comes with one time per keystroke), and writes a draft cue sheet with suggested sounds. Check the contact sheet, rename the labels, fix the sounds, and addkeyand the section anchors (where the music should change). For a motion-reel project, take times from itscues.js. Mind whattmeans: the start for most sounds, the fastest frame forwhoosh, the arrival forriser/swell. - Compose against it: copy
scripts/render_cue_demo.pyand run your copy asA22_SKILL=<skill> python3 my_score.py cues.json out_dir/ video.mp4(a copy finds the skill by itself in~/.claude/skills/or./.claude/skills/; anywhere else it needsA22_SKILL).Track.from_cues(...)fits the tempo so sections land on bar lines. If no tempo does that, it puts them on beats instead (a finished video's cuts usually sit on beats of some tempo), printing each anchor's error. The template's arrangement follows the order of the sections, whatever their names, and starts each part on its section's exact beat. Change its instruments, key, chords and density to fit the brief. Place entries and drops witht.section(name)/t.beat_at(seconds), and melodies witht.scale_note()so they agree with the in-key event sounds. - Render with stems:
t.render("mix.wav", "mix.mp3", stems_dir="stems"). Check the report: tempo fit, SFX level ladder,master chain offset +0 samples, impact peak reduction, LUFS/peak/mono. - Measure sync:
scripts/verify_sync.py cues.json stems/sfx.wavmust PASS (half a frame); it says how many events it could measure, and prints INCONCLUSIVE when none stand out. Run it on an effects-only file: on a mix that also has music, the music's own hits can read as errors. It checks sound against the cue sheet; whether the cue sheet matches the picture is step 1's job. Then makescripts/cue_preview_video.py cues.json mix.wav preview.mp4 --bpm N --video video.mp4so the user can watch the real picture, the cue labels and the sound together. - Mux with
scripts/mux_video.py(below). Cue-mode renders are already exactly the video's length.
Effects over a video that already has music
Step 1 as above (find_cues.py, then name and fix the moments), then one command:
uv run --with numpy --with pillow python3 <skill>/scripts/sfx_over_music.py video.mp4 cues.json out/ --preview
It renders the effects levelled against the video's own music, measures their sync, mixes them in
(picture copied, music not ducked, no added delay), and proves the music is unchanged: lag, level per
quarter second and dips, against what one AAC re-encode alone does. It ends with sync PASS, music KEPT
and prints each event's peak against the music's local peak (trim anything flagged with "gain").
Tempo plays no part here. Key: the tonal sounds (pop, chime, sparkle, success, glide) use the
sheet's key/scale; use the music's key if the user or the project knows it. The skill can't detect
a key, so don't guess one; without it, lean on untuned sounds (tap, tick, whoosh, swell, count).
If the music has its own hit right next to a cue, prefer a lighter sound there: the music can't duck
in this mode, so an impact would flam against it.
Workflow C: Seamless Loop (web hero)
Set "loop": true in the cue sheet (or pass Track(..., duration=D, loop=True)) and copy scripts/render_loop_demo.py.
- How it works: the arrangement plays twice and only the second pass is kept, so release and reverb tails from the end ring into the start. Event sounds are placed in three passes, so a riser or swell aimed at t=0 builds at the end of the file. The last 120 ms blend into the audio that truly precedes the start, and mastering runs on three copies with the middle one kept, so the limiter is already settled at the seam.
- Finding the moments:
scripts/find_cues.py hero.mp4 --out cues.json --sheet moments.png --loop(it says when the last frame leads back into the first). - Composing: make the last bar lead back to the first, e.g. V → I across the seam. Hold chords up to the bar line, and use whole bars: with
"bpm": "auto", a loop only gets tempos that make it whole bars (8.4 s → 3 bars at 85.714 BPM). The template works for any bar count, section names and major key: copy it and runA22_SKILL=<skill> python3 my_loop.py cues.json out_dir/ hero.mp4. - Checking: the render prints
loop seam: wrap step …x, curvature …x -> seamless(both under 4×). Runscripts/check_loop.py loop.wav --preview loop_x3.wavfor any file, andscripts/mux_video.py hero.mp4 loop.wav hero_x3.mp4 --loops 3so the team can watch the seam. On the demo, a naive one-pass cut measured 6.8×/11.5× (a click); loop mode measured 0.66×/1.35×. What the other loop numbers mean:references/cue_sheets.md→ Loops. - Delivering: the WAV is the sample-exact loop; treat the MP3 as a preview (encoder padding). For the site, either bake the sound into the video (
mux_video.py, unmute setsvideo.muted = false; simplest, may tick at the restart) or loop the WAV with Web Audio locked to the video (gapless; tested snippet inreferences/cue_sheets.md→ Loops). Muted autoplay hero videos need no audio until someone unmutes.
Workflow B: Background Track Only (no cues)
- Length: for a video, choose
bpmandbarssobars * 4 * (60 / bpm)fits the runtime, plustail_secof 2–3 s for reverb decay. Or use Workflow A with no events, which gives an exact length. - Direction and stems: references/engines_and_presets.md lists the 10 A22 engines, 80 algorithms, drum map and Permadice recipes. Follow references/composition_playbook.md: HPF/LPF separation, mono sub, subtle Permadice, mono-safe width.
scripts/render_10_demos.pyholds 10 working templates (76–128 BPM). - Loudness targets:
target_lufs−14 to −15 for music-led reels, −19.5 under voiceover (withlpf_hz=3400on pads/chords), about −16 for calm web beds.
Audio audit (every workflow; measured, the human listens)
- You can't hear audio. Don't claim you listened, and don't try to "listen" by reading an
.mp3with a file tool. Report numbers and say measured, not heard. Track.renderprintsLUFS | peak dBTP | mono -X LU | corr, plus the seam (loops) and sync (cues) lines. Loudness should be within ±0.5 LU of target, true peak<= -1.0 dBTP, mono loss<= 3 LU, correlation positive. Fix everyWARNINGbefore delivering.- Renders are deterministic: the same script gives a byte-identical WAV on the same machine. Across CPU architectures they differ only by rounding (about −80 dB or lower).
- Hand the user something to listen to: the mix,
demos/audition.html, acue_preview_video.pyMP4, or acheck_loop.py --previewfile.
Mux onto a video
# replace / add the soundtrack (silent video)
python3 <skill>/scripts/mux_video.py input.mp4 soundtrack.wav output.mp4
# under narration: the voice is untouched, the music ducks ~8 dB while it speaks,
# effects from --sfx play on top without ducking
uv run --with numpy python3 <skill>/scripts/mux_video.py input.mp4 out/stems/music.wav output.mp4 \
--mix-existing --music-db -6 --sfx out/stems/sfx.wav
# effects over a video's existing music: no ducking
python3 <skill>/scripts/mux_video.py input.mp4 sfx.wav output.mp4 --mix-existing --duck-ratio 0
Under a voiceover: the template does all of it in one command:
uv run --with numpy --with pillow python3 <skill>/scripts/render_cue_demo.py cues.json out/ video.mp4 --voiceover [--music-db -6] [--end-fade 0.8]
renders a quieter, darker bed (−19.5 LUFS, pad and keys low-passed at 3.4 kHz, no snare), measures
the sync, and writes out/final.mp4 plus out/final_no_sfx.mp4 (the same music without the effects
or the dips they cause, from stems/music_noduck.wav), each with the checks below. Match
--end-fade to a fade at the end of the picture. A copy of the template keeps the flag. By hand:
render the bed at target_lufs=-19.5 (with lpf_hz=3400 on pads/chords), then mix the stems: music.wav as the soundtrack (ducked) and sfx.wav through --sfx (not ducked,
so a click during a sentence stays audible). --music-db stacks on top of the render's level. After the
mix it prints whether the voice is unchanged (lag, level per quarter second, dips, against one AAC
re-encode alone), how far music + effects sit under the voice while it speaks and in the pauses, and
whether the limiter acted. Aim for about 6–10 dB under the voice in the pauses and 12+ dB while it
speaks: with the voice at −18 LUFS and the bed rendered at −19.5, --music-db -6 measured −9 dB in the
pauses and −14 dB under speech in a test. Room noise in the recording counts as a pause. --music-db
lowers only the music, so the effects end up that much hotter against it than the printed level ladder
(usually right under a voice); --sfx-db sets their level separately. The video's own audio keeps its
sample rate.
The output is always exactly the video's length. A longer soundtrack is cut with a fade (--fade-out). A shorter one is padded with silence and prints a WARNING; treat that as a bug and re-render longer.