1 ;;;; fret-diagrams.scm --
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2007 Carl D. Sorensen <c_sorensen@byu.edu>
7 (define (fret-parse-marking-list marking-list fret-count)
8 (let* ((fret-range (list 1 fret-count))
13 (let parse-item ((mylist marking-list))
14 (if (not (null? mylist))
15 (let* ((my-item (car mylist)) (my-code (car my-item)))
17 ((or (eq? my-code 'open)(eq? my-code 'mute))
18 (set! xo-list (cons* my-item xo-list)))
20 (set! barre-list (cons* (cdr my-item) barre-list)))
21 ((eq? my-code 'place-fret)
22 (set! dot-list (cons* (cdr my-item) dot-list))))
23 (parse-item (cdr mylist)))))
25 ;; calculate fret-range
26 (let ((maxfret 0) (minfret 99))
27 (let updatemax ((fret-list dot-list))
30 (let ((fretval (second (car fret-list))))
31 (if (> fretval maxfret) (set! maxfret fretval))
32 (if (< fretval minfret) (set! minfret fretval))
33 (updatemax (cdr fret-list)))))
34 (if (> maxfret fret-count)
35 (set! fret-range (list minfret
36 (let ((upfret (- (+ minfret fret-count) 1)))
37 (if (> maxfret upfret) maxfret upfret)))))
38 ; subtract fret from dots
39 (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))
40 (acons 'fret-range fret-range
41 (acons 'barre-list barre-list
42 (acons 'dot-list dot-list
43 (acons 'xo-list xo-list '()))))))
47 (define (subtract-base-fret base-fret dot-list)
48 "Subtract @var{base-fret} from every fret in @var{dot-list}"
51 (let ((this-list (car dot-list)))
52 (cons* (list (car this-list) (- (second this-list) base-fret) (if (null? (cddr this-list))
55 (subtract-base-fret base-fret (cdr dot-list))))))
57 (define (sans-serif-stencil layout props mag text)
58 "create a stencil in sans-serif font based on @var{layout} and @var{props}
59 with magnification @varr{mag} of the string @var{text}."
60 (let* ((my-props (prepend-alist-chain 'font-size (stepmag mag)
61 (prepend-alist-chain 'font-family 'sans props))))
62 (interpret-markup layout my-props text)))
65 (define (draw-strings string-count fret-range th size orientation)
66 "Draw the string lines for a fret diagram with
67 @var{string-count} strings and frets as indicated in @var{fret-range}.
68 Line thickness is given by @var{th}, fret & string spacing by
69 @var{size}. Orientation is determined by @var{orientation}. "
70 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
71 (sl (* (+ fret-count 1) size))
73 (half-thickness (* sth 0.5))
76 (if (eq? orientation 'normal)
77 (ly:make-stencil (list 'draw-line sth 0 0 0 sl)
78 (cons (- half-thickness) half-thickness)
79 (cons (- half-thickness) (+ sl half-thickness)))
80 (ly:make-stencil (list 'draw-line sth 0 0 sl 0)
81 (cons (- half-thickness) (+ sl half-thickness))
82 (cons (- half-thickness) half-thickness)))))
83 (if (= string-count 1)
85 (if (eq? orientation 'normal)
86 (ly:stencil-combine-at-edge
87 (draw-strings (- string-count 1) fret-range th size orientation) X RIGHT
90 (ly:stencil-combine-at-edge
91 (draw-strings (- string-count 1) fret-range th size orientation) Y UP
95 (define (draw-fret-lines fret-count string-count th size orientation)
96 "Draw @var{fret-count} fret lines for a fret diagram
97 with @var{string-count} strings. Line thickness is given by @var{th},
98 fret & string spacing by @var{size}. Orientation is given by @var{orientation}"
99 (let* (;(fret-length (* (- string-count 1) size))
101 ;(half-thickness (* sth 0.5))
103 (fret-line (draw-fret-line string-count th size orientation)))
106 (if (eq? orientation 'normal)
107 (ly:stencil-combine-at-edge (draw-fret-lines (- fret-count 1) string-count th size orientation) Y UP
110 (ly:stencil-combine-at-edge (draw-fret-lines (- fret-count 1) string-count th size orientation) X RIGHT
114 (define (draw-fret-line string-count th size orientation)
115 "Draw a fret line for a fret diagram."
116 (let* ((fret-length (* (- string-count 1) size))
118 (half-thickness (* sth 0.5)))
119 (if (eq? orientation 'normal)
120 (ly:make-stencil (list 'draw-line sth half-thickness size
121 (- fret-length half-thickness) size)
123 (cons (- half-thickness) half-thickness))
124 (ly:make-stencil (list 'draw-line sth 0 half-thickness
125 0 (- fret-length half-thickness))
126 (cons (- half-thickness) half-thickness)
127 (cons 0 fret-length)))))
129 (define (draw-thick-zero-fret props string-count th size orientation)
130 "Draw a thick zeroth fret for a fret diagram whose base fret is not 1."
131 (let* ((sth (* th size))
132 ; (top-fret-thick (* sth (chain-assoc-get 'top-fret-thickness props 3.0)))
133 (top-fret-thick (* sth 3.0))
134 ; (top-half-thick (* top-fret-thick 0.5))
135 (half-thick (* sth 0.5))
137 (x2 (+ half-thick (* size (- string-count 1))))
139 (y2 (+ top-fret-thick half-thick))
140 (x-extent (cons (- x1) x2))
141 (y-extent (cons 0 y2)))
142 (if (eq? orientation 'normal)
143 (ly:make-stencil (list 'round-filled-box x1 x2 y1 y2 sth)
145 (ly:make-stencil (list 'round-filled-box y1 y2 x1 x2 sth)
146 y-extent x-extent))))
149 (define (draw-frets layout props fret-range string-count th size orientation)
150 "Draw the fret lines for a fret diagram with
151 @var{string-count} strings and frets as indicated in @var{fret-range}.
152 Line thickness is given by @var{th}, fret & string spacing by
153 @var{size}. Orientation is given by @var{orientation}."
154 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
155 (fret-length (* (- string-count 1) size))
156 (half-thickness (* th 0.5))
157 (base-fret (car fret-range))
158 (fret-zero (draw-fret-line string-count th size orientation)))
159 (if (eq? orientation 'normal)
160 (ly:stencil-combine-at-edge
161 (draw-fret-lines fret-count string-count th size orientation) Y UP
164 (ly:stencil-combine-at-edge
166 (draw-fret-lines fret-count string-count th size orientation)
170 (define (draw-dots layout props string-count fret-count fret-range size finger-code
171 dot-position dot-radius dot-thickness dot-list orientation)
172 "Make dots for fret diagram."
174 (let* ((scale-dot-radius (* size dot-radius))
175 (scale-dot-thick (* size dot-thickness))
176 (dot-color (chain-assoc-get 'dot-color props 'black))
177 ; (finger-xoffset (chain-assoc-get 'finger-xoffset props -0.25))
178 ; (finger-yoffset (chain-assoc-get 'finger-yoffset props (- size)))
179 (finger-xoffset -0.25)
180 (finger-yoffset (- (* size 0.5)))
181 ; (dot-label-font-mag (* scale-dot-radius (chain-assoc-get 'dot-label-font-mag props 1.0)))
182 (dot-label-font-mag scale-dot-radius)
183 ; (string-label-font-mag (* size (chain-assoc-get 'label-font-mag props 0.7)))
184 (string-label-font-mag (* size 0.6))
185 ; (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
186 (mypair (car dot-list))
187 (restlist (cdr dot-list))
188 (string (car mypair))
190 (xpos (* size (if (eq? orientation 'normal)
191 (- string-count string)
192 (+ (- fret 1 ) dot-position))))
193 ;TODO -- figure out what 4 is and get rid of it
195 (ypos (* size (if (eq? orientation 'normal)
196 (+ 2 (- fret-count fret dot-position ))
197 (- string-count string))))
198 (extent (cons (- scale-dot-radius) scale-dot-radius))
199 (finger (caddr mypair))
200 (finger (if (number? finger) (number->string finger) finger))
201 (dotstencil (if (eq? dot-color 'white)
203 (make-circle-stencil scale-dot-radius scale-dot-thick #t)
206 (- scale-dot-radius (* 0.5 scale-dot-thick)) 0 #t)
208 (make-circle-stencil scale-dot-radius scale-dot-thick #t)))
209 (positioned-dot (begin
210 ;(display dotstencil)
211 (ly:stencil-translate-axis
212 (ly:stencil-translate-axis dotstencil xpos X)
216 (if (or (eq? finger '())(eq? finger-code 'none))
218 (if (eq? finger-code 'in-dot)
219 (let* ((finger-label (centered-stencil
220 (sans-serif-stencil layout props
221 dot-label-font-mag finger))))
222 (ly:stencil-translate-axis
223 (ly:stencil-translate-axis
226 (if (eq? dot-color 'white)
228 (ly:stencil-in-color finger-label 1 1 1)))
231 (if (eq? finger-code 'below-string)
234 (if (eq? orientation 'normal)
235 (ly:stencil-translate-axis
236 (ly:stencil-translate-axis
237 (centered-stencil (sans-serif-stencil layout props
238 string-label-font-mag finger))
240 (* size finger-yoffset) Y)
241 (ly:stencil-translate-axis
242 (ly:stencil-translate-axis
243 (centered-stencil (sans-serif-stencil layout props
244 string-label-font-mag finger))
245 (* size (+ 2 fret-count finger-yoffset)) X)
252 (draw-dots layout props string-count fret-count fret-range size finger-code
253 dot-position dot-radius dot-thickness restlist orientation)
254 labeled-dot-stencil))))
256 (define (draw-xo layout props string-count fret-range size xo-list orientation)
257 "Put open and mute string indications on diagram, as contained in @var{xo-list}."
258 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
259 (xo-font-mag (chain-assoc-get 'xo-font-magnification props 0.5))
261 ; (xo-horizontal-offset (* size (chain-assoc-get 'xo-horizontal-offset props -0.35)))
262 (xo-horizontal-offset (* size -0.35))
263 (mypair (car xo-list))
264 (restlist (cdr xo-list))
265 (glyph-string (if (eq? (car mypair) 'mute)
266 (chain-assoc-get 'mute-string props "X")
267 (chain-assoc-get 'open-string props "O")))
268 (xpos (+ (* (- string-count (cadr mypair)) size) xo-horizontal-offset ))
269 (glyph-stencil (if (eq? orientation 'normal)
270 (ly:stencil-translate-axis
271 (sans-serif-stencil layout props (* size xo-font-mag) glyph-string)
273 (ly:stencil-translate-axis
274 (sans-serif-stencil layout props (* size xo-font-mag) glyph-string)
276 ;(display "def-xo-mag" default-xo-magnification)
280 (draw-xo layout props string-count fret-range size restlist orientation)
283 (define (make-bezier-sandwich-list start stop base height thickness orientation)
284 " Make the argument list for a bezier sandwich from
285 @var{start} to @var{stop} with a baseline at @var{base}, a height of
286 @var{height}, and a thickness of @var{thickness}. If @var{orientation} is
287 @var{'normal}, @var{base} is a y coordinate, otherwise it's an x coordinate."
288 (let* ((width (+ (- stop start) 1))
289 (x1 (+ (* width thickness) start))
290 (x2 (- stop (* width thickness)))
291 (bottom-control-point-height (if (eq? orientation 'normal)
292 (+ base (- height thickness))
293 (- base (- height thickness))))
294 (top-control-point-height (if (eq? orientation 'normal)
297 ; order of points is: left cp low, right cp low, right end low, left end low
298 ; right cp high, left cp high, left end high, right end high.
299 (if (eq? orientation 'normal)
300 (list (cons x1 bottom-control-point-height)
301 (cons x2 bottom-control-point-height)
304 (cons x2 top-control-point-height)
305 (cons x1 top-control-point-height)
308 (list (cons bottom-control-point-height x1)
309 (cons bottom-control-point-height x2)
312 (cons top-control-point-height x2)
313 (cons top-control-point-height x1)
317 (define (draw-barre layout props string-count fret-range size finger-code dot-position dot-radius barre-list orientation)
318 "Create barre indications for a fret diagram"
319 (if (not (null? barre-list))
320 (let* ((string1 (caar barre-list))
321 (string2 (cadar barre-list))
322 (fret (caddar barre-list))
323 (top-fret (cadr fret-range))
324 (low-fret (car fret-range))
325 (barre-type (chain-assoc-get 'barre-type props 'curved))
326 (scale-dot-radius (* size dot-radius))
327 (barre-vertical-offset 0.5)
328 ; (barre-vertical-offset (chain-assoc-get 'barre-vertical-offset props 0.5))
329 ;; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant
330 (dot-center-y (* size
331 (- (+ 2 (- (cadr fret-range) fret)) dot-position)))
332 (dot-center-fret-coordinate (+ (- fret low-fret) dot-position))
333 (barre-fret-coordinate (+ dot-center-fret-coordinate (* (- barre-vertical-offset 0.5) dot-radius)))
334 (barre-start-string-coordinate (- string-count string1))
335 (barre-end-string-coordinate (- string-count string2))
336 (bottom (+ dot-center-y (* barre-vertical-offset scale-dot-radius)))
337 (left (* size (- string-count string1)))
338 (right (* size (- string-count string2)))
339 ;; (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
341 ;; (bezier-height (chain-assoc-get 'bezier-height props 0.5))
343 (bezier-list (if (eq? orientation 'normal)
344 (make-bezier-sandwich-list
345 (* size barre-start-string-coordinate)
346 (* size barre-end-string-coordinate)
347 (* size (+ 1 (- top-fret fret) barre-fret-coordinate))
348 (* size bezier-height)
349 (* size bezier-thick)
351 (make-bezier-sandwich-list
352 (* size barre-start-string-coordinate)
353 (* size barre-end-string-coordinate)
354 (* size barre-fret-coordinate)
355 (* size bezier-height)
356 (* size bezier-thick)
358 (barre-stencil (if (eq? barre-type 'straight)
359 (if (eq? orientation 'normal)
360 (ly:make-stencil (list 'draw-line (* size dot-radius) left dot-center-y right dot-center-y)
362 (cons (- dot-center-y scale-dot-radius) (+ dot-center-y scale-dot-radius)))
363 (ly:make-stencil (list 'draw-line (* size dot-radius)
364 (* size barre-fret-coordinate)
365 (* size barre-start-string-coordinate)
366 (* size barre-fret-coordinate)
367 (* size barre-end-string-coordinate))
368 (cons (- (* size barre-fret-coordinate) scale-dot-radius)
369 (+ (* size barre-fret-coordinate) scale-dot-radius))
370 (cons (* size barre-start-string-coordinate)
371 (* size barre-end-string-coordinate))))
372 (if (eq? orientation 'normal)
373 (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick))
375 (cons bottom (+ bottom (* size bezier-height))))
376 (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick))
377 (cons bottom (+ bottom (* size bezier-height)))
378 (cons left right))))))
379 (if (not (null? (cdr barre-list)))
380 (ly:stencil-add barre-stencil
381 (draw-barre layout props string-count fret-range size finger-code
382 dot-position dot-radius (cdr barre-list)))
385 (define (stepmag mag)
386 "Calculate the font step necessary to get a desired magnification"
387 (* 6 (/ (log mag) (log 2))))
389 (define (label-fret layout props string-count fret-range size orientation)
390 "Label the base fret on a fret diagram"
391 (let* ((base-fret (car fret-range))
392 ; (label-font-mag (chain-assoc-get 'label-font-mag props 0.7))
394 ; (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))
395 (label-vertical-offset -0.2)
396 (number-type (chain-assoc-get 'number-type props 'roman-lower))
397 (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
400 ((equal? number-type 'roman-lower) (fancy-format #f "~(~:@r~)" base-fret))
401 ((equal? number-type 'roman-upper) (fancy-format #f "~:@r" base-fret))
402 ((equal? 'arabic number-type) (fancy-format #f "~d" base-fret))
403 (else (fancy-format #f "~(~:@r~)" base-fret)))))
404 (if (eq? orientation 'normal)
405 (ly:stencil-translate-axis
406 (sans-serif-stencil layout props (* size label-font-mag) label-text)
407 (* size (+ fret-count label-vertical-offset)) Y)
408 (ly:stencil-translate-axis
409 (sans-serif-stencil layout props (* size label-font-mag) label-text)
410 (* size (+ 1 label-vertical-offset)) X))))
412 (define-builtin-markup-command (fret-diagram-verbose layout props marking-list)
415 ((size 1.0) ; needed for everything
416 (string-count 6) ; needed for everything
417 (fret-count 4) ; needed for everything
418 (orientation 'normal) ; needed for everything
419 (finger-code 'none) ; needed for both draw-dots and draw-barre
420 (thickness 0.5) ; needed for both draw-frets and draw-strings
421 (align-dir -0.4) ; needed only here
425 "Make a fret diagram containing the symbols indicated in @var{marking-list}.
430 \\markup \\fret-diagram-verbose
431 #'((mute 6) (mute 5) (open 4)
432 (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
436 produces a standard D@tie{}chord diagram without fingering indications.
438 Possible elements in @var{marking-list}:
441 @item (mute @var{string-number})
442 Place a small @q{x} at the top of string @var{string-number}.
444 @item (open @var{string-number})
445 Place a small @q{o} at the top of string @var{string-number}.
447 @item (barre @var{start-string} @var{end-string} @var{fret-number})
448 Place a barre indicator (much like a tie) from string @var{start-string}
449 to string @var{end-string} at fret @var{fret-number}.
451 @item (place-fret @var{string-number} @var{fret-number} @var{finger-value})
452 Place a fret playing indication on string @var{string-number} at fret
453 @var{fret-number} with an optional fingering label @var{finger-value}.
454 By default, the fret playing indicator is a solid dot. This can be
455 changed by setting the value of the variable @var{dot-color}. If the
456 @var{finger} part of the @code{place-fret} element is present,
457 @var{finger-value} will be displayed according to the setting of the
458 variable @var{finger-code}. There is no limit to the number of fret
459 indications per string.
461 (let* (;; note: here we get items from props that are needed in this routine, or that are needed in more than one
462 ;; of the procedures called from this routine. If they're only used in one of the sub-procedure, they're
463 ;; obtained in that procedure
464 ;;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available
465 ;;TODO -- adjust padding for fret label? it appears to be too close to dots
466 (default-dot-radius (if (eq? finger-code 'in-dot) 0.425 0.25)) ; bigger dots if labeled
467 (default-dot-position (if (eq? finger-code 'in-dot) (- 0.95 default-dot-radius) 0.6)) ; move up to make room for bigger if labeled
468 (dot-radius (chain-assoc-get 'dot-radius props default-dot-radius)) ; needed for both draw-dots and draw-barre
469 (dot-position (chain-assoc-get 'dot-position props default-dot-position)) ; needed for both draw-dots and draw-barre
470 (th (* (ly:output-def-lookup layout 'line-thickness)
471 thickness)) ; needed for both draw-frets and draw-strings
472 ;; (xo-padding (* th (chain-assoc-get 'padding props 2))) ; needed only here
473 (label-space (* 0.25 size))
474 (xo-padding (* th size 5))
475 (parameters (fret-parse-marking-list marking-list fret-count))
476 (dot-list (cdr (assoc 'dot-list parameters)))
477 (xo-list (cdr (assoc 'xo-list parameters)))
478 (fret-range (cdr (assoc 'fret-range parameters)))
479 (barre-list (cdr (assoc 'barre-list parameters)))
480 (fret-diagram-stencil (ly:stencil-add
481 (draw-strings string-count fret-range th size orientation)
482 (draw-frets layout props fret-range string-count th size orientation))))
483 (if (not (null? barre-list))
484 (set! fret-diagram-stencil (ly:stencil-add
485 (draw-barre layout props string-count fret-range size finger-code
486 dot-position dot-radius barre-list orientation)
487 fret-diagram-stencil)))
488 (if (not (null? dot-list))
489 (set! fret-diagram-stencil (ly:stencil-add
491 (draw-dots layout props string-count fret-count fret-range size finger-code
492 dot-position dot-radius th dot-list orientation))))
493 (if (= (car fret-range) 1)
494 (set! fret-diagram-stencil
495 (if (eq? orientation 'normal)
496 (ly:stencil-combine-at-edge fret-diagram-stencil Y UP
497 (draw-thick-zero-fret props string-count th size orientation))
498 (ly:stencil-combine-at-edge fret-diagram-stencil X LEFT
499 (draw-thick-zero-fret props string-count th size orientation)))))
500 (if (not (null? xo-list))
501 (set! fret-diagram-stencil
502 (if (eq? orientation 'normal)
503 (ly:stencil-combine-at-edge fret-diagram-stencil Y UP
504 (draw-xo layout props string-count fret-range size xo-list orientation) xo-padding )
505 (ly:stencil-combine-at-edge fret-diagram-stencil X LEFT
506 (draw-xo layout props string-count fret-range size xo-list orientation) xo-padding))))
507 (if (> (car fret-range) 1)
508 (set! fret-diagram-stencil
509 (if (eq? orientation 'normal)
510 (ly:stencil-combine-at-edge fret-diagram-stencil X label-dir
511 (label-fret layout props string-count fret-range size orientation) label-space)
512 (ly:stencil-combine-at-edge fret-diagram-stencil Y label-dir
513 (label-fret layout props string-count fret-range size orientation) label-space))))
514 (ly:stencil-aligned-to fret-diagram-stencil X align-dir)))
516 (define-builtin-markup-command (fret-diagram layout props definition-string)
519 (fret-diagram-verbose-markup)
520 "Make a (guitar) fret diagram. For example, say
523 \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
527 for fret spacing 3/4 of staff space, D chord diagram
529 Syntax rules for @var{definition-string}:
533 Diagram items are separated by semicolons.
540 @code{s:}@var{number} -- Set the fret spacing of the diagram (in staff
545 @code{t:}@var{number} -- Set the line thickness (in staff spaces).
549 @code{h:}@var{number} -- Set the height of the diagram in frets.
553 @code{w:}@var{number} -- Set the width of the diagram in strings.
557 @code{f:}@var{number} -- Set fingering label type
558 (0@tie{}= none, 1@tie{}= in circle on string, 2@tie{}= below string).
562 @code{d:}@var{number} -- Set radius of dot, in terms of fret spacing.
566 @code{p:}@var{number} -- Set the position of the dot in the fret space.
567 0.5 is centered; 1@tie{}is on lower fret bar, 0@tie{}is on upper fret bar.
571 @code{c:}@var{string1}@code{-}@var{string2}@code{-}@var{fret} -- Include a
572 barre mark from @var{string1} to @var{string2} on @var{fret}.
575 @var{string}@code{-}@var{fret} -- Place a dot on @var{string} at @var{fret}.
576 If @var{fret} is @samp{o}, @var{string} is identified as open.
577 If @var{fret} is @samp{x}, @var{string} is identified as muted.
580 @var{string}@code{-}@var{fret}@code{-}@var{fingering} -- Place a dot on
581 @var{string} at @var{fret}, and label with @var{fingering} as defined
582 by the @code{f:} code.
586 Note: There is no limit to the number of fret indications per string.
588 (let ((definition-list (fret-parse-definition-string props definition-string)))
589 (fret-diagram-verbose-markup layout (car definition-list) (cdr definition-list))))
591 (define (fret-parse-definition-string props definition-string)
592 "parse a fret diagram string and return a pair containing:
593 props, modified as necessary by the definition-string
594 a fret-indication list with the appropriate values"
595 (let* ((fret-count 4)
597 (fret-range (list 1 fret-count))
603 (items (string-split definition-string #\;)))
604 (let parse-item ((myitems items))
605 (if (not (null? (cdr myitems)))
606 (let ((test-string (car myitems)))
607 (case (car (string->list (substring test-string 0 1)))
608 ((#\s) (let ((size (get-numeric-from-key test-string)))
609 (set! new-props (acons 'size size new-props))))
610 ((#\f) (let* ((finger-code (get-numeric-from-key test-string))
611 (finger-id (case finger-code
614 ((2) 'below-string))))
616 (acons 'finger-code finger-id new-props))))
617 ((#\c) (set! output-list (cons-fret (cons 'barre (numerify (string-split (substring test-string 2) #\-)))
619 ((#\h) (let ((fret-count (get-numeric-from-key test-string)))
620 (set! new-props (acons 'fret-count fret-count new-props))))
621 ((#\w) (let ((string-count (get-numeric-from-key test-string)))
622 (set! new-props (acons 'string-count string-count new-props))))
623 ((#\d) (let ((dot-size (get-numeric-from-key test-string)))
624 (set! new-props (acons 'dot-radius dot-size new-props))))
625 ((#\p) (let ((dot-position (get-numeric-from-key test-string)))
626 (set! new-props (acons 'dot-position dot-position new-props))))
628 (let ((this-list (string-split test-string #\-)))
629 (if (string->number (cadr this-list))
630 (set! output-list (cons-fret (cons 'place-fret (numerify this-list)) output-list))
631 (if (equal? (cadr this-list) "x" )
632 (set! output-list (cons-fret (list 'mute (string->number (car this-list))) output-list))
633 (set! output-list (cons-fret (list 'open (string->number (car this-list))) output-list)))))))
634 (parse-item (cdr myitems)))))
635 (if (eq? new-props '())
636 `(,props . ,output-list)
637 `(,(cons new-props props) . ,output-list))))
639 (define (cons-fret new-value old-list)
640 " Put together a fret-list in the format desired by parse-string "
641 (if (eq? old-list '())
643 (cons* new-value old-list)))
645 (define (get-numeric-from-key keystring)
646 "Get the numeric value from a key of the form k:val"
647 (string->number (substring keystring 2 (string-length keystring))))
649 (define (numerify mylist)
650 "Convert string values to numeric or character"
653 (let ((numeric-value (string->number (car mylist))))
655 (cons* numeric-value (numerify (cdr mylist)))
656 (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))
658 (define-builtin-markup-command (fret-diagram-terse layout props definition-string)
661 (fret-diagram-verbose-markup)
662 "Make a fret diagram markup using terse string-based syntax.
667 \\markup \\fret-diagram-terse #\"x;x;o;2;3;2;\"
671 for a D@tie{}chord diagram.
673 Syntax rules for @var{definition-string}:
678 Strings are terminated by semicolons; the number of semicolons
679 is the number of strings in the diagram.
682 Mute strings are indicated by @samp{x}.
685 Open strings are indicated by @samp{o}.
688 A number indicates a fret indication at that fret.
691 If there are multiple fret indicators desired on a string, they
692 should be separated by spaces.
695 Fingerings are given by following the fret number with a @code{-},
696 followed by the finger indicator, e.g. @samp{3-2} for playing the third
697 fret with the second finger.
700 Where a barre indicator is desired, follow the fret (or fingering) symbol
701 with @code{-(} to start a barre and @code{-)} to end the barre.
704 ;; TODO -- change syntax to fret\string-finger
705 (let ((definition-list (fret-parse-terse-definition-string props definition-string)))
706 (fret-diagram-verbose-markup layout (car definition-list) (cdr definition-list))))
708 (define (fret-parse-terse-definition-string props definition-string)
709 "parse a fret diagram string that uses terse syntax; return a pair containing:
710 props, modified to include the string-count determined by the definition-string
711 a fret-indication list with the appropriate values"
712 ;TODO -- change syntax to fret\string-finger
713 (let* ((barre-start-list '())
716 (items (string-split definition-string #\;))
717 (string-count (- (length items) 1)))
718 (let parse-item ((myitems items))
719 (if (not (null? (cdr myitems)))
720 (let* ((test-string (car myitems))
721 (current-string (- (length myitems) 1))
722 (indicators (string-split test-string #\ )))
723 (let parse-indicators ((myindicators indicators))
724 (if (not (eq? '() myindicators))
725 (let* ((this-list (string-split (car myindicators) #\-))
726 (max-element-index (- (length this-list) 1))
727 (last-element (car (list-tail this-list max-element-index)))
728 (fret (if (string->number (car this-list)) (string->number (car this-list)) (car this-list))))
729 (if (equal? last-element "(")
731 (set! barre-start-list (cons-fret (list current-string fret) barre-start-list))
732 (set! this-list (list-head this-list max-element-index))))
733 (if (equal? last-element ")")
734 (let* ((this-barre (get-sub-list fret barre-start-list))
735 (insert-index (- (length this-barre) 1)))
736 (set! output-list (cons-fret (cons* 'barre (car this-barre) current-string (cdr this-barre))
738 (set! this-list (list-head this-list max-element-index))))
740 (set! output-list (cons-fret (cons* 'place-fret current-string (drop-paren (numerify this-list))) output-list))
741 (if (equal? (car this-list) "x" )
742 (set! output-list (cons-fret (list 'mute current-string) output-list))
743 (set! output-list (cons-fret (list 'open current-string) output-list))))
744 (parse-indicators (cdr myindicators)))))
745 (parse-item (cdr myitems)))))
746 (set! new-props (acons 'string-count string-count new-props))
748 `(,(cons new-props props) . ,output-list)))
750 (define (drop-paren item-list)
751 " drop a final parentheses from a fret indication list resulting from a terse string specification of barre."
752 (if (> (length item-list) 0)
753 (let* ((max-index (- (length item-list) 1))
754 (last-element (car (list-tail item-list max-index))))
755 (if (or (equal? last-element ")") (equal? last-element "("))
756 (list-head item-list max-index)
760 (define (get-sub-list value master-list)
761 " Get a sub-list whose cadr is equal to @var{value} from @var{master-list}"
762 (if (eq? master-list '())
764 (let ((sublist (car master-list)))
765 (if (equal? (cadr sublist) value)
767 (get-sub-list value (cdr master-list))))))