Webtools - Music Notation DSL
Added another tool to my set of web tools: a music notation editor which produces music score from a DSL.
I needed some organ music prepared for tomorrow. Typically this comes from an accompaniment book, which I generally don’t follow apart from the melody line. The music is printed in such a way that I’d had to turn the page, which is always a tad fraught. Times like this I probably would write the music out by hand in a music exercise book. But for 4 pieces that would’ve taken too long. Plus I didn’t have a pencil (not going to write it out in biro).
But I did have a coding agent, and the idea for a DSL. So I vibe-coded a small tool which allowed me to transcribe the music using text-based domain-specific language. And while Claude built the app, I did the language design.
The language is dead simple: just a bunch of space-separated “bar elements”, like notes (a..g), rests (.), and blanks (_). Notes can be prefixed by +’s and -’s to bump them up or down an octave. The duration is set by a numerical suffix: 4 for a crochet, 2 for a minim, 8 for a quaver, etc. The duration can apply to a group of notes using parentheses: 8(a b c d) is a group of four quavers. Furthermore, when used with quavers, they are connected by a beam.
Bars are separated using the pipe character (|). Two pipes (||) produce a double bar-line. Meta elements begin with % and can be used to set a title, specify the key signature (%ksig) or time signature (%tsig). There can be labelled sections to indicate the chorus or verse using %sect, and the %style element can be used to set the staff opacity. A new line produces a new staff, and the elements of each one are justified to fit across the page. There’s no restriction on how many beats can go into a bar. The app doesn’t care, it will simply display what is written.
Here’s an example:
%title Example Score
%style staff-opacity 0.5
%ksig E-min
%tsig 2/2
%sect Chorus
g2 g4. e8 | g2 .4 g | 4(f e) f8 .8 8(f f) | f2. . |
g f 8(e8. d16) | b2 . d | e f | e2. . ||
%sect Verse 1
e || d e g 8(+a +b) | +a2 +d +d |
_ +b2 +a g | +b2 .2 ||
The score is generated in Go compiled to WASM, and rendered to the frontend via SVG. It uses the canvas package, which can also produce PDFs that can be printed.
Now, this is by no means a fully-featured notation editor. Only the treble clef is supported (and yeah, it is a little wonky), and you cannot render chords, ties or slurs, sharps or flats, or anything else you’d normally see in printed music. But it fulfilled the need I had at the time. And yeah, I suppose I could ask the coding agent to add these features. But not right now. I rather keep the DSL relatively simple and free from locked-in decisions before I go down that path.