1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; (c) 1998--2012 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 (clef-octavation-markup oct style)
24 "The octavation sign formatting function. @var{oct} is supposed to be
25 a string holding the octavation number, @var{style} determines the
26 way the octavation number is displayed."
27 (let* ((delim (if (symbol? style)
29 ((parenthesized) (cons "(" ")"))
30 ((bracketed) (cons "[" "]"))
33 (text (string-concatenate (list (car delim) oct (cdr delim)))))
35 (make-vcenter-markup text)))
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 (define-public (format-metronome-markup event context)
42 (let ((hide-note (ly:context-property context 'tempoHideNote #f))
43 (text (ly:event-property event 'text))
44 (dur (ly:event-property event 'tempo-unit))
45 (count (ly:event-property event 'metronome-count)))
47 (metronome-markup text dur count hide-note)))
49 (define-public (metronome-markup text dur count hide-note)
50 (let* ((note-mark (if (and (not hide-note) (ly:duration? dur))
52 (make-note-by-number-markup (ly:duration-log dur)
53 (ly:duration-dot-count dur)
56 (count-markup (cond ((number? count)
58 (make-simple-markup (number->string count))
63 (make-simple-markup (number->string (car count)))
64 (make-simple-markup " ")
65 (make-simple-markup "–")
66 (make-simple-markup " ")
67 (make-simple-markup (number->string (cdr count))))))
69 (note-markup (if (and (not hide-note) count-markup)
72 (make-general-align-markup Y DOWN note-mark)
73 (make-simple-markup " ")
74 (make-simple-markup "=")
75 (make-simple-markup " ")
78 (text-markup (if (not (null? text))
79 (make-bold-markup text)
82 (if (and note-markup (not hide-note))
83 (make-line-markup (list text-markup
85 (list (make-simple-markup "(")
87 (make-simple-markup ")")))))
88 (make-line-markup (list text-markup)))
90 (make-line-markup (list note-markup))
91 (make-null-markup)))))
93 (define-public (format-mark-alphabet mark context)
94 (make-bold-markup (make-markalphabet-markup (1- mark))))
96 (define-public (format-mark-box-alphabet mark context)
97 (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
99 (define-public (format-mark-circle-alphabet mark context)
100 (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
102 (define-public (format-mark-letters mark context)
103 (make-bold-markup (make-markletter-markup (1- mark))))
105 (define-public (format-mark-numbers mark context)
106 (make-bold-markup (number->string mark)))
108 (define-public (format-mark-barnumbers mark context)
109 (make-bold-markup (number->string (ly:context-property context
110 'currentBarNumber))))
112 (define-public (format-mark-box-letters mark context)
113 (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
115 (define-public (format-mark-circle-letters mark context)
116 (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
118 (define-public (format-mark-box-numbers mark context)
119 (make-bold-markup (make-box-markup (number->string mark))))
121 (define-public (format-mark-circle-numbers mark context)
122 (make-bold-markup (make-circle-markup (number->string mark))))
124 (define-public (format-mark-box-barnumbers mark context)
125 (make-bold-markup (make-box-markup
126 (number->string (ly:context-property context
127 'currentBarNumber)))))
129 (define-public (format-mark-circle-barnumbers mark context)
130 (make-bold-markup (make-circle-markup
131 (number->string (ly:context-property context
132 'currentBarNumber)))))
135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 (define-public (format-bass-figure figure event context)
139 (let* ((fig (ly:event-property event 'figure))
140 (fig-markup (if (number? figure)
142 ;; this is not very elegant, but center-aligning
143 ;; all digits is problematic with other markups,
144 ;; and shows problems in the (lack of) overshoot
145 ;; of feta-alphabet glyphs.
147 (lambda (y) (make-translate-scaled-markup
152 ((eq? #t (ly:event-property event 'diminished))
153 (markup #:slashed-digit figure))
154 ((eq? #t (ly:event-property event 'augmented-slash))
155 (markup #:backslashed-digit figure))
156 (else (markup #:number (number->string figure 10)))))
159 (alt (ly:event-property event 'alteration))
163 #:general-align Y DOWN #:fontsize
164 (if (not (= alt DOUBLE-SHARP))
166 (alteration->text-accidental-markup alt))
169 (plus-markup (if (eq? #t (ly:event-property event 'augmented))
170 (markup #:number "+")
173 (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
174 (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
176 (if (and (not fig-markup) alt-markup)
178 (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
179 (set! alt-markup #f)))
182 ;; hmm, how to get figures centered between note, and
183 ;; lone accidentals too?
185 ;; (if (markup? fig-markup)
187 ;; fig-markup (markup #:translate (cons 1.0 0)
188 ;; #:center-align fig-markup)))
192 (markup #:put-adjacent
193 X (if (number? alt-dir)
197 #:pad-x 0.2 alt-markup)))
202 (markup #:put-adjacent
203 X (if (number? plus-dir)
207 #:pad-x 0.2 plus-markup)
210 (if (markup? fig-markup)
211 (markup #:fontsize -2 fig-markup)
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 (define (create-fretboard context grob placement-list)
219 "Convert @var{placement-list} into a fretboard @var{grob}."
221 (let* ((tunings (ly:context-property context 'stringTunings))
222 (my-string-count (length tunings))
223 (details (ly:grob-property grob 'fret-diagram-details)))
225 ;; Add string-count from string-tunings to fret-diagram-details.
226 (set! (ly:grob-property grob 'fret-diagram-details)
227 (acons 'string-count my-string-count details))
228 ;; Create the dot-placement list for the grob
229 (set! (ly:grob-property grob 'dot-placement-list) placement-list)))
232 (determine-frets context notes specified-info . rest)
233 "Determine string numbers and frets for playing @var{notes}
234 as a chord, given specified information @var{specified-info}.
235 @var{specified-info} is a list with two list elements,
236 specified strings @code{defined-strings} and
237 specified fingerings @code{defined-fingers}. Only a fingering of@tie{}0
238 will affect the fret selection, as it specifies an open string.
239 If @code{defined-strings} is @code{'()}, the context property
240 @code{defaultStrings} will be used as a list of defined strings.
241 Will look for predefined fretboards if @code{predefinedFretboardTable}
242 is not @code {#f}. If @var{rest} is present, it contains the
243 @code{FretBoard} grob, and a fretboard will be
244 created. Otherwise, a list of @code{(string fret finger)} lists will
249 (define (string-frets->placement-list string-frets string-count)
250 "Convert @var{string-frets} to @code{fret-diagram-verbose}
251 dot placement entries."
252 (let* ((placements (list->vector
253 (map (lambda (x) (list 'mute x))
254 (iota string-count 1)))))
256 (for-each (lambda (sf)
257 (let* ((string (car sf))
266 (list 'place-fret string fret finger)
267 (list 'place-fret string fret))))))
269 (vector->list placements)))
271 (define (placement-list->string-frets placement-list)
272 "Convert @var{placement-list} to string-fret list."
273 (map (lambda (x) (if (eq? (car x) 'place-fret)
276 (filter (lambda (l) (or (eq? (car l) 'place-fret)
277 (eq? (car l) 'open)))
280 (define (entry-count art-list)
281 "Count the number of entries in a list of articulations."
282 (length (filter (lambda (x) (not (null? x)))
285 (define (determine-frets-and-strings
292 "Determine the frets and strings used to play the notes in
293 @var{notes}, given @var{defined-strings} and @var{defined-fingers}
294 along with @var{minimum-fret}, @var{maximum-stretch}, and
295 @var{tuning}. Returns a list of @code{(string fret finger) lists."
298 (define restrain-open-strings (ly:context-property context
301 (define specified-frets '())
302 (define free-strings (iota (length tuning) 1))
304 (define (calc-fret pitch string tuning)
305 "Calculate the fret to play @var{pitch} on @var{string} with
307 (* 2 (- (ly:pitch-tones pitch) (ly:pitch-tones (list-ref tuning (1- string))))))
309 (define (note-pitch note)
310 "Get the pitch (in semitones) from @var{note}."
311 (ly:event-property note 'pitch))
313 (define (note-finger ev)
314 "Get the fingering from @var{ev}. Return @var{#f}
315 if no fingering is present."
316 (let* ((articulations (ly:event-property ev 'articulations))
319 (let* ((num (ly:event-property art 'digit)))
321 (if (and (ly:in-event-class? art 'fingering-event)
324 (set! finger-found num))))
328 (define (string-number event)
329 "Get the string-number from @var{event}. Return @var{#f}
330 if no string-number is present."
331 (let ((num (ly:event-property event 'string-number)))
336 (define (delete-free-string string)
339 (delete string free-strings))))
341 (define (close-enough fret)
342 "Decide if @var{fret} is acceptable, given the already used frets."
343 (if (null? specified-frets)
349 (map (lambda (specced-fret)
350 (or (eq? 0 specced-fret)
351 (and (not restrain-open-strings)
353 (>= maximum-stretch (abs (- fret specced-fret)))))
356 (define (string-qualifies string pitch)
357 "Can @var{pitch} be played on @var{string}, given already placed
359 (let* ((fret (calc-fret pitch string tuning)))
360 (and (or (and (not restrain-open-strings)
362 (>= fret minimum-fret))
364 (close-enough fret))))
366 (define (open-string string pitch)
367 "Is @var{pitch} and open-string note on @var{string}, given
369 (let* ((fret (calc-fret pitch string tuning)))
372 (define (set-fret! pitch-entry string finger)
373 (let ((this-fret (calc-fret (car pitch-entry)
377 (ly:warning (_ "Negative fret for pitch ~a on string ~a")
378 (car pitch-entry) string)
379 (if (not (integer? this-fret))
380 (ly:warning (_ "Missing fret for pitch ~a on string ~a")
381 (car pitch-entry) string)))
382 (delete-free-string string)
383 (set! specified-frets (cons this-fret specified-frets))
384 (list-set! string-fret-fingers
386 (list string this-fret finger))))
388 (define (kill-note! string-fret-fingers note-index)
389 (list-set! string-fret-fingers note-index (list #f #t)))
391 (define string-fret-fingers
392 (map (lambda (string finger)
395 (list string #f finger)))
396 defined-strings defined-fingers))
398 ;;; body of determine-frets-and-strings
399 (let* ((pitches (map note-pitch notes))
400 (pitch-alist (map cons pitches (iota (length pitches)))))
402 ;; handle notes with strings assigned and fingering of 0
404 (lambda (pitch-entry string-fret-finger)
405 (let* ((string (list-ref string-fret-finger 0))
406 (finger (if (eq? (length string-fret-finger) 3)
407 (list-ref string-fret-finger 2)
409 (pitch (car pitch-entry))
410 (digit (if (null? finger)
413 (if (or (not (null? string))
416 ;; here we handle fingers of 0 -- open strings
418 (find (lambda (string)
419 (open-string string pitch))
422 (set-fret! pitch-entry fit-string #f)
423 (ly:warning (_ "No open string for pitch ~a")
425 ;; here we handle assigned strings
427 (calc-fret pitch string tuning))
429 (ly:context-property context
432 (cond ((or (and (>= this-fret 0) (integer? this-fret))
433 (eq? handle-negative 'include))
434 (set-fret! pitch-entry string finger))
435 ((eq? handle-negative 'recalculate)
438 (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
441 (ly:warning (_ "Ignoring string request and recalculating."))
442 (list-set! string-fret-fingers
446 (list '() #f finger)))))
447 ((eq? handle-negative 'ignore)
450 (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
453 (ly:warning (_ "Ignoring note in tablature."))
454 (kill-note! string-fret-fingers
455 (cdr pitch-entry))))))))))
456 pitch-alist string-fret-fingers)
457 ;; handle notes without strings assigned -- sorted by pitch, so
458 ;; we need to use the alist to have the note number available
460 (lambda (pitch-entry)
461 (let* ((string-fret-finger (list-ref string-fret-fingers
463 (string (list-ref string-fret-finger 0))
464 (finger (if (eq? (length string-fret-finger) 3)
465 (list-ref string-fret-finger 2)
467 (pitch (car pitch-entry))
469 (find (lambda (string)
470 (string-qualifies string pitch))
472 (if (not (list-ref string-fret-finger 1))
474 (set-fret! pitch-entry fit-string finger)
476 (ly:warning (_ "No string for pitch ~a (given frets ~a)")
479 (kill-note! string-fret-fingers
480 (cdr pitch-entry)))))))
481 (sort pitch-alist (lambda (pitch-entry-a pitch-entry-b)
482 (ly:pitch<? (car pitch-entry-b)
483 (car pitch-entry-a)))))
484 string-fret-fingers)) ;; end of determine-frets-and-strings
486 (define (get-predefined-fretboard predefined-fret-table tuning pitches)
487 "Search through @var{predefined-fret-table} looking for a predefined
488 fretboard with a key of @var{(tuning . pitches)}. The search will check
489 both up and down an octave in order to accomodate transposition of the
490 chords. Returns a placement-list."
492 (define (get-fretboard key)
494 (hash-get-handle predefined-fret-table key)))
496 (cdr hash-handle) ; return table entry
499 ;; body of get-predefined-fretboard
500 (let ((test-fretboard (get-fretboard (cons tuning pitches))))
501 (if (not (null? test-fretboard))
503 (let ((test-fretboard
505 (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
506 (if (not (null? test-fretboard))
509 (cons tuning (map (lambda (x) (shift-octave x -1))
512 ;; body of determine-frets
513 (let* ((predefined-fret-table
514 (ly:context-property context 'predefinedDiagramTable))
515 (tunings (ly:context-property context 'stringTunings))
516 (string-count (length tunings))
517 (grob (if (null? rest) '() (car rest)))
518 (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
519 (defined-strings (map (lambda (x)
522 (ly:event-property x 'string-number)))
523 (car specified-info)))
524 (defined-fingers (map (lambda (x)
527 (ly:event-property x 'digit)))
528 (cadr specified-info)))
529 (default-strings (ly:context-property context 'defaultStrings '()))
530 (strings-used (if (and (zero? (entry-count defined-strings))
531 (not (zero? (entry-count default-strings))))
534 (predefined-fretboard
535 (if predefined-fret-table
536 (get-predefined-fretboard
537 predefined-fret-table
541 (if (null? predefined-fretboard)
543 (determine-frets-and-strings
547 (ly:context-property context 'minimumFret 0)
548 (ly:context-property context 'maximumFretStretch 4)
553 context grob (string-frets->placement-list
554 (filter (lambda (entry)
559 (placement-list->string-frets predefined-fretboard)
560 (create-fretboard context grob predefined-fretboard)))))
564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
567 ;; The TabNoteHead tablatureFormat callbacks.
569 ;; Calculate the fret from pitch and string number as letter
570 ;; The fret letter is taken from 'fretLabels if present
571 (define-public (fret-letter-tablature-format
572 context string-number fret-number)
573 (let ((labels (ly:context-property context 'fretLabels)))
576 ((= 0 (length labels))
577 (string (integer->char (+ fret-number (char->integer #\a)))))
578 ((and (<= 0 fret-number) (< fret-number (length labels)))
579 (list-ref labels fret-number))
581 (ly:warning (_ "No label for fret ~a (on string ~a);
582 only ~a fret labels provided")
583 fret-number string-number (length labels))
586 ;; Display the fret number as a number
587 (define-public (fret-number-tablature-format
588 context string-number fret-number)
590 (format #f "~a" fret-number)))
592 ;; The 5-string banjo has got a extra string, the fifth (duh), which
593 ;; starts at the fifth fret on the neck. Frets on the fifth string
594 ;; are referred to relative to the other frets:
595 ;; the "first fret" on the fifth string is really the sixth fret
596 ;; on the banjo neck.
597 ;; We solve this by defining a new fret-number-tablature function:
598 (define-public (fret-number-tablature-format-banjo
599 context string-number fret-number)
601 (number->string (cond
602 ((and (> fret-number 0) (= string-number 5))
604 (else fret-number)))))
606 ;; Tab note head staff position functions
608 ;; Define where in the staff to display a given string. Some forms of
609 ;; tablature put the tab note heads in the spaces, rather than on the
612 (define-public (tablature-position-on-lines context string-number)
613 (let* ((string-tunings (ly:context-property context 'stringTunings))
614 (string-count (length string-tunings))
615 (string-one-topmost (ly:context-property context 'stringOneTopmost))
616 (staff-line (- (* 2 string-number) string-count 1)))
617 (if string-one-topmost
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
624 (define-public ((every-nth-bar-number-visible n) barnum mp)
625 (= 0 (modulo barnum n)))
627 (define-public ((modulo-bar-number-visible n m) barnum mp)
628 (and (> barnum 1) (= m (modulo barnum n))))
630 (define-public ((set-bar-number-visibility n) tr)
631 (let ((bn (ly:context-property tr 'currentBarNumber)))
632 (ly:context-set-property! tr 'barNumberVisibility
633 (modulo-bar-number-visible n (modulo bn n)))))
635 (define-public (first-bar-number-invisible barnum mp)
638 (define-public (first-bar-number-invisible-save-broken-bars barnum mp)
640 (> (ly:moment-main-numerator mp) 0)))
642 (define-public (first-bar-number-invisible-and-no-parenthesized-bar-numbers barnum mp)
644 (= (ly:moment-main-numerator mp) 0)))
646 (define-public (robust-bar-number-function barnum measure-pos alt-number context)
647 (define (get-number-and-power an pow)
648 (if (<= an alt-number)
649 (get-number-and-power (+ an (expt 26 (1+ pow))) (1+ pow))
650 (cons (+ alt-number (- (expt 26 pow) an)) (1- pow))))
651 (define (make-letter so-far an pow)
654 (let ((pos (modulo (quotient an (expt 26 pow)) 26)))
655 (make-letter (string-append so-far
656 (substring "abcdefghijklmnopqrstuvwxyz"
661 (let* ((number-and-power (get-number-and-power 0 0))
662 (begin-measure (= 0 (ly:moment-main-numerator measure-pos)))
663 (maybe-open-parenthesis (if begin-measure "" "("))
664 (maybe-close-parenthesis (if begin-measure "" ")")))
665 (markup (string-append maybe-open-parenthesis
666 (number->string barnum)
668 (car number-and-power)
669 (cdr number-and-power))
670 maybe-close-parenthesis))))
672 (define-public (all-bar-numbers-visible barnum mp) #t)
675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
676 ;; percent repeat counters
678 (define-public ((every-nth-repeat-count-visible n) count context)
679 (= 0 (modulo count n)))
681 (define-public (all-repeat-counts-visible count context) #t)
683 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
684 ;; make-engraver helper macro
686 (defmacro-public make-engraver forms
687 "Helper macro for creating Scheme engravers.
689 The usual form for an engraver is an association list (or alist)
690 mapping symbols to either anonymous functions or to another such
693 @code{make-engraver} accepts forms where the first element is either
694 an argument list starting with the respective symbol, followed by the
695 function body (comparable to the way @code{define} is used for
696 defining functions), or a single symbol followed by subordinate forms
697 in the same manner. You can also just make an alist pair
698 literally (the @samp{car} is quoted automatically) as long as the
699 unevaluated @samp{cdr} is not a pair. This is useful if you already
700 have defined your engraver functions separately.
702 Symbols mapping to a function would be @code{initialize},
703 @code{start-translation-timestep}, @code{process-music},
704 @code{process-acknowledged}, @code{stop-translation-timestep}, and
705 @code{finalize}. Symbols mapping to another alist specified in the
706 same manner are @code{listeners} with the subordinate symbols being
707 event classes, and @code{acknowledgers} and @code{end-acknowledgers}
708 with the subordinate symbols being interfaces."
709 (let loop ((forms forms))
710 (if (cheap-list? forms)
712 ,@(map (lambda (form)
713 (if (pair? (car form))
714 `(cons ',(caar form) (lambda ,(cdar form) ,@(cdr form)))
715 `(cons ',(car form) ,(loop (cdr form)))))