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-transposition-markup oct style)
24 "The transposition sign formatting function. @var{oct} is supposed to be
25 a string holding the transposition number, @var{style} determines the
26 way the transposition 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 ;; We give 'styled-metronome-markup' an optional argument, 'glyph-font', to
42 ;; prepare using other fonts than 'fetaMusic.
43 ;; Currently it ensures that the default-fonts are used by the
44 ;; markup-command 'note-by-number' in 'metronome-markup' (see below).
46 ((styled-metronome-markup #:optional (glyph-font 'default))
48 (let ((hide-note (ly:context-property context 'tempoHideNote #f))
49 (text (ly:event-property event 'text))
50 (dur (ly:event-property event 'tempo-unit))
51 (count (ly:event-property event 'metronome-count)))
53 (metronome-markup glyph-font text dur count hide-note)))
55 (define-public format-metronome-markup
56 (styled-metronome-markup))
58 (define (metronome-markup glyph-font text dur count hide-note)
60 (if (and (not hide-note) (ly:duration? dur))
62 ;; We insert the (default)-font for flag-glyphs and
63 ;; note-head-glyphs to prepare the possibility to use
64 ;; other fonts and to make possible using
65 ;; \override MetronomeMark #'font-name = #<font-name>
66 ;; without affecting the note/flag-glyphs.
67 (make-override-markup (cons 'font-name glyph-font)
68 (make-note-by-number-markup
70 (ly:duration-dot-count dur)
73 (count-markup (cond ((number? count)
76 (number->string count))
82 (number->string (car count)))
83 (make-simple-markup " ")
84 (make-simple-markup "–")
85 (make-simple-markup " ")
87 (number->string (cdr count))))))
89 (note-markup (if (and (not hide-note) count-markup)
92 (make-general-align-markup Y DOWN note-mark)
93 (make-simple-markup " ")
94 (make-simple-markup "=")
95 (make-simple-markup " ")
98 (text-markup (if (not (null? text))
99 (make-bold-markup text)
102 (if (and note-markup (not hide-note))
103 (make-line-markup (list text-markup
105 (list (make-simple-markup "(")
107 (make-simple-markup ")")))))
108 (make-line-markup (list text-markup)))
110 (make-line-markup (list note-markup))
111 (make-null-markup)))))
113 (define-public (format-mark-alphabet mark context)
114 (make-bold-markup (make-markalphabet-markup (1- mark))))
116 (define-public (format-mark-box-alphabet mark context)
117 (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
119 (define-public (format-mark-circle-alphabet mark context)
120 (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
122 (define-public (format-mark-letters mark context)
123 (make-bold-markup (make-markletter-markup (1- mark))))
125 (define-public (format-mark-numbers mark context)
126 (make-bold-markup (number->string mark)))
128 (define-public (format-mark-barnumbers mark context)
129 (make-bold-markup (number->string (ly:context-property context
130 'currentBarNumber))))
132 (define-public (format-mark-box-letters mark context)
133 (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
135 (define-public (format-mark-circle-letters mark context)
136 (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
138 (define-public (format-mark-box-numbers mark context)
139 (make-bold-markup (make-box-markup (number->string mark))))
141 (define-public (format-mark-circle-numbers mark context)
142 (make-bold-markup (make-circle-markup (number->string mark))))
144 (define-public (format-mark-box-barnumbers mark context)
145 (make-bold-markup (make-box-markup
146 (number->string (ly:context-property context
147 'currentBarNumber)))))
149 (define-public (format-mark-circle-barnumbers mark context)
150 (make-bold-markup (make-circle-markup
151 (number->string (ly:context-property context
152 'currentBarNumber)))))
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 (define-public (format-bass-figure figure event context)
159 (let* ((fig (ly:event-property event 'figure))
160 (fig-markup (if (number? figure)
162 ;; this is not very elegant, but center-aligning
163 ;; all digits is problematic with other markups,
164 ;; and shows problems in the (lack of) overshoot
165 ;; of feta-alphabet glyphs.
167 (lambda (y) (make-translate-scaled-markup
172 ((eq? #t (ly:event-property event 'diminished))
173 (markup #:slashed-digit figure))
174 ((eq? #t (ly:event-property event 'augmented-slash))
175 (markup #:backslashed-digit figure))
176 (else (markup #:number (number->string figure 10)))))
179 (alt (ly:event-property event 'alteration))
183 #:general-align Y DOWN #:fontsize
184 (if (not (= alt DOUBLE-SHARP))
186 (alteration->text-accidental-markup alt))
189 (plus-markup (if (eq? #t (ly:event-property event 'augmented))
190 (markup #:number "+")
193 (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
194 (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
196 (if (and (not fig-markup) alt-markup)
198 (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
199 (set! alt-markup #f)))
202 ;; hmm, how to get figures centered between note, and
203 ;; lone accidentals too?
205 ;; (if (markup? fig-markup)
207 ;; fig-markup (markup #:translate (cons 1.0 0)
208 ;; #:center-align fig-markup)))
212 (markup #:put-adjacent
213 X (if (number? alt-dir)
217 #:pad-x 0.2 alt-markup)))
222 (markup #:put-adjacent
223 X (if (number? plus-dir)
227 #:pad-x 0.2 plus-markup)
230 (if (markup? fig-markup)
231 (markup #:fontsize -2 fig-markup)
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 (define (create-fretboard context grob placement-list)
239 "Convert @var{placement-list} into a fretboard @var{grob}."
241 (let* ((tunings (ly:context-property context 'stringTunings))
242 (my-string-count (length tunings))
243 (details (ly:grob-property grob 'fret-diagram-details)))
245 ;; Add string-count from string-tunings to fret-diagram-details.
246 (set! (ly:grob-property grob 'fret-diagram-details)
247 (acons 'string-count my-string-count details))
248 ;; Create the dot-placement list for the grob
249 (set! (ly:grob-property grob 'dot-placement-list) placement-list)))
252 (determine-frets context notes specified-info . rest)
253 "Determine string numbers and frets for playing @var{notes}
254 as a chord, given specified information @var{specified-info}.
255 @var{specified-info} is a list with two list elements,
256 specified strings @code{defined-strings} and
257 specified fingerings @code{defined-fingers}. Only a fingering of@tie{}0
258 will affect the fret selection, as it specifies an open string.
259 If @code{defined-strings} is @code{'()}, the context property
260 @code{defaultStrings} will be used as a list of defined strings.
261 Will look for predefined fretboards if @code{predefinedFretboardTable}
262 is not @code {#f}. If @var{rest} is present, it contains the
263 @code{FretBoard} grob, and a fretboard will be
264 created. Otherwise, a list of @code{(string fret finger)} lists will
269 (define (string-frets->placement-list string-frets string-count)
270 "Convert @var{string-frets} to @code{fret-diagram-verbose}
271 dot placement entries."
272 (let* ((placements (list->vector
273 (map (lambda (x) (list 'mute x))
274 (iota string-count 1)))))
276 (for-each (lambda (sf)
277 (let* ((string (car sf))
286 (list 'place-fret string fret finger)
287 (list 'place-fret string fret))))))
289 (vector->list placements)))
291 (define (placement-list->string-frets placement-list)
292 "Convert @var{placement-list} to string-fret list."
293 (map (lambda (x) (if (eq? (car x) 'place-fret)
296 (filter (lambda (l) (or (eq? (car l) 'place-fret)
297 (eq? (car l) 'open)))
300 (define (entry-count art-list)
301 "Count the number of entries in a list of articulations."
302 (length (filter (lambda (x) (not (null? x)))
305 (define (string-number event)
306 "Get the string-number from @var{event}. Return @var{#f}
307 if no string-number is present."
308 (let ((num (ly:event-property event 'string-number)))
309 (and (integer? num) (positive? num) num)))
311 (define (determine-frets-and-strings
318 "Determine the frets and strings used to play the notes in
319 @var{notes}, given @var{defined-strings} and @var{defined-fingers}
320 along with @var{minimum-fret}, @var{maximum-stretch}, and
321 @var{tuning}. Returns a list of @code{(string fret finger) lists."
324 (define restrain-open-strings (ly:context-property context
327 (define specified-frets '())
328 (define free-strings (iota (length tuning) 1))
330 (define (calc-fret pitch string tuning)
331 "Calculate the fret to play @var{pitch} on @var{string} with
333 (* 2 (- (ly:pitch-tones pitch) (ly:pitch-tones (list-ref tuning (1- string))))))
335 (define (note-pitch note)
336 "Get the pitch (in semitones) from @var{note}."
337 (ly:event-property note 'pitch))
339 (define (note-finger ev)
340 "Get the fingering from @var{ev}. Return @var{#f}
341 if no fingering is present."
342 (let* ((articulations (ly:event-property ev 'articulations))
344 (for-each (lambda (art)
345 (let* ((num (ly:event-property art 'digit)))
347 (if (and (ly:in-event-class? art 'fingering-event)
350 (set! finger-found num))))
354 (define (delete-free-string string)
357 (delete string free-strings))))
359 (define (close-enough fret)
360 "Decide if @var{fret} is acceptable, given the already used frets."
361 (every (lambda (specced-fret)
362 (or (zero? specced-fret)
364 (>= maximum-stretch (abs (- fret specced-fret)))))
367 (define (string-qualifies string pitch)
368 "Can @var{pitch} be played on @var{string}, given already placed
370 (let* ((fret (calc-fret pitch string tuning)))
371 (and (or (and (not restrain-open-strings)
373 (>= fret minimum-fret))
375 (close-enough fret))))
377 (define (open-string string pitch)
378 "Is @var{pitch} and open-string note on @var{string}, given
380 (let* ((fret (calc-fret pitch string tuning)))
383 (define (set-fret! pitch-entry string finger)
384 (let ((this-fret (calc-fret (car pitch-entry)
388 (ly:warning (_ "Negative fret for pitch ~a on string ~a")
389 (car pitch-entry) string)
390 (if (not (integer? this-fret))
391 (ly:warning (_ "Missing fret for pitch ~a on string ~a")
392 (car pitch-entry) string)))
393 (delete-free-string string)
394 (set! specified-frets (cons this-fret specified-frets))
395 (list-set! string-fret-fingers
397 (list string this-fret finger))))
399 (define (kill-note! string-fret-fingers note-index)
400 (list-set! string-fret-fingers note-index (list #f #t)))
402 (define string-fret-fingers
403 (map (lambda (string finger)
406 (list string #f finger)))
407 defined-strings defined-fingers))
409 ;;; body of determine-frets-and-strings
410 (let* ((pitches (map note-pitch notes))
411 (pitch-alist (map cons pitches (iota (length pitches)))))
413 ;; handle notes with strings assigned and fingering of 0
415 (lambda (pitch-entry string-fret-finger)
416 (let* ((string (list-ref string-fret-finger 0))
417 (finger (if (= (length string-fret-finger) 3)
418 (list-ref string-fret-finger 2)
420 (pitch (car pitch-entry))
421 (digit (if (null? finger)
424 (if (or (not (null? string))
427 ;; here we handle fingers of 0 -- open strings
429 (find (lambda (string)
430 (open-string string pitch))
433 (set-fret! pitch-entry fit-string #f)
434 (ly:warning (_ "No open string for pitch ~a")
436 ;; here we handle assigned strings
438 (calc-fret pitch string tuning))
440 (ly:context-property context
443 (cond ((or (and (>= this-fret 0) (integer? this-fret))
444 (eq? handle-negative 'include))
445 (set-fret! pitch-entry string finger))
446 ((eq? handle-negative 'recalculate)
449 (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
452 (ly:warning (_ "Ignoring string request and recalculating."))
453 (list-set! string-fret-fingers
457 (list '() #f finger)))))
458 ((eq? handle-negative 'ignore)
461 (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
464 (ly:warning (_ "Ignoring note in tablature."))
465 (kill-note! string-fret-fingers
466 (cdr pitch-entry))))))))))
467 pitch-alist string-fret-fingers)
468 ;; handle notes without strings assigned -- sorted by pitch, so
469 ;; we need to use the alist to have the note number available
471 (lambda (pitch-entry)
472 (let* ((string-fret-finger (list-ref string-fret-fingers
474 (string (list-ref string-fret-finger 0))
475 (finger (if (= (length string-fret-finger) 3)
476 (list-ref string-fret-finger 2)
478 (pitch (car pitch-entry))
480 (find (lambda (string)
481 (string-qualifies string pitch))
483 (if (not (list-ref string-fret-finger 1))
485 (set-fret! pitch-entry fit-string finger)
488 (list-ref notes (cdr pitch-entry))
489 (_ "No string for pitch ~a (given frets ~a)")
492 (kill-note! string-fret-fingers
493 (cdr pitch-entry)))))))
494 (sort pitch-alist (lambda (pitch-entry-a pitch-entry-b)
495 (ly:pitch<? (car pitch-entry-b)
496 (car pitch-entry-a)))))
497 string-fret-fingers)) ;; end of determine-frets-and-strings
499 (define (get-predefined-fretboard predefined-fret-table tuning pitches)
500 "Search through @var{predefined-fret-table} looking for a predefined
501 fretboard with a key of @var{(tuning . pitches)}. The search will check
502 both up and down an octave in order to accomodate transposition of the
503 chords. Returns a placement-list."
505 (define (get-fretboard key)
507 (hash-get-handle predefined-fret-table key)))
509 (cdr hash-handle) ; return table entry
512 ;; body of get-predefined-fretboard
513 (let ((test-fretboard (get-fretboard (cons tuning pitches))))
514 (if (not (null? test-fretboard))
516 (let ((test-fretboard
518 (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
519 (if (not (null? test-fretboard))
522 (cons tuning (map (lambda (x) (shift-octave x -1))
525 ;; body of determine-frets
526 (let* ((predefined-fret-table
527 (ly:context-property context 'predefinedDiagramTable))
528 (tunings (ly:context-property context 'stringTunings))
529 (string-count (length tunings))
530 (grob (if (null? rest) '() (car rest)))
531 (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
532 (defined-strings (map (lambda (x)
535 (or (string-number x) '())))
536 (car specified-info)))
537 (defined-fingers (map (lambda (x)
540 (ly:event-property x 'digit)))
541 (cadr specified-info)))
542 (default-strings (ly:context-property context 'defaultStrings '()))
543 (strings-used (if (and (zero? (entry-count defined-strings))
544 (not (zero? (entry-count default-strings))))
547 (predefined-fretboard
548 (if predefined-fret-table
549 (get-predefined-fretboard
550 predefined-fret-table
554 (if (null? predefined-fretboard)
556 (determine-frets-and-strings
560 (ly:context-property context 'minimumFret 0)
561 (ly:context-property context 'maximumFretStretch 4)
566 context grob (string-frets->placement-list
567 (filter (lambda (entry)
572 (placement-list->string-frets predefined-fretboard)
573 (create-fretboard context grob predefined-fretboard)))))
577 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
580 ;; The TabNoteHead tablatureFormat callbacks.
582 ;; Calculate the fret from pitch and string number as letter
583 ;; The fret letter is taken from 'fretLabels if present
584 (define-public (fret-letter-tablature-format
585 context string-number fret-number)
586 (let ((labels (ly:context-property context 'fretLabels)))
589 ((= 0 (length labels))
590 (string (integer->char (+ fret-number (char->integer #\a)))))
591 ((and (<= 0 fret-number) (< fret-number (length labels)))
592 (list-ref labels fret-number))
594 (ly:warning (_ "No label for fret ~a (on string ~a);
595 only ~a fret labels provided")
596 fret-number string-number (length labels))
599 ;; Display the fret number as a number
600 (define-public (fret-number-tablature-format
601 context string-number fret-number)
603 (format #f "~a" fret-number)))
605 ;; The 5-string banjo has got an extra string, the fifth (duh), which
606 ;; starts at the fifth fret on the neck. Frets on the fifth string
607 ;; are referred to relative to the other frets:
608 ;; the "first fret" on the fifth string is really the sixth fret
609 ;; on the banjo neck.
610 ;; We solve this by defining a new fret-number-tablature function:
611 (define-public (fret-number-tablature-format-banjo
612 context string-number fret-number)
614 (number->string (cond
615 ((and (> fret-number 0) (= string-number 5))
617 (else fret-number)))))
619 ;; Tab note head staff position functions
621 ;; Define where in the staff to display a given string. Some forms of
622 ;; tablature put the tab note heads in the spaces, rather than on the
625 (define-public (tablature-position-on-lines context string-number)
626 (let* ((string-tunings (ly:context-property context 'stringTunings))
627 (string-count (length string-tunings))
628 (string-one-topmost (ly:context-property context 'stringOneTopmost))
629 (staff-line (- (* 2 string-number) string-count 1)))
630 (if string-one-topmost
634 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
637 (define-public ((every-nth-bar-number-visible n) barnum mp)
638 (= 0 (modulo barnum n)))
640 (define-public ((modulo-bar-number-visible n m) barnum mp)
641 (and (> barnum 1) (= m (modulo barnum n))))
643 (define-public ((set-bar-number-visibility n) tr)
644 (let ((bn (ly:context-property tr 'currentBarNumber)))
645 (ly:context-set-property! tr 'barNumberVisibility
646 (modulo-bar-number-visible n (modulo bn n)))))
648 (define-public (first-bar-number-invisible barnum mp)
651 (define-public (first-bar-number-invisible-save-broken-bars barnum mp)
653 (> (ly:moment-main-numerator mp) 0)))
655 (define-public (first-bar-number-invisible-and-no-parenthesized-bar-numbers barnum mp)
657 (= (ly:moment-main-numerator mp) 0)))
659 (define-public (robust-bar-number-function barnum measure-pos alt-number context)
660 (define (get-number-and-power an pow)
661 (if (<= an alt-number)
662 (get-number-and-power (+ an (expt 26 (1+ pow))) (1+ pow))
663 (cons (+ alt-number (- (expt 26 pow) an)) (1- pow))))
664 (define (make-letter so-far an pow)
667 (let ((pos (modulo (quotient an (expt 26 pow)) 26)))
668 (make-letter (string-append so-far
669 (substring "abcdefghijklmnopqrstuvwxyz"
674 (let* ((number-and-power (get-number-and-power 0 0))
675 (begin-measure (= 0 (ly:moment-main-numerator measure-pos)))
676 (maybe-open-parenthesis (if begin-measure "" "("))
677 (maybe-close-parenthesis (if begin-measure "" ")")))
678 (markup (string-append maybe-open-parenthesis
679 (number->string barnum)
681 (car number-and-power)
682 (cdr number-and-power))
683 maybe-close-parenthesis))))
685 (define-public (all-bar-numbers-visible barnum mp) #t)
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
689 ;; percent repeat counters
691 (define-public ((every-nth-repeat-count-visible n) count context)
692 (= 0 (modulo count n)))
694 (define-public (all-repeat-counts-visible count context) #t)
696 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697 ;; make-engraver helper macro
699 (defmacro-public make-engraver forms
700 "Helper macro for creating Scheme engravers.
702 The usual form for an engraver is an association list (or alist)
703 mapping symbols to either anonymous functions or to another such
706 @code{make-engraver} accepts forms where the first element is either
707 an argument list starting with the respective symbol, followed by the
708 function body (comparable to the way @code{define} is used for
709 defining functions), or a single symbol followed by subordinate forms
710 in the same manner. You can also just make an alist pair
711 literally (the @samp{car} is quoted automatically) as long as the
712 unevaluated @samp{cdr} is not a pair. This is useful if you already
713 have defined your engraver functions separately.
715 Symbols mapping to a function would be @code{initialize},
716 @code{start-translation-timestep}, @code{process-music},
717 @code{process-acknowledged}, @code{stop-translation-timestep}, and
718 @code{finalize}. Symbols mapping to another alist specified in the
719 same manner are @code{listeners} with the subordinate symbols being
720 event classes, and @code{acknowledgers} and @code{end-acknowledgers}
721 with the subordinate symbols being interfaces."
722 (let loop ((forms forms))
723 (if (cheap-list? forms)
725 ,@(map (lambda (form)
726 (if (pair? (car form))
727 `(cons ',(caar form) (lambda ,(cdar form) ,@(cdr form)))
728 `(cons ',(car form) ,(loop (cdr form)))))