1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; (c) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;; Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)))
27 (define-public (metronome-markup text dur count hide-note)
28 (let* ((note-mark (if (and (not hide-note) (ly:duration? dur))
30 (make-note-by-number-markup (ly:duration-log dur)
31 (ly:duration-dot-count dur)
34 (count-markup (cond ((number? count)
36 (make-simple-markup (number->string count))
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))))))
47 (note-markup (if (and (not hide-note) count-markup)
50 (make-general-align-markup Y DOWN note-mark)
51 (make-simple-markup " ")
52 (make-simple-markup "=")
53 (make-simple-markup " ")
56 (text-markup (if (not (null? text))
57 (make-bold-markup text)
60 (if (and note-markup (not hide-note))
61 (make-line-markup (list text-markup
63 (list (make-simple-markup "(")
65 (make-simple-markup ")")))))
66 (make-line-markup (list text-markup)))
68 (make-line-markup (list note-markup))
69 (make-null-markup)))))
71 (define-public (format-mark-alphabet mark context)
72 (make-bold-markup (make-markalphabet-markup (1- mark))))
74 (define-public (format-mark-box-alphabet mark context)
75 (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
77 (define-public (format-mark-circle-alphabet mark context)
78 (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
80 (define-public (format-mark-letters mark context)
81 (make-bold-markup (make-markletter-markup (1- mark))))
83 (define-public (format-mark-numbers mark context)
84 (make-bold-markup (number->string mark)))
86 (define-public (format-mark-barnumbers mark context)
87 (make-bold-markup (number->string (ly:context-property context
90 (define-public (format-mark-box-letters mark context)
91 (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
93 (define-public (format-mark-circle-letters mark context)
94 (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
96 (define-public (format-mark-box-numbers mark context)
97 (make-bold-markup (make-box-markup (number->string mark))))
99 (define-public (format-mark-circle-numbers mark context)
100 (make-bold-markup (make-circle-markup (number->string mark))))
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)))))
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)))))
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116 (define-public (format-bass-figure figure event context)
117 (let* ((fig (ly:event-property event 'figure))
118 (fig-markup (if (number? figure)
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.
125 (lambda (y) (make-translate-scaled-markup
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)))))
137 (alt (ly:event-property event 'alteration))
141 #:general-align Y DOWN #:fontsize
142 (if (not (= alt DOUBLE-SHARP))
144 (alteration->text-accidental-markup alt))
147 (plus-markup (if (eq? #t (ly:event-property event 'augmented))
148 (markup #:number "+")
151 (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
152 (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
154 (if (and (not fig-markup) alt-markup)
156 (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
157 (set! alt-markup #f)))
160 ;; hmm, how to get figures centered between note, and
161 ;; lone accidentals too?
163 ;; (if (markup? fig-markup)
165 ;; fig-markup (markup #:translate (cons 1.0 0)
166 ;; #:center-align fig-markup)))
170 (markup #:put-adjacent
171 X (if (number? alt-dir)
175 #:pad-x 0.2 alt-markup)))
180 (markup #:put-adjacent
181 X (if (number? plus-dir)
185 #:pad-x 0.2 plus-markup)
188 (if (markup? fig-markup)
189 (markup #:fontsize -2 fig-markup)
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 (define (create-fretboard context grob placement-list)
197 "Convert @var{placement-list} into a fretboard @var{grob}."
199 (let* ((tunings (ly:context-property context 'stringTunings))
200 (my-string-count (length tunings))
201 (details (ly:grob-property grob 'fret-diagram-details)))
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)))
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
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)))))
234 (for-each (lambda (sf)
235 (let* ((string (car sf))
239 placements (1- string)
243 (list 'place-fret string fret finger)
244 (list 'place-fret string fret))))))
246 (vector->list placements)))
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)
253 (filter (lambda (l) (or (eq? (car l) 'place-fret)
254 (eq? (car l) 'open)))
257 (define (entry-count art-list)
258 (length (filter (lambda (x) (not (null? x)))
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."
267 (define (get-fretboard key)
269 (hash-get-handle predefined-fret-table key)))
271 (cdr hash-handle) ; return table entry
274 ;; body of get-predefined-fretboard
275 (let ((test-fretboard (get-fretboard (cons tuning pitches))))
276 (if (not (null? test-fretboard))
278 (let ((test-fretboard
280 (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
281 (if (not (null? test-fretboard))
284 (cons tuning (map (lambda (x) (shift-octave x -1))
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)
297 (ly:event-property x 'string-number)))
298 (car specified-info)))
299 (defined-fingers (map (lambda (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))))
309 (predefined-fretboard
310 (if predefined-fret-table
311 (get-predefined-fretboard
312 predefined-fret-table
316 (if (null? predefined-fretboard)
318 (determine-frets-and-strings
322 (ly:context-property context 'minimumFret 0)
323 (ly:context-property context 'maximumFretStretch 4)
328 context grob (string-frets->placement-list
329 string-frets string-count))))
331 (placement-list->string-frets predefined-fretboard)
332 (create-fretboard context grob predefined-fretboard)))))
335 (define (determine-frets-and-strings
343 (define (calc-fret pitch string tuning)
344 (- (ly:pitch-semitones pitch) (ly:pitch-semitones (list-ref tuning (1- string)))))
346 (define (note-pitch a)
347 (ly:event-property a 'pitch))
349 (define (note-pitch>? a b)
350 (ly:pitch<? (note-pitch b)
353 (define (note-finger ev)
354 (let* ((articulations (ly:event-property ev 'articulations))
358 (let* ((num (ly:event-property art 'digit)))
360 (if (and (eq? 'fingering-event (ly:event-property art 'class))
363 (set! finger-found num))))
368 (define (string-number event)
369 (let ((num (ly:event-property event 'string-number)))
374 (define (delete-free-string string)
377 (delete string free-strings))))
379 (define free-strings '())
380 (define unassigned-notes '())
381 (define specified-frets '())
383 (define (close-enough fret)
384 (if (null? specified-frets)
390 (map (lambda (specced-fret)
391 (or (eq? 0 specced-fret)
392 (>= maximum-stretch (abs (- fret specced-fret)))))
395 (define (string-qualifies string pitch)
396 (let* ((fret (calc-fret pitch string tuning)))
397 (and (>= fret minimum-fret)
398 (close-enough fret))))
400 (define (open-string string pitch)
401 (let* ((fret (calc-fret pitch string tuning)))
404 (define string-fret-fingering-tuples '())
406 (define (set-fret note string)
407 (let ((this-fret (calc-fret (ly:event-property note 'pitch)
411 (ly:warning (_ "Negative fret for pitch ~a on string ~a")
412 (note-pitch note) string))
413 (set! string-fret-fingering-tuples
417 string-fret-fingering-tuples))
418 (delete-free-string string)
419 (set! specified-frets (cons this-fret specified-frets))))
421 (define (pad-list target template)
422 (while (< (length target) (length template))
423 (set! target (if (null? target)
425 (append target '(()))))))
427 ;;; body of determine-frets-and-strings
428 (set! free-strings (map 1+ (iota (length tuning))))
430 ;; get defined-strings same length as notes
431 (pad-list defined-strings notes)
433 ;; get defined-fingers same length as notes
434 (pad-list defined-fingers notes)
436 ;; handle notes with strings assigned and fingering of 0
438 (lambda (note string finger)
439 (let ((digit (if (null? finger)
442 (if (and (null? string)
444 (set! unassigned-notes (cons note unassigned-notes))
447 (find (lambda (string)
448 (open-string string (note-pitch note)))
452 (delete-free-string fit-string)
453 (set-fret note fit-string))
455 (ly:warning (_ "No open string for pitch ~a")
457 (set! unassigned-notes (cons note unassigned-notes)))))
459 (delete-free-string string)
460 (set-fret note string))))))
461 notes defined-strings defined-fingers)
463 ;; handle notes without strings assigned
467 (find (lambda (string)
468 (string-qualifies string (note-pitch note)))
471 (set-fret note fit-string)
472 (ly:warning (_ "No string for pitch ~a (given frets ~a)")
475 (sort unassigned-notes note-pitch>?))
477 string-fret-fingering-tuples)
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482 ;; The TabNoteHead tablatureFormat callbacks.
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)))
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))
496 (ly:warning (_ "No label for fret ~a (on string ~a);
497 only ~a fret labels provided")
498 fret-number string-number (length labels))
501 ;; Display the fret number as a number
502 (define-public (fret-number-tablature-format
503 context string-number fret-number)
505 (format "~a" fret-number)))
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)
516 (number->string (cond
517 ((and (> fret-number 0) (= string-number 5))
519 (else fret-number)))))
521 ;; Tab note head staff position functions
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
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
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
539 (define-public ((every-nth-bar-number-visible n) barnum)
540 (= 0 (modulo barnum n)))
542 (define-public ((modulo-bar-number-visible n m) barnum)
543 (and (> barnum 1) (= m (modulo barnum n))))
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)))))
550 (define-public (first-bar-number-invisible barnum) (> barnum 1))
552 (define-public (all-bar-numbers-visible barnum) #t)
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 ;; percent repeat counters
558 (define-public ((every-nth-repeat-count-visible n) count context)
559 (= 0 (modulo count n)))
561 (define-public (all-repeat-counts-visible count context) #t)