Previous: Changing context default settings, Up: Interpretation contexts



9.1.6 Defining new contexts

Specific contexts, like Staff and Voice, are made of simple building blocks, and it is possible to compose engraver plug-ins in different combinations, thereby creating new types of contexts.

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

[image of music]

These settings are again done 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 gets a name. 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, without a 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_engraver,

\type "Engraver_group_engraver"

Put together, we get

\context {
  \name ImproVoice
  \type "Engraver_group_engraver"
  \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,1

\context {
  \Staff
  \accepts ImproVoice
}

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
}

Footnotes

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


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

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

Other languages: English.
Using automatic language selection.