Thursday, May 22, 2008

LilyPond baseline alignment

Baseline alignment of TextSpanner text takes some work in LilyPond.



The primary tricks to get that bit there are the \transparent and \combine markup commands, together with faking the right ascender- and descender-heights with an "f". All parts of that come from Markus, and thank God because I spent a good amount of time on the problem this weekend and only got partway there.

After grace positioning in LilyPond

LilyPond renders after grace notes well in a variety of metric positions.



\layout { indent = #0
ragged-right = ##t
}

\new Staff \with {
\remove Time_signature_engraver
} {
\set Score.proportionalNotationDuration = #(ly:make-moment 1 32)
\override Score.SpacingSpanner #'uniform-stretching = ##t
\override Score.SpacingSpanner #'strict-note-spacing = ##t
\override Score.SpacingSpanner #'strict-grace-spacing = ##t
\override Stem #'direction = #down
\afterGrace c'8 [ { c'32 [ c'32 c'32 c'32 ] } c'8 c'8 c'8 ]
c'8 [ \afterGrace c'8 { c'32 [ c'32 c'32 c'32 ] } c'8 c'8 ]
c'8 [ c'8 \afterGrace c'8 { c'32 [ c'32 c'32 c'32 ] } c'8 ]
c'8 [ c'8 c'8 \afterGrace c'8 ] { c'32 [ c'32 c'32 c'32 ] }
}

Time signatures and strict note spacing in LilyPond

Time signatures and strict note spacing in LiyPond disagree.



Time signatures with strict note spacing turned on. The bracket underextends and the time signature and noteheads overlap.




Time signatures with strict note spacing turned off. The bracket extends correctly and neither the time signaure nore noteheads overlap.

LilyPond whistle tones

Create whistle tones in LilyPond with small values to NoteHead #'font-size.



<<
{
\override Stem #'direction = #down
\override Stem #'transparent = ##t
\override NoteHead #'font-size = #-3
\override Accidental #'font-size = #-3
\override Script #'font-size = #-4
\override Script #'padding = #0.5
\override Script #'extra-offset = #'(-0.1 . 0)
s64
fs'''64 \flageolet
...
e''''64 \flageolet
\revert Stem #'transparent
\revert NoteHead #'font-size
\revert Accidental #'font-size
\revert Script #'font-size
\revert Script #'padding
}
\\
{
\override Beam #'font-size = #-3
\override NoteHead #'font-size = #-3
\once \override Script #'direction = #up
\once \override Script #'font-size = #-4
\once \override Script #'padding = #0.5
<
d'''
\tweak #'duration-log #1
d'
>64 [ \flageolet
\override NoteHead #'transparent = ##t
\override NoteHead #'no-ledgers = ##t
c'64
...
c'64 ]
\revert NoteHead #'transparent
\revert Beam #'font-size
}
>>

Two separate voices allow the harmonics and reference voice to stem independently.

Spacing LilyPond trill spanners

Consecutive LilyPond trill spanners overlap at the same vertical position. The spanner package in 2.11 fixes this with independent control of left and right padding.



\new Staff {
\override TrillSpanner #'bound-details #'right #'padding = #2
c'4 \startTrillSpan
c'4 \stopTrillSpan \startTrillSpan
\revert TrillSpanner #'bound-details #'right
c'4 \stopTrillSpan \startTrillSpan
c'4 \stopTrillSpan
}

Centering LilyPond page numbers

LilyPond page numbers print in lower left and right corners by default. Printing LilyPond page numbers somewhere other than in these default locations requires some additions to the \paper block.

Specify LilyPond page numbers explicitly with the \on-the-fly #print-page-number-check-first and \fromproperty #'page:page-number-string incantations.

\paper {
print-page-number = ##t
print-first-page-number = ##t
oddHeaderMarkup = \markup \fill-line { " " }
evenHeaderMarkup = \markup \fill-line { " " }
oddFooterMarkup = \markup { \fill-line {
\bold \fontsize #3 \on-the-fly #print-page-number-check-first
\fromproperty #'page:page-number-string } }
evenFooterMarkup = \markup { \fill-line {
\bold \fontsize #3 \on-the-fly #print-page-number-check-first
\fromproperty #'page:page-number-string } }
}

Centering occurs thanks to \fill-line.

A stem-centered triangle notehead in LilyPond

How do we get a stem-centered triangle notehead in LilyPond?

Max implemented an answer last year reusing the triangle- or do-shaped notehead from the shape notes series. Here's the first example.



To me the example shows two different series of notes. The first four notes show a type of "inward-pointing" orientation while the last four notes show a type of "outward-pointing" orientation.

Later Max sent even more examples.



These are perfect. The outward-pointing triangles in measure two will handle both Sciarrino's lip- and tongue-pizzicati and also the "play the highest pitch, whatever that is" symbol in Penderecki. The inward-pointing triangles in measure three are amazingly odd and so visually distinct, at the same time. To what use will they go? Maybe ingressive air or some type of reversed bowing or playing. There's definitely something decidedly "reversed" about these shapes.

A good accent notehead in LilyPond

A couple of weeks ago Max took up the challenge of making a good accent-shaped notehead. I first mailed the list back in April last year and then Jamie brought the request up again in February. Max did some pretty amazing stuff. He got the stems to center horizontally on the glyph, both up and down. And he and Jamie convinced me that a calligraphic version of glyph with thicker lower edge actually worked better than versions that were completely symmetric across the x-axis. Here are some of different variations we tried. Max cooked all these up and Jamie and I kept going through the different combinations.





And here's the final version. The trick wound up being that the calligraphic glyph had to rise just above center to make the vertical spacing work right.




This work patched into 2.11.22. And so now we have the right glyph for a tongue-ram.

Creating even vertical spacing in LilyPond

This breaks voice produces uneven vertical spacing.

\new Voice {
\overrideProperty #"Score.NonMusicalPaperColumn"
#'line-break-system-details #'((Y-offset . 0))
s1 \noBreak
s1 \noBreak
s1 \break
\overrideProperty #"Score.NonMusicalPaperColumn"
#'line-break-system-details #'((Y-offset . 15))
s1 \noBreak
s1 \noBreak
s1 \break
\overrideProperty #"Score.NonMusicalPaperColumn"
#'line-break-system-details #'((Y-offset . 30))
s1 \noBreak
s1 \noBreak
s1 \break
}

This breaks voice produces even vertical spacing.

\new Voice {
\overrideProperty #"Score.NonMusicalPaperColumn"
#'line-break-system-details
#'((Y-offset . 0) (alignment-offsets . (0)))
s1 \noBreak
s1 \noBreak
s1 \break
\overrideProperty #"Score.NonMusicalPaperColumn"
#'line-break-system-details
#'((Y-offset . 15) (alignment-offsets . (0)))
s1 \noBreak
s1 \noBreak
s1 \break
\overrideProperty #"Score.NonMusicalPaperColumn"
#'line-break-system-details
#'((Y-offset . 30) (alignment-offsets . (0)))
s1 \noBreak
s1 \noBreak
s1 \break
}

Even vertical spacing requires alignment-offets set to zero for even a single staff.

Making LilyPond accidentals consume no horizontal space

Accidentals consume no horizontal space with their X-extent set to zero.


\new Staff {
\set Score.proportionalNotationDuration = #(ly:make-moment 1 32)
\override Accidental #'X-extent = #'(0 . 0)
\override Accidental #'extra-offset = #'(-1 . 0)
c'16
cis'16
c'16
c'16
c'16
c'16
c'16
c'16
}

extra-offset is required to move zero-width accidentals away from the left edge of the parent notehead.

Breaking LilyPond tuplets across a line

LilyPond refuses explicit midduration \breaks by default. Here voice one houses explicit line-breaking information and voice two houses the real musical input. But even so the tuplet refuses to break across the line.



Here the tuplet does line break.



The second example \removes the Forbid_line_break_engraver from voice two explicitly. The first example does not.

\new Staff <<
\new Voice {
s1 \noBreak
s1 \break
s1 \noBreak
s1 \break
}
\new Voice \with {
\remove Forbid_line_break_engraver
} {
c'1
\times 4/5 { c'2 c'2 c'2 c'2 c'2 }
c'1
}
>>

It does not work to \remove the Forbid_line_break_engraver from the Staff, StaffGroup or Score contexts; only Voice and derived contexts implement the engraver in question.

Suppressing LilyPond clashing NoteColumn warnings

Stacked LilyPond voices can produce clashing NoteColumn warnings.



\new Staff <<
\new Voice {
\override Stem #'direction = #up
c''4 c''4 c''4 c''4
}
\new Voice {
\override Stem #'transparent = ##t
\override Stem #'length = #0
\override NoteHead #'duration-log = #1
e'4 e'4 e'4 e'4
}
\new Voice {
\override Stem #'direction = #down
c'4 c'4 c'4 c'4
}
>>


Preprocessing graphical objects...
348.ly:6:27: warning: ignoring too many clashing note columns
} { c''4 c''4 c''4
c''4 }
348.ly:6:22: warning: ignoring too many clashing note columns
} { c''4 c''4
c''4 c''4 }
348.ly:6:17: warning: ignoring too many clashing note columns
} { c''4
c''4 c''4 c''4 }
348.ly:6:12: warning: ignoring too many clashing note columns
} {
c''4 c''4 c''4 c''4 }

What's going on here is that the middle note of each 'chord' sets Stem #'transparent = ##t which renders stems transparent but causes clashing note column warnings.

The solution is to set \override NoteColumn #'ignore-collision = ##t somewhere in the score.

Wednesday, May 21, 2008

A LilyPond Moment is a tuple of rationals

From Han-Wen on the list on 1 May:

"Moments are used interchangeably for points and durations. There is a fine point, though; Moment is really a tuple of rationals, with the first being the 'main' time, and the 2nd the 'grace' time."

Monday, May 12, 2008

Printing LilyPond PaperColumn and NonMusicalPaperColumn

Both PaperColumn and NonMusicalPaperColumn can take a stencil.




\paper { ragged-right = ##t }
\layout { \context { \Score
proportionalNotationDuration = #(ly:make-moment 1 64) } }

\new Staff {
\override Score.NonMusicalPaperColumn #'stencil = #ly:paper-column::print
\override Beam #'positions = #'(-4 . -4)
c''8 [ e''8 g''8 b''8 ]
}

\new Staff {
\override Score.PaperColumn #'stencil = #ly:paper-column::print
\override Beam #'positions = #'(-4 . -4)
c''8 [ e''8 g''8 b''8 ]
}

The key here is that both PaperColum and NonMusicalPaperColumn subscribe to the paper-column interface.

LilyPond double stems across staves

LilyPond double stems work fine across staves.



\new PianoStaff <<
\new Staff = RH {
\time 4/8
c''8 [ c''8
\change Staff = LH
<<
{ e'8 }
\new Voice { \once \override Stem #'direction = #down e'8 }
>>
\change Staff = RH
c''8 ]
}
\new Staff = LH { s2 }
>>

The trick is in instantiating the parallel music << >> construct with no \\ separator between the first expression { e'8 } and the second { \once \override Stem #'direction = #down e'8 }.

LilyPond stem direction

LilyPond Stem #'direction takes #up, #down or a procedure.



\new Staff {
c'4
\once \override Stem #'direction = #ly:stem::calc-direction
c'4
c'4
c'4
}

\layout {
\context { \Staff
\override Stem #'direction = #down } }

Here Stem #'default-direction is #down and so the procedure #ly:stem::calc-direction is helpful.

LilyPond uniform-stretching

uniform-stretching and strict-note-spacing usually set to ##t together; setting uniform-stretching = ##t in isolation can cause problems. Here's an example with proportionalNotationDuration but without uniform-stretching.



And here's the same example with uniform-stretching turned on.



The effect shows up only before line breaks and is similar to that described here about strict-note-spacing set to ##t in isolation.

Custom LilyPond spanners

Custom spanners to segment material in sketches.



\new Staff {

\override TextSpanner #'dash-fraction = ##f
\override TextSpanner #'bound-details #'left
#'stencil-align-dir-y = #center
\override TextSpanner #'bound-details #'left
#'padding = #0.75
\override TextSpanner #'bound-details #'right
#'attach-dir = #right

\override TextSpanner #'bound-details #'left
#'text = \markup { \bold 7 }
\override TextSpanner #'bound-details #'right
#'text = \markup { \draw-line #'(0 . -1) }
c'4 \startTextSpan
c'4 \stopTextSpan

\override TextSpanner #'bound-details #'left
#'text = \markup { \bold 8 }
c'4 \startTextSpan
c'4 \stopTextSpan

}

The settings to stencil-align-dir-y and attach-dir are tricky.

Making LilyPond GrandStaff accept RhythmicStaff

GrandStaff does not by default accept RhythmicStaves. Context modification fixes this.



\new GrandStaff <<
\new RhythmicStaff { c'4 }
\new RhythmicStaff { c'4 }
\new RhythmicStaff { c'4 }
\new RhythmicStaff { c'4 }
>>

\layout { \context { \GrandStaff \accepts RhythmicStaff } }

Thanks to Rune.

LilyPond bar numbers every measure


\new Staff {
\override Score.BarNumber #'break-visibility = ##(#t #t #t)
c'1 c'1 c'1
\break
c'1 c'1 c'1
}

LilyPond outer tuplet bracket alignment

The inner triplet bracket in the first measure here aligns vertically with the lone quintuplet bracket in the second measure.



\new RhythmicStaff {
\override Staff.TupletBracket #'staff-padding = #5
\override Staff.TupletBracket #'padding = #2.25
\times 4/5 {
c'2.
\times 2/3 {
c'2
c'4
}
}
\times 4/5 {
c'2.
c'2
}
}

Is there a way to align both quintuplet brackets vertically?

LilyPond shifted first note



\new Score \with {
\override Clef #'space-alist = #'(
(ambitus extra-space . 2.0)
(staff-bar extra-space . 0.7)
(key-cancellation minimum-space . 3.5)
(key-signature minimum-space . 3.5)
(time-signature minimum-space . 4.2)
(first-note minimum-fixed-space . 15.0)
(next-note extra-space . 0.5)
(right-edge extra-space . 0.5))
} {
\new Staff \with {
\remove Time_signature_engraver
} {
c'4 c'4 c'4 c'4
d'4 d'4 d'4 d'4
}
}

LilyPond tempo equivalency markup



\new Staff {
c'1
^ \markup {
\override #'(baseline-skip . 5)
\column {
\line { [ \italic { Allegro staccatissimo } }
\line { \hspace #1 \fontsize #-3 \general-align #Y #down
\note #"4" #1 = 90
}
}
}
}

LilyPond cross-staff stems

Cross-staff stem lengths set manually.



\layout {
ragged-right = ##t
\context {
\Score
proportionalNotationDuration = #(ly:make-moment 1 48)
\override NonMusicalPaperColumn
#'line-break-system-details =
#'((alignment-offsets . (0 -8)))
autoBeaming = ##f } }

\new PianoStaff <<
\new Staff = "RH" {
#(set-accidental-style 'forget)
\override Beam #'positions = #'(8 . 8)
\override Stem #'direction = #up
<d'>16 [
<d'>16
<d'>16
<d'>16 ]
}
\new Staff = "LH" {
\clef bass
\override Stem #'flag-style = #'no-flag
\override Stem #'length = #20
\override Stem #'cross-staff = ##t
<c>16
<c>16
<c>16
<c>16
}
>>

Stem overrides to 'flag-style, 'length and 'cross-staff all matter.

LilyPond breath mark horizontal positioning

So the breath sign can position horizontally. From Mats:

"The horizontal position is determined using a completely different mechanism. For example, if you want to move the breathing sign further away from the next note, you can use \override BreathingSign #'space-alist #'next-note = #'(fixed-space . 2.5)"

Thursday, May 8, 2008

The last LilyPond tuplet bracket

Using tupletFullLength causes tuplet brackets to stretch just too far at the end of a piece. The solution is to use shorten-pair to manually shorten only the right end of the last bracket.



\new Staff {
\set Score.tupletFullLength = ##t
\times 2/3 { c'2 c'2 c'2 }
\times 2/3 { c'2 c'2 c'2 }
\once \override TupletBracket #'shorten-pair = #'(-0.2 . 1.2)
\times 2/3 { c'2 c'2 c'2 }
}

Here we set shorten-pair equal to #'(-0.2 . 1.2); the default is #'(-0.2 . -0.2).

Making LilyPond stems disappear

Setting Stem #'stencil = ##f can cause problems. Setting Stem #'length = #0 works better.



\new Staff {
\override Stem #'length = #0
c'4 d'4 e'4 f'4
g'4 a'4 b'4 c''4
d''4 e''4 f''4 g''4
}

But even zero-length stems extend to the center line. The no-stem-extend attribute in stem-interface helps.




\new Staff {
\override Stem #'length = #0
\override Stem #'no-stem-extend = ##t
c'4 d'4 e'4 f'4
g'4 a'4 b'4 c''4
d''4 e''4 f''4 g''4
}

Zero-length stems now exhibit true zero length.

LilyPond diamond noteheads

Black and white diamond noteheads are easy now.



\new Staff {
\set Score.proportionalNotationDuration = #(ly:make-moment 1 16)
\override Staff.TimeSignature #'transparent = ##t
\once \override NoteHead #'style = #'harmonic-black
d'4
\once \override NoteHead #'style = #'harmonic
d'4
}

Thanks to Max. Previously 'harmonic-black and 'harmonic weren't available as NoteHead #'style options.

Changing LilyPond instrument names midscore

Long and short instrument names change on the fly.



\paper {
ragged-right = ##t
left-margin = 30
indent = 0
}

\new Staff {
\override Staff.TimeSignature #'stencil = ##f
\override Score.BarNumber #'transparent = ##t
\set Staff.instrumentName = \markup { \hcenter-in #14 "Flauto" }
\set Staff.shortInstrumentName = \markup { \hcenter-in #14 "Fl." }
c'1 \break
c'1 \break
\set Staff.instrumentName = \markup { \column {
\hcenter-in #14 \line { "Flauto" }
\hcenter-in #14 \line { "contralto" } } }
\set Staff.shortInstrumentName = \markup { \column {
\hcenter-in #14 \line { "Fl." }
\hcenter-in #14 \line { "contr." } } }
c'1 \break
c'1 \break
}

Using LilyPond dummy bars

Line- and page-breaks require bars. This is true even for midmeasure breaks which require the dummy construction \bar "". Midmeasure breaks also necessitate the removal of the Forbid_line_break_engraver from all Voice contexts.

\layout {
\context { \Voice \remove Forbid_line_break_engraver }
}

\new Score <<
\new Staff <<
\new Voice { s1 s2 \bar "" \break s2 s1 }
\new Voice { c'1 c'1 c'1 }
>>
\new Staff { c'1 c'1 c'1 }
\new Staff { c'1 c'1 c'1 }
\new Staff { c'1 c'1 c'1 }
>>

Fortunately, each midmeasure break requires only one such dummy bar. The midmeasure break here cuts across four different vertically stacked Voices.

LilyPond TupletBracket #'if-no-beam

From Rune on the list: "The property TupletBracket #'bracket-visibility should be set to either ##t, ##f or #'if-no-beam."

There's an #'if-no-beam?

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.