]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
* Ignore test failures on non-i386/amd64 architectures (Closes:
[lilypond.git] / scm / translation-functions.scm
1 ;;;; translation-functions.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2009 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     (metronome-markup text dur count hide-note)))
12
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)))))
41
42 (define-public (format-mark-alphabet mark context)
43   (make-bold-markup (make-markalphabet-markup (1- mark))))
44
45 (define-public (format-mark-box-alphabet mark context)
46   (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
47
48 (define-public (format-mark-circle-alphabet mark context)
49   (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
50
51 (define-public (format-mark-letters mark context)
52   (make-bold-markup (make-markletter-markup (1- mark))))
53
54 (define-public (format-mark-numbers mark context)
55   (make-bold-markup (number->string mark)))
56
57 (define-public (format-mark-barnumbers mark context)
58   (make-bold-markup (number->string (ly:context-property context 'currentBarNumber))))
59
60 (define-public (format-mark-box-letters mark context)
61   (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
62
63 (define-public (format-mark-circle-letters mark context)
64   (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
65
66 (define-public (format-mark-box-numbers mark context)
67   (make-bold-markup (make-box-markup (number->string mark))))
68
69 (define-public (format-mark-circle-numbers mark context)
70   (make-bold-markup (make-circle-markup (number->string mark))))
71
72 (define-public (format-mark-box-barnumbers mark context)
73   (make-bold-markup (make-box-markup
74     (number->string (ly:context-property context 'currentBarNumber)))))
75
76 (define-public (format-mark-circle-barnumbers mark context)
77   (make-bold-markup (make-circle-markup
78     (number->string (ly:context-property context 'currentBarNumber)))))
79
80
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 ;; Bass figures.
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84
85 (define-public (format-bass-figure figure event context)
86   (let* ((fig (ly:event-property event 'figure))
87          (fig-markup (if (number? figure)
88
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)
96
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))
118
119          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
120          (plus-dir (ly:context-property context 'figuredBassPlusDirection))
121          )
122
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)))
127
128
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)))
136
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                       )))
146
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)
162
163     ))
164
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; fret diagrams
167
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))
174
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)))))
180
181        (for-each (lambda (sf)
182                    (let*
183                        ((string (car sf))
184                         (fret (cadr sf))
185                         (finger (caddr sf)))
186
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)))
198     
199   (define (get-predefined-fretboard predefined-fret-table tuning pitches)
200 ;   (_i "Search through @var{predefined-fret-table} looking for a predefined
201 ;fretboard with a key of @var{(tuning . pitches)}.  The search will check
202 ;both up and down an octave in order to accomodate transposition of the
203 ;chords.")
204    (define (get-fretboard key)
205     (let ((hash-handle
206            (hash-get-handle predefined-fret-table key)))
207      (if hash-handle
208          (cdr hash-handle)  ; return table entry
209          '())))
210
211    (let ((test-fretboard (get-fretboard (cons tuning pitches))))
212     (if (not (null? test-fretboard))
213         test-fretboard
214         (let ((test-fretboard 
215                (get-fretboard
216                 (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
217          (if (not (null? test-fretboard))
218              test-fretboard
219              (get-fretboard 
220               (cons tuning (map (lambda (x) (shift-octave x -1)) pitches))))))))
221
222 ;; body.
223   (let*
224       ((tunings (ly:context-property context 'stringTunings))
225        (my-string-count (length tunings))
226        (details (ly:grob-property grob 'fret-diagram-details))
227        (predefined-frets
228          (ly:context-property context 'predefinedDiagramTable)) 
229        (minimum-fret (ensure-number
230                       (ly:context-property context 'minimumFret) 0))
231        (max-stretch (ensure-number
232                       (ly:context-property context 'maximumFretStretch) 4))
233        (string-frets (determine-frets-mf notes string-numbers
234                                          minimum-fret max-stretch
235                                          tunings))
236        (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes)))
237
238     (set! (ly:grob-property grob 'fret-diagram-details)
239
240           (if (null? details)
241               (acons 'string-count (length tunings) '())
242               (acons 'string-count (length tunings) details)))
243     (set! (ly:grob-property grob 'dot-placement-list)
244         (if predefined-frets
245             (let ((predefined-fretboard 
246                     (get-predefined-fretboard
247                       predefined-frets
248                       tunings
249                       pitches)))
250               (if (null? predefined-fretboard)
251                   (string-frets->dot-placement 
252                         string-frets my-string-count)  ;no predefined diagram
253                   predefined-fretboard)) ;found default diagram
254             (string-frets->dot-placement string-frets my-string-count)))))
255
256 (define-public (determine-frets-mf notes string-numbers
257                                    minimum-fret max-stretch
258                                    tunings)
259
260   (define (calc-fret pitch string tuning)
261     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
262
263   (define (note-pitch a)
264     (ly:event-property a 'pitch))
265
266   (define (note-pitch>? a b)
267     (ly:pitch<? (note-pitch b)
268                 (note-pitch a)))
269
270   (define (note-finger ev)
271     (let* ((articulations (ly:event-property ev 'articulations))
272            (finger-found #f))
273
274       (map (lambda (art)
275              (let*
276                  ((num (ly:event-property art 'digit)))
277
278                (if (and (eq? 'fingering-event (ly:event-property art 'class))
279                         (number? num))
280                    (set! finger-found num))))
281            articulations)
282
283       finger-found))
284   
285   (define (note-string ev)
286     (let* ((articulations (ly:event-property ev 'articulations))
287            (string-found #f))
288
289       (map (lambda (art)
290              (let*
291                  ((num (ly:event-property art 'string-number)))
292
293                (if (number? num)
294                    (set! string-found num))))
295            articulations)
296
297       string-found))
298
299   (define (del-string string)
300                       (if (number? string)
301                           (set! free-strings
302                                 (delete string free-strings))))
303   (define specified-frets '())
304   (define free-strings '())
305   
306   (define (close-enough fret)
307                        (reduce
308                         (lambda (x y)
309                           (and x y))
310                         #t
311                         (map (lambda (specced-fret)
312                                (> max-stretch (abs (- fret specced-fret))))
313                              specified-frets)
314                         ))
315   
316   (define (string-qualifies string pitch)
317     (let*
318         ((fret (calc-fret pitch string tunings)))
319          
320          (and (>= fret minimum-fret)
321               (close-enough fret))
322          
323          ))
324                            
325   (define string-fret-fingering-tuples '())
326   (define (set-fret note string)
327                     (set! string-fret-fingering-tuples
328                           (cons (list string
329                                       (calc-fret (ly:event-property note 'pitch)
330                                                  string tunings)
331                                       (note-finger note))
332                                 string-fret-fingering-tuples))
333                     (del-string string))
334        
335
336   ;;; body.
337   (set! specified-frets
338         (filter identity (map
339                       (lambda (note)
340                         (if (note-string note)
341                             (calc-fret (note-pitch note)
342                                        (note-string note) tunings)
343                             #f))
344                       notes)))
345
346
347   (set! free-strings (map 1+ (iota (length tunings))))
348     
349   (for-each (lambda (note)
350               (del-string (note-string note)))
351             notes)
352   
353   
354   (for-each
355    (lambda (note)
356      (if (note-string note)
357          (set-fret note (note-string note))
358          (let*
359              ((fit-string (find (lambda (string) 
360                                (string-qualifies string (note-pitch note)))
361                             free-strings)))
362            (if fit-string
363                (set-fret note fit-string)
364                (ly:warning "No string for pitch ~a (given frets ~a)" 
365                            (note-pitch note)
366                            specified-frets))
367                            
368                )))
369    (sort notes note-pitch>?))
370
371   string-fret-fingering-tuples)