]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
* scm/translation-functions.scm (determine-frets-mf): new
[lilypond.git] / scm / translation-functions.scm
1 ;;;; translation-functions.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2006 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                           (if (eq? #t (ly:event-property event 'diminished))
76                               (markup #:slashed-digit figure)
77                               (markup #:number (number->string figure 10))))
78                          #f
79                          ))
80          (alt (ly:event-property event 'alteration))
81          (alt-markup
82           (if (number? alt)
83               (markup
84                #:general-align Y DOWN #:fontsize
85                (if (not (= alt DOUBLE-SHARP))
86                    -2 2)
87                (alteration->text-accidental-markup alt))
88               
89               #f))
90          (plus-markup (if (eq? #t (ly:event-property event 'augmented))
91                           (markup #:number "+")
92                           #f))
93
94          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
95          (plus-dir (ly:context-property context 'figuredBassPlusDirection))
96          )
97
98     (if (and (not fig-markup) alt-markup)
99         (begin
100           (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
101           (set! alt-markup #f)))
102
103
104     ;; hmm, how to get figures centered between note, and
105     ;; lone accidentals too?
106     
107     ;;    (if (markup? fig-markup)
108     ;;  (set!
109     ;;   fig-markup (markup #:translate (cons 1.0 0)
110     ;;                      #:hcenter fig-markup)))
111
112     (if alt-markup
113         (set! fig-markup
114               (markup #:put-adjacent
115                       fig-markup X
116                       (if (number? alt-dir)
117                           alt-dir
118                           LEFT)
119                       #:pad-x 0.2 alt-markup
120                       )))
121
122     
123     (if plus-markup
124         (set! fig-markup
125               (if fig-markup
126                   (markup #:put-adjacent
127                           fig-markup
128                           X (if (number? plus-dir)
129                                 plus-dir
130                                 LEFT)
131                           #:pad-x 0.2 plus-markup)
132                   plus-markup)))
133     
134     (if (markup? fig-markup)
135         (markup #:fontsize -2 fig-markup)
136         empty-markup)
137
138     ))
139
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; fret diagrams
142
143 (define-public (determine-frets context grob notes string-numbers)
144   (define (ensure-number a b)
145     (if (number? a)
146         a
147         b))
148 (let*
149       ((tunings (ly:context-property context 'stringTunings))
150        (minimum-fret (ensure-number
151                       (ly:context-property context 'minimumFret) 0))
152        (string-frets (determine-frets-mf notes string-numbers
153                                          minimum-fret
154                                          tunings)))
155
156               
157   (set! (ly:grob-property grob 'string-count) (length tunings))
158   (set! (ly:grob-property grob 'string-frets) string-frets)
159
160   ))
161
162 (define-public (determine-frets-mf notes string-numbers
163                                    minimum-fret
164                                    tunings)
165
166   (define (calc-fret pitch string tuning)
167     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
168
169   (define (note-pitch a)
170     (ly:event-property a 'pitch))
171
172   (define (note-pitch<? a b)
173     (ly:pitch<? (note-pitch a)
174                 (note-pitch b)))
175   
176   (define (note-ev-string ev)
177     (let* ((articulations (ly:event-property ev 'articulations))
178            (string-found #f))
179
180       (map (lambda (art)
181              (let*
182                  ((num (ly:event-property art 'string-number)))
183
184                (if (number? num)
185                    (set! string-found num))))
186            articulations)
187       string-found))
188
189   (let*
190       ((free-strings (map 1+ (iota (length tunings))))
191        (del-string (lambda (string)
192                      (if (number? string)
193                          (set! free-strings
194                                (delete string free-strings)))))
195        (string-qualifies (lambda (string pitch)
196                            (and (>= (calc-fret pitch string tunings)
197                                     minimum-fret))))
198        (string-frets '())
199        (set-fret (lambda (note string)
200                    (set! string-frets
201                         (acons string
202                                (calc-fret (ly:event-property note 'pitch)
203                                           string tunings)
204                                string-frets))
205                    (del-string string)
206                    ))
207        
208
209        )
210     
211     (for-each (lambda (note)
212                 (del-string (note-ev-string note)))
213               notes)
214
215
216     (for-each
217      (lambda (note)
218        (if (note-ev-string note)
219            (set-fret note (note-ev-string note))
220            (let*
221                ((string (find (lambda (string) (string-qualifies string
222                                                                  (note-pitch note)))
223                               (reverse free-strings))))
224
225                 (set-fret note string))))
226      (sort notes note-pitch<?))
227
228     
229     (display string-frets)
230
231
232     string-frets))
233