Node:Combining music into compound expressions, Next:, Previous:Octave entry, Up:Tutorial



Combining music into compound expressions

To print more than one staff, each piece of music that makes up a staff is marked by adding \context Staff before it. These Staff's are then grouped inside \simultaneous { and }, as is demonstrated here:

          \simultaneous {
            \new Staff { \clef violin c'' }
            \new Staff { \clef bass c }
          }
          

[picture of music]

In this example, \simultaneous indicates that both music fragments happen at the same time, and must be printed stacked vertically. The notation << .. >> can also be used as a shorthand for \simultaneous { .. }.

The command \new introduces a "notation context". To understand this concept, imagine that you are performing a piece of music. When you are playing, you combine the symbols printed at a certain point with contextual information. For example, without knowing the current clef, and the accidentals in the last measure, it would be impossible to determine the pitch of a note. In other words, this information forms context that helps you decipher a score. LilyPond produces notation from music, so in effect, it does the inverse of reading scores. Therefore, it also needs to keep track of contextual information. This information is maintained in "notation contexts." There are several types of contexts, e.g. Staff, Voice and Score, but also Lyrics and ChordNames. Prepending \new to a chunk of music indicates what kind of context to use for interpreting it, and ensures that the argument is interpreted with a fresh instance of the context indicated.



We can now typeset a melody with two staves:

          \score {
            \notes
            << \new Staff {
                \time 3/4
                \clef violin
                \relative c'' {
                  e2( d4 c2 b4 a8[ a]
                  b[ b] g[ g] a2.) }
              }
              \new Staff {
                 \clef bass
                 c2 e4  g2.
                 f4 e d c2.
              }
            >>
            \paper {}
          }
          

[picture of music]

The example shows how small chunks of music, for example, the notes c2, e4, etc. of the second staff, are combined to form a larger chunk by enclosing it in braces. Again, a larger chunk is formed by prefix \new Staff to it, and that chunk is combined with << >>. This mechanism is similar with 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 example shows a sequence of expressions, where each expression is contained in the next one. The simplest expressions are numbers and operators (like +, * and /). Parentheses are used to group expressions. In LilyPond input, a similar mechanism is used. Here, the simplest expressions are notes and rests. By enclosing expressions in << >> and { }, more complex music is formed. The \new command also forms new expressions; prepending it to a music expression yields a new expression.

Like mathematical expressions, music expressions can be nested arbitrarily deep, e.g.

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

[picture of music]

When spreading expressions over multiple lines, it is customary to use an indent that indicates the nesting level. Formatting music like this eases reading, and helps you insert the right amount of closing braces at the end of an expression. For example,

     \score {
       \notes <<
         {
           ...
         }
         {
           ...
         }
       >>
     }
     

For more information on context see the Technical manual description in Interpretation context.


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

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