]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
Merge branch 'lilypond/translation' of ssh://trettig@git.sv.gnu.org/srv/git/lilypond...
[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     ;;                      #:hcenter fig-markup)))
138
139     (if alt-markup
140         (set! fig-markup
141               (markup #:put-adjacent
142                       fig-markup X
143                       (if (number? alt-dir)
144                           alt-dir
145                           LEFT)
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                           fig-markup
155                           X (if (number? plus-dir)
156                                 plus-dir
157                                 LEFT)
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   (define (ensure-number a b)
172     (if (number? a)
173         a
174         b))
175 (let*
176       ((tunings (ly:context-property context 'stringTunings))
177        (minimum-fret (ensure-number
178                       (ly:context-property context 'minimumFret) 0))
179        (max-stretch (ensure-number
180                       (ly:context-property context 'maximumFretStretch) 4))
181        (string-frets (determine-frets-mf notes string-numbers
182                                          minimum-fret max-stretch
183                                          tunings)))
184
185               
186   (set! (ly:grob-property grob 'string-count) (length tunings))
187   (set! (ly:grob-property grob 'string-fret-finger-combinations) string-frets)
188
189   ))
190
191 (define-public (determine-frets-mf notes string-numbers
192                                    minimum-fret max-stretch
193                                    tunings)
194
195   (define (calc-fret pitch string tuning)
196     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
197
198   (define (note-pitch a)
199     (ly:event-property a 'pitch))
200
201   (define (note-pitch<? a b)
202     (ly:pitch<? (note-pitch a)
203                 (note-pitch b)))
204
205   (define (note-finger ev)
206     (let* ((articulations (ly:event-property ev 'articulations))
207            (finger-found #f))
208
209       (map (lambda (art)
210              (let*
211                  ((num (ly:event-property art 'digit)))
212
213                (if (and (eq? 'fingering-event (ly:event-property art 'class))
214                         (number? num))
215                    (set! finger-found num))))
216            articulations)
217
218       finger-found))
219   
220   (define (note-string ev)
221     (let* ((articulations (ly:event-property ev 'articulations))
222            (string-found #f))
223
224       (map (lambda (art)
225              (let*
226                  ((num (ly:event-property art 'string-number)))
227
228                (if (number? num)
229                    (set! string-found num))))
230            articulations)
231
232       string-found))
233
234   (define (del-string string)
235                       (if (number? string)
236                           (set! free-strings
237                                 (delete string free-strings))))
238   (define specified-frets '())
239   (define free-strings '())
240   
241   (define (close-enough fret)
242                        (reduce
243                         (lambda (x y)
244                           (and x y))
245                         #t
246                         (map (lambda (specced-fret)
247                                (> max-stretch (abs (- fret specced-fret))))
248                              specified-frets)
249                         ))
250   
251   (define (string-qualifies string pitch)
252     (let*
253         ((fret (calc-fret pitch string tunings)))
254          
255          (and (>= fret minimum-fret)
256               (close-enough fret))
257          
258          ))
259                            
260   (define string-fret-fingering-tuples '())
261   (define (set-fret note string)
262                     (set! string-fret-fingering-tuples
263                           (cons (list string
264                                       (calc-fret (ly:event-property note 'pitch)
265                                                  string tunings)
266                                       (note-finger note))
267                                 string-fret-fingering-tuples))
268                     (del-string string))
269        
270
271   ;;; body.
272   (set! specified-frets
273         (filter identity (map
274                       (lambda (note)
275                         (if (note-string note)
276                             (calc-fret (note-pitch note)
277                                        (note-string note) tunings)
278                             #f))
279                       notes)))
280
281
282   (set! free-strings (map 1+ (iota (length tunings))))
283     
284   (for-each (lambda (note)
285               (del-string (note-string note)))
286             notes)
287   
288   
289   (for-each
290    (lambda (note)
291      (if (note-string note)
292          (set-fret note (note-string note))
293          (let*
294              ((string (find (lambda (string) (string-qualifies string
295                                                                (note-pitch note)))
296                               (reverse free-strings))))
297            (if string
298                (set-fret note string)
299                (ly:warning "No string for pitch ~a (given frets ~a)" (note-pitch note)
300                            specified-frets))
301                            
302                )))
303    (sort notes note-pitch<?))
304
305   string-fret-fingering-tuples)