Introducing Musicotron
Introducing Musicotron#

What was the first arcade game with background music? Technically, it’s Space Invaders, with its Jaws-like repeating dirge. If that’s too minimalist for you, then it’s Rally-X. They relied on sound chips (the TI SN76477 and Namco WSG, respectively) but they needed code to drive the chip, and a data table to represent the tune.
The music players in 8bitworkshop are inspired by these ancient designs. They’re pretty simple – a song is a stream of notes and wait values, and every video frame the player checks the stream and pokes the sound chip if neccessary.
The format is tiny and easy to play on a 6502 or a Z80, but to generate a music file you had to use a Python script that massaged a MIDI file into that byte stream.
I finally made the converter into an app: Musicotron, a Dithertron-style browser tool for music.
The song format#
The runtime players are deliberately simple. The song is a linear byte stream:
Byte |
Meaning |
|---|---|
|
note index ( |
|
wait |
|
back-reference: |
|
end of song |
Note that there is no “note-off” command – all notes have a short decay, so it’s best for piano-type music. Most players schedule them to channels in a round-robin fashion.
Note indices are limited to 64 entries in practice.
Those note tables are generated by mknotes*.py, which uses
notehz = a440 * 2 ^ ((note - 49) / 12)
so index 49 is A4 (MIDI 69) and index 0 is MIDI 20. That gives the usual
start = 20, num = 64 window (MIDI 20-83). The C64 SID table starts lower,
at C0 (MIDI 12). Anything outside the window is dropped and reported.
Compressing with back-references#
Most songs repeat themselves, so the music players in the latest 8bitworkshop IDE support compression. The encoder replaces a repeated run with
0xfe <offset> <length>
which means “rewind offset bytes and replay length of them.” Runs are
always literal, never another reference, so a decoder needs no
decompression buffer – just a read pointer it can move backwards. On the
8-bit players it’s a dozen lines of C.
This is a modest improvement, variable from about 5%-30%.
Voice allocation#
Three or four voices is not a lot – even the first General MIDI devices had 24 voice polyphony. So the converter has to use some heuristics to triage notes.
The converter collects all the note-ons that land on the same frame, then spends the voice budget in order. Each dropped note gets a reason, and the tool shows the breakdown, e.g.:
Maple Leaf Rag, NES: 1402 note-ons
emitted 1158
out of range 52 (below MIDI 20 or above MIDI 83)
too many voices 192 (four notes on one frame, three voices)
Each of the MIDI channels in the score can be tweaked:
Solo and a mute checkbox.
Priority. Drag channels up or down, higher wins over lower.
Cap, a per-channel voice limit, so busy chorded instruments don’t take over the melody.
A global Voices rule for tie-breaks: first in stream (the reference behavior), lowest pitch, highest pitch, or loudest.
GM percussion (channel 10) is detected and left unchecked by default, so the drum track isn’t encoded as out-of-range pitched notes. Supporting drums/sound effects is an idea for a future player.
Platforms#
The targets mirror the players already in 8bitworkshop:
Id |
Chip |
Voices |
Frame rate |
|---|---|---|---|
|
TIA |
2 |
60 |
|
POKEY |
4 |
60 |
|
NES APU |
3 |
60.0988 |
|
Game Boy APU |
3 |
59.7275 |
|
SID |
3 |
50 |
|
SN76489 |
3 |
59.92 |
|
SN76489 |
3 |
59.92 |
|
AY-3-8910 |
3 |
50 |
|
AY-3-8913 |
3 |
60.0016 |
Most of these chips work by dividing a clock to drive a pulse generator. The divisor is an integer with a limited range, so the frequencies you can drive are limited, and they lose resolution in the upper frequencies.
We build the note-to-frequency table to minimize the error in frequencies, but you’ll notice that each chip has a recognizable detuned quality. Especially TIA and POKEY – the latter can be configured with 2 high-resolution voices, but we have it set up for 4 low-resolution voices. (There’s a duty-cycle thing which improves resolution, but you can’t hear it in the web app.)
How this is different from vgm2vgr3#
I’ve also been working on vgm2vgr3 which compresses and plays back the actual VGM register streams. vgm2vgr3 starts from a VGM file – a precise log of every write to the sound chip’s registers, usually captured from an emulator or exported by a tracker. Its compression scheme works very well, but the decoder routine is complex and takes up way more CPU cycles.
Try it#
Web app: 8bitworkshop.com/musicotron
Python reference:
8bit-tools/sound/midi2song.pyLatest 8bitworkshop IDE: 8bitworkshop.com/latest
If you make something with it, tell us about it!