Music maker

music-maker#

A small music maker. Pick a song and press play.

tempo 120 bpm

songs

The Legend of Zelda: Ocarina of Time

The Legend of Zelda: Majora's Mask

The Legend of Zelda: The Wind Waker

The Legend of Zelda: Twilight Princess

The Legend of Zelda: Skyward Sword

The Legend of Zelda: Breath of the Wild

Pokémon Diamond and Pearl

Pokémon HeartGold and SoulSilver

Every channel has its own grid, one under another. Rows are pitches, high at the top. Notes in the same column on one channel sound together, as a chord.

notes

how-it-works#

Every song is written as text, one line per channel, in a tiny music language. The grid and the text are two views of one song.

The language is a small subset of MML, the format Japanese home computers used for music. One line per channel, starting with its letter, A to H. Notes are the letters a to g. A # or + after a note sharpens it, a - flattens it. A number sets the length: 1 is a whole note, 2 a half, 4 a quarter, 8 an eighth, 16 a sixteenth, and a dot adds half again. r rests. ^ ties the next length onto the note before it.

Notes that sound together are written as a chord: braces around the notes, with one length after the closing brace, like {ceg}4. Inside the braces, < and > step the octave for the notes after them in that chord only. A few letters steer the channel instead of playing: o sets the octave and < > step it down or up, l sets the default length for notes with no number of their own, v is volume from 0 to 15, @ picks an instrument from the fixed table below, and t sets the tempo. [ ] plays what is inside again, twice unless a number follows the closing bracket. L marks where the loop restarts, so anything before it plays once, as an intro. A semicolon starts a comment.

@0 pulse 12.5%   @4 triangle    @8  strings
@1 pulse 25%     @5 noise       @9  cello
@2 pulse 50%     @6 ocarina     @10 harp
@3 pulse 75%     @7 flute       @11 piano
@12 synth lead   @13 soft square

Everything lands on a sixteenth-note grid. That grid is what lets the text and the grid view stay two views of one song, one line per channel with every length spelled out.

A @6 t190 o4 d f >d

That line is the ocarina's opening of Song of Storms: instrument 6, 190 beats a minute, octave 4, then D, F, and the D above.

Nothing here is scheduled live. Pressing play renders the whole loop into one stretch of audio, start to finish, and then loops that recording. No timer places notes while the music plays, so a busy page cannot make it stutter. A tempo change renders again and swaps in at the next loop boundary.

The rendering path, in three small pictures:

song text, one line per channel
            |
            v
notes on a sixteenth-note grid
            |
            v
each note given a voice
            |
            v
the whole loop rendered ahead
of time into one recording
            |
            v
playback loops the recording

The text is parsed once into notes on the step grid. Each note is given a voice, and an offline audio context renders the whole loop into a single recording before anything plays. Playback loops that recording between its loop points, so an intro before the L plays once and the rest repeats.

A  piano    ----\
B  strings  -----\
C  bass     ------>  one recording
D  drums    -----/
E  harp     ----/
F to H      ----/

Every channel renders into the same recording. There is one recording per song, not one per channel, and nothing is mixed live while the music plays.

a tempo change
            |
            v
the new loop renders
in the background
            |
            v
at the next loop boundary
the new recording takes over

Changing the tempo while a song plays does not stop it. The old recording plays on to the boundary, then stops as the new one starts, already inside its loop. While the song is stopped, a tempo change waits for the next press of play.

Two families of voices do the playing. The chip voices are the old hardware channels: pulse waves at their real duty cycles, the triangle channel with its stepped 4-bit wave, noise from a shift register, and envelopes that move in steps instead of fading. Two chip voices are newer and break the hardware rule on purpose: the synth lead and the soft square, whose harmonics roll off and pass through a filter, closer to the sampled synths the DS played than to a raw square wave. The sampled voices cover the parts the consoles played from recordings, the N64 and the DS both being sample machines: a few recorded notes per instrument, pitch-shifted to reach the rest, with a small generated room echo for the space the consoles added. The samples are short excerpts from VSCO-2 CE, a community orchestra library released CC0. All sixteen files together come to about 308 KB.

The grid itself is Ben's idea, from a Swift Playground he wrote as a student: a grid of blocks you toggle while the music plays. This is that idea taught to read text.