]> git.donarmstrong.com Git - lilypond.git/blob - scm/tablature.scm
e32d712e19ae97986b84422a2e99b2fa8dd5fe22
[lilypond.git] / scm / tablature.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2009--2012 Marc Hohl <marc@hohlart.de>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18
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)))
24
25     (case style
26       ((cross) "2cross"))))
27
28 ;; ensure we only call note head callback when
29 ;; 'style = 'cross
30 (define-public (tab-note-head::whiteout-if-style-set grob)
31   (let ((style (ly:grob-property grob 'style)))
32
33     (if (and (symbol? style)
34              (eq? style 'cross))
35         (stencil-whiteout (ly:note-head::print grob))
36         (tab-note-head::print grob))))
37
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)
42
43 ;; define sans serif-style tab-Clefs as a markup:
44 (define-markup-command (customTabClef
45                         layout props num-strings staff-space)
46   (integer? number?)
47   #:category music
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)))
53
54     (interpret-markup layout props
55                       (markup #:vcenter #:bold
56                               #:override (cons 'font-family 'sans)
57                               #:fontsize font-size
58                               #:override (cons 'baseline-skip base-skip)
59                               #:left-align #:center-column ("T" "A" "B")))))
60
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)))
64
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)
71                                0))
72                (staff-space (ly:staff-symbol-staff-space grob)))
73
74           (grob-interpret-markup grob (make-customTabClef-markup line-count
75                                                                  staff-space)))
76         ;; otherwise, we simply use the default printing routine
77         (ly:clef::print grob))))
78
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)))
83
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
89         X-extent)))
90
91 (define-public (tabvoice::draw-double-stem-for-half-notes grob)
92   (let ((stem (ly:stem::print grob)))
93
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
99         stem)))
100
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)))
109
110     (if (< (ly:pitch-tones right-pitch) (ly:pitch-tones left-pitch))
111         -0.75
112         0.75)))
113
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) '())))
122
123     (if spanner-start
124         ;; tab note head is right bound of a tie and left of spanner,
125         ;; -> parenthesize it at all events
126         (begin
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)))
140
141               (if tab-note-head-visible
142                   ;; tab note head is visible
143                   (if tab-note-head-parenthesized
144                       (begin
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)))
149
150             ;; tie is not split
151             (ly:grob-set-property! tied-tab-note-head 'transparent #t)))))
152
153
154
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)))
160     (if spanner-start
161         ;; tab note head is between a tie and a slur/glissando
162         ;; -> parenthesize it at all events
163         (begin
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)))
171
172           (if tab-note-head-visible
173               ;; tab note head is visible
174               (if tab-note-head-parenthesized
175                   (begin
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))))))
180
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
193                               (lambda (p)
194                                 (cons (car p)
195                                       (- (cdr p)
196                                          (* staff-space
197                                             (ly:grob-property grob 'direction)
198                                             0.35))))
199                               control-points)))
200
201     (ly:grob-set-property! grob 'control-points new-control-points)
202     (ly:slur::print grob)))
203
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)))
210
211     (and cautionary
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)))
216
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)))
223
224
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))
229
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))
235                          arts))
236           (eq? (ly:grob-property grob 'style) 'harmonic))))
237
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
256                            (interval-length
257                             (ly:stencil-extent
258                              (grob-interpret-markup grob "8")
259                              X)))))
260
261     (if (is-harmonic? grob)
262         (set! output-grob (harmonic-proc output-grob
263                                          harmonic-half-thick
264                                          harmonic-width
265                                          harmonic-angularity
266                                          harmonic-padding)))
267     (if cautionary
268         (set! output-grob (cautionary-proc output-grob
269                                            cautionary-half-thick
270                                            cautionary-width
271                                            cautionary-angularity
272                                            cautionary-padding)))
273     (ly:stencil-translate-axis (centered-stencil output-grob)
274                                column-offset
275                                X)))
276
277 ;; Harmonic definitions
278
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
284   ;; 1/2
285   ;; 1/3 2/3
286   ;; 1/4 2/4 3/4 etc.
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
292   (vector 12
293           7   19
294           5   12    24
295           4    9    16   28
296           3    7    12   19    31
297           2.7  5.8  9.7  14.7  21.7  33.7
298           2.3  5    8    12    17    24    36
299           2    4.4  7    10    14    19    26  38 ))
300
301 (define partial-pitch
302   (vector '(0 0 0)
303           '(1 0 0)
304           '(1 4 0)
305           '(2 0 0)
306           '(2 2 0)
307           '(2 4 0)
308           '(2 6 -1/2)
309           '(3 0 0)
310           '(3 1 0)))
311
312 (define fret-partials
313   '(("0" . 0)
314     ("12" . 1)
315     ("7" . 2)
316     ("19" . 2)
317     ("5" . 3)
318     ("24" . 3)
319     ("4" . 4)
320     ("9" . 4)
321     ("16" . 4)
322     ("3" . 5)
323     ("2.7" . 6)
324     ("2.3" . 7)
325     ("2" . 8)))
326
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)
332                       (- den 1)
333                       1/2)
334                    nom -1)))
335     (number->string (vector-ref node-positions index))))
336
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)))
341
342     (ly:make-pitch (first pitch)
343                    (second pitch)
344                    (third pitch))))
345
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)))
350
351     (ly:make-pitch (first pitch)
352                    (second pitch)
353                    (third pitch))))
354
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)))
361     (cond
362      ((pair? es)
363       (ly:music-set-property! music 'elements
364                               (map (lambda (x) (calc-harmonic-pitch pitch x)) es)))
365      ((ly:music? e)
366       (ly:music-set-property! music 'element (calc-harmonic-pitch pitch e)))
367      ((ly:pitch? p)
368       (begin
369         (set! p (ly:pitch-transpose p pitch))
370         (ly:music-set-property! music 'pitch p))))
371     music))
372
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)))
377     (cond
378      ((pair? elts)
379       (for-each make-harmonic elts))
380      ((ly:music? elt)
381       (make-harmonic elt))
382      ((music-is-of-type? mus 'note-event)
383       (set! (ly:music-property mus 'articulations)
384             (append
385              (ly:music-property mus 'articulations)
386              (list (make-music 'HarmonicEvent))))))
387     mus))