| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Where tweaks need to be reused with different music expressions, it is often convenient to make the tweak part of a music function. In this section, we discuss only substitution functions, where the object is to substitute a variable into a piece of LilyPond input code. Other more complex functions are described in (extending)Music functions section ‘Music functions’ in Extending.
| 5.6.1 Substitution function syntax | ||
| 5.6.2 Substitution function examples |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Making a function that substitutes a variable into LilyPond code is easy. The general form of these functions is
function =
#(define-music-function
(arg1 arg2 …)
(type1? type2? …)
#{
…music…
#})
|
where
argN | nth argument |
typeN? | a scheme type predicate for which argN
must return #t. |
…music… | normal LilyPond input, using $ (in places where only
LilyPond constructs are allowed) or # (to use it as a Scheme
value or music function argument or music inside of music lists) to
reference arguments
(eg. ‘#arg1’). |
The list of type predicates is required. Some of the most common type predicates used in music functions are:
boolean? cheap-list? (use instead of ‘list?’ for faster processing) ly:duration? ly:music? ly:pitch? markup? number? pair? string? symbol? |
For a list of available type predicates, see Predefined type predicates. User-defined type predicates are also allowed.
Notation Reference: Predefined type predicates.
Extending LilyPond: (extending)Music functions section ‘Music functions’ in Extending.
Installed Files: ‘lily/music-scheme.cc’, ‘scm/c++.scm’, ‘scm/lily.scm’.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section introduces some substitution function examples. These are not intended to be exhaustive, but rather to demonstrate some of the possibilities of simple substitution functions.
In the first example, a function is defined that simplifies setting the padding of a TextScript:
padText = #(define-music-function (padding) (number?) #{ \once \override TextScript.padding = #padding #}) \relative { c''4^"piu mosso" b a b \padText #1.8 c4^"piu mosso" b a b \padText #2.6 c4^"piu mosso" b a b }
In addition to numbers, we can use music expressions such as notes for arguments to music functions:
custosNote = #(define-music-function (note) (ly:music?) #{ \tweak NoteHead.stencil #ly:text-interface::print \tweak NoteHead.text \markup \musicglyph "custodes.mensural.u0" \tweak Stem.stencil ##f #note #}) \relative { c'4 d e f \custosNote g }
Both of those functions are simple single expressions where only
the last element of a function call or override is missing. For
those particular function definitions, there is a simpler
alternative syntax, namely just writing out the constant part of
the expression and replacing its final missing element with
\etc:
padText = \once \override TextScript.padding = \etc \relative { c''4^"piu mosso" b a b \padText #1.8 c4^"piu mosso" b a b \padText #2.6 c4^"piu mosso" b a b }
custosNote = \tweak NoteHead.stencil #ly:text-interface::print \tweak NoteHead.text \markup \musicglyph "custodes.mensural.u0" \tweak Stem.stencil ##f \etc \relative { c'4 d e f \custosNote g }
Substitution functions with multiple arguments can be defined:
tempoPadded = #(define-music-function (padding tempotext) (number? markup?) #{ \once \override Score.MetronomeMark.padding = #padding \tempo \markup { \bold #tempotext } #}) \relative { \tempo \markup { "Low tempo" } c''4 d e f g1 \tempoPadded #4.0 "High tempo" g4 f e d c1 }
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated by gub on July 13, 2020 using texi2html 1.82.
Outros idiomas: català, deutsch, español, français, italiano, 日本語.
Sobre seleção automática de idioma.