]> git.donarmstrong.com Git - lilypond.git/blob - scm/tablature.scm
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scm / tablature.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2009 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 ;; default tunings for common string instruments
19 ;; guitar tunings
20 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
21 (define-public guitar-seven-string-tuning '(4 -1 -5 -10 -15 -20 -25))
22 (define-public guitar-drop-d-tuning '(4 -1 -5 -10 -15 -22))
23 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
24 (define-public guitar-open-d-tuning '(2 -3 -6 -10 -15 -22))
25 (define-public guitar-dadgad-tuning '(2 -3 -7 -10 -15 -22))
26 (define-public guitar-lute-tuning '(4 -1 -6 -10 -15 -20))
27 (define-public guitar-asus4-tuning '(4 -3 -8 -10 -15 -20))
28 ;; bass tunings
29 (define-public bass-tuning '(-17 -22 -27 -32))
30 (define-public bass-four-string-tuning '(-17 -22 -27 -32))
31 (define-public bass-drop-d-tuning '(-17 -22 -27 -34))
32 (define-public bass-five-string-tuning '(-17 -22 -27 -32 -37))
33 (define-public bass-six-string-tuning '(-12 -17 -22 -27 -32 -37))
34 ;; mandolin
35 (define-public mandolin-tuning '(16 9 2 -5))
36 ;; tunings for 5-string banjo
37 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
38 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
39 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
40 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
41 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
42 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
43 (define-public (four-string-banjo tuning)
44   (reverse (cdr (reverse tuning))))
45 ;; ukulele tunings
46 (define-public ukulele-tuning '(9 4 0 7)) ;ukulele  a' e' c' g'
47 (define-public ukulele-d-tuning '(11 6 2 9)) ;ukulele d tuning, b' fis' d' a'
48 (define-public ukulele-tenor-tuning '(-5 0 4 9)) ;tenor ukulele, g c' e' a'
49 (define-public ukulele-baritone-tuning '(-10 -5 -1 4)) ;baritone ukulele, d g b e'
50
51
52 ;; for more control over glyph-name calculations,
53 ;; we use a custom callback for tab note heads
54 ;; which will ignore 'style = 'do
55 (define-public (tab-note-head::calc-glyph-name grob)
56   (let ((style (ly:grob-property grob 'style)))
57
58     (case style
59       ((cross) "2cross"))))
60
61 ;; ensure we only call note head callback when
62 ;; 'style = 'cross
63 (define-public (tab-note-head::whiteout-if-style-set grob)
64   (let ((style (ly:grob-property grob 'style)))
65
66     (if (and (symbol? style)
67              (eq? style 'cross))
68         (stencil-whiteout (ly:note-head::print grob))
69         (ly:text-interface::print grob))))
70
71 ;; definitions for the "moderntab" clef:
72 ;; the "moderntab" clef will be added to the list of known clefs,
73 ;; so it can be used as any other clef: \clef "moderntab"
74 (add-new-clef "moderntab" "markup.moderntab" 0 0 0)
75
76 ;; define sans serif-style tab-Clefs as a markup:
77 (define-markup-command (customTabClef
78                                 layout props num-strings staff-space)
79   (integer? number?)
80   #:category music
81   "Draw a tab clef sans-serif style."
82   (define (square x) (* x x))
83   (let* ((scale-factor (/ staff-space 1.5))
84          (font-size (- (* num-strings 1.5 scale-factor) 7))
85          (base-skip (* (square (+ (* num-strings 0.195) 0.4)) scale-factor)))
86
87     (interpret-markup layout props
88                       (markup #:vcenter #:bold
89                               #:override (cons 'font-family 'sans)
90                               #:fontsize font-size
91                               #:override (cons 'baseline-skip base-skip)
92                               #:left-align #:center-column ("T" "A" "B")))))
93
94 ;; this function decides which clef to take
95 (define-public (clef::print-modern-tab-if-set grob)
96   (let ((glyph (ly:grob-property grob 'glyph)))
97
98     ;; which clef is wanted?
99     (if (string=? glyph "markup.moderntab")
100         ;; if it is "moderntab", we'll draw it
101         (let* ((staff-symbol (ly:grob-object grob 'staff-symbol))
102                (line-count (ly:grob-property staff-symbol 'line-count))
103                (staff-space (ly:staff-symbol-staff-space grob)))
104
105           (grob-interpret-markup grob (make-customTabClef-markup line-count
106                                                                  staff-space)))
107         ;; otherwise, we simply use the default printing routine
108         (ly:clef::print grob))))
109
110 ;; if stems are drawn, it is nice to have a double stem for
111 ;; (dotted) half notes to distinguish them from quarter notes:
112 (define-public (tabvoice::draw-double-stem-for-half-notes grob)
113   (let ((stem (ly:stem::print grob)))
114
115     ;; is the note a (dotted) half note?
116     (if (= 1 (ly:grob-property grob 'duration-log))
117         ;; yes -> draw double stem
118         (ly:stencil-combine-at-edge stem X RIGHT stem 0.5)
119         ;; no -> draw simple stem
120         stem)))
121
122 ;; as default, the glissando line between fret numbers goes
123 ;; upwards, here we have a function to correct this behavior:
124 (define-public (glissando::calc-tab-extra-dy grob)
125   (let* ((original (ly:grob-original grob))
126          (left-bound (ly:spanner-bound original LEFT))
127          (right-bound (ly:spanner-bound original RIGHT))
128          (left-pitch (ly:event-property (event-cause left-bound) 'pitch))
129          (right-pitch (ly:event-property (event-cause right-bound) 'pitch)))
130
131     (if (< (ly:pitch-semitones right-pitch) (ly:pitch-semitones left-pitch))
132         -0.75
133         0.75)))
134
135 ;; for ties in tablature, fret numbers that are tied to should be invisible,
136 ;; except for 'tied to' numbers after a line break;; these will be
137 ;; parenthesized (thanks to Neil for his solution):
138 (define-public (parenthesize-tab-note-head grob)
139   ;; Helper function to parenthesize tab noteheads,
140   ;; since we can't use ParenthesesItem at this stage
141   ;; This is basically the same as the C++ function
142   ;; in accidental.cc, converted to Scheme
143   (let* ((font (ly:grob-default-font grob))
144          (open (stencil-whiteout
145                 (ly:font-get-glyph font "accidentals.leftparen")))
146          (close (stencil-whiteout
147                  (ly:font-get-glyph font "accidentals.rightparen")))
148          (me (ly:text-interface::print grob)))
149
150     (ly:stencil-combine-at-edge
151      (ly:stencil-combine-at-edge me X LEFT open) X RIGHT close)))
152
153 ;; ParenthesesItem doesn't work very well for TabNoteHead, since
154 ;; the parentheses are too small and clash with the staff-lines
155 ;; Define a callback for the 'stencils property which will tweak
156 ;; the parentheses' appearance for TabNoteHead
157 (define-public (parentheses-item::calc-tabstaff-parenthesis-stencils grob)
158   ;; the grob we want to parenthesize
159   (let ((victim (ly:grob-array-ref (ly:grob-object grob 'elements) 0)))
160
161     ;; check whether it's a note head
162     (if (grob::has-interface victim 'note-head-interface)
163         (begin
164           ;; tweak appearance before retrieving
165           ;; list of stencils '(left-paren right-paren)
166           ;; get the font-size from victim (=TabNoteHead) to handle
167           ;; grace notes properly
168           (ly:grob-set-property! grob 'font-size
169                                  (ly:grob-property victim 'font-size))
170           (ly:grob-set-property! grob 'padding 0)
171           ;; apply whiteout to each element of the list
172           (map stencil-whiteout
173                (parentheses-item::calc-parenthesis-stencils grob)))
174         (parentheses-item::calc-parenthesis-stencils grob))))
175
176 ;; the handler for ties in tablature; according to TabNoteHead #'details,
177 ;; the 'tied to' note is handled differently after a line break
178 (define-public (tie::handle-tab-note-head grob)
179   (let* ((original (ly:grob-original grob))
180          (tied-tab-note-head (ly:spanner-bound grob RIGHT))
181          (siblings (if (ly:grob? original)
182                        (ly:spanner-broken-into original) '())))
183
184     (if (and (>= (length siblings) 2)
185              (eq? (car (last-pair siblings)) grob))
186         ;; tie is split -> get TabNoteHead #'details
187         (let* ((details (ly:grob-property tied-tab-note-head 'details))
188                (tied-properties (assoc-get 'tied-properties details '()))
189                (tab-note-head-parenthesized (assoc-get 'parenthesize tied-properties #t))
190                ;; we need the begin-of-line entry in the 'break-visibility vector
191                (tab-note-head-visible
192                 (vector-ref (assoc-get 'break-visibility
193                                        tied-properties #(#f #f #t)) 2)))
194
195           (if tab-note-head-visible
196               ;; tab note head is visible
197               (if tab-note-head-parenthesized
198                   (ly:grob-set-property! tied-tab-note-head 'stencil
199                                          (lambda (grob)
200                                            (parenthesize-tab-note-head grob))))
201               ;; tab note head is invisible
202               (ly:grob-set-property! tied-tab-note-head 'transparent #t)))
203
204         ;; tie is not split -> make fret number invisible
205         (ly:grob-set-property! tied-tab-note-head 'transparent #t))))
206
207 ;; repeat ties occur within alternatives in a repeat construct;
208 ;; TabNoteHead #'details handles the appearance in this case
209 (define-public (repeat-tie::handle-tab-note-head grob)
210   (let* ((tied-tab-note-head (ly:grob-object grob 'note-head))
211          (details (ly:grob-property tied-tab-note-head 'details))
212          (repeat-tied-properties (assoc-get 'repeat-tied-properties details '()))
213          (tab-note-head-visible (assoc-get 'note-head-visible repeat-tied-properties #t))
214          (tab-note-head-parenthesized (assoc-get 'parenthesize repeat-tied-properties #t)))
215
216     (if tab-note-head-visible
217         ;; tab note head is visible
218         (if tab-note-head-parenthesized
219             (ly:grob-set-property! tied-tab-note-head 'stencil
220                                    (lambda (grob)
221                                      (parenthesize-tab-note-head grob))))
222         ;; tab note head is invisible
223         (ly:grob-set-property! tied-tab-note-head 'transparent #t))))
224
225 ;; the slurs should not be too far apart from the corresponding fret number, so
226 ;; we move the slur towards the TabNoteHeads:
227 (define-public (slur::draw-tab-slur grob)
228   ;; TODO: use a less "brute-force" method to decrease
229   ;; the distance between the slur ends and the fret numbers
230   (let* ((staff-space (ly:staff-symbol-staff-space grob))
231          (control-points (ly:grob-property grob 'control-points))
232          (new-control-points (map
233                               (lambda (p)
234                                 (cons (car p)
235                                       (- (cdr p)
236                                          (* staff-space
237                                             (ly:grob-property grob 'direction)
238                                             0.35))))
239                               control-points)))
240
241     (ly:grob-set-property! grob 'control-points new-control-points)
242     (ly:slur::print grob)))