Encoding sheet music for HandyMusic
.mus blob. This page is the bridge
between the two — how a note's pitch becomes a
nXo token, how its rhythm becomes a count of 60 Hz
ticks, and how the parallel voices of a score stay aligned. It assumes you have
read the note-format and track-command tables in the
hmcc guide; here they are put to work on real bars of
music. For the driver that replays the result and the C entry points that start
and stop it, see the HandyMusic engine page.1. The mental model: ticks, not tempo
Standard notation is written in tempo-relative time: a quarter note is one beat, and how long a beat lasts depends on a metronome mark you set once at the top of the piece. HandyMusic has no such concept. The driver updates once per video frame — a fixed 60 Hz tick — and every duration in a track is simply a count of those ticks. Sixty ticks is one second; there is no beats-per-minute field anywhere in the format.
So transcribing a score is a two-part translation done note by note. The
pitch of each note becomes a three-character nXo token
(section 2). The rhythmic value of each note
— quarter, eighth, dotted half — becomes a tick count you compute once
you have chosen how many ticks a beat is worth (section 3).
Everything else on the page — the barlines, the time signature, the beaming
— is bookkeeping that helps you keep the running tick total correct; it never
appears in the script.
The upside of a raw tick grid is that it is exact and never drifts. The cost is that you are the sequencer: nothing enforces that a bar adds up, and nothing keeps two voices in step except the tick counts you write. The habit that makes this manageable — annotating each bar with its running tick total — is the single most useful thing on this page, and section 5 is built around it.
2. Pitch: staff notes to nXo
A note token is three characters, nXo: the note letter
(a–g), an accidental character, and a single
octave digit. The accidental is . for a natural, s for
a sharp and b for a flat — so c.4 is middle C,
fs5 is F♯ in the fifth octave and eb3 is
E♭ in the third. hmcc turns the token into a frequency relative to
A4 = 440 Hz and folds it through the Lynx divider
table; you never write a frequency yourself.
2.1. Octaves and middle C
The octave digit follows scientific pitch notation, the same numbering
most notation software prints: the octave number increments at C, and
middle C is c.4. Concert A above it — the note
your tuner calls A440 — is a.4. Reading a treble staff, the
notes map to tokens like this:
nXo tokens. Middle C
(c.4) sits on its ledger line below the staff; the octave digit
rolls over to 5 at the next C above.Bass-clef material works the same way with lower octave digits: the C two
octaves below middle C is c.2, and a bass line often lives in
octaves 1–3. If a note looks an octave off when you play it back, the
octave digit is almost always the culprit — check it against
c.4 = middle C first.
2.2. Accidentals and tuning
Write the accidental the score shows, choosing whichever enharmonic spelling
matches the key — as3 and bb3 are the same pitch,
so use the one the sheet music prints. A key signature is not stored anywhere:
apply it yourself as you read, so a piece in D major means typing fs
and cs every time those notes occur (or their natural forms where the
music cancels them).
One subtlety carries over from the hmcc guide:
notes are only in tune if the instrument's tuning period matches its
waveform. Each instrument in your bank sets its own tuning; as long
as you picked (or copied) a correctly tuned instrument, a.4 really is
440 Hz. If a whole voice sounds sharp or flat by a constant amount, the fix is
in the instrument's tuning, not in the notes.
3. Rhythm: note values to ticks
3.1. Choose a ticks-per-beat resolution
Because durations are whole ticks, you fix the rhythm grid by deciding how many ticks a beat (a quarter note, in common time) is worth. Call that number the resolution, B. Every other note value is a simple fraction or multiple of it, and the effective tempo falls out of the arithmetic:
tempo (BPM) = 3600 ÷ B B = 3600 ÷ tempo
Pick B so the shortest note you need stays a whole number of ticks.
A resolution of 24 gives clean thirds and halves (a triplet-eighth is
8 ticks, a sixteenth is 6) at 150 BPM; 30 is a natural
120 BPM if you only need down to sixteenths; 48 buys you a slow,
finely divisible 75 BPM. When a division does not land on an integer,
round it and let the annotated bar total (section 5)
absorb the rounding so the bar still closes exactly.
3.2. Quick conversion table
With the beat set to B ticks, the common note values are:
| Note value | In beats | Ticks | B=24 (150 BPM) | B=30 (120 BPM) | B=48 (75 BPM) |
|---|---|---|---|---|---|
| whole | 4 | 4×B | 96 | 120 | 192 |
| half | 2 | 2×B | 48 | 60 | 96 |
| dotted quarter | 1½ | 3×B÷2 | 36 | 45 | 72 |
| quarter (beat) | 1 | B | 24 | 30 | 48 |
| eighth | ½ | B÷2 | 12 | 15 | 24 |
| triplet eighth | ⅓ | B÷3 | 8 | 10 | 16 |
| sixteenth | ¼ | B÷4 | 6 | 7½ → 8 | 12 |
A dot adds half the note's value, so a dotted note is 1.5×
its plain length; a tie just adds the two tick counts together into one longer
number.
3.3. Notes, rests and holds
A note in a track is the pitch token followed by its tick count —
e.4 30 plays E4 for thirty ticks, then the track moves to the
next command. Write one note or command per line: hmcc reads a
single event from each line and treats anything left over as an error, so a bar is
a short column of note lines, not a row. Three more things are worth knowing when
you turn note values into these counts:
- A new note token retriggers the instrument, so two notes at the same
pitch written back to back (
g.4 15 g.4 15) sound as two distinct attacks — you do not need a gap between them just to hear both. - For an actual silence, use
rest n; it releases the current note and waits n ticks. A rest in the score is arestin the script, with the same tick count any note of that value would get. - To hold one note longer than a single command — a note tied across a
barline, say — add
wait nafter it, or simply write the summed tick count on the note. For a detached, staccato feel, split a beat into a short note plus arestthat fills the remainder (c.4 8 rest 22for a clipped quarter at B=30).
4. Worked example: a melody
Take the opening phrase of Beethoven's “Ode to Joy” — four bars
of common time, entirely in C major so there are no accidentals to track. We will
score it at B = 30 ticks per beat (120 BPM), so each plain
quarter is 30 and every bar must total 120.
4.1. Reading the staff
The first two bars read E E F G, G F E D — all quarter notes. Under each note head is the token and tick count it becomes:
30 ticks and
each bar sums to 120 — the number written in the trailing
comment of the script below.4.2. The finished track block
Bars 3 and 4 continue C C D E, E· D D: the third bar is
four more quarters, and the last bar is a dotted quarter (45), an
eighth (15) and a half note (60) — still
120 ticks. Written as an hmcc track block — one note per line, with
each bar's running total in a comment on its last note so a miscount is obvious at
a glance:
OdeToJoy 150
{
pan $ff
using MellowTriangle
e.4 30
e.4 30
f.4 30
g.4 30 ; bar 1 — 120
g.4 30
f.4 30
e.4 30
d.4 30 ; bar 2 — 120
c.4 30
c.4 30
d.4 30
e.4 30 ; bar 3 — 120
e.4 45
d.4 15
d.4 60 ; bar 4 — 120
end
}
The 150 after the block name is the track's priority, marking it as
the main block — the channel's entry point. using
MellowTriangle picks the instrument (any name defined in the instrument
script; MellowTriangle is one of the demo bank's). pan
$ff opens both stereo sides. To make the tune loop instead of stopping,
wrap the four bars in loop -1 … endloop
in place of the final end.
4.3. Compiling it
Put the block in a track file (say ode.txt) and the instrument it
names in an instrument file (inst.txt), then compile the pair for the
address the cartridge will load the blob at — $b000 in the
default config:
hmcc $b000 ode.mus inst.txt ode.txt
That single-track invocation writes ode.mus, which you
.incbin at the reserved region and hand to the driver exactly as the
hmcc build flow and the
HandyMusic engine page describe. Additional voices
are just more track files appended to the same command line, one per Mikey
channel — which is the subject of the next section.
5. Worked example: keeping voices aligned
A full score is up to four parallel tracks — one per Mikey audio channel, in the order they are passed to hmcc. The driver advances each track by its own tick counts completely independently; nothing synchronises them but the totals you write. If bar 3 of the bass adds up to 118 ticks while the melody's bar 3 adds up to 120, the two voices are two ticks out of phase from there to the end of the piece, and the error accumulates every bar. This is the one failure mode worth designing against, and the defence is the running per-bar total.
Here is a two-bar loop scored across three voices — melody, bass and a drum pattern — all at B = 30, so every voice must close each bar on 120 ticks. The melody plays quarters, the bass plays half notes, and the drums alternate kick and snare on each beat; different note values, identical bar totals:
; ---- track 1: melody (channel 0) ----
Lead 150
{
pan $ff
using MellowTriangle
c.5 30
e.5 30
g.5 30
e.5 30 ; bar 1 — 120
d.5 30
f.5 30
a.5 30
f.5 30 ; bar 2 — 120
end
}
; ---- track 2: bass (channel 1) ----
Bass 150
{
pan $ff
using Electar
c.3 60
g.2 60 ; bar 1 — 120
d.3 60
a.2 60 ; bar 2 — 120
end
}
; ---- track 3: drums (channel 2) ----
Drums 150
{
pan $ff
using Kick
sfx 30 ; beat 1
using SnareShort
sfx 30 ; beat 2
using Kick
sfx 30 ; beat 3
using SnareShort
sfx 30 ; beat 4 — bar 1, 120
using Kick
sfx 30
using SnareShort
sfx 30
using Kick
sfx 30
using SnareShort
sfx 30 ; bar 2 — 120
end
}
Percussion is played by firing an instrument as a hit with
sfx ticks after selecting it with using,
so the drum track counts ticks exactly like the pitched ones — four
sfx 30 hits close the bar on 120. Compile all three behind one output,
in channel order:
hmcc $b000 song.mus inst.txt lead.txt bass.txt drums.txt
If you ever hear the voices slip against each other as the loop repeats, do not reach for the note values — add up each bar in each track and find the one whose comment lies.
6. Tips and pitfalls
- Annotate every bar with its running total. It is the only practical way to catch a dropped or doubled tick, and it makes voice-alignment bugs (section 5) visible without playing anything.
- Factor repeated material into blocks. A phrase that recurs
— a chorus, a drum pattern — can live in its own block and be reused
with
call/return, exactly as the bundled demo tracks do. Keep the tick total of a called block fixed so callers can budget around it. - Loop the whole track for BGM. A background tune wraps its body
in
loop -1…endloopso it repeats forever; a jingle ends withend. - Pick instruments before you transcribe. Timbre lives entirely in the instrument bank (hmcc guide §3); the track only references instruments by name. Copying and lightly editing an existing bank is far quicker than authoring envelopes from scratch.
- Mind rounding. When a tempo makes a short note value non-integer (the sixteenth at B=30 is 7½ ticks), round consistently and let one note per bar absorb the remainder so the bar total stays exact.
- Verify by ear, not just by build. A track that compiles and
boots can still be transposed an octave, a voice out of phase, or a hair out of
tune. Play the
.muson hardware or an emulator and listen before calling it done.
7. See also
The hmcc guide is the reference for the full script
grammar this page draws on — the instrument and
SFX language, the track commands, and the
output layout and base address. The
HandyMusic engine page covers the runtime that
replays the compiled blob and the C API that starts and stops it, and the
Sound page sets HandyMusic against the lighter
snd engine used elsewhere in the SDK. The format and driver are
specified in full in design/LYNX_HANDYMUSIC_DESIGN.md.