]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
Merge master into nested-bookparts
[lilypond.git] / scm / translation-functions.scm
1 ;;;; translation-functions.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
7
8 ;; metronome marks
9 (define-public (format-metronome-markup text dur count context)
10   (let* ((hide-note (eq? #t (ly:context-property context 'tempoHideNote)))
11          (note-mark (if (and (not hide-note) (ly:duration? dur))
12                       (make-smaller-markup
13                        (make-note-by-number-markup (ly:duration-log dur)
14                                                    (ly:duration-dot-count dur)
15                                                    1))
16                       (make-null-markup)))
17          (note-markup (if (and (not hide-note) (number? count) (> count 0) )
18                         (make-concat-markup (list
19                           (make-general-align-markup Y DOWN note-mark)
20                           (make-simple-markup " ")
21                           (make-simple-markup  "=")
22                           (make-simple-markup " ")
23                           (make-simple-markup (number->string count))))
24                       #f))
25          (text-markup (if (not (null? text))
26                         (make-bold-markup text)
27                         #f)))
28     (if text-markup
29       (if (and note-markup (not hide-note))
30         (make-line-markup (list text-markup
31           (make-concat-markup (list (make-simple-markup "(")
32                                     note-markup
33                                     (make-simple-markup ")")))))
34         (make-line-markup (list text-markup)))
35       (if note-markup
36         (make-line-markup (list note-markup))
37         (make-null-markup)))))
38
39 (define-public (format-mark-alphabet mark context)
40   (make-bold-markup (make-markalphabet-markup (1- mark))))
41
42 (define-public (format-mark-box-alphabet mark context)
43   (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
44
45 (define-public (format-mark-circle-alphabet mark context)
46   (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
47
48 (define-public (format-mark-letters mark context)
49   (make-bold-markup (make-markletter-markup (1- mark))))
50
51 (define-public (format-mark-numbers mark context)
52   (make-bold-markup (number->string mark)))
53
54 (define-public (format-mark-barnumbers mark context)
55   (make-bold-markup (number->string (ly:context-property context 'currentBarNumber))))
56
57 (define-public (format-mark-box-letters mark context)
58   (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
59
60 (define-public (format-mark-circle-letters mark context)
61   (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
62
63 (define-public (format-mark-box-numbers mark context)
64   (make-bold-markup (make-box-markup (number->string mark))))
65
66 (define-public (format-mark-circle-numbers mark context)
67   (make-bold-markup (make-circle-markup (number->string mark))))
68
69 (define-public (format-mark-box-barnumbers mark context)
70   (make-bold-markup (make-box-markup
71     (number->string (ly:context-property context 'currentBarNumber)))))
72
73 (define-public (format-mark-circle-barnumbers mark context)
74   (make-bold-markup (make-circle-markup
75     (number->string (ly:context-property context 'currentBarNumber)))))
76
77
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 ;; Bass figures.
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81
82 (define-public (format-bass-figure figure event context)
83   (let* ((fig (ly:event-property event 'figure))
84          (fig-markup (if (number? figure)
85
86                          ;; this is not very elegant, but center-aligning all digits
87                          ;; is problematic with other markups, and shows problems
88                          ;; in the (lack of) overshoot of feta alphabet glyphs.
89                          
90                          ((if (<= 10 figure)
91                               (lambda (y) (make-translate-scaled-markup (cons -0.7 0) y))
92                               identity)
93
94                           (cond
95                                 ((eq? #t (ly:event-property event 'diminished))
96                                          (markup #:slashed-digit figure))
97                                 ((eq? #t (ly:event-property event 'augmented-slash))
98                                          (markup #:backslashed-digit figure))
99                                 (else (markup #:number (number->string figure 10)))))
100                          #f
101                          ))
102          (alt (ly:event-property event 'alteration))
103          (alt-markup
104           (if (number? alt)
105               (markup
106                #:general-align Y DOWN #:fontsize
107                (if (not (= alt DOUBLE-SHARP))
108                    -2 2)
109                (alteration->text-accidental-markup alt))
110               
111               #f))
112          (plus-markup (if (eq? #t (ly:event-property event 'augmented))
113                           (markup #:number "+")
114                           #f))
115
116          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
117          (plus-dir (ly:context-property context 'figuredBassPlusDirection))
118          )
119
120     (if (and (not fig-markup) alt-markup)
121         (begin
122           (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
123           (set! alt-markup #f)))
124
125
126     ;; hmm, how to get figures centered between note, and
127     ;; lone accidentals too?
128     
129     ;;    (if (markup? fig-markup)
130     ;;  (set!
131     ;;   fig-markup (markup #:translate (cons 1.0 0)
132     ;;                      #:center-align fig-markup)))
133
134     (if alt-markup
135         (set! fig-markup
136               (markup #:put-adjacent
137                       X (if (number? alt-dir)
138                             alt-dir
139                             LEFT)
140                       fig-markup
141                       #:pad-x 0.2 alt-markup
142                       )))
143
144     
145     (if plus-markup
146         (set! fig-markup
147               (if fig-markup
148                   (markup #:put-adjacent
149                           X (if (number? plus-dir)
150                                 plus-dir
151                                 LEFT)
152                           fig-markup
153                           #:pad-x 0.2 plus-markup)
154                   plus-markup)))
155     
156     (if (markup? fig-markup)
157         (markup #:fontsize -2 fig-markup)
158         empty-markup)
159
160     ))
161
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163 ;; fret diagrams
164
165 (define-public (determine-frets context grob notes string-numbers)
166   
167   (define (ensure-number a b)
168     (if (number? a)
169         a
170         b))
171
172   (define (string-frets->dot-placement string-frets string-count)
173     (let*
174       ((desc (list->vector
175               (map (lambda (x) (list 'mute  (1+ x)))
176                    (iota string-count)))))
177
178        (for-each (lambda (sf)
179                    (let*
180                        ((string (car sf))
181                         (fret (cadr sf))
182                         (finger (caddr sf)))
183
184                        (vector-set! 
185                          desc (1- string)
186                          (if (= 0 fret)
187                              (list 'open string)
188                              (if finger
189                                  (list 'place-fret string fret finger)
190                                  (list 'place-fret string fret))
191                                       ))
192                      ))
193                  string-frets)
194        (vector->list desc)))
195
196 ;; body.
197   (let*
198       ((tunings (ly:context-property context 'stringTunings))
199        (my-string-count (length tunings))
200        (details (ly:grob-property grob 'fret-diagram-details))
201        (predefined-frets
202          (ly:context-property context 'predefinedDiagramTable)) 
203        (minimum-fret (ensure-number
204                       (ly:context-property context 'minimumFret) 0))
205        (max-stretch (ensure-number
206                       (ly:context-property context 'maximumFretStretch) 4))
207        (string-frets (determine-frets-mf notes string-numbers
208                                          minimum-fret max-stretch
209                                          tunings))
210        (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes)))
211
212     (set! (ly:grob-property grob 'fret-diagram-details)
213
214           (if (null? details)
215               (acons 'string-count (length tunings) '())
216               (acons 'string-count (length tunings) details)))
217     (set! (ly:grob-property grob 'dot-placement-list)
218         (if predefined-frets
219             (let ((hash-handle 
220                     (hash-get-handle
221                       predefined-frets
222                       (cons tunings pitches))))
223               (if hash-handle 
224                   (cdr hash-handle)  ;found default diagram
225                   (string-frets->dot-placement 
226                         string-frets my-string-count)))
227             (string-frets->dot-placement string-frets my-string-count)))))
228
229 (define-public (determine-frets-mf notes string-numbers
230                                    minimum-fret max-stretch
231                                    tunings)
232
233   (define (calc-fret pitch string tuning)
234     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
235
236   (define (note-pitch a)
237     (ly:event-property a 'pitch))
238
239   (define (note-pitch<? a b)
240     (ly:pitch<? (note-pitch a)
241                 (note-pitch b)))
242
243   (define (note-finger ev)
244     (let* ((articulations (ly:event-property ev 'articulations))
245            (finger-found #f))
246
247       (map (lambda (art)
248              (let*
249                  ((num (ly:event-property art 'digit)))
250
251                (if (and (eq? 'fingering-event (ly:event-property art 'class))
252                         (number? num))
253                    (set! finger-found num))))
254            articulations)
255
256       finger-found))
257   
258   (define (note-string ev)
259     (let* ((articulations (ly:event-property ev 'articulations))
260            (string-found #f))
261
262       (map (lambda (art)
263              (let*
264                  ((num (ly:event-property art 'string-number)))
265
266                (if (number? num)
267                    (set! string-found num))))
268            articulations)
269
270       string-found))
271
272   (define (del-string string)
273                       (if (number? string)
274                           (set! free-strings
275                                 (delete string free-strings))))
276   (define specified-frets '())
277   (define free-strings '())
278   
279   (define (close-enough fret)
280                        (reduce
281                         (lambda (x y)
282                           (and x y))
283                         #t
284                         (map (lambda (specced-fret)
285                                (> max-stretch (abs (- fret specced-fret))))
286                              specified-frets)
287                         ))
288   
289   (define (string-qualifies string pitch)
290     (let*
291         ((fret (calc-fret pitch string tunings)))
292          
293          (and (>= fret minimum-fret)
294               (close-enough fret))
295          
296          ))
297                            
298   (define string-fret-fingering-tuples '())
299   (define (set-fret note string)
300                     (set! string-fret-fingering-tuples
301                           (cons (list string
302                                       (calc-fret (ly:event-property note 'pitch)
303                                                  string tunings)
304                                       (note-finger note))
305                                 string-fret-fingering-tuples))
306                     (del-string string))
307        
308
309   ;;; body.
310   (set! specified-frets
311         (filter identity (map
312                       (lambda (note)
313                         (if (note-string note)
314                             (calc-fret (note-pitch note)
315                                        (note-string note) tunings)
316                             #f))
317                       notes)))
318
319
320   (set! free-strings (map 1+ (iota (length tunings))))
321     
322   (for-each (lambda (note)
323               (del-string (note-string note)))
324             notes)
325   
326   
327   (for-each
328    (lambda (note)
329      (if (note-string note)
330          (set-fret note (note-string note))
331          (let*
332              ((fit-string (find (lambda (string) 
333                                (string-qualifies string (note-pitch note)))
334                             free-strings)))
335            (if fit-string
336                (set-fret note fit-string)
337                (ly:warning "No string for pitch ~a (given frets ~a)" 
338                            (note-pitch note)
339                            specified-frets))
340                            
341                )))
342    (sort notes note-pitch<?))
343
344   string-fret-fingering-tuples)