Skip to content

Music on the SNES: Krautrecker's songs, the effects through its driver - #100

Merged
skerkewitz-agent[bot] merged 1 commit into
developfrom
snes-music
Nov 30, 2026
Merged

skerkewitz-agent[bot] merged 1 commit into
developfrom
snes-music

Conversation

@skerkewitz-agent

Copy link
Copy Markdown
Contributor

What

The SNES now plays music. A track in [sound] music that is a zip exported by Krautrecker (Export for SNES) plays on Krautrecker's driver, and the sound effects play through the same driver on the voices the song leaves them. This is step 3 of the plan: music on the SNES, with effects, as you chose.

A game calls sdevkit_music_init(&music_bank) in game_init, before or after sdevkit_sfx_init. You chose this API: the library can't name the generated data itself, because dampf refuses a symbol nobody defines. The decision record: Krautrecker's songs reach the SNES through sdevkit_music_init and the driver's loader (ADR-6G3HKN6P). It amends the records on the driver and on the effects.

music = [{ file = "assets/music/level.zip", name = "level" }, "assets/music/title.ogg"]

How

Build (cli/.../sfx/Krautrecker.java, Build.java):

  • reads the zip's manifest.json and the four .bin files;
  • refuses port interfaces other than 1, a manifest with an error, and songs whose driver.bin differ;
  • writes music_bank into build/gen/music.c, with a song's parts split at 32 KB.

Effects: they go after every song:

  • their samples at the highest free address of the game's songs;
  • their directory entries after the highest first free entry.

Sfx checks that they fit below every echo buffer and that every song reserves enough voices. In a game with a zip, the effects' bank points at music_bank, so either init can come first.

Library (lib/sound/sfx_voices.h, shared by the SNES and Rachael):

  • the driver goes up through the boot code;
  • a song is loaded with one LOAD from $0800 to the end of the effects' samples, gaps as zeros, then PLAY;
  • the same song again: only PLAY. A file's track sends STOP on the SNES.
  • The effects' SRCN is offset by their first directory entry.
  • The SNES side overlaps the LOAD exchanges: the CPU gathers the next three bytes while the driver stores the last ones.

Desktop and web: a zip plays nothing (ADR-6G3E49C5). Rachael does what the driver does for the effects (unmute, and DIR at PLAY), so the effects play there as before.

Sound check: new lines play, stop and load A N H, decoded the same way in dampf.emu.SoundLog, Mesen's Lua script and Rachael's log.

Check project: dev/music_check plays Krautrecker 0.4.0's demo Sprite Run, which I exported from a clean copy of the 0.4.0 tag with reservedSfxVoices 2. The zip keeps only the manifest and the .bin files, 16 KB. The check also plays two of steinschlag's effects, a stop, the same song again, and a WAV track (which stops the song on the SNES). It loads the song twice: music first, effects after.

Verified

  • ./gradlew build: 1289 tests, 0 failed. New tests:
    • KrautreckerTest, 7 tests: reading, refusals, layout, generated C, room and voice checks.
    • MusicCheckTest, 2 tests: the same sound.txt in the emulator, desktop and web, with the expected loads, plays and stops; Mesen when present. It also reads the DSP's registers in dampf's emulator after tick 31: the song plays (DIR is its directory page), and the gem's key-on reached voice 6 with directory entry $0D, which points at $66F8, the song's first free byte, where the gem's BRR lies.
  • dev/compare_example.sh dev/music_check: Mesen and the desktop give the same 118 sound lines, the same checksums for 350 ticks, and identical screenshots.
  • Also tried with your Sprite Run zip (no reserved voices, music only), in a scratch project: emulator, desktop and Mesen agree.
  • Benchmark (dev/bench_examples.sh against develop): geomean 1.000. catchme, schach and steinschlag change by 1 to 2 cycles a tick. The examples without effects or music are unchanged.

What follows

  • Generated code: new library functions; their worst cases are unbounded, as sdevkit_sfx_init's already were, because they wait for the sound CPU. lib/README.md's cost table, cost-baseline.txt and backend-output.txt are updated. DebugCallsTest no longer lists the music calls: they aren't empty on the SNES any more.
  • Loading speed: Krautrecker's loader moves about 246 bytes a frame with the overlap (176 without it), less than the boot code's 350. Sprite Run's 24 KB take about 2 seconds. The cause is in the driver's loop. I added it as section 9 to Krautrecker's docs/superdevkit-findings.md, uncommitted there, for you to forward.
  • C guide and its rules: unchanged.
  • The game: nothing changes until it lists a zip. No entry in docs/game-findings.md.
  • Documents:
    • docs/sound.md (Music, The checks, Not built), docs/testing.md and docs/history.md;
    • docs/roadmap.md: the "not scheduled" music item is gone;
    • lib/README.md, lib/SNES_HARDWARE.md and lib/snes/README.md;
    • NOTICE: a Krautrecker section. A ROM with a song contains Krautrecker's MIT driver.
  • Book pages stale at the release: guide/graphics-and-sound, guide/the-desktop, guide/snes-hardware, reference/console-api, reference/superdevkit, reference/limits and reference/runtime-and-memory.

Open

  • The effects are silent in a game with songs until a song plays: the driver sets DIR only at PLAY (driver release 1's gap).
  • For the example (step 4), your Sprite Run zip needs re-exporting with reservedSfxVoices ≥ 1 if the example plays effects. Your zip's song differs from the 0.4.0 demo (9837 bytes against 9549). The driver is the same.

🤖 Generated with Claude Code

…ough it (ADR-6G3HKN6P)

A track in [sound] music that is a zip exported by Krautrecker plays on
the SNES. The build reads the zip (port interface 1, the manifest's
errors, one driver for all songs) and writes music_bank into
build/gen/music.c; the effects go after every song's samples and entries,
and Sfx checks the room and the voices the songs leave.

sdevkit_music_init uploads Krautrecker's driver through the boot code;
sdevkit_music_play loads a song with one LOAD (three bytes an exchange,
overlapped with the driver's stores) and sends PLAY; sdevkit_music_stop
sends STOP. The effects' bank points at music_bank, so either init can
come first. The desktop plays nothing for a zip.

sound.txt gets play, stop and load lines, decoded the same way in
dampf.emu.SoundLog, Mesen's script and Rachael. dev/music_check and
MusicCheckTest check it in dampf's emulator, on the desktop, the web
and against Mesen; KrautreckerTest checks the build's reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@skerkewitz-agent
skerkewitz-agent Bot merged commit 46032d5 into develop Nov 30, 2026
4 checks passed
@skerkewitz-agent
skerkewitz-agent Bot deleted the snes-music branch November 30, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant