RogerBW's Blog

A ukulele songbook with Crystal and Typst 28 July 2026

There are already other ways to do this. Of course I had to do it the hard way.

The standard approach to a ukulele songbook is to give lyrics and chords, usually with chord diagrams. And there is a standard Typst package to do this sort of thing, conchord, but I was never able to get it to work showing ukulele chord diagrams rather than six-string guitar, or in the specific layout I wanted.

So. Two steps needed.

One, generate SVG chord diagrams. I wrote this bit in Crystal because I felt like it, it's fast, and it has JSON reading and XML writing available in the core language. My approach doesn't try to run a music theory engine the way conchord does; it just takes a list of chords. (I borrowed the ukulele chord list from GNU Lilypond. Fortunately I'd written a sanity checker; its versions of Adim and D♯dim, while technically valid in terms of notes, have the same finger at different frets on different strings…) I ignore much of the sophistication of the Lilypond notation: if the same finger is on different strings, I count it as a barre chord. Easy.

"XML writing", you say? Yup, because SVG is XML. A dedicated SVG library package might have made things easier, but I'm not doing hugely complicated graphics anyway.

So that got me a library of SVG chord diagrams, which obviously Typst can use directly as graphics. Getting it to include a row of them at the top of the sheet is easy enough; I wrote a chordchart convenience method so that I can just tell it to put in "C G Am Fmaj7" and it'll all show up in the right place.

The other fiddly thing, step two, is putting chord names over the lyrics, and I'd had an unsuccessful bash at this before. I knew I wanted to parse the "[chord] lyric" syntax that I'd used before (turns out this is actually the "Chordify" syntax), but I had an inspiration: I could go through a line chunk by chunk, identifying each as a chord or a piece of lyric text based on the square brackets, and allocating them accordingly to upper or lower cells in a grid. Since the chord always precedes the word with which it's to be played, this is relatively straightforward. That's done by feeding the line with chords and words to the overchord function.

So that was working jolly well, and then I realised I had all the tools I needed to add a pointless extra feature: a transposition engine! So now there's an optional parameter transpose to any chordchart or overchord calls, which will push the chords up or down that number of semitones, and of course the Lilypond chord listing already has a voicing for each of its chords in each key. I don't actually need this, but it seemed as though someone might find it useful.

I haven't uploaded this yet because large parts of it are still subject to change, but if anyone wants the working version give me a shout.

Add A Comment

Your Name
Your Email
Your Comment

Note that I will only approve comments that relate to the blog post itself, not ones that relate only to previous comments. This is to ensure that the blog remains outside the scope of the UK's Online Safety Act (2023).

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech bayern beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter disaster doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 essen 2024 essen 2025 existential risk falklands war fandom fanfic fantasy feminism filk film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror horrorm science fiction hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo 2025 hugo-nebula reread humour in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow openscad opera parody paul temple perl perl weekly challenge photography podcast poetry politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant review reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense talon television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 typst vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1