]> git.donarmstrong.com Git - lilypond.git/blob - scm/harp-pedals.scm
Merge master into nested-bookparts
[lilypond.git] / scm / harp-pedals.scm
1 ;;;; harp-pedals.scm --
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2008 Reinhold Kainhofer <reinhold@kainhofer.com>
6
7
8
9 (define-builtin-markup-command (harp-pedal layout props definition-string) (string?)
10   instrument-specific-markup ; markup type for the documentation!
11   ((size 1.0)
12    (harp-pedal-details)
13    (thickness 0.5))
14   "Make a harp pedal diagram.
15
16 Possible elements in @var{definition-string}:
17
18 @table @code
19 @item ^
20 pedal is up
21 @item -
22 pedal is neutral
23 @item v
24 pedal is down
25 @item |
26 vertical divider line
27 @item o
28 the following pedal should be circled (indicating a change)
29 @end table
30
31 The function also checks if the string has the typical form of three
32 pedals, then the divider and then the remaining four pedals. If not it
33 prints out a warning. However, in any case, it will also print each symbol
34 in the order as given. This means you can place the divider (even multiple
35 dividers) anywhere you want, but you'll have to live with the warnings.
36
37 The appearance of the diagram can be tweaked inter alia using the size property
38 of the TextScript grob (@code{\\override Voice.TextScript #'size = #0.3}) for
39 the overall, the thickness property
40 (@code{\\override Voice.TextScript #'thickness = #3}) for the line thickness of
41 the horizontal line and the divider. The remaining configuration (box sizes,
42 offsets and spaces) is done by the harp-pedal-details  list of properties
43 (@code{\\override Voice.TextScript #'harp-pedal-details #'box-width = #1}).
44 It contains the following settings: @code{box-offset} (vertical shift of the
45 box center for up/down pedals), @code{box-width}, @code{box-height},
46 @code{space-before-divider} (the spacing between two boxes before the
47 divider) and @code{space-after-divider} (box spacing after the divider).
48
49 @lilypond[verbatim,quote]
50 \\markup \\harp-pedal #\"^-v|--ov^\"
51 @end lilypond
52 "
53   (make-harp-pedal layout props (harp-pedals-parse-string definition-string)))
54
55
56 ;; There is also a \harp-pedal-verbose version, which takes a list of -1/0/1
57 ;; directions, o and a possible |. It's commented out, because it has some
58 ;; issues (see below) and does not add any new functionality over \harp-pedal
59 ;; The caveats:
60 ;;   1) the | cannot be given as a string "|" but as a character #\| and
61 ;;      the "o" has to be given as #\o.
62 ;;   2) if one wants to use directions like UP, CENTER or DOWN, one cannot use
63 ;;      '(UP DOWN CENTER #\| ....), because the contents of that list are
64 ;;      never evaluated to -1/0/1. Instead one has to explicitly create a
65 ;;      list like (list UP DOWN CENTER #\| ....)
66 ;;
67 ;; (define-builtin-markup-command (harp-pedal-verbose layout props pedal-list) (list?)
68 ;;   instrument-specific-markup ; markup type
69 ;;   ((size 1.0)
70 ;;    (harp-pedal-details)
71 ;;    (thickness 0.5))
72 ;;   "Make a harp pedal diagram containing the directions indicated in @var{pedal-list}."
73 ;;   (make-harp-pedal layout props pedal-list))
74
75
76
77 ;; Parse the harp pedal definition string into list of directions (-1/0/1), #\o and #\|
78 (define (harp-pedals-parse-string definition-string)
79  "Parse a harp pedals diagram string and return a list containing 1, 0, -1, #\\o or #\\|"
80   (map (lambda (c)
81     (case c
82       ((#\^) 1)
83       ((#\v) -1)
84       ((#\-) 0)
85       ((#\| #\o) c)
86       (else c)))
87     (string->list definition-string)))
88
89
90 ;; Analyze the pedal-list: Return (pedalcount . (divider positions))
91 (define (harp-pedal-info pedal-list)
92   (let check ((pedals pedal-list)
93               (pedalcount 0)
94               (dividerpositions '()))
95     (if (null? pedals)
96       (cons pedalcount (reverse dividerpositions))
97
98       (case (car pedals)
99         ((-1 0 1) (check (cdr pedals) (+ pedalcount 1) dividerpositions))
100         ((#\|)    (check (cdr pedals) pedalcount (cons pedalcount dividerpositions)))
101         (else     (check (cdr pedals) pedalcount dividerpositions))))))
102
103
104 ;; Sanity checks, spit out warning if pedal-list violates the conventions
105 (define (harp-pedal-check pedal-list)
106   "Perform some sanity checks for harp pedals (7 pedals, divider after third)"
107   (let ((info (harp-pedal-info pedal-list)))
108     ; 7 pedals:
109     (if (not (equal? (car info) 7))
110       (ly:warning "Harp pedal diagram contains ~a pedals rather than the usual 7." (car info)))
111     ; One divider after third pedal:
112     (if (null? (cdr info))
113       (ly:warning "Harp pedal diagram does not contain a divider (usually after third pedal).")
114       (if (not (equal? (cdr info) '(3)))
115         (ly:warning "Harp pedal diagram contains dividers at positions ~a. Normally, there is only one divider after the third pedal." (cdr info))))))
116
117
118 (define (make-harp-pedal layout props pedal-list)
119   "Make a harp pedals diagram markup"
120
121   (harp-pedal-check pedal-list)
122
123   (let* ((size (chain-assoc-get 'size props 1.2))
124         (details (chain-assoc-get 'harp-pedal-details props '()))
125         (dy (* size (assoc-get 'box-offset details 0.8))) ; offset of the box center from the line
126         (line-width (* (ly:output-def-lookup layout 'line-thickness)
127                        (chain-assoc-get 'thickness props 0.5)))
128         (box-width (* size (assoc-get 'box-width details 0.4)))
129         (box-hheight (* size (/ (assoc-get 'box-height details 1.0) 2))) ; half the box-height, saves some divisions by 2
130         (spacebeforedivider (* size (assoc-get 'space-before-divider details 0.8))) ; full space between boxes before the first divider
131         (spaceafterdivider (* size (assoc-get 'space-after-divider details 0.8))) ; full space between boxes
132         (circle-thickness (* (ly:output-def-lookup layout 'line-thickness)
133                        (assoc-get 'circle-thickness details 0.5)))
134         (circle-x-padding (* size (assoc-get 'circle-x-padding details 0.15)))
135         (circle-y-padding (* size (assoc-get 'circle-y-padding details 0.2)))
136         (box-x-dimensions (lambda (prev-x p space) (cons (+ prev-x space)
137                                                    (+ prev-x space box-width))))
138         (box-y-dimensions (lambda (prev-x p space) (cons (- (* p dy) box-hheight)
139                                                          (+ (* p dy) box-hheight))))
140         (divider-stencil (lambda (xpos) (make-line-stencil line-width
141                                                      xpos (- 0 dy box-hheight)
142                                                      xpos (+ dy box-hheight))))
143         (result (let process-pedal  ((remaining pedal-list)
144                                      (prev-x 0)
145                                      (stencils '())
146                                      (circled #f)
147                                      (space spacebeforedivider))
148           ; Terminal condition of the recursion, return (final-x . stencil-list)
149           (if (null? remaining)
150             (cons (+ prev-x space) (reverse stencils))
151
152             (case (car remaining)
153               ((1 0 -1)  ; Pedal up/neutral/down
154                   (let* ((p (car remaining))
155                         (stencil (make-filled-box-stencil
156                                    (box-x-dimensions prev-x p space)
157                                    (box-y-dimensions prev-x p space)))
158                         (pedal-stencil 
159                           (if circled 
160                               (oval-stencil stencil circle-thickness 
161                                             circle-x-padding circle-y-padding)
162                               stencil))
163                         (new-prev-x (+ prev-x space box-width)))
164                     (process-pedal (cdr remaining) new-prev-x 
165                                    (cons pedal-stencil stencils) #f space)))
166               ((#\|)  ; Divider line
167                   (let* ((xpos (+ prev-x space))
168                          (stencil (divider-stencil xpos))
169                          (new-prev-x (+ prev-x space)))
170                     (process-pedal (cdr remaining) new-prev-x 
171                                    (cons stencil stencils) 
172                                    circled spaceafterdivider)))
173               ((#\o)  ; Next pedal should be circled
174                   (process-pedal (cdr remaining) prev-x stencils #t space))
175               (else
176                   (ly:warning "Unhandled entry in harp-pedal: ~a" 
177                               (car remaining))
178                   (process-pedal (cdr remaining) 
179                                  prev-x stencils circled space))))))
180         (final-x (car result))
181         (stencils (cdr result)))
182     ; Add the horizontal line and combine all stencils:
183     (apply ly:stencil-add
184       (cons
185         (make-line-stencil line-width 0 0 final-x 0)
186         stencils))))
187