1 ;;;; fret-diagrams.scm --
\r
3 ;;;; source file of the GNU LilyPond music typesetter
\r
5 ;;;; (c) 2004 Carl D. Sorensen <c_sorensen@byu.edu>
\r
8 (define (fret-parse-marking-list marking-list fret-count)
\r
9 (let* ((fret-range (list 1 fret-count))
\r
14 (let parse-item ((mylist marking-list))
\r
15 (if (not (null? mylist))
\r
16 (let* ((my-item (car mylist)) (my-code (car my-item)))
\r
18 ((or (eq? my-code 'open)(eq? my-code 'mute))
\r
19 (set! xo-list (cons* my-item xo-list)))
\r
20 ((eq? my-code 'barre)
\r
21 (set! barre-list (cons* (cdr my-item) barre-list)))
\r
22 ((eq? my-code 'place-fret)
\r
23 (set! dot-list (cons* (cdr my-item) dot-list))))
\r
24 (parse-item (cdr mylist)))))
\r
25 ; calculate fret-range
\r
26 (let ((maxfret 0) (minfret 99))
\r
27 (let updatemax ((fret-list dot-list))
\r
28 (if (null? fret-list)
\r
30 (let ((fretval (second (car fret-list))))
\r
31 (if (> fretval maxfret) (set! maxfret fretval))
\r
32 (if (< fretval minfret) (set! minfret fretval))
\r
33 (updatemax (cdr fret-list)))))
\r
34 (if (> maxfret fret-count)
\r
35 (set! fret-range (list minfret
\r
36 (let ((upfret (- (+ minfret fret-count) 1)))
\r
37 (if (> maxfret upfret) maxfret upfret)))))
\r
38 ; subtract fret from dots
\r
39 (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))
\r
40 ; (display "barre-list ") (display barre-list) (display "\n")
\r
41 (acons 'fret-range fret-range
\r
42 (acons 'barre-list barre-list
\r
43 (acons 'dot-list dot-list
\r
44 (acons 'xo-list xo-list '()))))))
\r
46 (define (subtract-base-fret base-fret dot-list)
\r
47 "Subtract @var{base-fret} from every fret in @var{dot-list}"
\r
48 (if (null? dot-list)
\r
50 (let ((this-list (car dot-list)))
\r
51 (cons* (list (car this-list) (- (second this-list) base-fret) (if (null? (cddr this-list))
\r
54 (subtract-base-fret base-fret (cdr dot-list))))))
\r
56 (define (draw-strings string-count fret-range th size)
\r
57 "Draw the strings (vertical lines) for a fret diagram with @var{string-count} strings and frets as indicated
\r
58 in @var{fret-range}. Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
\r
59 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
\r
60 (sl (* (+ fret-count 1) size))
\r
62 (half-thickness (* sth 0.5))
\r
64 (string-stencil (ly:make-stencil (list 'draw-line sth 0 0 0 sl)
\r
65 (cons (- half-thickness) half-thickness)
\r
66 (cons (- half-thickness) (+ sl half-thickness)))))
\r
67 (if (= string-count 1)
\r
69 (ly:stencil-combine-at-edge
\r
70 (draw-strings (- string-count 1) fret-range th size) X RIGHT
\r
74 (define (draw-fret-lines fret-count string-count th size)
\r
75 "Draw @var{fret-count} frets (horizontal lines) for a fret diagram with @var{string-count} strings.
\r
76 Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
\r
77 (let* ((fret-length (* (- string-count 1) size))
\r
79 (half-thickness (* sth 0.5))
\r
81 (fret-line (ly:make-stencil (list 'draw-line sth half-thickness size (- fret-length half-thickness) size)
\r
82 (cons 0 fret-length)
\r
83 (cons (- size half-thickness) (+ size half-thickness)))))
\r
84 (if (= fret-count 1)
\r
86 (ly:stencil-combine-at-edge fret-line Y UP
\r
87 (draw-fret-lines (- fret-count 1) string-count th size)
\r
90 (define (draw-thick-top-fret props string-count th size)
\r
91 "Draw a thick top fret for a fret diagram whose base fret is not 1."
\r
92 (let* ((sth (* th size))
\r
93 (top-fret-thick (* sth (chain-assoc-get 'top-fret-thickness props 3.0)))
\r
94 (top-half-thick (* top-fret-thick 0.5))
\r
95 (half-thick (* sth 0.5))
\r
97 (x2 (+ half-thick (* size (- string-count 1))))
\r
100 (x-extent (cons (- x1) x2))
\r
101 (y-extent (cons 0 y2)))
\r
102 (ly:make-stencil (list 'round-filled-box x1 x2 y1 y2 th)
\r
103 x-extent y-extent)))
\r
106 (define (draw-frets paper props fret-range string-count th size)
\r
107 "Draw the frets (horizontal lines) for a fret diagram with @var{string-count} strings and frets as indicated
\r
108 in @var{fret-range}. Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
\r
109 (let* ((top-fret-thick (* th (chain-assoc-get 'top-fret-thickness props 3.0)))
\r
110 (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
\r
111 (fret-length (* (- string-count 1) size))
\r
112 (half-thickness (* th 0.5))
\r
113 (base-fret (car fret-range)))
\r
114 (ly:stencil-combine-at-edge
\r
115 (draw-fret-lines fret-count string-count th size) Y UP
\r
116 (if (= base-fret 1)
\r
117 (draw-thick-top-fret props string-count th size)
\r
118 (draw-fret-lines 1 string-count th size))
\r
121 (define (get-center-shift extent)
\r
122 "Return the amount of shift required to make @var{extent} be centered"
\r
123 (let ((minval (car extent))
\r
124 (maxval (cdr extent)))
\r
125 (/ (- minval maxval) 2)))
\r
128 ;; ly:stencil-get-extent is missing from 2.2 on
\r
129 ;(define (centered-text-stencil font text)
\r
130 ;"Create a centered text stencil of @var{text} in font @var{font}"
\r
131 ;(let* ((text-stencil (fontify-text font text))
\r
132 ; (x-shift (get-center-shift (ly:stencil-get-extent text-stencil X)))
\r
133 ; (y-shift (get-center-shift y-extent (ly:stencil-get-extent text-stencil Y))))
\r
134 ; (ly:stencil-translate-axis
\r
135 ; (ly:stencil-translate-axis text-stencil x-shift X)
\r
138 (define (draw-dots paper props string-count fret-range size dot-position dot-list)
\r
139 "Make dots for fret diagram."
\r
140 ; (display "In draw-dots" dot-list)
\r
141 (let* ((finger-code (chain-assoc-get 'finger-code props 'none))
\r
142 (dot-radius (* size (chain-assoc-get 'dot-radius props 0.25)))
\r
143 (finger-xoffset (chain-assoc-get 'finger-xoffset props -0.25))
\r
144 (finger-yoffset (chain-assoc-get 'finger-yoffset props (- size)))
\r
145 (label-font-name (chain-assoc-get 'label-font-name props "cmss8"))
\r
146 (dot-label-font-mag (* size (chain-assoc-get 'dot-label-font-mag props .45)))
\r
147 (dot-circle-font-mag (* size (chain-assoc-get 'dot-circle-font-mag props .75)))
\r
148 (string-label-font-mag (* size (chain-assoc-get 'string-label-font-mag props 0.6)))
\r
149 (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
\r
150 (mypair (car dot-list))
\r
151 (restlist (cdr dot-list))
\r
152 (xpos (* size (- string-count (car mypair))))
\r
153 ;TODO -- figure out what 4 is and get rid of it
\r
155 (ypos (* size (+ 4 (- fret-count (cadr mypair) dot-position ))))
\r
156 (finger (caddr mypair))
\r
157 (finger (if (number? finger) (number->string finger) finger))
\r
158 (string-label-font (ly:paper-get-font paper `(((font-magnification . ,string-label-font-mag)
\r
159 (font-name . ,label-font-name)))))
\r
160 (dot-label-font (ly:paper-get-font paper `(((font-magnification . ,dot-label-font-mag)
\r
161 (font-name . ,label-font-name)))))
\r
162 (dot-circle-font (ly:paper-get-font paper `(((font-magnification . ,dot-circle-font-mag)
\r
163 (font-name . ,label-font-name)))))
\r
164 (extent (cons (- dot-radius) dot-radius))
\r
165 (dotstencil (if (or (eq? finger '())(eq? finger-code 'none))
\r
166 (ly:make-stencil (list 'dot xpos ypos dot-radius ) extent extent)
\r
167 (if (eq? finger-code 'white-circled)
\r
168 ; TODO -- Get nice circled numbers in the font, instead of this kludge
\r
169 ; UGH -- Constants in here need to go
\r
170 ; Note: for finger (this stencil) xpos should be related only to font size, ypos should be related to both font size and dot-position
\r
171 ; These are not yet worked out, and should be. But perhaps I'd like first to get a nice font for circled numbers / letters
\r
173 (ly:stencil-translate-axis
\r
174 (ly:stencil-translate-axis
\r
175 (fontify-text dot-label-font finger) (- xpos (* size 0.2)) X)
\r
176 (- ypos (* 1 dot-radius size)) Y)
\r
177 ; These lines are a better way to do it, but they require ly:stencil-get-extent, which is missing from 2.2.0
\r
178 ; (centered-text-stencil dot-label-font finger) xpos X)
\r
180 ;UGH -- Constants in xpos and ypos need to go. xpos should be related to font mag,
\r
181 ; ypos should be related to both font magnification and dot-position. Again, I may want to wait for the nice font with
\r
182 ; convenient zero point (right at the center of the dot).
\r
183 ;TODO -- Alternatively, perhaps I should query for the extent of the glyph,
\r
184 ; and place it accordingly. That actually sounds better
\r
185 (ly:stencil-translate-axis
\r
186 (ly:stencil-translate-axis
\r
187 (fontify-text dot-circle-font "O") (- xpos (* 0.6 dot-circle-font-mag)) X)
\r
188 (- ypos (* 0.5 dot-circle-font-mag)) Y))
\r
189 ; These lines are a better way to do it, but they require ly:stencil-get-exten, which is missing from 2.2.0
\r
190 ; (centered-text-stencil dot-circle-font "O") xpos X)
\r
192 (if (eq? finger-code 'below-string)
\r
194 (ly:make-stencil (list 'dot xpos ypos dot-radius ) extent extent)
\r
195 (ly:stencil-translate-axis
\r
196 (ly:stencil-translate-axis
\r
197 (fontify-text string-label-font finger) (+ xpos (* size finger-xoffset)) X)
\r
198 (* size finger-yoffset) Y)))))))
\r
199 (if (null? restlist)
\r
201 (ly:stencil-add (draw-dots paper props string-count fret-range size dot-position restlist)
\r
204 (define (draw-xo paper props string-count fret-range size xo-list)
\r
205 "Put open and mute string indications on diagram, as contained in @var{xo-list}."
\r
206 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
\r
207 (xo-font-mag (* size (chain-assoc-get 'xo-font-magnification props 0.5)))
\r
208 (xo-font-name (chain-assoc-get 'xo-font-name props "cmss8"))
\r
209 (xo-horizontal-offset (* size (chain-assoc-get 'xo-horizontal-offset props -0.35)))
\r
210 (font (ly:paper-get-font paper `(((font-magnification . ,xo-font-mag)
\r
211 (font-name . ,xo-font-name)))))
\r
212 (mypair (car xo-list))
\r
213 (restlist (cdr xo-list))
\r
214 (glyph-string (if (eq? (car mypair) 'mute) "X" "O"))
\r
215 (xpos (+ (* (- string-count (cadr mypair)) size) xo-horizontal-offset ))
\r
216 (glyph-stencil (ly:stencil-translate-axis (fontify-text font glyph-string) xpos X)))
\r
217 (if (null? restlist)
\r
220 (draw-xo paper props string-count fret-range size restlist)
\r
223 (define (make-bezier-sandwich-list left right bottom height thickness)
\r
224 " Make the argument list for a horizontal bezier sandwich from @var{left} to @var{right} with a bottom at @var{bottom},
\r
225 a height of @var{height}, and a thickness of @var{thickness}."
\r
226 (let* ((width (+ (- right left) 1))
\r
227 (x1 (+ (* width thickness) left))
\r
228 (x2 (- right (* width thickness)))
\r
229 (bottom-control-point-height (+ bottom (- height thickness)))
\r
230 (top-control-point-height (+ bottom height)))
\r
231 ; order of points is: left cp low, right cp low, right end low, left end low
\r
232 ; right cp high, left cp high, left end high, right end high.
\r
233 (list (cons x1 bottom-control-point-height) (cons x2 bottom-control-point-height) (cons right bottom) (cons left bottom)
\r
234 (cons x2 top-control-point-height) (cons x1 top-control-point-height) (cons left bottom) (cons right bottom))))
\r
236 (define (draw-barre paper props string-count fret-range size dot-position barre-list)
\r
237 "Create barre indications for a fret diagram"
\r
238 (if (not (null? barre-list))
\r
239 (let* ((string1 (caar barre-list))
\r
240 (string2 (cadar barre-list))
\r
241 (fret (caddar barre-list))
\r
242 ; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant
\r
243 (bottom (* size (- (+ 2 (- (cadr fret-range) fret)) dot-position)))
\r
244 (left (* size (- string-count string1)))
\r
245 (right (* size (- string-count string2)))
\r
246 (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
\r
247 (bezier-height (chain-assoc-get 'bezier-height props 0.5))
\r
248 (bezier-list (make-bezier-sandwich-list left right bottom (* size bezier-height) (* size bezier-thick)))
\r
249 (sandwich-stencil (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick) )
\r
251 (cons 0 (+ bottom (* size bezier-height))))))
\r
252 (if (not (null? (cdr barre-list)))
\r
253 (ly:stencil-add sandwich-stencil
\r
254 (draw-barre paper props string-count fret-range size dot-position (cdr barre-list)))
\r
255 sandwich-stencil ))))
\r
257 (define (label-fret paper props string-count fret-range size)
\r
258 "Label the base fret on a fret diagram"
\r
259 (let* ((base-fret (car fret-range))
\r
260 (label-font-mag (chain-assoc-get 'fret-label-font-magnification props 0.8))
\r
261 (label-horizontal-offset (chain-assoc-get 'fret-label-horizontal-offset props -0.5))
\r
262 (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))
\r
263 (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
\r
264 (font (ly:paper-get-font paper `(((font-magnification . ,(* label-font-mag size))(font-name . "cmss8")
\r
265 (font-encoding Tex-text))))))
\r
266 (ly:stencil-translate-axis
\r
267 (ly:stencil-translate-axis (fontify-text font (if (> base-fret 1)
\r
268 (format #f "~(~:@r~)" base-fret)
\r
269 " ")) (* size (+ string-count label-horizontal-offset)) X)
\r
270 (* size (+ fret-count label-vertical-offset)) Y)))
\r
272 (def-markup-command (fret-diagram-verbose paper props marking-list)
\r
274 "Make a fret diagram containing the symbols indicated in @var{marking-list}
\r
275 Syntax: \\fret-diagram marking-list
\r
278 \\markup \\fret-diagram #\'((mute 6) (mute 5) (open 4) (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
\r
280 will produce a standard D chord diagram without fingering indications.
\r
281 Possible elements in @var{marking-list}:
\r
282 (mute string-number) -- place a small 'x' at the top of string @var{string-number}
\r
283 (open string-number) -- place a small 'o' at the top of string @var{string-number}
\r
284 (barre start-string end-string fret-number) -- place a barre indicator (much like a tie) from string @var{start-string}
\r
285 to string @var{end-string} at fret @var{fret-number}
\r
286 (place-fret string-number fret-number finger-value) -- place a fret playing indication
\r
287 on string @var{string-number} at fret @var{fret-number} with an optional fingering label @var{finger-value}
\r
288 By default, the fret playing indicator is a solid dot. If the @var{finger} part of the place-fret element is present,
\r
289 @var{finger-value} will be displayed according to the setting of the variable @var{finger-code}
\r
290 There is no limit to the number of fret indications per string."
\r
291 (make-fret-diagram paper props marking-list))
\r
293 (define (make-fret-diagram paper props marking-list)
\r
294 " Make a fret diagram markup"
\r
296 ; note: here we get items from props that are needed in this routine, or that are needed in more than one
\r
297 ; of the procedures called from this routine. If they're only used in one of the sub-procedure, they're
\r
298 ; obtained in that procedure
\r
300 (size (chain-assoc-get 'size props 1.0)) ; needed for everything
\r
301 ;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available
\r
302 (string-count (chain-assoc-get 'string-count props 6)) ; needed for everything
\r
303 (fret-count (chain-assoc-get 'fret-count props 4)) ; needed for everything
\r
304 (dot-position (chain-assoc-get 'dot-position props 0.6)) ; needed for both draw-dots and draw-barre
\r
305 (th (* (ly:paper-lookup paper 'linethickness)
\r
306 (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings
\r
308 (alignment (chain-assoc-get 'alignment props -0.4)) ; needed only here
\r
309 (xo-padding (* th (chain-assoc-get 'xo-padding props 2))) ; needed only here
\r
311 (parameters (fret-parse-marking-list marking-list fret-count))
\r
312 (dot-list (cdr (assoc 'dot-list parameters)))
\r
313 (xo-list (cdr (assoc 'xo-list parameters)))
\r
314 (fret-range (cdr (assoc 'fret-range parameters)))
\r
315 (barre-list (cdr (assoc 'barre-list parameters)))
\r
316 (fret-diagram-stencil (ly:stencil-add
\r
317 (draw-strings string-count fret-range th size)
\r
318 (draw-frets paper props fret-range string-count th size))))
\r
319 (if (not (null? dot-list))
\r
320 (set! fret-diagram-stencil (ly:stencil-add
\r
321 (draw-dots paper props string-count fret-range size dot-position dot-list)
\r
322 fret-diagram-stencil)))
\r
323 (if (not (null? xo-list))
\r
324 (set! fret-diagram-stencil (ly:stencil-combine-at-edge
\r
325 fret-diagram-stencil Y UP
\r
326 (draw-xo paper props string-count fret-range size xo-list) xo-padding 0)))
\r
327 (if (not (null? barre-list))
\r
328 (set! fret-diagram-stencil (ly:stencil-add
\r
329 (draw-barre paper props string-count fret-range size dot-position barre-list)
\r
330 fret-diagram-stencil)))
\r
331 (set! fret-diagram-stencil (ly:stencil-add fret-diagram-stencil (label-fret paper props string-count fret-range size)))
\r
332 (ly:stencil-align-to! fret-diagram-stencil X alignment)
\r
333 fret-diagram-stencil))
\r
335 (def-markup-command (fret-diagram paper props size definition-string)
\r
337 "Syntax: \\fret-diagram size definition-string
\r
338 eg: \\markup \\fret-diagram #0.75 #\"6-x;5-x;4-o;3-2;2-3;1-2;\"
\r
339 for fret spacing 3/4 of staff space, D chord diagram
\r
340 Syntax rules for @var{definition-string}:
\r
341 Diagram items are separated by semicolons.
\r
343 t:number -- set the line thickness (in staff spaces). Default 0.05
\r
344 h:number -- set the height of the diagram in frets. Default 4
\r
345 w:number -- set the width of the diagram in strings. Default 6
\r
346 f:number -- set fingering label type
\r
347 (0 = none, 1 = in circle on string, 2 = below string) Default 0
\r
348 d:number -- set radius of dot, in terms of fret spacing. Default 0.25
\r
349 p:number -- set the position of the dot in the fret space. 0.5 is centered; 1 is on lower fret bar,
\r
350 0 is on upper fret bar. Default 0.6
\r
351 c:string1-string2-fret -- include a barre mark from string1 to string2 on fret
\r
352 string-fret -- place a dot on string at fret. If fret is o, string is identified
\r
353 as open. If fret is x, string is identified as muted.
\r
354 string-fret-fingering -- place a dot on string at fret, and label with fingering as
\r
355 defined by f: code.
\r
356 Note: There is no limit to the number of fret indications per string."
\r
357 (set! props (acons 'size size props))
\r
358 (let ((definition-list (parse-definition-string props definition-string)))
\r
359 (make-fret-diagram paper (car definition-list) (cdr definition-list))))
\r
361 (define (parse-definition-string props definition-string)
\r
362 "parse a fret diagram string and return a pair containing:
\r
363 props, modified as necessary by the definition-string
\r
364 a fret-indication list with the appropriate values"
\r
365 (let* ((fret-count 4)
\r
371 (fret-range (list 1 fret-count))
\r
376 (items (string-split definition-string #\;)))
\r
377 (let parse-item ((myitems items))
\r
378 (if (not (null? (cdr myitems)))
\r
379 (let ((test-string (car myitems)))
\r
380 (case (car (string->list (substring test-string 0 1)))
\r
381 ((#\f) (let* ((finger-code (get-numeric-from-key test-string))
\r
382 (finger-id (case finger-code
\r
384 ((1) 'white-circled)
\r
385 ((2) 'below-string))))
\r
387 (acons 'finger-code finger-id props))))
\r
388 ((#\t) (let ((thickness (get-numeric-from-key test-string)))
\r
389 (set! props (acons 'thickness thickness props))))
\r
390 ((#\c) (set! output-list (cons-fret (cons 'barre (numerify (string-split (substring test-string 2) #\-)))
\r
392 ((#\h) (let ((fret-count (get-numeric-from-key test-string)))
\r
393 (set! props (acons 'fret-count fret-count props))))
\r
394 ((#\w) (let ((string-count (get-numeric-from-key test-string)))
\r
395 (set! props (acons 'string-count string-count props))))
\r
396 ((#\d) (let ((dot-size (get-numeric-from-key test-string)))
\r
397 (set! props (acons 'dot-radius dot-size props))))
\r
398 ((#\p) (let ((dot-position (get-numeric-from-key test-string)))
\r
399 (set! props (acons 'dot-position dot-position props))))
\r
401 (let ((this-list (string-split test-string #\-)))
\r
402 (if (string->number (cadr this-list))
\r
403 (set! output-list (cons-fret (cons 'place-fret (numerify this-list)) output-list))
\r
404 (if (equal? (cadr this-list) "x" )
\r
405 (set! output-list (cons-fret (list 'mute (string->number (car this-list))) output-list))
\r
406 (set! output-list (cons-fret (list 'open (string->number (car this-list))) output-list)))))))
\r
407 (parse-item (cdr myitems)))))
\r
408 `((,props) . ,output-list)))
\r
410 (define (cons-fret new-value old-list)
\r
411 " Put together a fret-list in the format desired by parse-string "
\r
412 (if (eq? old-list '())
\r
414 (cons* new-value old-list)))
\r
416 (define (get-numeric-from-key keystring)
\r
417 "Get the numeric value from a key of the form k:val"
\r
418 (string->number (substring keystring 2 (string-length keystring) )))
\r
420 (define (numerify mylist)
\r
421 "Convert string values to numeric or character"
\r
424 (let ((numeric-value (string->number (car mylist))))
\r
426 (cons* numeric-value (numerify (cdr mylist)))
\r
427 (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))
\r