Pitches

Adding ambitus per voice

Ambitus can be added per voice. In this case, the ambitus must be moved manually to prevent collisions.

\new Staff <<
\new Voice \with {
\consists "Ambitus_engraver"
} \relative c'' {
\override Ambitus #'X-offset = #2.0
\voiceOne
c4 a d e
f1
}
\new Voice \with {
\consists "Ambitus_engraver"
} \relative c' {
\voiceTwo
es4 f g as
b1
}
>>


Altering the length of beamed stems

Stem lengths on beamed notes can be varied by overriding the beamed-lengths property of the details of the Stem. If a single value is used as an argument, the length applies to all stems. When multiple arguments are used, the first applies to eighth notes, the second to sixteenth notes and so on. The final argument also applies to all notes shorter than the note length of the final argument. Non-integer arguments may also be used.

\relative c'' {
\override Stem #'(details beamed-lengths) = #'(2)
a8[ a] a16[ a] a32[ a]
\override Stem #'(details beamed-lengths) = #'(8 10 12)
a8[ a] a16[ a] a32[ a] r8
\override Stem #'(details beamed-lengths) = #'(8)
a8[ a]
\override Stem #'(details beamed-lengths) = #'(8.5)
a8[ a]
\revert Stem #'details
a8[ a] a16[ a] a32[ a] r16
}


Ambitus with multiple voices

Adding the Ambitus_engraver to the Staff context creates a single ambitus per staff, even in the case of staves with multiple voices.

\new Staff \with {
\consists "Ambitus_engraver"
}
<<
\new Voice \relative c'' {
\voiceOne
c4 a d e
f1
}
\new Voice \relative c' {
\voiceTwo
es4 f g as
b1
}
>>


Ambitus

Ambitus indicate pitch ranges for voices.

Accidentals only show up if they are not part of the key signature. AmbitusNoteHead grobs also have ledger lines.

\layout {
\context {
\Voice
\consists "Ambitus_engraver"
}
}

<<
\new Staff {
\relative c' {
\time 2/4
c4 f'
}
}
\new Staff {
\relative c' {
\time  2/4
\key d \major
cis4 as'
}
}
>>


Applying note head styles depending on the step of the scale

The shapeNoteStyles property can be used to define various note head styles for each step of the scale (as set by the key signature or the tonic property). This property requires a set of symbols, which can be purely arbitrary (geometrical expressions such as triangle, cross, and xcircle are allowed) or based on old American engraving tradition (some latin note names are also allowed).

That said, to imitate old American song books, there are several predefined note head styles available through shortcut commands such as \aikenHeads or \sacredHarpHeads.

This example shows different ways to obtain shape note heads, and demonstrates the ability to transpose a melody without losing the correspondence between harmonic functions and note head styles.

fragment = {
\key c \major
c2 d
e2 f
g2 a
b2 c
}

\new Staff {
\transpose c d
\relative c' {
\set shapeNoteStyles = #'#(do re mi fa
#f la ti)
\fragment
}

\break

\relative c' {
\set shapeNoteStyles = #'#(cross triangle fa #f
mensural xcircle diamond)
\fragment
}
}


Changing the ambitus gap

It is possible to change the default gap setting for ambitus.

\layout {
\context {
\Voice
\consists "Ambitus_engraver"
}
}

\new Staff {
\time 2/4
% Default setting
c'4 g''
}

\new Staff {
\time 2/4
\override AmbitusLine #'gap = #0
c'4 g''
}

\new Staff {
\time 2/4
\override AmbitusLine #'gap = #1
c'4 g''
}

\new Staff {
\time 2/4
\override AmbitusLine #'gap = #1.5
c'4 g''
}


Changing the interval of lines on the stave

staffLineLayoutFunction is used to change the position of notes. This snippet shows setting its value to ly:pitch-semitones in order to produce a chromatic scale with the distance between each space and line of the stave equal to one semitone.

scale = \relative c' {
a4 ais b c
cis4 d dis e
f4 fis g gis
a1
}

\new Staff \with {
\remove "Accidental_engraver"
staffLineLayoutFunction = #ly:pitch-semitones
}
{
<<
\scale
\context NoteNames {
\set printOctaveNames = ##f
\scale
}
>>
}


Clefs can be transposed by arbitrary amounts

Clefs can be transposed by arbitrary amounts, not just by octaves.

\relative c' {
\clef treble
c4 c c c
\clef "treble_8"
c4 c c c
\clef "treble_5"
c4 c c c
\clef "treble^3"
c4 c c c
}


Coloring notes depending on their pitch

It is possible to color note heads depending on their pitch and/or their names: the function used in this example even makes it possible to distinguish enharmonics.

%Association list of pitches to colors.
#(define color-mapping
(list
(cons (ly:make-pitch 0 0 NATURAL) (x11-color 'red))
(cons (ly:make-pitch 0 0 SHARP) (x11-color 'green))
(cons (ly:make-pitch 0 1 FLAT) (x11-color 'green))
(cons (ly:make-pitch 0 2 NATURAL) (x11-color 'red))
(cons (ly:make-pitch 0 2 SHARP) (x11-color 'green))
(cons (ly:make-pitch 0 3 FLAT) (x11-color 'red))
(cons (ly:make-pitch 0 3 NATURAL) (x11-color 'green))
(cons (ly:make-pitch 0 4 SHARP) (x11-color 'red))
(cons (ly:make-pitch 0 5 NATURAL) (x11-color 'green))
(cons (ly:make-pitch 0 5 FLAT) (x11-color 'red))
(cons (ly:make-pitch 0 6 SHARP) (x11-color 'red))
(cons (ly:make-pitch 0 1 NATURAL) (x11-color 'blue))
(cons (ly:make-pitch 0 3 SHARP) (x11-color 'blue))
(cons (ly:make-pitch 0 4 FLAT) (x11-color 'blue))
(cons (ly:make-pitch 0 5 SHARP) (x11-color 'blue))
(cons (ly:make-pitch 0 6 FLAT) (x11-color 'blue))))

%Compare pitch and alteration (not octave).
#(define (pitch-equals? p1 p2)
(and
(= (ly:pitch-alteration p1) (ly:pitch-alteration p2))
(= (ly:pitch-notename p1) (ly:pitch-notename p2))))

#(define (pitch-to-color pitch)
(let ((color (assoc pitch color-mapping pitch-equals?)))
(if color
(cdr color))))

(pitch-to-color
(ly:event-property (event-cause grob) 'pitch)))

\score {
\new Staff \relative c' {
c8 b d dis ees f g aes
}
}


Creating a sequence of notes on various pitches

In music that contains many occurrences of the same sequence of notes at different pitches, the following music function may prove useful. It takes a note, of which only the pitch is used. This example creates the rhythm used throughout Mars, from Gustav Holst’s The Planets.

rhythm =
#(define-music-function (parser location p) (ly:pitch?)
"Make the rhythm in Mars (the Planets) at the given pitch"
#{ \times 2/3 { $p 8$p $p }$p 4 $p$p 8 $p$p 4 #})

\new Staff {
\time 5/4
\rhythm c'
\rhythm c''
\rhythm g
}


Forcing a clef symbol to be displayed

When a clef sign has already been displayed and it has not been changed to a different clef, then repeating the \clef command will be ignored by LilyPond, since it is not a change of clef. It is possible to force the clef to be redisplayed using the command \set Staff.forceClef = ##t.

\relative c' {
\clef treble
c1
\clef treble
c1
\set Staff.forceClef = ##t
c1
\clef treble
c1
}


Generating random notes

This Scheme-based snippet generates 24 random notes (or as many as required), based on the current time (or any randomish number specified instead, in order to obtain the same random notes each time): i.e., to get different random note patterns, just change this number.

\score {
{
\$(let ((random-state (seed->random-state (current-time))))
(make-sequential-music
(map (lambda (x)
(let ((idx (random 12 random-state)))
(make-event-chord
(list
(make-music 'NoteEvent
'duration (ly:make-duration 2 0 1 1)
'pitch (ly:make-pitch
(quotient idx 7)
(remainder idx 7)
0))))))
(make-list 24))))
}
}


Hiding accidentals on tied notes at the start of a new system

This shows how to hide accidentals on tied notes at the start of a new system.

\relative c'' {
\override Accidental #'hide-tied-accidental-after-break = ##t
cis1~ cis~
\break
cis
}


Keep change clefs full sized

When a clef is changed, the clef sign displayed is smaller than the initial clef. This can be overridden with full-size-change.

\relative c' {
\clef "treble"
c1
\clef "bass"
c1
\clef "treble"
c1
\override Staff.Clef #'full-size-change = ##t
\clef "bass"
c1
\clef "treble"
c1
\revert Staff.Clef #'full-size-change
\clef "bass"
c1
\clef "treble"
c1
}


Makam example

Makam is a type of melody from Turkey using 1/9th-tone microtonal alterations. Consult the initialization file ‘ly/makam.ly’ for details of pitch names and alterations.

% Initialize makam settings
\include "makam.ly"

\relative c' {
\set Staff.keySignature = #((6 . ,(- KOMA)) (3 . ,BAKIYE))
c4 cc db fk
gbm4 gfc gfb efk
fk4 db cc c
}


The commonly used \key command sets the keySignature property, in the Staff context.

To create non-standard key signatures, set this property directly. The format of this command is a list:

 \set Staff.keySignature = #(((octave . step) . alter) ((octave . step) . alter) ...)  where, for each element in the list, octave specifies the octave (0 being the octave from middle C to the B above), step specifies the note within the octave (0 means C and 6 means B), and alter is ,SHARP ,FLAT ,DOUBLE-SHARP etc. (Note the leading comma.) The accidentals in the key signature will appear in the reverse order to that in which they are specified.

Alternatively, for each item in the list, using the more concise format (step . alter) specifies that the same alteration should hold in all octaves.

For microtonal scales where a “sharp” is not 100 cents, alter refers to the alteration as a proportion of a 200-cent whole tone.

Here is an example of a possible key signature for generating a whole-tone scale:

\relative c' {
\set Staff.keySignature = #(((0 . 6) . ,FLAT)
((0 . 5) . ,FLAT)
((0 . 3) . ,SHARP))
c4 d e fis
aes4 bes c2
}


Numbers as easy note heads

Easy notation note heads use the note-names property of the NoteHead object to determine what appears inside the note head. By overriding this property, it is possible to print numbers representing the scale-degree.

A simple engraver can be created to do this for every note head object it sees.

#(define Ez_numbers_engraver
(make-engraver
(acknowledgers
((note-head-interface engraver grob source-engraver)
(let* ((context (ly:translator-context engraver))
(tonic-pitch (ly:context-property context 'tonic))
(tonic-name (ly:pitch-notename tonic-pitch))
(grob-pitch
(ly:event-property (event-cause grob) 'pitch))
(grob-name (ly:pitch-notename grob-pitch))
(delta (modulo (- grob-name tonic-name) 7))
(note-names
(make-vector 7 (number->string (1+ delta)))))
(ly:grob-set-property! grob 'note-names note-names))))))

#(set-global-staff-size 26)

\layout {
ragged-right = ##t
\context {
\Voice
\consists \Ez_numbers_engraver
}
}

\relative c' {
c4 d e f
g4 a b c \break

\key a \major
a,4 b cis d
e4 fis gis a \break

\key d \dorian
d,4 e f g
a4 b c d
}


Orchestra choir and piano template

This template demonstrates the use of nested StaffGroup and GrandStaff contexts to sub-group instruments of the same type together, and a way to use \transpose so that variables hold music for transposing instruments at concert pitch.

#(set-global-staff-size 17)
\paper {
indent = 3.0\cm  % space for instrumentName
short-indent = 1.5\cm  % space for shortInstrumentName
}

fluteMusic = \relative c' { \key g \major g'1 b }
% Pitches as written on a manuscript for Clarinet in A
% are transposed to concert pitch.
clarinetMusic = \transpose c' a
\relative c'' { \key bes \major bes1 d }
trumpetMusic = \relative c { \key g \major g''1 b }
% Key signature is often omitted for horns
hornMusic = \transpose c' f
\relative c { d'1 fis }
percussionMusic = \relative c { \key g \major g1 b }
sopranoMusic = \relative c'' { \key g \major g'1 b }
sopranoLyrics = \lyricmode { Lyr -- ics }
altoIMusic = \relative c' { \key g \major g'1 b }
altoIIMusic = \relative c' { \key g \major g'1 b }
altoILyrics =  \sopranoLyrics
altoIILyrics = \lyricmode { Ah -- ah }
tenorMusic = \relative c' { \clef "treble_8" \key g \major g1 b }
tenorLyrics = \sopranoLyrics
pianoRHMusic = \relative c { \key g \major g''1 b }
pianoLHMusic = \relative c { \clef bass \key g \major g1 b }
violinIMusic = \relative c' { \key g \major g'1 b }
violinIIMusic = \relative c' { \key g \major g'1 b }
violaMusic = \relative c { \clef alto \key g \major g'1 b }
celloMusic = \relative c { \clef bass \key g \major g1 b }
bassMusic = \relative c { \clef "bass_8" \key g \major g,1 b }

\score {
<<
\new StaffGroup = "StaffGroup_woodwinds" <<
\new Staff = "Staff_flute" {
\set Staff.instrumentName = #"Flute"
% shortInstrumentName, midiInstrument, etc.
% may be set here as well
\fluteMusic
}
\new Staff = "Staff_clarinet" {
\set Staff.instrumentName =
\markup { \concat { "Clarinet in B" \flat } }
% Declare that written Middle C in the music
%  to follow sounds a concert B flat, for
%  output using sounded pitches such as MIDI.
\transposition bes
% Print music for a B-flat clarinet
\transpose bes c' \clarinetMusic
}
>>
\new StaffGroup = "StaffGroup_brass" <<
\new Staff = "Staff_hornI" {
\set Staff.instrumentName = #"Horn in F"
\transposition f
\transpose f c' \hornMusic
}
\new Staff = "Staff_trumpet" {
\set Staff.instrumentName = #"Trumpet in  C"
\trumpetMusic
}
>>
\new RhythmicStaff = "RhythmicStaff_percussion" <<
\set RhythmicStaff.instrumentName = #"Percussion"
\percussionMusic
>>
\new PianoStaff <<
\set PianoStaff.instrumentName = #"Piano"
\new Staff { \pianoRHMusic }
\new Staff { \pianoLHMusic }
>>
\new ChoirStaff = "ChoirStaff_choir" <<
\new Staff = "Staff_soprano" {
\set Staff.instrumentName = #"Soprano"
\new Voice = "soprano"
\sopranoMusic
}
\new Lyrics \lyricsto "soprano" { \sopranoLyrics }
\new GrandStaff = "GrandStaff_altos"
\with { \accepts Lyrics } <<
\new Staff = "Staff_altoI"  {
\set Staff.instrumentName = #"Alto I"
\new Voice = "altoI"
\altoIMusic
}
\new Lyrics \lyricsto "altoI" { \altoILyrics }
\new Staff = "Staff_altoII" {
\set Staff.instrumentName = #"Alto II"
\new Voice = "altoII"
\altoIIMusic
}
\new Lyrics \lyricsto "altoII" { \altoIILyrics }
>>
\new Staff = "Staff_tenor" {
\set Staff.instrumentName = #"Tenor"
\new Voice = "tenor"
\tenorMusic
}
\new Lyrics \lyricsto "tenor" { \tenorLyrics }
>>
\new StaffGroup = "StaffGroup_strings" <<
\new GrandStaff = "GrandStaff_violins" <<
\new Staff = "Staff_violinI" {
\set Staff.instrumentName = #"Violin I"
\violinIMusic
}
\new Staff = "Staff_violinII" {
\set Staff.instrumentName = #"Violin II"
\violinIIMusic
}
>>
\new Staff = "Staff_viola" {
\set Staff.instrumentName = #"Viola"
\violaMusic
}
\new Staff = "Staff_cello" {
\set Staff.instrumentName = #"Cello"
\celloMusic
}
\new Staff = "Staff_bass" {
\set Staff.instrumentName = #"Double Bass"
\bassMusic
}
>>
>>
\layout { }
}


Ottava text

Internally, \ottava sets the properties ottavation (for example, to 8va or 8vb) and middleCPosition. To override the text of the bracket, set ottavation after invoking \ottava.

{
\ottava #1
\set Staff.ottavation = #"8"
c''1
\ottava #0
c'1
\ottava #1
\set Staff.ottavation = #"Text"
c''1
}


Preventing extra naturals from being automatically added

In accordance with standard typesetting rules, a natural sign is printed before a sharp or flat if a previous accidental on the same note needs to be canceled. To change this behavior, set the extraNatural property to f in the Staff context.

\relative c'' {
aeses4 aes ais a
\set Staff.extraNatural = ##f
aeses4 aes ais a
}


Preventing natural signs from being printed when the key signature changes

When the key signature changes, natural signs are automatically printed to cancel any accidentals from previous key signatures. This may be prevented by setting to f the printKeyCancellation property in the Staff context.

\relative c' {
\key d \major
a4 b cis d
\key g \minor
a4 bes c d
\set Staff.printKeyCancellation = ##f
\key d \major
a4 b cis d
\key g \minor
a4 bes c d
}


Quoting another voice with transposition

Quotations take into account the transposition of both source and target. In this example, all instruments play sounding middle C; the target is an instrument in F. The target part may be transposed using \transpose. In this case, all the pitches (including the quoted ones) are transposed.

\addQuote clarinet {
\transposition bes
\repeat unfold 8 { d'16 d' d'8 }
}

\transposition es'
\repeat unfold 16 { a8 }
}

quoteTest = {
% french horn
\transposition f
g'4
<< \quoteDuring #"clarinet" { \skip 4 } s4^"clar." >>
<< \quoteDuring #"sax" { \skip 4 } s4^"sax." >>
g'4
}

{
\set Staff.instrumentName =
\markup {
\center-column { Horn \line { in F } }
}
\quoteTest
\transpose c' d' << \quoteTest s4_"up a tone" >>
}


Separating key cancellations from key signature changes

By default, the accidentals used for key cancellations are placed adjacent to those for key signature changes. This behavior can be changed by overriding the 'break-align-orders property of the BreakAlignment grob.

The value of 'break-align-orders is a vector of length 3, with quoted lists of breakable items as elements. This example only modifies the second list, moving key-cancellation before staff-bar; by modifying the second list, break alignment behavior only changes in the middle of a system, not at the beginning or the end.

\new Staff {
\override Score.BreakAlignment #'break-align-orders =
#'#((left-edge ambitus breathing-sign clef staff-bar
key-cancellation key-signature time-signature custos)

(left-edge ambitus breathing-sign clef key-cancellation
staff-bar key-signature time-signature custos)

(left-edge ambitus breathing-sign clef key-cancellation
key-signature staff-bar time-signature custos))

\key des \major
c'1
\bar "||"
\key bes \major
c'1
}


Transposing pitches with minimum accidentals ("Smart" transpose)

This example uses some Scheme code to enforce enharmonic modifications for notes in order to have the minimum number of accidentals. In this case, the following rules apply:

Double accidentals should be removed

B sharp -> C

E sharp -> F

C flat -> B

F flat -> E

In this manner, the most natural enharmonic notes are chosen.

#(define (naturalize-pitch p)
(let ((o (ly:pitch-octave p))
(a (* 4 (ly:pitch-alteration p)))
;; alteration, a, in quarter tone steps,
;; for historical reasons
(n (ly:pitch-notename p)))
(cond
((and (> a 1) (or (eq? n 6) (eq? n 2)))
(set! a (- a 2))
(set! n (+ n 1)))
((and (< a -1) (or (eq? n 0) (eq? n 3)))
(set! a (+ a 2))
(set! n (- n 1))))
(cond
((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
(if (< n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
(if (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))
(ly:make-pitch o n (/ a 4))))

#(define (naturalize music)
(let ((es (ly:music-property music 'elements))
(e (ly:music-property music 'element))
(p (ly:music-property music 'pitch)))
(if (pair? es)
(ly:music-set-property!
music 'elements
(map (lambda (x) (naturalize x)) es)))
(if (ly:music? e)
(ly:music-set-property!
music 'element
(naturalize e)))
(if (ly:pitch? p)
(begin
(set! p (naturalize-pitch p))
(ly:music-set-property! music 'pitch p)))
music))

naturalizeMusic =
#(define-music-function (parser location m)
(ly:music?)
(naturalize m))

music = \relative c' { c4 d e g }

\score {
\new Staff {
\transpose c ais { \music }
\naturalizeMusic \transpose c ais { \music }
\transpose c deses { \music }
\naturalizeMusic \transpose c deses { \music }
}
\layout { }
}


Tweaking clef properties

The command \clef "treble_8" is equivalent to setting clefGlyph, clefPosition (which controls the vertical position of the clef), middleCPosition and clefOctavation. A clef is printed when any of the properties except middleCPosition are changed.

Note that changing the glyph, the position of the clef, or the octavation does not in itself change the position of subsequent notes on the staff: the position of middle C must also be specified to do this. The positional parameters are relative to the staff center line, positive numbers displacing upwards, counting one for each line and space. The clefOctavation value would normally be set to 7, -7, 15 or -15, but other values are valid.

When a clef change takes place at a line break the new clef symbol is printed at both the end of the previous line and the beginning of the new line by default. If the warning clef at the end of the previous line is not required it can be suppressed by setting the Staff property explicitClefVisibility to the value end-of-line-invisible. The default behavior can be recovered with \unset Staff.explicitClefVisibility.

The following examples show the possibilities when setting these properties manually. On the first line, the manual changes preserve the standard relative positioning of clefs and notes, whereas on the second line, they do not.

\layout { ragged-right = ##t }

{
% The default treble clef
c'1
% The standard bass clef
\set Staff.clefGlyph = #"clefs.F"
\set Staff.clefPosition = #2
\set Staff.middleCPosition = #6
c'1
% The baritone clef
\set Staff.clefGlyph = #"clefs.C"
\set Staff.clefPosition = #4
\set Staff.middleCPosition = #4
c'1
% The standard choral tenor clef
\set Staff.clefGlyph = #"clefs.G"
\set Staff.clefPosition = #-2
\set Staff.clefOctavation = #-7
\set Staff.middleCPosition = #1
c'1
% A non-standard clef
\set Staff.clefPosition = #0
\set Staff.clefOctavation = #0
\set Staff.middleCPosition = #-4
c'1 \break

% The following clef changes do not preserve
% the normal relationship between notes and clefs:

\set Staff.clefGlyph = #"clefs.F"
\set Staff.clefPosition = #2
c'1
\set Staff.clefGlyph = #"clefs.G"
c'1
\set Staff.clefGlyph = #"clefs.C"
c'1
\set Staff.clefOctavation = #7
c'1
\set Staff.clefOctavation = #0
\set Staff.clefPosition = #0
c'1

\set Staff.middleCPosition = #0
c'1
}


Using autochange with more than one voice

Using autochange with more than one voice.

\score
{
\new PianoStaff
<<
\new Staff = "up" {
<<
\set Timing.beamExceptions = #'()
\set Timing.beatStructure = #'(4)
\new Voice {
\voiceOne
\autochange
\relative c' {
g8 a b c d e f g
g,8 a b c d e f g
}
}

\new Voice {
\voiceTwo
\autochange
\relative c' {
g8 a b c d e f g
g,,8 a b c d e f g
}
}
>>
}

\new Staff = "down" {
\clef bass
}
>>
}
`