4.3.1 Contexts explained

When music is printed, many notational elements which do not appear explicitly in the input file must be added to the output. For example, compare the input and output of the following example:

\relative { cis''4 cis2. | a4 a2. | }

[image of music]

The input is rather sparse, but in the output, bar lines, an accidental, a clef, and a time signature have been added. When LilyPond interprets the input the musical information is parsed from left to right, similar to the way a performer reads the score. While reading the input, the program remembers where measure boundaries are, and which pitches require explicit accidentals. This information must be held on several levels. For example, an accidental affects only a single staff, while a bar line must be synchronized across the entire score.

Within LilyPond, these rules and bits of information are grouped in contexts. We have already introduced the Voice context. Others are the Staff and Score contexts. Contexts are hierarchical to reflect the hierarchical nature of a musical score. For example, a Staff context can contain many Voice contexts, and a Score context can contain many Staff contexts.

context-example

Each context has the responsibility for enforcing some notation rules, creating some notation objects and maintaining the associated properties. For example, the Voice context may introduce an accidental and then the Staff context maintains the rule to show or suppress the accidental for the remainder of the measure.

As another example, the synchronization of bar lines is, by default, handled in the Score context. However, in some music we may not want the bar lines to be synchronized – consider a polymetric score in 4/4 and 3/4 time. In such cases, we must modify the default settings of the Score and Staff contexts.

For very simple scores, contexts are created implicitly, and you need not be aware of them. For larger pieces, such as anything with more than one staff, they must be created explicitly to make sure that you get as many staves as you need, and that they are in the correct order. For typesetting pieces with specialized notation, it is usual to modify existing, or even to define totally new, contexts.

In addition to the Score, Staff, and Voice contexts there are contexts which fit between the score and staff levels to control staff groups, such as the PianoStaff and ChoirStaff contexts. There are also alternative staff and voice contexts, and contexts for lyrics, percussion, fret boards, figured bass, etc.

The names of all context types are formed from one or more words, each word being capitalized and joined immediately to the preceding word with no hyphen or underscore, e.g., GregorianTranscriptionStaff.

See also

Notation Reference: Contexts explained.


LilyPond Learning Manual v2.25.20 (development-branch).