4
\$\begingroup\$

While learning to play the keyboard, I find it helpful to write down the score of the pieces I play, regardless of whether a score already exists.

The code is written using Frescobaldi as IDE and using LilyPond (docs) as format and engraving tool under the hood. The music is engraved to PDF and a MIDI file is generated.

Writing music in LilyPond is somewhat similar to writing a book in LaTeX.

I want to share one of the easier pieces. A shortened, simplified piano version of Morning Wood (Morgenstemning) by Edvard Grieg. Specifically, it's a 1:1 translation of the version provided by Flowkey (no affiliation).


Morning Mood - Edvard Grieg.ly

\version "2.18.2"
\language "english"

\header {
  title = "Morning Mood"
  % Morgenstemning Suite No. 1, Op. 46 (Peer Gynt)
  composer = "Edvard Grieg"
  tagline = \markup {
    Engraved at
    \simple #(strftime "%Y-%m-%d" (localtime (current-time)))
    with \with-url #"http://lilypond.org/"
    \line { LilyPond \simple #(lilypond-version) (http://lilypond.org/) }
  }
}

topNotes =
\relative c'' {
  \time 6/8
  \clef treble
  \key c \major
  \override Score.BarNumber.break-visibility = ##(#f #t #t)
    g8 e d c d e g e d c d16 e d e g8 e g a e a g e d c4.
    g'8 e d c d e g e d c d16 e d e g8 e g a e a b gs fs e4.
    b'8 gs fs e fs gs b gs fs e fs16 gs fs gs b8 gs b c gs c d b a g4.
    g8 e d c d e g e d c d16 e d e g4. a4. <e c'>2.

}

bottomNotes =
\relative c {
  \time 6/8
  \clef bass
  \key c \major
    <c e g>2. <c e g> <c e g>4. <c e a>4. <c e g>4. g'8 e d
    <c e g>2. <c e g> <c e g>4. <c e a>4. <e gs b>4. b'8 gs fs
    <e gs b>2. <e gs b>2. <e gs b>4. <c' gs e>4. <g b d>4 . d'8 b a
    <c, e g>4. <c e a>4.  <c e g>4. <c e a>4. <c e g>4. <c f a>4. <c e g>2.

}

\score {
  <<
    \new Staff \with {
    instrumentName = "Piano"
    midiInstrument = "acoustic grand"
  } { \topNotes }
    \new Staff \with {
    instrumentName = "Piano"
    midiInstrument = "acoustic grand"
  } { \bottomNotes }
  >>
  \layout {}
  \midi {}
}

Partial screenshot of the resulting PDF:

Morning Mood - Edvard Grieg

Audio: here

Note: the chords should be sustained longer than can be heard in the provided audio, an unfortunate conversion artefact unrelated to the code written.

Thoughts of interest:

  • I'd expect the tagline has to be declared in the footer, but so far every example I've seen declares it in the header. So that's where I put it. Regardless of where you declare it though, it gets placed at the bottom of the page anyway.

  • There's code duplication between the top staff and bottom staff. Both have a MIDI instrument assigned, in this case the same instrument. Can I extract this declaration to a variable? In this case it doesn't matter much, except it looks stupid. But on a 4-hand piece it would look extra stupid. Perhaps this can be fixed by grouping staves, but so far I haven't figured it out.

  • I'm writing in relative mode. At times, this makes the notation look odd. Where you'd expect 2 parts to look exactly alike in the code, it's possible they won't if the note before it was different. While entering notes is relatively easy this way, it may be a bad idea to continue this route.

  • I'm using \override Score.BarNumber.break-visibility = ##(#f #t #t) to show measure numbers at the beginning of every measure and after every staff break (I don't need them at the end of the staff, hence the first parameter is #f false). If I understand correctly, this can only be declared in a part included by the score but not at the score itself. So I put it in the topNotes. Smells like it would be better to use a separate variable to store score modifiers like that, but I don't know how to do that in an obvious and neat manner.

  • I put a line break at every 4 measures because the part happened to be a multiple of 4 (16 measures) and that was just about the amount I could fit on my screen without side-scrolling.

Basically, it works like a charm, the result looks like a charm, but the code itself could use some serious clean-up.

\$\endgroup\$
4
\$\begingroup\$

There's a lot of boilerplate in LilyPond to put all the parts together. In your case, you used:

\score {
  <<
    \new Staff \with {
    instrumentName = "Piano"
    midiInstrument = "acoustic grand"
  } { \topNotes }
    \new Staff \with {
    instrumentName = "Piano"
    midiInstrument = "acoustic grand"
  } { \bottomNotes }
  >>
  …
}

… which is not quite right. I recommend:

global = {
  \time 6/8
  \key c \major
}

\score {
  \new PianoStaff <<
    \set PianoStaff.instrumentName = #"Piano"
    \new Staff = "upper" {
      \global
      \clef treble
      \rhNotes
    }
    \new Staff = "lower" {
      \global
      \clef bass
      \lhNotes
    }
  >>
  \layout {}
  \midi {}
}

This has three consequences:

  • The \PianoStaff directive creates a curly brace that binds the two staves together, indicating that they are to be played on a single instrument.

    Note that "acoustic grand" is the default MIDI instrument anyway, so I didn't bother to specify it.

  • Defining global parameters reduces duplication.
  • In my opinion, in this case, the clef would be better specified as a property of the staff, rather than as a property of the notes. (However, in more advanced music, where the clef can change in the middle of a piece, I would specify the clefs among the notes, as you did.)

In my opinion, showing every single bar number is annoying overkill:

\override Score.BarNumber.break-visibility = ##(#f #t #t)

Better to declutter the score so that you can put more useful numbers there such as fingering.

Assuming that you did want to use the \override directive, it would be better to put it in the global variable that I introduced above.


It is very good practice to include all bar checks to help you catch rhythm mistakes, and to make the code easier to read:

g8 e d c d e | g e d c d16 e d e | g8 e g a e a | g e d c4. |

Assuming that this is the entire piece rather than an excerpt, you should end it with a double bar line:

g8 e d c d e | g e d c d16 e d e | g4. a4. | <e c'>2. \bar "|."

Writing the pitches in relative mode is fine. Absolute mode would be a pain.

\$\endgroup\$
  • \$\begingroup\$ Honestly, the extra bar numbers helped while inserting the score. Now it's complete, you're right. It's clutter that should be replaced by more important remarks. Overall a good answer, thanks for enlightening me. \$\endgroup\$ – Mast Jan 11 '18 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.