]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
figured bass: Implement backslashed figures (raised 6th steps)
[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 dur count context)
10   (let* ((note-mark (make-smaller-markup
11                      (make-note-by-number-markup (ly:duration-log dur)
12                                                  (ly:duration-dot-count dur)
13                                                  1))))  
14     (make-line-markup
15      (list
16       (make-general-align-markup Y DOWN note-mark)
17       (make-simple-markup  "=")
18       (make-simple-markup (number->string count))))))
19
20 (define-public (format-mark-alphabet mark context)
21   (make-bold-markup (make-markalphabet-markup (1- mark))))
22
23 (define-public (format-mark-box-alphabet mark context)
24   (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
25
26 (define-public (format-mark-circle-alphabet mark context)
27   (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
28
29 (define-public (format-mark-letters mark context)
30   (make-bold-markup (make-markletter-markup (1- mark))))
31
32 (define-public (format-mark-numbers mark context)
33   (make-bold-markup (number->string mark)))
34
35 (define-public (format-mark-barnumbers mark context)
36   (make-bold-markup (number->string (ly:context-property context 'currentBarNumber))))
37
38 (define-public (format-mark-box-letters mark context)
39   (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
40
41 (define-public (format-mark-circle-letters mark context)
42   (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
43
44 (define-public (format-mark-box-numbers mark context)
45   (make-bold-markup (make-box-markup (number->string mark))))
46
47 (define-public (format-mark-circle-numbers mark context)
48   (make-bold-markup (make-circle-markup (number->string mark))))
49
50 (define-public (format-mark-box-barnumbers mark context)
51   (make-bold-markup (make-box-markup
52     (number->string (ly:context-property context 'currentBarNumber)))))
53
54 (define-public (format-mark-circle-barnumbers mark context)
55   (make-bold-markup (make-circle-markup
56     (number->string (ly:context-property context 'currentBarNumber)))))
57
58
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60 ;; Bass figures.
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62
63 (define-public (format-bass-figure figure event context)
64   (let* ((fig (ly:event-property event 'figure))
65          (fig-markup (if (number? figure)
66
67                          ;; this is not very elegant, but center-aligning all digits
68                          ;; is problematic with other markups, and shows problems
69                          ;; in the (lack of) overshoot of feta alphabet glyphs.
70                          
71                          ((if (<= 10 figure)
72                               (lambda (y) (make-translate-scaled-markup (cons -0.7 0) y))
73                               identity)
74
75                           (cond
76                                 ((eq? #t (ly:event-property event 'diminished))
77                                          (markup #:slashed-digit figure))
78                                 ((eq? #t (ly:event-property event 'augmented-slash))
79                                          (markup #:backslashed-digit figure))
80                                 (else (markup #:number (number->string figure 10)))))
81                          #f
82                          ))
83          (alt (ly:event-property event 'alteration))
84          (alt-markup
85           (if (number? alt)
86               (markup
87                #:general-align Y DOWN #:fontsize
88                (if (not (= alt DOUBLE-SHARP))
89                    -2 2)
90                (alteration->text-accidental-markup alt))
91               
92               #f))
93          (plus-markup (if (eq? #t (ly:event-property event 'augmented))
94                           (markup #:number "+")
95                           #f))
96
97          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
98          (plus-dir (ly:context-property context 'figuredBassPlusDirection))
99          )
100
101     (if (and (not fig-markup) alt-markup)
102         (begin
103           (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
104           (set! alt-markup #f)))
105
106
107     ;; hmm, how to get figures centered between note, and
108     ;; lone accidentals too?
109     
110     ;;    (if (markup? fig-markup)
111     ;;  (set!
112     ;;   fig-markup (markup #:translate (cons 1.0 0)
113     ;;                      #:hcenter fig-markup)))
114
115     (if alt-markup
116         (set! fig-markup
117               (markup #:put-adjacent
118                       fig-markup X
119                       (if (number? alt-dir)
120                           alt-dir
121                           LEFT)
122                       #:pad-x 0.2 alt-markup
123                       )))
124
125     
126     (if plus-markup
127         (set! fig-markup
128               (if fig-markup
129                   (markup #:put-adjacent
130                           fig-markup
131                           X (if (number? plus-dir)
132                                 plus-dir
133                                 LEFT)
134                           #:pad-x 0.2 plus-markup)
135                   plus-markup)))
136     
137     (if (markup? fig-markup)
138         (markup #:fontsize -2 fig-markup)
139         empty-markup)
140
141     ))
142
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 ;; fret diagrams
145
146 (define-public (determine-frets context grob notes string-numbers)
147   (define (ensure-number a b)
148     (if (number? a)
149         a
150         b))
151 (let*
152       ((tunings (ly:context-property context 'stringTunings))
153        (minimum-fret (ensure-number
154                       (ly:context-property context 'minimumFret) 0))
155        (max-stretch (ensure-number
156                       (ly:context-property context 'maximumFretStretch) 4))
157        (string-frets (determine-frets-mf notes string-numbers
158                                          minimum-fret max-stretch
159                                          tunings)))
160
161               
162   (set! (ly:grob-property grob 'string-count) (length tunings))
163   (set! (ly:grob-property grob 'string-fret-finger-combinations) string-frets)
164
165   ))
166
167 (define-public (determine-frets-mf notes string-numbers
168                                    minimum-fret max-stretch
169                                    tunings)
170
171   (define (calc-fret pitch string tuning)
172     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
173
174   (define (note-pitch a)
175     (ly:event-property a 'pitch))
176
177   (define (note-pitch<? a b)
178     (ly:pitch<? (note-pitch a)
179                 (note-pitch b)))
180
181   (define (note-finger ev)
182     (let* ((articulations (ly:event-property ev 'articulations))
183            (finger-found #f))
184
185       (map (lambda (art)
186              (let*
187                  ((num (ly:event-property art 'digit)))
188
189                (if (and (eq? 'fingering-event (ly:event-property art 'class))
190                         (number? num))
191                    (set! finger-found num))))
192            articulations)
193
194       finger-found))
195   
196   (define (note-string ev)
197     (let* ((articulations (ly:event-property ev 'articulations))
198            (string-found #f))
199
200       (map (lambda (art)
201              (let*
202                  ((num (ly:event-property art 'string-number)))
203
204                (if (number? num)
205                    (set! string-found num))))
206            articulations)
207
208       string-found))
209
210   (define (del-string string)
211                       (if (number? string)
212                           (set! free-strings
213                                 (delete string free-strings))))
214   (define specified-frets '())
215   (define free-strings '())
216   
217   (define (close-enough fret)
218                        (reduce
219                         (lambda (x y)
220                           (and x y))
221                         #t
222                         (map (lambda (specced-fret)
223                                (> max-stretch (abs (- fret specced-fret))))
224                              specified-frets)
225                         ))
226   
227   (define (string-qualifies string pitch)
228     (let*
229         ((fret (calc-fret pitch string tunings)))
230          
231          (and (>= fret minimum-fret)
232               (close-enough fret))
233          
234          ))
235                            
236   (define string-fret-fingering-tuples '())
237   (define (set-fret note string)
238                     (set! string-fret-fingering-tuples
239                           (cons (list string
240                                       (calc-fret (ly:event-property note 'pitch)
241                                                  string tunings)
242                                       (note-finger note))
243                                 string-fret-fingering-tuples))
244                     (del-string string))
245        
246
247   ;;; body.
248   (set! specified-frets
249         (filter identity (map
250                       (lambda (note)
251                         (if (note-string note)
252                             (calc-fret (note-pitch note)
253                                        (note-string note) tunings)
254                             #f))
255                       notes)))
256
257
258   (set! free-strings (map 1+ (iota (length tunings))))
259     
260   (for-each (lambda (note)
261               (del-string (note-string note)))
262             notes)
263   
264   
265   (for-each
266    (lambda (note)
267      (if (note-string note)
268          (set-fret note (note-string note))
269          (let*
270              ((string (find (lambda (string) (string-qualifies string
271                                                                (note-pitch note)))
272                               (reverse free-strings))))
273            (if string
274                (set-fret note string)
275                (ly:warning "No string for pitch ~a (given frets ~a)" (note-pitch note)
276                            specified-frets))
277                            
278                )))
279    (sort notes note-pitch<?))
280
281   string-fret-fingering-tuples)