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