Showing posts with label extension. Show all posts
Showing posts with label extension. Show all posts

Tuesday, May 6, 2008

Smart LilyPond context definition

LilyPond makes it easy to define new contexts with special attributes.

\layout {
\context {
\Staff
\type Engraver_group
\name TwoLineStaff
\alias Staff
\override StaffSymbol #'line-positions = #'(-2 2)
}
\context {
\Score
\accepts TwoLineStaff
}
}

This layout block defines a special two-line staff.

\new TwoLineStaff { c'4 d'4 e'4 f'4 }




Red and blue versions of our special TwoLineStaff are available like this.

\layout {
\context {
\Staff
\type Engraver_group
\name TwoLineStaff
\alias Staff
\override StaffSymbol #'line-positions = #'(-2 2)
}
\context {
\TwoLineStaff
\type Engraver_group
\name RedTwoLineStaff
\alias TwoLineStaff
\override NoteHead #'color = #red
\override Stem #'color = #red
\override Dots #'color = #red
}
\context {
\TwoLineStaff
\type Engraver_group
\name BlueTwoLineStaff
\alias TwoLineStaff
\override NoteHead #'color = #blue
\override Stem #'color = #blue
\override Dots #'color = #blue
}
\context {
\Score
\accepts RedTwoLineStaff
\accepts BlueTwoLineStaff
}
}

RedTwoLineStaff and BlueTwoLineStaff here extend TwoLineStaff in the manner of concrete classes extending an abstract base class.

\new Score <<
\new RedTwoLineStaff { c'4 d'4 e'4 f'4 }
\new BlueTwoLineStaff { c'4 d'4 e'4 f'4 }
>>




These definitions of RedTwoLineStaff and BlueTwoLineStaff avoid code duplication and let us have both types of staff in a single score. In a later post we'll look at a different way to define red and blue two-line staves for different editions of the same score.