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