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