Previous: Changing context default settings, Up: Interpretation contexts


9.2.6 Defining new contexts

Specific contexts, like Staff and Voice, are made of simple building blocks. It is possible to create new types of contexts with different combinations of engraver plug-ins.

The next example shows how to build a different type of Voice context from scratch. It will be similar to Voice, but only prints centered slash noteheads. It can be used to indicate improvisation in jazz pieces,

[image of music]

These settings are defined within a \context block inside a \layout block,

\layout {
  \context {
    ...
  }
}

In the following discussion, the example input shown should go on the ... in the previous fragment.

First the context's name is defined. Instead of Voice it will be called ImproVoice ,

\name ImproVoice

Since it is similar to the Voice , we want commands that work on (existing) Voice s to remain working. This is achieved by giving the new context an alias Voice ,

\alias Voice

The context will print notes, and instructive texts

\consists Note_heads_engraver
\consists Text_engraver

but only on the center line,

\consists Pitch_squash_engraver
squashedPosition = #0

The Pitch_squash_engraver modifies note heads (created by Note_heads_engraver) and sets their vertical position to the value of squashedPosition, in this case 0, the center line.

The notes look like a slash, and have no stem,

\override NoteHead #'style = #'slash
\override Stem #'transparent = ##t

All these plug-ins have to cooperate, and this is achieved with a special plug-in, which must be marked with the keyword \type. This should always be Engraver_group,

\type "Engraver_group"

Put together, we get

\context {
  \name ImproVoice
  \type "Engraver_group"
  \consists "Note_heads_engraver"
  \consists "Text_engraver"
  \consists Pitch_squash_engraver
  squashedPosition = #0
  \override NoteHead #'style = #'slash
  \override Stem #'transparent = ##t
  \alias Voice
}

Contexts form hierarchies. We want to hang the ImproVoice under Staff , just like normal Voices. Therefore, we modify the Staff definition with the \accepts command,

\context {
  \Staff
  \accepts ImproVoice
}

The opposite of \accepts is \denies, which is sometimes needed when reusing existing context definitions.

Putting both into a \layout block, like

\layout {
  \context {
    \name ImproVoice
    ...
  }
  \context {
    \Staff
    \accepts "ImproVoice"
  }
}

Then the output at the start of this subsection can be entered as

\relative c'' {
  a4 d8 bes8
  \new ImproVoice {
    c4^"ad lib" c
    c4 c^"undress"
    c c_"while playing :)"
  }
  a1
}

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

Report errors to http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs.

Other languages: English.
Using automatic language selection.