Skip to content

Music notation on computers

yuuki edited this page Sep 9, 2025 · 11 revisions

MusicXML is an interchange format for music scores. However, it is too verbose to write by hand. MuseScore is a practical application that supports it.

LilyPond notation is useful for writing simple scores. It is less interchangeable, although python-ly provides limited support for converting to MusicXML.

Below are fragments of LilyPond code for nursery rhymes. For the full code and instructions, see #How to convert LilyPond files. All files are available in my repo.

Row, Row, Row Your Boat

\relative c' {
  \time 6/8
  c4. c4. | c4 d8 e4. | e4 d8 e4 f8 | g2. |
  c8[ c8 c8] g8[ g8 g8] | e8[ e8 e8] c8[ c8 c8] | g'4 f8 e4 d8 | c2. \fine
}

Sheet music for "Row, Row, Row Your Boat"

row.webm

Twinkle, Twinkle, Little Star

How to convert LilyPond files

Prerequisites:

Example of the full LilyPond code for "Row, Row, Row Your Boat" (row.ly):

\version "2.24.4"

\score {
  <<
  \chords {
    c2. | s2.*3 |
    c2. | s2. | g2. | c2.
  }
  \relative c' {
    \time 6/8
    c4. c4. | c4 d8 e4. | e4 d8 e4 f8 | g2. | \break
    c8[ c8 c8] g8[ g8 g8] | e8[ e8 e8] c8[ c8 c8] | g'4 f8 e4 d8 | c2. \fine
  }
  >>

  \layout {
    \autoBreaksOff
    indent = #0
    line-width = #120
  }

  \midi {
    \tempo 4. = 120
  }
}

Convert LilyPond to SVG and MIDI:

lilypond --svg -dcrop -dmidi-extension=mid row.ly

Set the SVG background to white:

rsvg-convert -b white -f svg -o row.cropped.svg row.cropped.svg

Convert MIDI to WAV:

fluidsynth -ni /path/to/FluidR3_GM.sf2 row.mid -F row.wav

Convert WAV to WebM:

ffmpeg -i row.wav -c:a libopus row.webm

Convert LilyPond to MusicXML (extracting only the \relative block):

python3 -c 'import re, sys; print(re.search(r"\\relative.*?{.*?}", open(sys.argv[1]).read(), re.DOTALL).group(0))' row.ly \
| ly musicxml > row.musicxml