Hello, Lilypond

Explorations in music engraving via org-mode

Somewhere there’s Music

If I can express the speed of light, what about sound? My next quest in this adventure is leveraging org-babel to generate snippets of music notation. Like, how difficult can that be?

On my way in, I know very little. I might know a great deal about music notations, sure, but all I know of Lilypond is two decades1 old and even then I was more into Musescore. What I do know of Lilypond is it is emaculately academic about notation, and that sounds like a nice thing to have.

From what I (dimly) recall, it retains a latex-like feel to the notation. It can’t be any worse than CSound, which is a topic in itself. For starters, though, is it possible to not only render some snip of score, but to have the process render and ready a MIDI? That’s the proof of concept du jour.

Adventures in ob-lilypond

I notice the ob-lilypond module doesn’t appear on MELPA, but perhaps that’s because it is core. I ran the following elisp as suggested on their GitHub2:

(org-babel-do-load-languages
'org-babel-load-languages
'((ruby . t)
  (C . t)
  (emacs-lisp . t)
;;  (sh t)
  (dot t)
  (clojure t)
  (ditaa t)
  (lilypond t)))

That should probably go into my Emacs init.el if it wasn’t already the default. Just to clarify my context, I’m running UbuntuStudio 23.10, so KDE/Qt based and should work fine with Rosegarden (although I haven’t visited that app in decades). So I’ll have that and LilyPond installed under 23.10.

Given all this, the compile to PNG and upload of the media is automatic. I’m just hitting publish, then giving permission to use the local lilypond:

mixolydian.png

That’s in need of some serious cropping, but otherwise presents a nice face and easy to do. There’s a note tagged to the end of the org file that needs to be deleted to effect any future updates to this image.

It is, however, curious and odd none of the offered examples offer any hints on cropping the image. The lilypond executable command can be set in the customize-group for org-babel, but I’m not getting the crop option. Also suggested was adding #(ly:set-option 'crop #t) but again, added to the lilypond block has no effect on the output, although I do see a mixolydian.cropped.png created by the process, so the puzzle is really how to get that inserted instead of the full-page version. In the HTML example org source I do notice they append a #+results: line followed by the local filename, although they don’t put cropped in there, and putting it in did not change the published page.

A cursory search suggests Lilypond folks typically use ImageMagick to post-process a crop, but that seems clumsy within this Org2Blog process and none of the examples given suggest anything special to be done! Also, speaking of clumsy, every time the image is regenerated, we create and upload an additional (numbered) PNG which is then added to the Activitypub export (to the max of 3), so here again, the ActivityPub plug makes no allowances for image corrections, even on drafts.

The lilypond src block is also ignoring my caption tag, whereas I get a nice Fig. 1 when I normally include an image.

Clearly not out of the woods on this just yet.

The ob-lilypond example had a preamble to define version and paper that should be part of a #+SETUPFILE somewhere.

 

#+LaTeX_HEADER: \usepackage[paperheight=4cm,paperwidth=17cm,textwidth=15cm]{geometry}
#+name: version
#+begin_src emacs-lisp :exports none :results silent :eval yes
"\\version \"2.12.3\""
#+end_src
#+name: paper
#+begin_src emacs-lisp :exports none :results silent :eval yes
  "\\paper {
	indent = 0\\mm
	line-width = 170\\mm
    oddFooterMarkup=##f
    oddHeaderMarkup=##f
    bookTitleMarkup=##f
    scoreTitleMarkup=##f
  }"
#+end_src

On posting to WordPress, I’m initially asked permission to eval every block on every print; this is known behaviour and since the given fix didn’t work for me, I’ve set a global accept on babel block evaluations. May the good lord have mercy on my heathen soul.

What about Rosegarden

This is UbuntuStudio so I was surprised to find it not already install, but easily fixed via apt. On install it recommends a soundfont, so I included that, but on first run I’m told there will be no sound without Qsynth. That gets added, run, presumably background? Rosegarden now has sound but it is very s-l-o-w and choppy when configured for alsa and pipewire, or quasi configured, maybe. Pipewire for a jackd app? That will be a puzzle for another day.

As it turns out, I may be fine for my first while just coding in Lilypond directly. Like, how hard could that be?

Footnotes:

1

circa y2k a lot of my work was documented using \(\LaTeX\) generated via DocBook xml files, for example the Linux Kernelbook Project actually allowed me to begin an open textbook on kernel internals, the notion being to have a living document that could be professionally published by Macmillan, translated and all that, but fundamentally online, free and current. Unfortunately the kernel lords of the day were wont to participate.

2

I had to comment out the sh reference, and the failure of that run killed the Org2Blog menu. I copied the contents, quit the previous post buffer and killed this one, then started a new buffer and pasted this back in (this was before posting any drafts)

Author: garym

the reality of me is just me

One thought on “Hello, Lilypond”

Comments are closed.