]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
Fix Issue 1035 -- Add context property for negative frets
[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 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 @code{defined-strings} and
215 specified fingerings @code{defined-fingers}.  Only a fingering of@tie{}0
216 will affect the fret selection, as it specifies an open string.
217 If @code{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 @code{FretBoard} grob, and a fretboard will be
222 created.  Otherwise, a list of @code{(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 (determine-frets-and-strings
262             notes
263             defined-strings
264             defined-fingers
265             minimum-fret
266             maximum-stretch
267             tuning)
268
269     (define (calc-fret pitch string tuning)
270       (- (ly:pitch-semitones pitch) (ly:pitch-semitones (list-ref tuning (1- string)))))
271
272     (define (note-pitch a)
273       (ly:event-property a 'pitch))
274
275     (define (note-pitch>? a b)
276       (ly:pitch<? (note-pitch b)
277                   (note-pitch a)))
278
279     (define (note-finger ev)
280       (let* ((articulations (ly:event-property ev 'articulations))
281              (finger-found #f))
282
283         (map (lambda (art)
284                (let* ((num (ly:event-property art 'digit)))
285
286                  (if (and (eq? 'fingering-event (ly:event-property art 'class))
287                           (number? num)
288                           (> num 0))
289                    (set! finger-found num))))
290              articulations)
291
292         finger-found))
293
294     (define (string-number event)
295       (let ((num (ly:event-property event 'string-number)))
296         (if (number? num)
297           num
298           #f)))
299
300     (define (delete-free-string string)
301       (if (number? string)
302         (set! free-strings
303           (delete string free-strings))))
304
305     (define free-strings '())
306     (define unassigned-notes '())
307     (define specified-frets '())
308
309     (define (close-enough fret)
310       (if (null? specified-frets)
311         #t
312         (reduce
313           (lambda (x y)
314             (and x y))
315           #t
316           (map (lambda (specced-fret)
317                  (or (eq? 0 specced-fret)
318                      (>= maximum-stretch (abs (- fret specced-fret)))))
319                specified-frets))))
320
321     (define (string-qualifies string pitch)
322       (let* ((fret (calc-fret pitch string tuning)))
323         (and (>= fret minimum-fret)
324              (close-enough fret))))
325
326     (define (open-string string pitch)
327       (let* ((fret (calc-fret pitch string tuning)))
328         (eq? fret 0)))
329
330     (define string-fret-fingering-tuples '())
331
332     (define (set-fret note string)
333       (let ((this-fret (calc-fret (ly:event-property note 'pitch)
334                                   string
335                                   tuning)))
336         (if (< this-fret 0)
337           (ly:warning (_ "Negative fret for pitch ~a on string ~a")
338                       (note-pitch note) string))
339         (set! string-fret-fingering-tuples
340           (cons (list string
341                       this-fret
342                       (note-finger note))
343                 string-fret-fingering-tuples))
344         (delete-free-string string)
345         (set! specified-frets (cons this-fret specified-frets))))
346
347     (define (pad-list target template)
348       (while (< (length target) (length template))
349              (set! target (if (null? target)
350                             '(())
351                             (append target '(()))))))
352
353     ;;; body of determine-frets-and-strings
354     (set! free-strings (map 1+ (iota (length tuning))))
355
356     ;; get defined-strings same length as notes
357     (pad-list defined-strings notes)
358
359     ;; get defined-fingers same length as notes
360     (pad-list defined-fingers notes)
361
362     ;; handle notes with strings assigned and fingering of 0
363     (for-each
364       (lambda (note string finger)
365         (let ((digit (if (null? finger)
366                        #f
367                        finger)))
368           (if (and (null? string)
369                    (not (eq? digit 0)))
370             (set! unassigned-notes (cons note unassigned-notes))
371             (if (eq? digit 0)
372               (let ((fit-string
373                       (find (lambda (string)
374                               (open-string string (note-pitch note)))
375                             free-strings)))
376                 (if fit-string
377                   (begin
378                     (delete-free-string fit-string)
379                     (set-fret note fit-string))
380                   (begin
381                     (ly:warning (_ "No open string for pitch ~a")
382                                 (note-pitch note))
383                     (set! unassigned-notes (cons note unassigned-notes)))))
384               (let ((this-fret (calc-fret (note-pitch note) string tuning))
385                     (handle-negative
386                       (ly:context-property context
387                                            'handleNegativeFrets
388                                            'recalculate)))
389                 (cond ((or (>= this-fret 0)
390                            (eq? handle-negative 'include))
391                        (begin
392                          (delete-free-string string)
393                          (set-fret note string)))
394                       ((eq? handle-negative 'recalculate)
395                        (begin
396                          (ly:warning (_ "Requested string for pitch requires negative fret: string ~a pitch ~a") string (note-pitch note))
397                          (ly:warning (_ "Ignoring string request."))
398                          (set! unassigned-notes (cons note unassigned-notes))))))))))
399       notes defined-strings defined-fingers)
400
401     ;; handle notes without strings assigned
402     (for-each
403       (lambda (note)
404         (let ((fit-string
405                 (find (lambda (string)
406                         (string-qualifies string (note-pitch note)))
407                       free-strings)))
408           (if fit-string
409             (set-fret note fit-string)
410             (ly:warning (_ "No string for pitch ~a (given frets ~a)")
411                         (note-pitch note)
412                         specified-frets))))
413       (sort unassigned-notes note-pitch>?))
414
415     string-fret-fingering-tuples) ;; end of determine-frets-and-strings
416
417   (define (get-predefined-fretboard predefined-fret-table tuning pitches)
418     "Search through @var{predefined-fret-table} looking for a predefined
419 fretboard with a key of @var{(tuning . pitches)}.  The search will check
420 both up and down an octave in order to accomodate transposition of the
421 chords.  Returns a placement-list."
422
423     (define (get-fretboard key)
424       (let ((hash-handle
425              (hash-get-handle predefined-fret-table key)))
426         (if hash-handle
427             (cdr hash-handle)  ; return table entry
428             '())))
429
430
431
432     ;; body of get-predefined-fretboard
433     (let ((test-fretboard (get-fretboard (cons tuning pitches))))
434       (if (not (null? test-fretboard))
435           test-fretboard
436           (let ((test-fretboard
437                  (get-fretboard
438                   (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
439             (if (not (null? test-fretboard))
440                 test-fretboard
441                 (get-fretboard
442                  (cons tuning (map (lambda (x) (shift-octave x -1))
443                                    pitches))))))))
444
445   ;; body of determine-frets
446   (let* ((predefined-fret-table
447           (ly:context-property context 'predefinedDiagramTable))
448          (tunings (ly:context-property context 'stringTunings))
449          (string-count (length tunings))
450          (grob (if (null? rest) '() (car rest)))
451          (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
452          (defined-strings (map (lambda (x)
453                                  (if (null? x)
454                                      x
455                                      (ly:event-property x 'string-number)))
456                                (car specified-info)))
457          (defined-fingers (map (lambda (x)
458                                  (if (null? x)
459                                      x
460                                      (ly:event-property x 'digit)))
461                                (cadr specified-info)))
462          (default-strings (ly:context-property context 'defaultStrings '()))
463          (strings-used (if (and (zero? (entry-count defined-strings))
464                                 (not (zero? (entry-count default-strings))))
465                            default-strings
466                            defined-strings))
467          (predefined-fretboard
468           (if predefined-fret-table
469               (get-predefined-fretboard
470                predefined-fret-table
471                tunings
472                pitches)
473               '())))
474      (if (null? predefined-fretboard)
475          (let ((string-frets
476                 (determine-frets-and-strings
477                  notes
478                  strings-used
479                  defined-fingers
480                  (ly:context-property context 'minimumFret 0)
481                  (ly:context-property context 'maximumFretStretch 4)
482                  tunings)))
483             (if (null? grob)
484                 string-frets
485                 (create-fretboard
486                  context grob (string-frets->placement-list
487                                 string-frets string-count))))
488          (if (null? grob)
489              (placement-list->string-frets predefined-fretboard)
490              (create-fretboard context grob predefined-fretboard)))))
491
492
493
494 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
495 ;; tablature
496
497 ;; The TabNoteHead tablatureFormat callbacks.
498
499 ;; Calculate the fret from pitch and string number as letter
500 ;; The fret letter is taken from 'fretLabels if present
501 (define-public (fret-letter-tablature-format
502                 context string-number fret-number)
503  (let ((labels (ly:context-property context 'fretLabels)))
504   (make-vcenter-markup
505    (cond
506     ((= 0 (length labels))
507      (string (integer->char (+ fret-number (char->integer #\a)))))
508     ((and (<= 0 fret-number) (< fret-number (length labels)))
509      (list-ref labels fret-number))
510     (else
511      (ly:warning (_ "No label for fret ~a (on string ~a);
512 only ~a fret labels provided")
513                 fret-number string-number (length labels))
514      ".")))))
515
516 ;; Display the fret number as a number
517 (define-public (fret-number-tablature-format
518                 context string-number fret-number)
519   (make-vcenter-markup
520     (format "~a" fret-number)))
521
522 ;; The 5-string banjo has got a extra string, the fifth (duh), which
523 ;; starts at the fifth fret on the neck.  Frets on the fifth string
524 ;; are referred to relative to the other frets:
525 ;;   the "first fret" on the fifth string is really the sixth fret
526 ;;   on the banjo neck.
527 ;; We solve this by defining a new fret-number-tablature function:
528 (define-public (fret-number-tablature-format-banjo
529                 context string-number fret-number)
530  (make-vcenter-markup
531   (number->string (cond
532                    ((and (> fret-number 0) (= string-number 5))
533                     (+ fret-number 5))
534                    (else fret-number)))))
535
536 ;;  Tab note head staff position functions
537 ;;
538 ;;  Define where in the staff to display a given string.  Some forms of
539 ;;  tablature put the tab note heads in the spaces, rather than on the
540 ;;  lines
541
542 (define-public (tablature-position-on-lines context string-number)
543  (let* ((string-tunings (ly:context-property context 'stringTunings))
544         (string-count (length string-tunings))
545         (string-one-topmost (ly:context-property context 'stringOneTopmost))
546         (staff-line (- (* 2 string-number) string-count 1)))
547   (if string-one-topmost
548       (- staff-line)
549       staff-line)))
550
551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ;; bar numbers
553
554 (define-public ((every-nth-bar-number-visible n) barnum)
555   (= 0 (modulo barnum n)))
556
557 (define-public ((modulo-bar-number-visible n m) barnum)
558   (and (> barnum 1) (= m (modulo barnum n))))
559
560 (define-public ((set-bar-number-visibility n) tr)
561   (let ((bn (ly:context-property tr 'currentBarNumber)))
562     (ly:context-set-property! tr 'barNumberVisibility
563                               (modulo-bar-number-visible n (modulo bn n)))))
564
565 (define-public (first-bar-number-invisible barnum) (> barnum 1))
566
567 (define-public (all-bar-numbers-visible barnum) #t)
568
569
570 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
571 ;; percent repeat counters
572
573 (define-public ((every-nth-repeat-count-visible n) count context)
574   (= 0 (modulo count n)))
575
576 (define-public (all-repeat-counts-visible count context) #t)