MAGIC BYTES
A four-track synthwave EP for the NES sound chip, released as one 53 KB file that is a NES cartridge, a PDF and a shell script at the same time.
// One download of the file, opened three ways. NES: arrows pick, X or Enter plays, Z stops. PDF: the liner notes, drawn by pdf.js. sh: a real Linux boots in an emulator (8 MB, about ten seconds) and runs the file as a script.
An EP that ships as a single file, magic-bytes.nes. What it is depends on what you open it with:
| Open it with | and it is |
|---|---|
| a NES emulator or a flash cart | the EP itself: a track-select screen, with the songs played by the 2A03 |
| a PDF reader | the sleeve: cover art, liner notes, and a page on how the file works |
sh |
the track list and an ASCII logo, with an offer to render WAVs if it finds nsf2wav, viper or an ffmpeg built with libgme |
The player above does all three in the browser from one download. It fetches the file once and hands the same bytes to a NES emulator, a PDF renderer and an emulated Linux machine. Nothing is converted on the way.
The music
Four tracks, seven and a half minutes, written for the five channels of the NES: two pulse waves, a triangle, noise, and one-bit DPCM samples for the drums.
| # | Title | Time | BPM | Key |
|---|---|---|---|---|
| 1 | Magic Bytes | 1:45 | 104 | A minor |
| 2 | Mapper Zero Highway | 1:41 | 128 | E minor |
| 3 | Neon Xref | 1:59 | 92 | D minor |
| 4 | Trailing Data | 2:05 | 80 | F# minor |
Every note was typed, not played. The score is a Python file that writes viper’s .vip tracker format, one phrase per bar. viper compiles that to bytecode for a 1.7 KB 6502 sound driver and links the four songs into one NSF. The cartridge runs that same driver and those same bytes, and the MP3s on this page are viper’s renders of the NSF, with one gain applied to the whole EP and nothing else.
How one file is three
000000 4E 45 53 1A 02 01 0A 00 ... iNES header
000010 shell script (4 KB) = PRG-ROM $8000-$8FFF, never executed
contains "%PDF-1.4" and opens PDF object 1, a stream
001010 sound driver, songs, samples = PRG-ROM $9000..
006010 player code, vectors = PRG-ROM $E000-$FFFF
008010 CHR-ROM (8 KB) eight unused tiles hold the NSF header
00A010 endstream; the real PDF objects; two xref tables; %%EOF
NES. It is a plain NROM cartridge. A loader reads the 16-byte header, 32 KB of program and 8 KB of graphics, and ignores the 12 KB that follow.
Shell. The header is the script’s first line. Header byte 6, the flags, is $0A, which is a newline. That ends line 1 before the header’s zero bytes, and those zero bytes are what would make bash call the file binary and refuse it. A console reads $0A as mapper 0 with the battery and four-screen bits set, and neither matters to this board. The script proper starts at byte 16, which is also the first byte of program ROM, and it ends with exit long before any machine code.
The shell does print one complaint, NES: not found, for that first line. It can’t be prevented, so the script’s first real command erases it from the terminal.
PDF. Readers accept the %PDF header anywhere in the first 1,024 bytes, so it sits inside a here-document that the shell throws away. The same here-document opens a PDF stream object whose content is the entire cartridge. Some readers count a PDF’s internal offsets from byte 0 and others from the header, so the file carries two cross-reference tables, placed so that each kind of reader lands on the one that is right for it.
NSF. Nearly a fourth format. Program ROM is the album’s NSF image byte for byte, and the NSF’s 128-byte header is parked in eight unused graphics tiles. Two dd calls in the script put them back together, which is how sh magic-bytes.nes --nsf works and what the WAV rendering is built on.
The shell tab is a real machine
The sh tab doesn’t imitate a terminal. It boots a 32-bit Linux kernel and BusyBox in v86, with the file copied into the root filesystem, and runs sh magic-bytes.nes. The prompt afterwards is live, so hexdump -C magic-bytes.nes | head and sh magic-bytes.nes --nsf both work. The emulated machine has no audio renderer installed, so there the script only prints.
Using the file elsewhere
sh magic-bytes.nes # track list; asks before rendering anything
sh magic-bytes.nes --render [DIR] # WAVs, no questions
sh magic-bytes.nes --nsf [FILE] # just the NSF, for any NSF player
cp magic-bytes.nes notes.pdf # for PDF readers that go by the file name
It runs in Mesen and jsnes, under bash, dash and BusyBox sh, and opens in Ghostscript, PDFium (Chrome), MuPDF, qpdf, pdf.js and pdfminer. Three known edges: zsh won’t execute it directly (sh magic-bytes.nes works everywhere), a strict PDF parser that insists on %PDF at byte 0 will reject it, and because of the battery bit an emulator may write a save file next to it. It hasn’t been tried on real hardware yet.
