1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2009--2012 Marc Hohl <marc@hohlart.de>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 ;; for more control over glyph-name calculations,
20 ;; we use a custom callback for tab note heads
21 ;; which will ignore 'style = 'do
22 (define-public (tab-note-head::calc-glyph-name grob)
23 (let ((style (ly:grob-property grob 'style)))
28 ;; ensure we only call note head callback when
30 (define-public (tab-note-head::whiteout-if-style-set grob)
31 (let ((style (ly:grob-property grob 'style)))
33 (if (and (symbol? style)
35 (stencil-whiteout (ly:note-head::print grob))
36 (tab-note-head::print grob))))
38 ;; definitions for the "moderntab" clef:
39 ;; the "moderntab" clef will be added to the list of known clefs,
40 ;; so it can be used as any other clef: \clef "moderntab"
41 (add-new-clef "moderntab" "markup.moderntab" 0 0 0)
43 ;; define sans serif-style tab-Clefs as a markup:
44 (define-markup-command (customTabClef
45 layout props num-strings staff-space)
48 "Draw a tab clef sans-serif style."
49 (define (square x) (* x x))
50 (let* ((scale-factor (/ staff-space 1.5))
51 (font-size (- (* num-strings 1.5 scale-factor) 7))
52 (base-skip (* (square (+ (* num-strings 0.195) 0.4)) scale-factor)))
54 (interpret-markup layout props
55 (markup #:vcenter #:bold
56 #:override (cons 'font-family 'sans)
58 #:override (cons 'baseline-skip base-skip)
59 #:left-align #:center-column ("T" "A" "B")))))
61 ;; this function decides which clef to take
62 (define-public (clef::print-modern-tab-if-set grob)
63 (let ((glyph (ly:grob-property grob 'glyph)))
65 ;; which clef is wanted?
66 (if (string=? glyph "markup.moderntab")
67 ;; if it is "moderntab", we'll draw it
68 (let* ((staff-symbol (ly:grob-object grob 'staff-symbol))
69 (line-count (if (ly:grob? staff-symbol)
70 (ly:grob-property staff-symbol 'line-count)
72 (staff-space (ly:staff-symbol-staff-space grob)))
74 (grob-interpret-markup grob (make-customTabClef-markup line-count
76 ;; otherwise, we simply use the default printing routine
77 (ly:clef::print grob))))
79 ;; if stems are drawn, it is nice to have a double stem for
80 ;; (dotted) half notes to distinguish them from quarter notes:
81 (define-public (tabvoice::make-double-stem-width-for-half-notes grob)
82 (let ((X-extent (ly:stem::width grob)))
84 ;; is the note a (dotted) half note?
85 (if (= 1 (ly:grob-property grob 'duration-log))
86 ;; yes -> return double stem width
87 (cons (car X-extent) (+ 0.5 (* 2 (cdr X-extent))))
88 ;; no -> return simple stem width
91 (define-public (tabvoice::draw-double-stem-for-half-notes grob)
92 (let ((stem (ly:stem::print grob)))
94 ;; is the note a (dotted) half note?
95 (if (= 1 (ly:grob-property grob 'duration-log))
96 ;; yes -> draw double stem
97 (ly:stencil-combine-at-edge stem X RIGHT stem 0.5)
98 ;; no -> draw simple stem
101 ;; as default, the glissando line between fret numbers goes
102 ;; upwards, here we have a function to correct this behavior:
103 (define-public (glissando::calc-tab-extra-dy grob)
104 (let* ((original (ly:grob-original grob))
105 (left-bound (ly:spanner-bound original LEFT))
106 (right-bound (ly:spanner-bound original RIGHT))
107 (left-pitch (ly:event-property (event-cause left-bound) 'pitch))
108 (right-pitch (ly:event-property (event-cause right-bound) 'pitch)))
110 (if (< (ly:pitch-tones right-pitch) (ly:pitch-tones left-pitch))
114 ;; the handler for ties in tablature; according to TabNoteHead #'details,
115 ;; the 'tied to' note is handled differently after a line break
116 (define-public (tie::handle-tab-note-head grob)
117 (let* ((original (ly:grob-original grob))
118 (tied-tab-note-head (ly:spanner-bound grob RIGHT))
119 (spanner-start (ly:grob-property tied-tab-note-head 'span-start #f))
120 (siblings (if (ly:grob? original)
121 (ly:spanner-broken-into original) '())))
124 ;; tab note head is right bound of a tie and left of spanner,
125 ;; -> parenthesize it at all events
127 (ly:grob-set-property! tied-tab-note-head 'display-cautionary #t)
128 (ly:grob-set-property! tied-tab-note-head 'stencil tab-note-head::print))
129 ;; otherwise, check whether tie is split:
130 (if (and (>= (length siblings) 2)
131 (eq? (car (last-pair siblings)) grob))
132 ;; tie is split -> get TabNoteHead #'details
133 (let* ((details (ly:grob-property tied-tab-note-head 'details))
134 (tied-properties (assoc-get 'tied-properties details '()))
135 (tab-note-head-parenthesized (assoc-get 'parenthesize tied-properties #t))
136 ;; we need the begin-of-line entry in the 'break-visibility vector
137 (tab-note-head-visible
138 (vector-ref (assoc-get 'break-visibility
139 tied-properties #(#f #f #t)) 2)))
141 (if tab-note-head-visible
142 ;; tab note head is visible
143 (if tab-note-head-parenthesized
145 (ly:grob-set-property! tied-tab-note-head 'display-cautionary #t)
146 (ly:grob-set-property! tied-tab-note-head 'stencil tab-note-head::print)))
147 ;; tab note head is invisible
148 (ly:grob-set-property! tied-tab-note-head 'transparent #t)))
151 (ly:grob-set-property! tied-tab-note-head 'transparent #t)))))
155 ;; repeat ties occur within alternatives in a repeat construct;
156 ;; TabNoteHead #'details handles the appearance in this case
157 (define-public (repeat-tie::handle-tab-note-head grob)
158 (let* ((tied-tab-note-head (ly:grob-object grob 'note-head))
159 (spanner-start (ly:grob-property tied-tab-note-head 'span-start #f)))
161 ;; tab note head is between a tie and a slur/glissando
162 ;; -> parenthesize it at all events
164 (ly:grob-set-property! tied-tab-note-head 'display-cautionary #t)
165 (ly:grob-set-property! tied-tab-note-head 'stencil tab-note-head::print))
166 ;; otherwise check 'details
167 (let* ((details (ly:grob-property tied-tab-note-head 'details))
168 (repeat-tied-properties (assoc-get 'repeat-tied-properties details '()))
169 (tab-note-head-visible (assoc-get 'note-head-visible repeat-tied-properties #t))
170 (tab-note-head-parenthesized (assoc-get 'parenthesize repeat-tied-properties #t)))
172 (if tab-note-head-visible
173 ;; tab note head is visible
174 (if tab-note-head-parenthesized
176 (ly:grob-set-property! tied-tab-note-head 'display-cautionary #t)
177 (ly:grob-set-property! tied-tab-note-head 'stencil tab-note-head::print)))
178 ;; tab note head is invisible
179 (ly:grob-set-property! tied-tab-note-head 'transparent #t))))))
181 ;; the slurs should not be too far apart from the corresponding fret number, so
182 ;; we move the slur towards the TabNoteHeads; moreover, if the left fret number is
183 ;; the right-bound of a tie, we'll set it in parentheses:
184 (define-public (slur::draw-tab-slur grob)
185 ;; TODO: use a less "brute-force" method to decrease
186 ;; the distance between the slur ends and the fret numbers
187 (let* ((original (ly:grob-original grob))
188 (left-bound (ly:spanner-bound original LEFT))
189 (left-tab-note-head (ly:grob-property left-bound 'cause))
190 (staff-space (ly:staff-symbol-staff-space grob))
191 (control-points (ly:grob-property grob 'control-points))
192 (new-control-points (map
197 (ly:grob-property grob 'direction)
201 (ly:grob-set-property! grob 'control-points new-control-points)
202 (ly:slur::print grob)))
204 ;; The glissando routine works similarly to the slur routine; if the
205 ;; fret number is "tied to", it should become parenthesized.
206 (define-public (glissando::draw-tab-glissando grob)
207 (let* ((original (ly:grob-original grob))
208 (left-tab-note-head (ly:spanner-bound original LEFT))
209 (cautionary (ly:grob-property left-tab-note-head 'display-cautionary #f)))
212 ;; increase left padding to avoid collision between
213 ;; closing parenthesis and glissando line
214 (ly:grob-set-nested-property! grob '(bound-details left padding) 0.5))
215 (ly:line-spanner::print grob)))
217 ;; for \tabFullNotation, the stem tremolo beams are too big in comparison to
218 ;; normal staves; this wrapper function scales accordingly:
219 (define-public (stem-tremolo::calc-tab-width grob)
220 (let ((width (ly:stem-tremolo::calc-width grob))
221 (staff-space (ly:staff-symbol-staff-space grob)))
222 (/ width staff-space)))
225 ;; a callback for custom fret labels
226 (define-public ((tab-note-head::print-custom-fret-label fret) grob)
227 (ly:grob-set-property! grob 'text fret)
228 (tab-note-head::print grob))
230 (define-public (tab-note-head::print grob)
231 (define (is-harmonic? grob)
232 (let ((arts (ly:event-property (event-cause grob) 'articulations)))
233 (or (pair? (filter (lambda (a)
234 (ly:in-event-class? a 'harmonic-event))
236 (eq? (ly:grob-property grob 'style) 'harmonic))))
238 (let* ((cautionary (ly:grob-property grob 'display-cautionary #f))
239 (details (ly:grob-property grob 'details '()))
240 (harmonic-props (assoc-get 'harmonic-properties details '()))
241 (harmonic-angularity (assoc-get 'angularity harmonic-props 2))
242 (harmonic-half-thick (assoc-get 'half-thickness harmonic-props 0.075))
243 (harmonic-padding (assoc-get 'padding harmonic-props 0))
244 (harmonic-proc (assoc-get 'procedure harmonic-props parenthesize-stencil))
245 (harmonic-width (assoc-get 'width harmonic-props 0.25))
246 (cautionary-props (assoc-get 'cautionary-properties details '()))
247 (cautionary-angularity (assoc-get 'angularity cautionary-props 2))
248 (cautionary-half-thick (assoc-get 'half-thickness cautionary-props 0.075))
249 (cautionary-padding (assoc-get 'padding cautionary-props 0))
250 (cautionary-proc (assoc-get 'procedure cautionary-props parenthesize-stencil))
251 (cautionary-width (assoc-get 'width cautionary-props 0.25))
252 (output-grob (ly:text-interface::print grob))
253 (ref-grob (grob-interpret-markup grob "8"))
254 (offset-factor (assoc-get 'head-offset details 3/5))
255 (column-offset (* offset-factor
258 (grob-interpret-markup grob "8")
261 (if (is-harmonic? grob)
262 (set! output-grob (harmonic-proc output-grob
268 (set! output-grob (cautionary-proc output-grob
269 cautionary-half-thick
271 cautionary-angularity
272 cautionary-padding)))
273 (ly:stencil-translate-axis (centered-stencil output-grob)
277 ;; Harmonic definitions
279 (define node-positions
280 ;; for the node on m/n-th of the string length, we get the corresponding
281 ;; (exact) fret position by calculating p=(-12/log 2)*log(1-(m/n));
282 ;; since guitarists normally use the forth fret and not the 3.8th, here
283 ;; are rounded values, ordered by
287 ;; The value for 2/4 is irrelevant in practical, bacause the string sounds
288 ;; only one octave higher, not two, but since scheme normalizes the fractions
289 ;; anyway, these values are simply placeholders for easier indexing.
290 ;; According to the arithmetic sum, the position of m/n is at 1/2*(n-2)(n-1)+(m-1)
291 ;; if we start counting from zero
297 2.7 5.8 9.7 14.7 21.7 33.7
299 2 4.4 7 10 14 19 26 38 ))
301 (define partial-pitch
312 (define fret-partials
327 (define-public (ratio->fret ratio)
328 "Calculate a fret number given @var{ratio} for the harmonic."
329 (let* ((nom (numerator ratio))
330 (den (denominator ratio))
331 (index (+ (* (- den 2)
335 (number->string (vector-ref node-positions index))))
337 (define-public (ratio->pitch ratio)
338 "Calculate a pitch given @var{ratio} for the harmonic."
339 (let* ((partial (1- (denominator ratio)))
340 (pitch (vector-ref partial-pitch partial)))
342 (ly:make-pitch (first pitch)
346 (define-public (fret->pitch fret)
347 "Calculate a pitch given @var{fret} for the harmonic."
348 (let* ((partial (assoc-get fret fret-partials 0))
349 (pitch (vector-ref partial-pitch partial)))
351 (ly:make-pitch (first pitch)
355 (define-public (calc-harmonic-pitch pitch music)
356 "Calculate the harmonic pitches in @var{music} given
357 @var{pitch} as the non-harmonic pitch."
358 (let ((es (ly:music-property music 'elements))
359 (e (ly:music-property music 'element))
360 (p (ly:music-property music 'pitch)))
363 (ly:music-set-property! music 'elements
364 (map (lambda (x) (calc-harmonic-pitch pitch x)) es)))
366 (ly:music-set-property! music 'element (calc-harmonic-pitch pitch e)))
369 (set! p (ly:pitch-transpose p pitch))
370 (ly:music-set-property! music 'pitch p))))
373 (define-public (make-harmonic mus)
374 "Convert music variable @var{mus} to harmonics."
375 (let ((elts (ly:music-property mus 'elements))
376 (elt (ly:music-property mus 'element)))
379 (for-each make-harmonic elts))
382 ((music-is-of-type? mus 'note-event)
383 (set! (ly:music-property mus 'articulations)
385 (ly:music-property mus 'articulations)
386 (list (make-music 'HarmonicEvent))))))