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