]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / scm / translation-functions.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; (c) 1998--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; metronome marks
22
23 (define-public (format-metronome-markup text dur count context)
24   (let* ((hide-note (eq? #t (ly:context-property context 'tempoHideNote))))
25     (metronome-markup text dur count hide-note)))
26
27 (define-public (metronome-markup text dur count hide-note)
28   (let* ((note-mark (if (and (not hide-note) (ly:duration? dur))
29                         (make-smaller-markup
30                          (make-note-by-number-markup (ly:duration-log dur)
31                                                      (ly:duration-dot-count dur)
32                                                      1))
33                         #f))
34          (note-markup (if (and (not hide-note) (number? count) (> count 0) )
35                           (make-concat-markup
36                            (list
37                             (make-general-align-markup Y DOWN note-mark)
38                             (make-simple-markup " ")
39                             (make-simple-markup "=")
40                             (make-simple-markup " ")
41                             (make-simple-markup (number->string count))))
42                           #f))
43          (text-markup (if (not (null? text))
44                           (make-bold-markup text)
45                           #f)))
46     (if text-markup
47         (if (and note-markup (not hide-note))
48             (make-line-markup (list text-markup
49                                     (make-concat-markup
50                                      (list (make-simple-markup "(")
51                                            note-markup
52                                            (make-simple-markup ")")))))
53             (make-line-markup (list text-markup)))
54         (if note-markup
55             (make-line-markup (list note-markup))
56             (make-null-markup)))))
57
58 (define-public (format-mark-alphabet mark context)
59   (make-bold-markup (make-markalphabet-markup (1- mark))))
60
61 (define-public (format-mark-box-alphabet mark context)
62   (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
63
64 (define-public (format-mark-circle-alphabet mark context)
65   (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
66
67 (define-public (format-mark-letters mark context)
68   (make-bold-markup (make-markletter-markup (1- mark))))
69
70 (define-public (format-mark-numbers mark context)
71   (make-bold-markup (number->string mark)))
72
73 (define-public (format-mark-barnumbers mark context)
74   (make-bold-markup (number->string (ly:context-property context
75                                                          'currentBarNumber))))
76
77 (define-public (format-mark-box-letters mark context)
78   (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
79
80 (define-public (format-mark-circle-letters mark context)
81   (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
82
83 (define-public (format-mark-box-numbers mark context)
84   (make-bold-markup (make-box-markup (number->string mark))))
85
86 (define-public (format-mark-circle-numbers mark context)
87   (make-bold-markup (make-circle-markup (number->string mark))))
88
89 (define-public (format-mark-box-barnumbers mark context)
90   (make-bold-markup (make-box-markup
91                      (number->string (ly:context-property context
92                                                           'currentBarNumber)))))
93
94 (define-public (format-mark-circle-barnumbers mark context)
95   (make-bold-markup (make-circle-markup
96                      (number->string (ly:context-property context
97                                                           'currentBarNumber)))))
98
99
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;; Bass figures.
102
103 (define-public (format-bass-figure figure event context)
104   (let* ((fig (ly:event-property event 'figure))
105          (fig-markup (if (number? figure)
106
107                          ;; this is not very elegant, but center-aligning
108                          ;; all digits is problematic with other markups,
109                          ;; and shows problems in the (lack of) overshoot
110                          ;; of feta-alphabet glyphs.
111                          ((if (<= 10 figure)
112                               (lambda (y) (make-translate-scaled-markup
113                                            (cons -0.7 0) y))
114                               identity)
115
116                           (cond
117                            ((eq? #t (ly:event-property event 'diminished))
118                             (markup #:slashed-digit figure))
119                            ((eq? #t (ly:event-property event 'augmented-slash))
120                             (markup #:backslashed-digit figure))
121                            (else (markup #:number (number->string figure 10)))))
122                          #f))
123
124          (alt (ly:event-property event 'alteration))
125          (alt-markup
126           (if (number? alt)
127               (markup
128                #:general-align Y DOWN #:fontsize
129                (if (not (= alt DOUBLE-SHARP))
130                    -2 2)
131                (alteration->text-accidental-markup alt))
132               #f))
133
134          (plus-markup (if (eq? #t (ly:event-property event 'augmented))
135                           (markup #:number "+")
136                           #f))
137
138          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
139          (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
140
141     (if (and (not fig-markup) alt-markup)
142         (begin
143           (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
144           (set! alt-markup #f)))
145
146
147     ;; hmm, how to get figures centered between note, and
148     ;; lone accidentals too?
149
150     ;;    (if (markup? fig-markup)
151     ;;  (set!
152     ;;   fig-markup (markup #:translate (cons 1.0 0)
153     ;;                      #:center-align fig-markup)))
154
155     (if alt-markup
156         (set! fig-markup
157               (markup #:put-adjacent
158                       X (if (number? alt-dir)
159                             alt-dir
160                             LEFT)
161                       fig-markup
162                       #:pad-x 0.2 alt-markup)))
163
164     (if plus-markup
165         (set! fig-markup
166               (if fig-markup
167                   (markup #:put-adjacent
168                           X (if (number? plus-dir)
169                                 plus-dir
170                                 LEFT)
171                           fig-markup
172                           #:pad-x 0.2 plus-markup)
173                   plus-markup)))
174
175     (if (markup? fig-markup)
176         (markup #:fontsize -2 fig-markup)
177         empty-markup)))
178
179
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;; fret diagrams
182
183 (define-public (determine-frets context grob notes string-numbers)
184
185   (define (ensure-number a b)
186     (if (number? a)
187         a
188         b))
189
190   (define (string-frets->dot-placement string-frets string-count)
191     (let* ((desc (list->vector
192                   (map (lambda (x) (list 'mute  (1+ x)))
193                        (iota string-count)))))
194
195       (for-each (lambda (sf)
196                   (let* ((string (car sf))
197                          (fret (cadr sf))
198                          (finger (caddr sf)))
199
200                     (vector-set!
201                      desc (1- string)
202                      (if (= 0 fret)
203                          (list 'open string)
204                          (if finger
205                              (list 'place-fret string fret finger)
206                              (list 'place-fret string fret))))))
207                 string-frets)
208
209       (vector->list desc)))
210
211   (define (get-predefined-fretboard predefined-fret-table tuning pitches)
212 ;   (_i "Search through @var{predefined-fret-table} looking for a predefined
213 ;fretboard with a key of @var{(tuning . pitches)}.  The search will check
214 ;both up and down an octave in order to accomodate transposition of the
215 ;chords.")
216     (define (get-fretboard key)
217       (let ((hash-handle
218              (hash-get-handle predefined-fret-table key)))
219         (if hash-handle
220             (cdr hash-handle)  ; return table entry
221             '())))
222
223     (let ((test-fretboard (get-fretboard (cons tuning pitches))))
224       (if (not (null? test-fretboard))
225           test-fretboard
226           (let ((test-fretboard
227                  (get-fretboard
228                   (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
229             (if (not (null? test-fretboard))
230                 test-fretboard
231                 (get-fretboard
232                  (cons tuning (map (lambda (x) (shift-octave x -1))
233                                    pitches))))))))
234
235 ;; body.
236   (let* ((tunings (ly:context-property context 'stringTunings))
237          (my-string-count (length tunings))
238          (details (ly:grob-property grob 'fret-diagram-details))
239          (predefined-frets
240           (ly:context-property context 'predefinedDiagramTable))
241          (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
242          (predefined-fretboard
243           (if predefined-frets
244               (get-predefined-fretboard
245                predefined-frets
246                tunings
247                pitches)
248               '())))
249
250     (set! (ly:grob-property grob 'fret-diagram-details)
251           (if (null? details)
252               (acons 'string-count my-string-count '())
253               (acons 'string-count my-string-count details)))
254
255     (set! (ly:grob-property grob 'dot-placement-list)
256           (if (not (null? predefined-fretboard))
257               predefined-fretboard
258               (let* ((minimum-fret
259                       (ensure-number
260                        (ly:context-property context 'minimumFret)
261                        0))
262                      (max-stretch
263                       (ensure-number
264                        (ly:context-property context 'maximumFretStretch)
265                        4))
266                      (string-frets
267                       (determine-frets-mf
268                        notes
269                        string-numbers
270                        minimum-fret
271                        max-stretch
272                        tunings)))
273                 (string-frets->dot-placement
274                   string-frets
275                   my-string-count))))))
276
277 (define-public (determine-frets-mf notes string-numbers
278                                    minimum-fret max-stretch
279                                    tunings)
280
281   (define (calc-fret pitch string tuning)
282     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
283
284   (define (note-pitch a)
285     (ly:event-property a 'pitch))
286
287   (define (note-pitch>? a b)
288     (ly:pitch<? (note-pitch b)
289                 (note-pitch a)))
290
291   (define (note-finger ev)
292     (let* ((articulations (ly:event-property ev 'articulations))
293            (finger-found #f))
294
295       (map (lambda (art)
296              (let* ((num (ly:event-property art 'digit)))
297
298                (if (and (eq? 'fingering-event (ly:event-property art 'class))
299                         (number? num))
300                    (set! finger-found num))))
301            articulations)
302
303       finger-found))
304
305   (define (note-string ev)
306     (let* ((articulations (ly:event-property ev 'articulations))
307            (string-found #f))
308
309       (map (lambda (art)
310              (let* ((num (ly:event-property art 'string-number)))
311
312                (if (number? num)
313                    (set! string-found num))))
314            articulations)
315
316       string-found))
317
318   (define (del-string string)
319     (if (number? string)
320         (set! free-strings
321               (delete string free-strings))))
322
323   (define specified-frets '())
324   (define free-strings '())
325
326   (define (close-enough fret)
327     (reduce
328      (lambda (x y)
329        (and x y))
330      #t
331      (map (lambda (specced-fret)
332             (> max-stretch (abs (- fret specced-fret))))
333           specified-frets)))
334
335   (define (string-qualifies string pitch)
336     (let* ((fret (calc-fret pitch string tunings)))
337       (and (>= fret minimum-fret)
338            (close-enough fret))))
339
340   (define string-fret-fingering-tuples '())
341   (define (set-fret note string)
342     (set! string-fret-fingering-tuples
343           (cons (list string
344                       (calc-fret (ly:event-property note 'pitch)
345                                  string tunings)
346                       (note-finger note))
347                 string-fret-fingering-tuples))
348     (del-string string))
349
350
351   ;;; body.
352   (set! specified-frets
353         (filter identity (map
354                           (lambda (note)
355                             (if (note-string note)
356                                 (calc-fret (note-pitch note)
357                                            (note-string note) tunings)
358                                 #f))
359                           notes)))
360
361   (set! free-strings (map 1+ (iota (length tunings))))
362
363   (for-each (lambda (note)
364               (del-string (note-string note)))
365             notes)
366
367
368   (for-each
369    (lambda (note)
370      (if (note-string note)
371          (set-fret note (note-string note))
372          (let* ((fit-string (find (lambda (string)
373                                     (string-qualifies string (note-pitch note)))
374                                   free-strings)))
375            (if fit-string
376                (set-fret note fit-string)
377                (ly:warning "No string for pitch ~a (given frets ~a)"
378                            (note-pitch note)
379                            specified-frets)))))
380    (sort notes note-pitch>?))
381
382   string-fret-fingering-tuples)
383
384
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; tablature
387
388 ;; The TabNoteHead tablatureFormat callback.
389 ;; Compute the text grob-property
390 (define-public (fret-number-tablature-format string context event)
391   (let* ((tuning (ly:context-property context 'stringTunings))
392          (pitch (ly:event-property event 'pitch)))
393
394     (make-whiteout-markup
395      (make-vcenter-markup
396       (format
397        "~a"
398        (- (ly:pitch-semitones pitch)
399           (list-ref tuning
400                     ;; remove 1 because list index starts at 0
401                     ;;and guitar string at 1.
402                     (1- string))))))))
403
404 ;; The 5-string banjo has got a extra string, the fifth (duh), which
405 ;; starts at the fifth fret on the neck.  Frets on the fifth string
406 ;; are referred to relative to the other frets:
407 ;;   the "first fret" on the fifth string is really the sixth fret
408 ;;   on the banjo neck.
409 ;; We solve this by defining a new fret-number-tablature function:
410 (define-public (fret-number-tablature-format-banjo string context event)
411   (let* ((tuning (ly:context-property context 'stringTunings))
412          (pitch (ly:event-property event 'pitch)))
413
414     (make-whiteout-markup
415      (make-vcenter-markup
416       (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (1- string)))))
417         (number->string (cond
418                          ((and (> fret 0) (= string 5))
419                           (+ fret 5))
420                          (else fret))))))))
421
422
423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424 ;; bar numbers
425
426 (define-public ((every-nth-bar-number-visible n) barnum)
427   (= 0 (modulo barnum n)))
428
429 (define-public ((modulo-bar-number-visible n m) barnum)
430   (and (> barnum 1) (= m (modulo barnum n))))
431
432 (define-public ((set-bar-number-visibility n) tr)
433   (let ((bn (ly:context-property tr 'currentBarNumber)))
434     (ly:context-set-property! tr 'barNumberVisibility
435                               (modulo-bar-number-visible n (modulo bn n)))))
436
437 (define-public (first-bar-number-invisible barnum) (> barnum 1))
438
439 (define-public (all-bar-numbers-visible barnum) #t)
440
441
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 ;; percent repeat counters
444
445 (define-public ((every-nth-repeat-count-visible n) count context)
446   (= 0 (modulo count n)))
447
448 (define-public (all-repeat-counts-visible count context) #t)