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