Sheetmusic4J 0.0.5: LilyPond Import and Export

Sheetmusic4J 0.0.5: LilyPond Import and Export

Sheetmusic4J is a Java(FX) library to render and interact with sheet music. After MusicXML, MIDI, ABC and Guitar Pro, version 0.0.5 adds another format: LilyPond.

Thanks to Dani_E2e on Reddit and Jurgen Lambrecht for suggesting LilyPond support!

What Is LilyPond?

LilyPond is the free, text-based engraving program that many musicians and publishers use for beautiful printed scores. You write the music as plain text in a .ly file, and LilyPond compiles it into a PDF. A lot of classical repertoire, from Bach chorales to full orchestral scores, is available as .ly source, for example at the Mutopia Project.

\version "2.24.0"
\header {
  title = "C Major Scale"
}

\relative c' {
  \clef treble
  \key c \major
  \time 4/4
  c4 d e f | g a b c | c b a g | f e d c |
}

Import and Export

With the ScoreFile facade, a .ly file can be loaded and rendered natively in JavaFX, without running LilyPond. Any score, whatever its original format, can also be saved as LilyPond to get a high-quality PDF out of it:

Score score = ScoreFile.load(Path.of("bach-bwv610.ly"));

// ...and write any score back out as LilyPond
ScoreFile.save(score, Path.of("bach-copy.ly"));

LilyPond’s input is closer to a programming language than to a data format, so the reader works like LilyPond itself: a parser resolves note names, default durations and variables, an interpreter runs the music through time into staves, voices, lyrics and chord names, and an assembler cuts the result into measures and derives what LilyPond leaves implicit, like ties across bar lines, accidentals and beams.

Supported are, among others, \relative, \fixed and \transpose, chords, ties, slurs, tuplets, grace notes, repeats with alternatives, multiple staves and voices, piano staves, lyrics, chord names, dynamics, tempo and rehearsal marks. Engraving tweaks like \override and Scheme code are skipped, because Sheetmusic4J does its own layout. The full list is on the LilyPond page.

The test fixtures include real-world snippets from the LilyPond documentation (a Bach organ chorale, an orchestral score, tablature, a Stockhausen piece…), which is a nice way to discover what still needs work.

Engraving Improvements

Real LilyPond scores exercised the engraver in new ways, which led to improvements that benefit every format:

  • Tuplets now go on the stem side like beams do, or where the score says (placement is also read from MusicXML), clear the noteheads and beams, and support nested tuplets.
  • Octave clefs, such as the guitar’s treble clef with an 8 below, and the percussion clef.
  • Identical layouts across CPU architectures, by avoiding Math.pow differences in note spacing.

Try It

Read the full status update on the Sheetmusic4J website, check the code examples to get started, or look at the difference on GitHub.

If a LilyPond file doesn’t load or render correctly, please let me know on GitHub, ideally with the file attached.