]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
62a408ce9652da38c2320c22d4b3240edec572a1
[lilypond.git] / scm / translation-functions.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; (c) 1998--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; metronome marks
22
23 (define-public (format-metronome-markup text dur count context)
24   (let* ((hide-note (eq? #t (ly:context-property context 'tempoHideNote))))
25     (metronome-markup text dur count hide-note)))
26
27 (define-public (metronome-markup text dur count hide-note)
28   (let* ((note-mark (if (and (not hide-note) (ly:duration? dur))
29                         (make-smaller-markup
30                          (make-note-by-number-markup (ly:duration-log dur)
31                                                      (ly:duration-dot-count dur)
32                                                      1))
33                         #f))
34          (note-markup (if (and (not hide-note) (number? count) (> count 0) )
35                           (make-concat-markup
36                            (list
37                             (make-general-align-markup Y DOWN note-mark)
38                             (make-simple-markup " ")
39                             (make-simple-markup "=")
40                             (make-simple-markup " ")
41                             (make-simple-markup (number->string count))))
42                           #f))
43          (text-markup (if (not (null? text))
44                           (make-bold-markup text)
45                           #f)))
46     (if text-markup
47         (if (and note-markup (not hide-note))
48             (make-line-markup (list text-markup
49                                     (make-concat-markup
50                                      (list (make-simple-markup "(")
51                                            note-markup
52                                            (make-simple-markup ")")))))
53             (make-line-markup (list text-markup)))
54         (if note-markup
55             (make-line-markup (list note-markup))
56             (make-null-markup)))))
57
58 (define-public (format-mark-alphabet mark context)
59   (make-bold-markup (make-markalphabet-markup (1- mark))))
60
61 (define-public (format-mark-box-alphabet mark context)
62   (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
63
64 (define-public (format-mark-circle-alphabet mark context)
65   (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
66
67 (define-public (format-mark-letters mark context)
68   (make-bold-markup (make-markletter-markup (1- mark))))
69
70 (define-public (format-mark-numbers mark context)
71   (make-bold-markup (number->string mark)))
72
73 (define-public (format-mark-barnumbers mark context)
74   (make-bold-markup (number->string (ly:context-property context
75                                                          'currentBarNumber))))
76
77 (define-public (format-mark-box-letters mark context)
78   (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
79
80 (define-public (format-mark-circle-letters mark context)
81   (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
82
83 (define-public (format-mark-box-numbers mark context)
84   (make-bold-markup (make-box-markup (number->string mark))))
85
86 (define-public (format-mark-circle-numbers mark context)
87   (make-bold-markup (make-circle-markup (number->string mark))))
88
89 (define-public (format-mark-box-barnumbers mark context)
90   (make-bold-markup (make-box-markup
91                      (number->string (ly:context-property context
92                                                           'currentBarNumber)))))
93
94 (define-public (format-mark-circle-barnumbers mark context)
95   (make-bold-markup (make-circle-markup
96                      (number->string (ly:context-property context
97                                                           'currentBarNumber)))))
98
99
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;; Bass figures.
102
103 (define-public (format-bass-figure figure event context)
104   (let* ((fig (ly:event-property event 'figure))
105          (fig-markup (if (number? figure)
106
107                          ;; this is not very elegant, but center-aligning
108                          ;; all digits is problematic with other markups,
109                          ;; and shows problems in the (lack of) overshoot
110                          ;; of feta-alphabet glyphs.
111                          ((if (<= 10 figure)
112                               (lambda (y) (make-translate-scaled-markup
113                                            (cons -0.7 0) y))
114                               identity)
115
116                           (cond
117                            ((eq? #t (ly:event-property event 'diminished))
118                             (markup #:slashed-digit figure))
119                            ((eq? #t (ly:event-property event 'augmented-slash))
120                             (markup #:backslashed-digit figure))
121                            (else (markup #:number (number->string figure 10)))))
122                          #f))
123
124          (alt (ly:event-property event 'alteration))
125          (alt-markup
126           (if (number? alt)
127               (markup
128                #:general-align Y DOWN #:fontsize
129                (if (not (= alt DOUBLE-SHARP))
130                    -2 2)
131                (alteration->text-accidental-markup alt))
132               #f))
133
134          (plus-markup (if (eq? #t (ly:event-property event 'augmented))
135                           (markup #:number "+")
136                           #f))
137
138          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
139          (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
140
141     (if (and (not fig-markup) alt-markup)
142         (begin
143           (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
144           (set! alt-markup #f)))
145
146
147     ;; hmm, how to get figures centered between note, and
148     ;; lone accidentals too?
149
150     ;;    (if (markup? fig-markup)
151     ;;  (set!
152     ;;   fig-markup (markup #:translate (cons 1.0 0)
153     ;;                      #:center-align fig-markup)))
154
155     (if alt-markup
156         (set! fig-markup
157               (markup #:put-adjacent
158                       X (if (number? alt-dir)
159                             alt-dir
160                             LEFT)
161                       fig-markup
162                       #:pad-x 0.2 alt-markup)))
163
164     (if plus-markup
165         (set! fig-markup
166               (if fig-markup
167                   (markup #:put-adjacent
168                           X (if (number? plus-dir)
169                                 plus-dir
170                                 LEFT)
171                           fig-markup
172                           #:pad-x 0.2 plus-markup)
173                   plus-markup)))
174
175     (if (markup? fig-markup)
176         (markup #:fontsize -2 fig-markup)
177         empty-markup)))
178
179
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;; fret diagrams
182
183 (define (create-fretboard context grob placement-list)
184   "Convert @var{placement-list} into a fretboard @var{grob}."
185
186   (let* ((tunings (ly:context-property context 'stringTunings))
187          (my-string-count (length tunings))
188          (details (ly:grob-property grob 'fret-diagram-details)))
189
190     ;; Add string-count from string-tunings to fret-diagram-details.
191     (set! (ly:grob-property grob 'fret-diagram-details)
192             (acons 'string-count my-string-count details))
193     ;; Create the dot-placement list for the grob
194     (set! (ly:grob-property grob 'dot-placement-list) placement-list)))
195
196 (define-public
197   (determine-frets context notes defined-strings . rest)
198   "Determine string numbers and frets for playing @var{notes}
199 as a chord, given specified string numbers @var{defined-strings}.
200 Will look for predefined fretboards if @code{predefinedFretboardTable}
201 is not @code {#f}.  If @var{rest} is present, it contains the
202 FretBoard grob, and a fretboard will be
203 created.  Otherwise, a list of (string fret finger) lists will
204 be returned)."
205
206   ;;  helper functions
207
208   (define (string-frets->placement-list string-frets string-count)
209     "Convert @var{string-frets} to @code{fret-diagram-verbose}
210 dot placement entries."
211     (let* ((placements (list->vector
212                         (map (lambda (x) (list 'mute  (1+ x)))
213                              (iota string-count)))))
214
215       (for-each (lambda (sf)
216                   (let* ((string (car sf))
217                          (fret (cadr sf))
218                          (finger (caddr sf)))
219                     (vector-set!
220                      placements (1- string)
221                      (if (= 0 fret)
222                          (list 'open string)
223                          (if finger
224                              (list 'place-fret string fret finger)
225                              (list 'place-fret string fret))))))
226                 string-frets)
227       (vector->list placements)))
228
229   (define (placement-list->string-frets placement-list)
230     "Convert @var{placement-list} to string-fret list."
231     (map (lambda (x) (if (eq? (car x) 'place-fret)
232                          (cdr x)
233                          (list (cadr x) 0)))
234          (filter (lambda (l) (or (eq? (car l) 'place-fret)
235                                  (eq? (car l) 'open)))
236                  placement-list)))
237
238
239   (define (get-predefined-fretboard predefined-fret-table tuning pitches)
240     "Search through @var{predefined-fret-table} looking for a predefined
241 fretboard with a key of @var{(tuning . pitches)}.  The search will check
242 both up and down an octave in order to accomodate transposition of the
243 chords.  Returns a placement-list."
244
245     (define (get-fretboard key)
246       (let ((hash-handle
247              (hash-get-handle predefined-fret-table key)))
248         (if hash-handle
249             (cdr hash-handle)  ; return table entry
250             '())))
251
252     ;; body of get-predefined-fretboard
253     (let ((test-fretboard (get-fretboard (cons tuning pitches))))
254       (if (not (null? test-fretboard))
255           test-fretboard
256           (let ((test-fretboard
257                  (get-fretboard
258                   (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
259             (if (not (null? test-fretboard))
260                 test-fretboard
261                 (get-fretboard
262                  (cons tuning (map (lambda (x) (shift-octave x -1))
263                                    pitches))))))))
264
265   ;; body of determine-frets
266   (let* ((predefined-fret-table
267           (ly:context-property context 'predefinedDiagramTable))
268          (tunings (ly:context-property context 'stringTunings))
269          (string-count (length tunings))
270          (grob (if (null? rest) '() (car rest)))
271          (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
272          (predefined-fretboard
273           (if predefined-fret-table
274               (get-predefined-fretboard
275                predefined-fret-table
276                tunings
277                pitches)
278               '())))
279
280      (if (null? predefined-fretboard)
281          (let ((string-frets
282                 (determine-frets-and-strings
283                  notes
284                  defined-strings
285                  (ly:context-property context 'minimumFret 0)
286                  (ly:context-property context 'maximumFretStretch 4)
287                  tunings)))
288             (if (null? grob)
289                 string-frets
290                 (create-fretboard
291                  context grob (string-frets->placement-list
292                                 string-frets string-count))))
293          (if (null? grob)
294              (placement-list->string-frets predefined-fretboard)
295              (create-fretboard context grob predefined-fretboard)))))
296
297
298 (define (determine-frets-and-strings
299           notes defined-strings minimum-fret maximum-stretch tuning)
300
301   (define (calc-fret pitch string tuning)
302     (- (ly:pitch-semitones pitch) (list-ref tuning (1- string))))
303
304   (define (note-pitch a)
305     (ly:event-property a 'pitch))
306
307   (define (note-pitch>? a b)
308     (ly:pitch<? (note-pitch b)
309                 (note-pitch a)))
310
311   (define (note-finger ev)
312     (let* ((articulations (ly:event-property ev 'articulations))
313            (finger-found #f))
314
315       (map (lambda (art)
316              (let* ((num (ly:event-property art 'digit)))
317
318                (if (and (eq? 'fingering-event (ly:event-property art 'class))
319                         (number? num))
320                    (set! finger-found num))))
321            articulations)
322
323       finger-found))
324
325   (define (string-number event)
326     (let ((num (ly:event-property event 'string-number)))
327       (if (number? num)
328           num
329           #f)))
330
331
332   (define (delete-free-string string)
333     (if (number? string)
334         (set! free-strings
335               (delete string free-strings))))
336
337   (define free-strings '())
338   (define unassigned-notes '())
339   (define specified-frets '())
340
341   (define (close-enough fret)
342     (if (null? specified-frets)
343         #t
344         (reduce
345           (lambda (x y)
346             (and x y))
347           #t
348           (map (lambda (specced-fret)
349                  (> maximum-stretch (abs (- fret specced-fret))))
350                specified-frets))))
351
352   (define (string-qualifies string pitch)
353     (let* ((fret (calc-fret pitch string tuning)))
354       (and (>= fret minimum-fret)
355            (close-enough fret))))
356
357   (define string-fret-fingering-tuples '())
358
359   (define (set-fret note string)
360     (let ((this-fret (calc-fret (ly:event-property note 'pitch)
361                                 string
362                                 tuning)))
363        (set! string-fret-fingering-tuples
364              (cons (list string
365                          this-fret
366                          (note-finger note))
367                    string-fret-fingering-tuples))
368        (delete-free-string string)
369        (set! specified-frets (cons this-fret specified-frets))))
370
371   ;;; body of determine-frets-and-strings
372   (set! free-strings (map 1+ (iota (length tuning))))
373
374   ;; get defined-strings same length as notes
375   (while (< (length defined-strings) (length notes))
376          (set! defined-strings (append defined-strings '(()))))
377
378   ;; handle notes with strings assigned
379   (for-each
380     (lambda (note string)
381       (if (null? string)
382           (set! unassigned-notes (cons note unassigned-notes))
383           (let ((this-string (string-number string)))
384             (delete-free-string this-string)
385             (set-fret note this-string))))
386     notes defined-strings)
387
388   ;; handle notes without strings assigned
389   (for-each
390    (lambda (note)
391      (let* ((fit-string
392               (find (lambda (string)
393                       (string-qualifies string (note-pitch note)))
394                     free-strings)))
395         (if fit-string
396             (set-fret note fit-string)
397             (ly:warning "No string for pitch ~a (given frets ~a)"
398                         (note-pitch note)
399                         specified-frets))))
400    (sort unassigned-notes note-pitch>?))
401
402    string-fret-fingering-tuples)
403
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 ;; tablature
406
407 ;; The TabNoteHead tablatureFormat callbacks.
408
409 ;; Calculate the fret from pitch and string number as letter
410 ;; The fret letter is taken from 'fretLabels if present
411 (define-public (fret-letter-tablature-format
412                 context string-number fret-number)
413  (let ((labels (ly:context-property context 'fretLabels)))
414   (make-vcenter-markup
415    (cond
416     ((= 0 (length labels))
417      (string (integer->char (+ fret-number (char->integer #\a)))))
418     ((and (<= 0 fret-number) (< fret-number (length labels)))
419      (list-ref labels fret-number))
420     (else
421      (ly:warning "No label for fret ~a (on string ~a);
422 only ~a fret labels provided"
423                  fret-number string-number (length labels))
424        ".")))))
425
426 ;; Display the fret number as a number
427 (define-public (fret-number-tablature-format
428                 context string-number fret-number)
429   (make-vcenter-markup
430     (format "~a" fret-number)))
431
432 ;; The 5-string banjo has got a extra string, the fifth (duh), which
433 ;; starts at the fifth fret on the neck.  Frets on the fifth string
434 ;; are referred to relative to the other frets:
435 ;;   the "first fret" on the fifth string is really the sixth fret
436 ;;   on the banjo neck.
437 ;; We solve this by defining a new fret-number-tablature function:
438 (define-public (fret-number-tablature-format-banjo
439                 context string-number fret-number)
440  (make-vcenter-markup
441   (number->string (cond
442                    ((and (> fret-number 0) (= string-number 5))
443                     (+ fret-number 5))
444                    (else fret-number)))))
445
446 ;;  Tab note head staff position functions
447 ;;
448 ;;  Define where in the staff to display a given string.  Some forms of
449 ;;  tablature put the tab note heads in the spaces, rather than on the
450 ;;  lines
451
452 (define-public (tablature-position-on-lines context string-number)
453  (let* ((string-tunings (ly:context-property context 'stringTunings))
454         (string-count (length string-tunings))
455         (string-one-topmost (ly:context-property context 'stringOneTopmost))
456         (staff-line (- (* 2 string-number) string-count 1)))
457   (if string-one-topmost
458       (- staff-line)
459       staff-line)))
460
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;; bar numbers
463
464 (define-public ((every-nth-bar-number-visible n) barnum)
465   (= 0 (modulo barnum n)))
466
467 (define-public ((modulo-bar-number-visible n m) barnum)
468   (and (> barnum 1) (= m (modulo barnum n))))
469
470 (define-public ((set-bar-number-visibility n) tr)
471   (let ((bn (ly:context-property tr 'currentBarNumber)))
472     (ly:context-set-property! tr 'barNumberVisibility
473                               (modulo-bar-number-visible n (modulo bn n)))))
474
475 (define-public (first-bar-number-invisible barnum) (> barnum 1))
476
477 (define-public (all-bar-numbers-visible barnum) #t)
478
479
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;; percent repeat counters
482
483 (define-public ((every-nth-repeat-count-visible n) count context)
484   (= 0 (modulo count n)))
485
486 (define-public (all-repeat-counts-visible count context) #t)