coding style, mostly
[opera_libre.git] / definitions / functions.ly
blob7e4d493f5aaa249838803af5b2e3d5861ef8feb6
1 %------------------------------------------------------------------%
2 % Opéra Libre -- functions.ly %
3 % %
4 % (c) Valentin Villenave, 2008 %
5 % %
6 %------------------------------------------------------------------%
9 %%%%%%%%%%%%%%%%%%%%%%%%% Music Shortcuts %%%%%%%%%%%%%%%%%%%%%%%%%%
11 %% Rhythm shortcuts -----------------------------------------------%
13 t =
14 #(define-music-function (parser location music) (ly:music?)
15 #{ \times 2/3 $music #})
17 tt =
18 #(define-music-function (parser location music) (ly:music?)
19 #{ \times 4/5 $music #})
21 ttt =
22 #(define-music-function (parser location music) (ly:music?)
23 #{ \times 4/6 $music #})
25 tttt =
26 #(define-music-function (parser location music) (ly:music?)
27 #{ \times 4/7 $music #})
30 %% Polyphony shortcuts --------------------------------------------%
32 pl =
33 #(define-music-function (parser location one two) (ly:music? ly:music?)
34 #{ << { \voiceTwo $one } \\ { \voiceOne $two } >> #})
36 parallel=
37 #(define-music-function (parser location droite gauche) (ly:music? ly:music?)
38 #{ <<
39 \context Staff = "droite" $droite
40 \context Staff = "gauche" $gauche
41 >> #})
43 PianoDeuxMains=
44 #(define-music-function (parser location droite gauche) (ly:music? ly:music?)
45 #{ <<
46 \new Staff = "droite" \with { \override VerticalAxisGroup #'remove-empty = ##f }
47 $droite
48 \new Staff = "gauche" \with { \override VerticalAxisGroup #'remove-empty = ##f }
49 $gauche
50 >> #})
52 %% Articulation shortcuts -----------------------------------------%
54 #(define (make-script x)
55 (make-music 'ArticulationEvent
56 'articulation-type x))
58 #(define (add-script m x)
59 (let ( (eventname (ly:music-property m 'name)))
60 (if (equal? eventname 'EventChord)
61 (let ( (elements (ly:music-property m 'elements)) )
62 (if (not (equal? (ly:music-property (car elements)
63 'name) 'RestEvent))
64 (set! (ly:music-property m 'elements)
65 (append elements (list
66 (make-script x)))))))
67 m))
69 #(define (double-script m t tt)
70 (add-script (add-script m t) tt))
72 st =
73 #(define-music-function (parser location music)
74 (ly:music?)
75 (define (make-script-music m)
76 (add-script m "staccato"))
77 (music-map make-script-music music))
79 acc =
80 #(define-music-function (parser location music)
81 (ly:music?)
82 (define (make-script-music m)
83 (add-script m "accent"))
84 (music-map make-script-music music))
86 det =
87 #(define-music-function (parser location music)
88 (ly:music?)
89 (define (make-script-music m)
90 (add-script m "tenuto"))
91 (music-map make-script-music music))
93 marc =
94 #(define-music-function (parser location music)
95 (ly:music?)
96 (define (make-script-music m)
97 (add-script m "marcato"))
98 (music-map make-script-music music))
100 stdet =
101 #(define-music-function (parser location music)
102 (ly:music?)
103 (define (make-script-music m)
104 (add-script m "portato"))
105 (music-map make-script-music music))
107 accdet =
108 #(define-music-function (parser location music)
109 (ly:music?)
110 (define (make-script-music m)
111 (double-script m "tenuto" "accent"))
112 (music-map make-script-music music))
114 marcdet =
115 #(define-music-function (parser location music)
116 (ly:music?)
117 (define (make-script-music m)
118 (double-script m "tenuto" "marcato"))
119 (music-map make-script-music music))
121 accst =
122 #(define-music-function (parser location music)
123 (ly:music?)
124 (define (make-script-music m)
125 (double-script m "accent" "staccato"))
126 (music-map make-script-music music))
128 marcst =
129 #(define-music-function (parser location music)
130 (ly:music?)
131 (define (make-script-music m)
132 (double-script m "marcato" "staccato"))
133 (music-map make-script-music music))
136 CaV=
137 #(let ((m (make-music 'ArticulationEvent
138 'articulation-type "flageolet")))
139 (set! (ly:music-property m 'tweaks)
140 (acons 'font-size -3
141 (ly:music-property m 'tweaks)))
145 %% Music shortcuts ------------------------------------------------%
147 #(define (octave-up noteevent)
148 (let* ((pitch (ly:music-property noteevent 'pitch))
149 (octave (ly:pitch-octave pitch))
150 (note (ly:pitch-notename pitch))
151 (alteration (ly:pitch-alteration pitch))
152 (duration (ly:music-property noteevent 'duration))
153 (newnoteevent
154 (make-music 'NoteEvent
155 'duration duration
156 'pitch (ly:make-pitch (1- octave) note alteration))))
157 newnoteevent))
159 #(define (octavize-chord elements)
160 (cond ((null? elements) elements)
161 ((eq? (ly:music-property (car elements) 'name) 'NoteEvent)
162 (cons (car elements)
163 (cons (octave-up (car elements))
164 (octavize-chord (cdr elements)))))
165 (else (cons (car elements) (octavize-chord (cdr elements))))))
167 #(define (octavize music)
168 (let* ((es (ly:music-property music 'elements))
169 (e (ly:music-property music 'element))
170 (name (ly:music-property music 'name)))
171 (cond ((eq? name 'EventChord)
172 (ly:music-set-property! music 'elements (octavize-chord es)))
173 ((pair? es)
174 (for-each (lambda(x) (octavize x)) es))
175 ((ly:music? e)
176 (octavize e))))
177 music)
179 oct = #(define-music-function (parser location mus) (ly:music?)
180 (octavize mus))
182 %%%%%%%%%%%%%%%%%%%%%%%% Layout Functions %%%%%%%%%%%%%%%%%%%%%%%%%%
184 %% Music layout ---------------------------------------------------%
186 droite = { \change Staff = "droite" }
188 gauche = { \change Staff = "gauche" }
190 %% Music formatting -----------------------------------------------%
192 graceNotes =
193 #(define-music-function (parser location notes) (ly:music?)
194 #{ \tiny $notes \normalsize #})
196 parlato =
197 #(define-music-function (parser location notes) (ly:music?)
198 #{ \override NoteHead #'style = #'cross
199 $notes
200 \revert NoteHead #'style #})
202 %%%%%%%%%%%%%%%%%%%%%%%%%% In-score Text %%%%%%%%%%%%%%%%%%%%%%%%%%%
204 %% Expressive indications -----------------------------------------%
206 #(define-markup-command (indic layout props arg) (markup?)
207 (interpret-markup layout props
208 (markup #:whiteout #:small #:italic arg)))
210 cmb =
211 #(define-music-function (parser location nuance texte )
212 (string? string? )
213 (make-dynamic-script
214 (markup #:dynamic nuance
215 #:hspace .6
216 #:text #:medium #:upright texte )))
219 ten =
220 #(define-music-function (parser location music) (ly:music?)
222 (equal? (ly:music-property music 'name) 'EventChord)
223 (set! (ly:music-property music 'elements)
224 (append (ly:music-property music 'elements)
225 (list (make-music 'TextScriptEvent 'text
226 (markup #:translate (cons 4 0)
227 #:indic "(ten.)"))))))
228 music)
230 ind =
231 #(define-music-function (parser location text music) (string? ly:music?)
233 (equal? (ly:music-property music 'name) 'EventChord)
234 (set! (ly:music-property music 'elements)
235 (append (ly:music-property music 'elements)
236 (list (make-music 'TextScriptEvent 'direction 1
237 'text (markup #:indic text))))))
238 music)
240 #(define (make-text-span music t)
241 (set! (ly:music-property music 'elements)
242 (append (ly:music-property music 'elements)
243 (list (make-music 'TextSpanEvent
244 'span-direction t))))
245 music)
246 startTxt =
247 #(define-music-function (parser location texte music ) (string? ly:music?)
248 #{ \override TextSpanner #'bound-details #'left #'text =
249 \markup { \bold $texte }
250 $(make-text-span music -1)#})
252 stopTxt =
253 #(define-music-function (parser location music ) (ly:music?)
254 (make-text-span music 1))
256 %% Tempo indications ----------------------------------------------%
258 #(define-markup-command (mvt layout props arg) (markup?)
259 (interpret-markup layout props
260 (markup #:huge #:bold arg)))
262 #(define ((make-format-movement-markup-function text) duration count context)
263 (markup #:mvt text #:hspace 1
265 #:general-align Y DOWN #:smaller
266 #:note-by-number (ly:duration-log duration)
267 (ly:duration-dot-count duration)
270 (number->string count)
271 ")"))
274 #(define (string->duration duration-string)
275 "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a duration object."
276 (let* ((length (string-length duration-string))
277 (dot-index (or (string-index duration-string #\.) length))
278 (len (substring duration-string 0 dot-index))
279 (dots (- length dot-index)))
280 (ly:make-duration (cond ((string=? len "breve") -1)
281 ((string=? len "longa") -2)
282 ((string=? len "maxima") -3)
283 (else (log2 (string->number len))))
284 dots 1 1)))
286 mouv =
287 #(define-music-function (parser location text duration count music)
288 (string? string? integer? ly:music?)
290 \set Score.metronomeMarkFormatter = #(make-format-movement-markup-function $text)
291 \set Score.tempoWholesPerMinute = #$(ly:moment-mul (ly:make-moment count 1)
292 (ly:duration-length
293 (string->duration duration)))
294 \set Score.tempoUnitDuration = #$(string->duration duration)
295 \set Score.tempoUnitCount = #$count
296 $music
297 \set Score.metronomeMarkFormatter = #format-metronome-markup
301 %%%%%%%%%%%%%%%%%%%%%%%%%% Text Functions %%%%%%%%%%%%%%%%%%%%%%%%%%
303 %% Lyrics formatting ----------------------------------------------%
305 freestyleOn = {
306 \override Lyrics . LyricExtender #'stencil = ##f }
308 freestyleOff = {
309 \revert Lyrics . LyricExtender #'stencil }
311 leftSyl = {
312 \once \override LyricText #'self-alignment-X = #0.9 }
314 dash = {
315 \once \override LyricHyphen #'minimum-distance = #4
316 \once \override LyricHyphen #'length = #2
317 \once \override LyricHyphen #'thickness = #1.2
320 ital = {
321 \once \override LyricText #'font-shape = #'italic }
323 %% Scenography formatting ---------------------------------------%
325 #(define-markup-command (did layout props text) (markup?)
326 (interpret-markup layout props
327 (markup #:override '(line-width . 40)
328 #:override '(box-padding . 1)
329 #:override '(corner-radius . 2)
330 #:rounded-box #:sans #:italic #:small #:justify-string text)))
332 #(define-markup-command (init-did layout props text) (markup?)
333 (interpret-markup layout props
334 (markup
335 ; #:override (cons 'line-width (* 1 (chain-assoc-get 'line-width props)))
336 #:fill-line (
337 #:override '(line-width . 50)
338 #:override '(box-padding . 1.5)
339 #:override '(corner-radius . 2)
340 #:rounded-box #:sans #:italic #:small #:justify-string text))))
342 #(define-markup-command (vspace layout props amount) (number?)
343 (let ((amount (* amount 3.0)))
344 (if (> amount 0)
345 (ly:make-stencil "" (cons -1 1) (cons 0 amount))
346 (ly:make-stencil "" (cons -1 1) (cons amount amount)))))