Next: , Previous: Octave entry, Up: Tutorial



2.7 Music expressions explained

In input files, music is represent by so-called music expressions. We have already seen some in the previous examples; a single note is a music expression

     a4

[image of music]

Enclosing a group of notes in braces creates a new music expression

     { a4 g4 }

[image of music]

Putting a bunch of music expressions (e.g., notes) in braces, means that they should be played in sequence. The result again is a music expression, which can be grouped with other expressions sequentially. Here, the expression from the previous example is combined with two notes

     { { a4 g } f g }

[image of music]

This technique is useful for non-monophonic music. To enter music with more voices or more staves, we also combine expressions in parallel. Two voices that should play at the same time, are entered as a simultaneous combination of two sequences. A `simultaneous' music expression is formed by enclosing expressions in << and >>. In the following example, three sequences (all containing two separate notes) are combined simultaneously

     <<
       { a4 g }
       { f e }
       { d b }
     >>

[image of music]

This mechanism is similar to mathematical formulas: a big formula is created by composing small formulas. Such formulas are called expressions, and their definition is recursive, so you can make arbitrarily complex and large expressions. For example,

1

1 + 2

(1 + 2) * 3

((1 + 2) * 3) / (4 * 5)

This is a sequence of expressions, where each expression is contained in the next one. The simplest expressions are numbers, and larger ones are made by combining expressions with operators (like +, * and /) and parentheses. Like mathematical expressions, music expressions can be nested arbitrarily deep, which is necessary for complex music like polyphonic scores.

Note that this example only has one staff, whereas the previous example had three separate staves. That is because this example begins with a single note. To determine the number of staves, LilyPond looks at the first element. If it is a single note, there is one staff; if there is a simultaneous expression, there is more than one staff.

     {
       c <<c e>>
       << { e f } { c <<b d>> } >>
     }

[image of music]

Music files with deep nesting can be confusing to enter and maintain. One convention that helps against this confusion is indenting. When entering a file with deep nesting of braces and angles, it is customary to use an indent that indicates the nesting level. Formatting music like this eases reading and helps you insert the right number of closing braces at the end of an expression. For example,

<<
  {
    ...
  }
  {
    ...
  }
>>

Some editors have special support for entering LilyPond, and can help indenting source files. See Editor support for more information.

This page is for LilyPond-2.6.3 (stable-branch).

Report errors to <bug-lilypond@gnu.org>.

Other languages: English.
Using automatic language selection.