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