1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; (c) 1998--2015 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 (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)))
46 (metronome-markup text dur count hide-note)))
47 (export format-metronome-markup)
49 (define (metronome-markup text dur count hide-note)
51 (if (and (not hide-note) (ly:duration? dur))
53 (make-note-by-number-markup
55 (ly:duration-dot-count dur)
58 (count-markup (cond ((number? count)
61 (number->string count))
67 (number->string (car count)))
68 (make-simple-markup " ")
69 (make-simple-markup "–")
70 (make-simple-markup " ")
72 (number->string (cdr count))))))
74 (note-markup (if (and (not hide-note) count-markup)
77 (make-general-align-markup Y DOWN note-mark)
78 (make-simple-markup " ")
79 (make-simple-markup "=")
80 (make-simple-markup " ")
83 (text-markup (if (not (null? text))
84 (make-bold-markup text)
87 (if (and note-markup (not hide-note))
88 (make-line-markup (list text-markup
90 (list (make-simple-markup "(")
92 (make-simple-markup ")")))))
93 (make-line-markup (list text-markup)))
95 (make-line-markup (list note-markup))
96 (make-null-markup)))))
98 (define-public (format-mark-alphabet mark context)
99 (make-bold-markup (make-markalphabet-markup (1- mark))))
101 (define-public (format-mark-box-alphabet mark context)
102 (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
104 (define-public (format-mark-circle-alphabet mark context)
105 (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
107 (define-public (format-mark-letters mark context)
108 (make-bold-markup (make-markletter-markup (1- mark))))
110 (define-public (format-mark-numbers mark context)
111 (make-bold-markup (number->string mark)))
113 (define-public (format-mark-barnumbers mark context)
114 (make-bold-markup (number->string (ly:context-property context
115 'currentBarNumber))))
117 (define-public (format-mark-box-letters mark context)
118 (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
120 (define-public (format-mark-circle-letters mark context)
121 (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
123 (define-public (format-mark-box-numbers mark context)
124 (make-bold-markup (make-box-markup (number->string mark))))
126 (define-public (format-mark-circle-numbers mark context)
127 (make-bold-markup (make-circle-markup (number->string mark))))
129 (define-public (format-mark-box-barnumbers mark context)
130 (make-bold-markup (make-box-markup
131 (number->string (ly:context-property context
132 'currentBarNumber)))))
134 (define-public (format-mark-circle-barnumbers mark context)
135 (make-bold-markup (make-circle-markup
136 (number->string (ly:context-property context
137 'currentBarNumber)))))
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 (define-public (format-bass-figure figure event context)
144 (let* ((fig (ly:event-property event 'figure))
145 (fig-markup (if (number? figure)
147 ;; this is not very elegant, but center-aligning
148 ;; all digits is problematic with other markups,
149 ;; and shows problems in the (lack of) overshoot
150 ;; of feta-alphabet glyphs.
152 (lambda (y) (make-translate-scaled-markup
157 ((eq? #t (ly:event-property event 'diminished))
158 (markup #:slashed-digit figure))
159 ((eq? #t (ly:event-property event 'augmented-slash))
160 (markup #:backslashed-digit figure))
161 (else (markup #:number (number->string figure 10)))))
164 (alt (ly:event-property event 'alteration))
168 #:general-align Y DOWN #:fontsize
169 (if (not (= alt DOUBLE-SHARP))
171 (alteration->text-accidental-markup alt))
174 (plus-markup (if (eq? #t (ly:event-property event 'augmented))
175 (markup #:number "+")
178 (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
179 (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
181 (if (and (not fig-markup) alt-markup)
183 (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
184 (set! alt-markup #f)))
187 ;; hmm, how to get figures centered between note, and
188 ;; lone accidentals too?
190 ;; (if (markup? fig-markup)
192 ;; fig-markup (markup #:translate (cons 1.0 0)
193 ;; #:center-align fig-markup)))
197 (markup #:put-adjacent
198 X (if (number? alt-dir)
202 #:pad-x 0.2 alt-markup)))
207 (markup #:put-adjacent
208 X (if (number? plus-dir)
212 #:pad-x 0.2 plus-markup)
215 (if (markup? fig-markup)
216 (markup #:fontsize -2 fig-markup)
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 (define (create-fretboard context grob placement-list)
224 "Convert @var{placement-list} into a fretboard @var{grob}."
226 (let* ((tunings (ly:context-property context 'stringTunings))
227 (my-string-count (length tunings))
228 (details (ly:grob-property grob 'fret-diagram-details)))
230 ;; Add string-count from string-tunings to fret-diagram-details.
231 (set! (ly:grob-property grob 'fret-diagram-details)
232 (acons 'string-count my-string-count details))
233 ;; Create the dot-placement list for the grob
234 (set! (ly:grob-property grob 'dot-placement-list) placement-list)))
237 (determine-frets context notes specified-info . rest)
238 "Determine string numbers and frets for playing @var{notes}
239 as a chord, given specified information @var{specified-info}.
240 @var{specified-info} is a list with two list elements,
241 specified strings @code{defined-strings} and
242 specified fingerings @code{defined-fingers}. Only a fingering of@tie{}0
243 will affect the fret selection, as it specifies an open string.
244 If @code{defined-strings} is @code{'()}, the context property
245 @code{defaultStrings} will be used as a list of defined strings.
246 Will look for predefined fretboards if @code{predefinedFretboardTable}
247 is not @code {#f}. If @var{rest} is present, it contains the
248 @code{FretBoard} grob, and a fretboard will be
249 created. Otherwise, a list of @code{(string fret finger)} lists will
251 If the context-property @code{supportNonIntegerFret} is set @code{#t},
252 micro-tones are supported for TabStaff, but not not for FretBoards."
256 (define (string-frets->placement-list string-frets string-count)
257 "Convert @var{string-frets} to @code{fret-diagram-verbose}
258 dot placement entries."
259 (let* ((placements (list->vector
260 (map (lambda (x) (list 'mute x))
261 (iota string-count 1)))))
263 (for-each (lambda (sf)
264 (let* ((string (car sf))
273 (list 'place-fret string fret finger)
274 (list 'place-fret string fret))))))
276 (vector->list placements)))
278 (define (placement-list->string-frets placement-list)
279 "Convert @var{placement-list} to string-fret list."
280 (map (lambda (x) (if (eq? (car x) 'place-fret)
283 (filter (lambda (l) (or (eq? (car l) 'place-fret)
284 (eq? (car l) 'open)))
287 (define (entry-count art-list)
288 "Count the number of entries in a list of articulations."
289 (length (filter (lambda (x) (not (null? x)))
292 (define (string-number event)
293 "Get the string-number from @var{event}. Return @var{#f}
294 if no string-number is present."
295 (let ((num (ly:event-property event 'string-number)))
296 (and (integer? num) (positive? num) num)))
298 (define (determine-frets-and-strings
305 "Determine the frets and strings used to play the notes in
306 @var{notes}, given @var{defined-strings} and @var{defined-fingers}
307 along with @var{minimum-fret}, @var{maximum-stretch}, and
308 @var{tuning}. Returns a list of @code{(string fret finger) lists."
311 (define restrain-open-strings (ly:context-property context
314 (define specified-frets '())
315 (define free-strings (iota (length tuning) 1))
317 (define (calc-fret pitch string tuning)
318 "Calculate the fret to play @var{pitch} on @var{string} with
320 (* 2 (- (ly:pitch-tones pitch) (ly:pitch-tones (list-ref tuning (1- string))))))
322 (define (note-pitch note)
323 "Get the pitch (in semitones) from @var{note}."
324 (ly:event-property note 'pitch))
326 (define (note-finger ev)
327 "Get the fingering from @var{ev}. Return @var{#f}
328 if no fingering is present."
329 (let* ((articulations (ly:event-property ev 'articulations))
331 (for-each (lambda (art)
332 (let* ((num (ly:event-property art 'digit)))
334 (if (and (ly:in-event-class? art 'fingering-event)
337 (set! finger-found num))))
341 (define (delete-free-string string)
344 (delete string free-strings))))
346 (define (close-enough fret)
347 "Decide if @var{fret} is acceptable, given the already used frets."
348 (every (lambda (specced-fret)
349 (or (zero? specced-fret)
351 (>= maximum-stretch (abs (- fret specced-fret)))))
354 (define (string-qualifies string pitch)
355 "Can @var{pitch} be played on @var{string}, given already placed
357 (let* ((fret (calc-fret pitch string tuning)))
358 (and (or (and (not restrain-open-strings)
360 (>= fret minimum-fret))
362 (ly:context-property context 'supportNonIntegerFret #f)
364 (integer? (truncate fret))
366 (close-enough fret))))
368 (define (open-string string pitch)
369 "Is @var{pitch} and open-string note on @var{string}, given
371 (let* ((fret (calc-fret pitch string tuning)))
374 (define (set-fret! pitch-entry string finger)
375 (let ((this-fret (calc-fret (car pitch-entry)
379 (ly:warning (_ "Negative fret for pitch ~a on string ~a")
380 (car pitch-entry) string)
382 (not (integer? this-fret))
383 (not (ly:context-property context 'supportNonIntegerFret #f)))
384 (ly:warning (_ "Missing fret for pitch ~a on string ~a")
385 (car pitch-entry) string)))
386 (delete-free-string string)
387 (set! specified-frets (cons this-fret specified-frets))
388 (list-set! string-fret-fingers
390 (list string this-fret finger))))
392 (define (kill-note! string-fret-fingers note-index)
393 (list-set! string-fret-fingers note-index (list #f #t)))
395 (define string-fret-fingers
396 (map (lambda (string finger)
399 (list string #f finger)))
400 defined-strings defined-fingers))
402 ;;; body of determine-frets-and-strings
403 (let* ((pitches (map note-pitch notes))
404 (pitch-alist (map cons pitches (iota (length pitches)))))
406 ;; handle notes with strings assigned and fingering of 0
408 (lambda (pitch-entry string-fret-finger)
409 (let* ((string (list-ref string-fret-finger 0))
410 (finger (if (= (length string-fret-finger) 3)
411 (list-ref string-fret-finger 2)
413 (pitch (car pitch-entry))
414 (digit (if (null? finger)
417 (if (or (not (null? string))
420 ;; here we handle fingers of 0 -- open strings
422 (find (lambda (string)
423 (open-string string pitch))
426 (set-fret! pitch-entry fit-string #f)
427 (ly:warning (_ "No open string for pitch ~a")
429 ;; here we handle assigned strings
431 (calc-fret pitch string tuning))
433 (ly:context-property context
436 (cond ((or (and (>= this-fret 0) (integer? this-fret))
437 (eq? handle-negative 'include))
438 (set-fret! pitch-entry string finger))
439 ((eq? handle-negative 'recalculate)
442 (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
445 (ly:warning (_ "Ignoring string request and recalculating."))
446 (list-set! string-fret-fingers
450 (list '() #f finger)))))
451 ((eq? handle-negative 'ignore)
454 (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
457 (ly:warning (_ "Ignoring note in tablature."))
458 (kill-note! string-fret-fingers
459 (cdr pitch-entry))))))))))
460 pitch-alist string-fret-fingers)
461 ;; handle notes without strings assigned -- sorted by pitch, so
462 ;; we need to use the alist to have the note number available
464 (lambda (pitch-entry)
465 (let* ((string-fret-finger (list-ref string-fret-fingers
467 (string (list-ref string-fret-finger 0))
468 (finger (if (= (length string-fret-finger) 3)
469 (list-ref string-fret-finger 2)
471 (pitch (car pitch-entry))
473 (find (lambda (string)
474 (string-qualifies string pitch))
476 (if (not (list-ref string-fret-finger 1))
478 (set-fret! pitch-entry fit-string finger)
481 (list-ref notes (cdr pitch-entry))
482 (_ "No string for pitch ~a (given frets ~a)")
485 (kill-note! string-fret-fingers
486 (cdr pitch-entry)))))))
487 (sort pitch-alist (lambda (pitch-entry-a pitch-entry-b)
488 (ly:pitch<? (car pitch-entry-b)
489 (car pitch-entry-a)))))
490 string-fret-fingers)) ;; end of determine-frets-and-strings
492 (define (get-predefined-fretboard predefined-fret-table tuning pitches)
493 "Search through @var{predefined-fret-table} looking for a predefined
494 fretboard with a key of @var{(tuning . pitches)}. The search will check
495 both up and down an octave in order to accomodate transposition of the
496 chords. Returns a placement-list."
498 (define (get-fretboard key)
500 (hash-get-handle predefined-fret-table key)))
502 (cdr hash-handle) ; return table entry
505 ;; body of get-predefined-fretboard
506 (let ((test-fretboard (get-fretboard (cons tuning pitches))))
507 (if (not (null? test-fretboard))
509 (let ((test-fretboard
511 (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
512 (if (not (null? test-fretboard))
515 (cons tuning (map (lambda (x) (shift-octave x -1))
518 ;; body of determine-frets
519 (let* ((predefined-fret-table
520 (ly:context-property context 'predefinedDiagramTable))
521 (tunings (ly:context-property context 'stringTunings))
522 (string-count (length tunings))
523 (grob (if (null? rest) '() (car rest)))
524 (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
525 (defined-strings (map (lambda (x)
528 (or (string-number x) '())))
529 (car specified-info)))
530 (defined-fingers (map (lambda (x)
533 (ly:event-property x 'digit)))
534 (cadr specified-info)))
535 (default-strings (ly:context-property context 'defaultStrings '()))
536 (strings-used (if (and (zero? (entry-count defined-strings))
537 (not (zero? (entry-count default-strings))))
540 (predefined-fretboard
541 (if predefined-fret-table
542 (get-predefined-fretboard
543 predefined-fret-table
547 (if (null? predefined-fretboard)
549 (determine-frets-and-strings
553 (ly:context-property context 'minimumFret 0)
554 (ly:context-property context 'maximumFretStretch 4)
559 context grob (string-frets->placement-list
560 (filter (lambda (entry)
565 (placement-list->string-frets predefined-fretboard)
566 (create-fretboard context grob predefined-fretboard)))))
570 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
573 ;; The TabNoteHead tablatureFormat callbacks.
575 ;; Calculate the fret from pitch and string number as letter
576 ;; The fret letter is taken from 'fretLabels if present
577 (define-public (fret-letter-tablature-format
578 context string-number fret-number)
579 (let ((labels (ly:context-property context 'fretLabels)))
580 (make-translate-scaled-markup '(0 . -0.5)
582 ((= 0 (length labels))
583 (string (integer->char (+ fret-number (char->integer #\a)))))
584 ((and (<= 0 fret-number) (< fret-number (length labels)))
585 (list-ref labels fret-number))
587 (ly:warning (_ "No label for fret ~a (on string ~a);
588 only ~a fret labels provided")
589 fret-number string-number (length labels))
592 ;; Display the fret number as a number
593 (define-public (fret-number-tablature-format
594 context string-number fret-number)
595 (if (integer? fret-number)
597 (format #f "~a" fret-number))
598 ;; for non-integer fret-number print p.e. "2½"
599 (let* ((whole-part (truncate fret-number))
600 (remaining (- fret-number whole-part))
602 (if (and (zero? whole-part) (not (zero? remaining)))
604 (format #f "~a" whole-part)))
606 (if (zero? remaining)
608 (format #f "~a" remaining))))
610 (list (make-vcenter-markup fret)
612 ;; the value `-2.5' is my choice
613 (make-fontsize-markup -2.5 frac)))))))
615 ;; The 5-string banjo has got an extra string, the fifth (duh), which
616 ;; starts at the fifth fret on the neck. Frets on the fifth string
617 ;; are referred to relative to the other frets:
618 ;; the "first fret" on the fifth string is really the sixth fret
619 ;; on the banjo neck.
620 ;; We solve this by defining a new fret-number-tablature function:
621 (define-public (fret-number-tablature-format-banjo
622 context string-number fret-number)
624 (number->string (cond
625 ((and (> fret-number 0) (= string-number 5))
627 (else fret-number)))))
629 ;; Tab note head staff position functions
631 ;; Define where in the staff to display a given string. Some forms of
632 ;; tablature put the tab note heads in the spaces, rather than on the
635 (define-public (tablature-position-on-lines context string-number)
636 (let* ((string-tunings (ly:context-property context 'stringTunings))
637 (string-count (length string-tunings))
638 (string-one-topmost (ly:context-property context 'stringOneTopmost))
639 (staff-line (- (* 2 string-number) string-count 1)))
640 (if string-one-topmost
644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
647 (define ((every-nth-bar-number-visible n) barnum mp)
648 (= 0 (modulo barnum n)))
649 (export every-nth-bar-number-visible)
651 (define ((modulo-bar-number-visible n m) barnum mp)
652 (and (> barnum 1) (= m (modulo barnum n))))
653 (export modulo-bar-number-visible)
655 (define ((set-bar-number-visibility n) tr)
656 (let ((bn (ly:context-property tr 'currentBarNumber)))
657 (ly:context-set-property! tr 'barNumberVisibility
658 (modulo-bar-number-visible n (modulo bn n)))))
659 (export set-bar-number-visibility)
661 (define-public (first-bar-number-invisible barnum mp)
664 (define-public (first-bar-number-invisible-save-broken-bars barnum mp)
666 (> (ly:moment-main-numerator mp) 0)))
668 (define-public (first-bar-number-invisible-and-no-parenthesized-bar-numbers barnum mp)
670 (= (ly:moment-main-numerator mp) 0)))
672 (define-public (robust-bar-number-function barnum measure-pos alt-number context)
673 (define (get-number-and-power an pow)
674 (if (<= an alt-number)
675 (get-number-and-power (+ an (expt 26 (1+ pow))) (1+ pow))
676 (cons (+ alt-number (- (expt 26 pow) an)) (1- pow))))
677 (define (make-letter so-far an pow)
680 (let ((pos (modulo (quotient an (expt 26 pow)) 26)))
681 (make-letter (string-append so-far
682 (substring "abcdefghijklmnopqrstuvwxyz"
687 (let* ((number-and-power (get-number-and-power 0 0))
688 (begin-measure (= 0 (ly:moment-main-numerator measure-pos)))
689 (maybe-open-parenthesis (if begin-measure "" "("))
690 (maybe-close-parenthesis (if begin-measure "" ")")))
691 (markup (string-append maybe-open-parenthesis
692 (number->string barnum)
694 (car number-and-power)
695 (cdr number-and-power))
696 maybe-close-parenthesis))))
698 (define-public (all-bar-numbers-visible barnum mp) #t)
701 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
702 ;; percent repeat counters
704 (define ((every-nth-repeat-count-visible n) count context)
705 (= 0 (modulo count n)))
706 (export every-nth-repeat-count-visible)
708 (define-public (all-repeat-counts-visible count context) #t)
710 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
713 (define-public (make-semitone->pitch pitches)
714 "Convert @var{pitches}, an unordered list of note values
715 covering (after disregarding octaves) all absolute pitches in need of
716 conversion, into a function converting semitone numbers (absolute
717 pitch missing enharmonic information) back into note values.
719 For a key signature without accidentals
721 c cis d es e f fis g gis a bes b
723 might be a good choice, covering Bb major to A major and their
724 parallel keys, and melodic/harmonic C minor to A minor."
725 ;; TODO: short-circuit lcm calculation once we know it will be large
726 (let* ((size (apply lcm (map (lambda (pitch)
727 (denominator (/ (ly:pitch-tones pitch) 6)))
729 ;; Normal tunings need 12 steps per octave, quartertone
730 ;; tunings 24, Makam needs 108. But microtunings might cause
732 (lookup (if (> size 400)
734 (make-vector size #f))))
737 (let* ((rawoct (/ (ly:pitch-tones pitch) 6))
740 (val (ly:pitch-transpose pitch
741 (ly:make-pitch (- oct) 0))))
742 (if (hash-table? lookup)
743 (hashv-set! lookup ref val)
744 (vector-set! lookup (* size ref) val))))
747 "Convert @var{semitone} numbers into note values. If the
748 originally specified list of pitches does not contain a note
749 corresponding to @var{semitone} (disregarding octaves), @code{#f} is
751 (let* ((rawoct (/ semitone 12))
754 (val (if (hash-table? lookup)
755 (hashv-ref lookup ref)
756 (let ((ref (* (vector-length lookup) ref)))
758 (vector-ref lookup ref))))))
760 (ly:pitch-transpose val (ly:make-pitch oct 0)))))))
762 (define ((shift-semitone->pitch key semitone->pitch) semitone)
763 "Given a function @var{semitone->pitch} converting a semitone number
764 into a note value for a lookup table created in relation to@tie{}C,
765 returns a corresponding function in relation to @var{key}. The note
766 values returned by this function differ only enharmonically from the
767 original @var{semitone->pitch} function."
768 (ly:pitch-transpose (semitone->pitch (- semitone (* 2 (ly:pitch-tones key))))
771 (export shift-semitone->pitch)