]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / chord-entry.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 ;; for define-safe-public when byte-compiling using Guile V2
19 (use-modules (scm safe-utility-defs) (ice-9 receive))
20
21 (define-session-public chordmodifiers '())
22
23 (define-public (construct-chord-elements root duration modifications)
24   "Build a chord on root using modifiers in @var{modifications}.
25 @code{NoteEvents} have duration @var{duration}.
26
27 Notes: Natural 11 is left from chord if not explicitly specified.
28
29 Entry point for the parser."
30   (let* ((flat-mods (flatten-list modifications))
31          (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
32          (complete-chord '())
33          (bass #f)
34          (inversion #f)
35          (lead-mod #f)
36          (explicit-11 #f)
37          (explicit-2/4 #f)
38          (omit-3 #f)
39          (start-additions #t))
40
41     (define (interpret-inversion chord mods)
42       "Read /FOO part.  Side effect: INVERSION is set."
43       (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
44           (begin
45             (set! inversion (cadr mods))
46             (set! mods (cddr mods))))
47       (interpret-bass chord mods))
48
49     (define (interpret-bass chord mods)
50       "Read /+FOO part.  Side effect: BASS is set."
51       (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
52           (begin
53             (set! bass (cadr mods))
54             (set! mods (cddr mods))))
55       (if (pair? mods)
56           (ly:parser-error
57            (format #f (_ "Spurious garbage following chord: ~A") mods)))
58       chord)
59
60     (define (interpret-removals  chord mods)
61       (define (inner-interpret chord mods)
62         (if (and (pair? mods) (ly:pitch? (car mods)))
63             (inner-interpret (remove-step (+ 1  (ly:pitch-steps (car mods))) chord)
64                              (cdr mods))
65             (interpret-inversion chord mods)))
66       (if (and (pair? mods) (eq? (car mods) 'chord-caret))
67           (inner-interpret chord (cdr mods))
68           (interpret-inversion chord mods)))
69
70     (define (interpret-additions chord mods)
71       "Interpret additions.  TODO: should restrict modifier use?"
72       (cond ((null? mods) chord)
73             ((ly:pitch? (car mods))
74              (case (pitch-step (car mods))
75                ((11) (set! explicit-11 #t))
76                ((2 4) (set! explicit-2/4 #t))
77                ((3) (set! omit-3 #f)))
78              (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
79                                   (cdr mods)))
80             ((procedure? (car mods))
81              (interpret-additions ((car mods) chord)
82                                   (cdr mods)))
83             (else (interpret-removals chord mods))))
84
85     (define (pitch-octavated-strictly-below p root)
86       "return P, but octavated, so it is below ROOT"
87       (ly:make-pitch (+ (ly:pitch-octave root)
88                         (if (> (ly:pitch-notename root)
89                                (ly:pitch-notename p))
90                             0 -1))
91                      (ly:pitch-notename p)
92                      (ly:pitch-alteration p)))
93
94     (define (process-inversion complete-chord)
95       "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
96 Return (INVERSION . REST-OF-CHORD).
97
98 Side effect: put original pitch in INVERSION.
99 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
100 the bass specified.
101
102 "
103       (let* ((root (car complete-chord))
104              (inv? (lambda (y)
105                      (and (= (ly:pitch-notename y)
106                              (ly:pitch-notename inversion))
107                           (= (ly:pitch-alteration y)
108                              (ly:pitch-alteration inversion)))))
109              (rest-of-chord (remove inv? complete-chord))
110              (inversion-candidates (filter inv? complete-chord))
111              (down-inversion (pitch-octavated-strictly-below inversion root)))
112         (if (pair? inversion-candidates)
113             (set! inversion (car inversion-candidates))
114             (begin
115               (set! bass inversion)
116               (set! inversion #f)))
117         (if inversion
118             (cons down-inversion rest-of-chord)
119             rest-of-chord)))
120     ;; root is always one octave too low.
121     ;; something weird happens when this is removed,
122     ;; every other chord is octavated. --hwn... hmmm.
123     (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
124     ;; skip the leading : , we need some of the stuff following it.
125     (if (pair? flat-mods)
126         (if (eq? (car flat-mods) 'chord-colon)
127             (set! flat-mods (cdr flat-mods))
128             (set! start-additions #f)))
129     ;; remember modifier
130     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
131         (begin
132           (set! lead-mod (car flat-mods))
133           (set! flat-mods (cdr flat-mods))))
134     ;; extract first number if present, and build pitch list.
135     (if (and (pair? flat-mods)
136              (ly:pitch?  (car flat-mods))
137              (not (eq? lead-mod sus-modifier)))
138         (begin
139           (cond ((= (pitch-step (car flat-mods)) 11)
140                  (set! explicit-11 #t))
141                 ((equal? (ly:make-pitch 0 4 0) (car flat-mods))
142                  (set! omit-3 #t)))
143           (set! base-chord
144                 (stack-thirds (car flat-mods) the-canonical-chord))
145           (set! flat-mods (cdr flat-mods))))
146     ;; apply modifier
147     (if (procedure? lead-mod)
148         (set! base-chord (lead-mod base-chord)))
149     (set! complete-chord
150           (if start-additions
151               (interpret-additions base-chord flat-mods)
152               (interpret-removals base-chord flat-mods)))
153     ;; if sus has been given neither 2 or 4, we add 4.
154     (if (and (eq? lead-mod sus-modifier)
155              (not explicit-2/4))
156         (set! complete-chord (cons (ly:make-pitch 0 4 0) complete-chord)))
157     (set! complete-chord (sort complete-chord ly:pitch<?))
158     ;; If natural 11 + natural 3 is present, but not given explicitly,
159     ;; we remove the 11.
160     (if (and (not explicit-11)
161              (get-step 11 complete-chord)
162              (get-step 3 complete-chord)
163              (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
164              (= 0 (ly:pitch-alteration (get-step 3 complete-chord))))
165         (set! complete-chord (remove-step 11 complete-chord)))
166     ;; if omit-3 has been set (and not reset by an explicit 3
167     ;; somewhere), we remove the 3
168     (if omit-3
169         (set! complete-chord (remove-step 3 complete-chord)))
170     ;; must do before processing inversion/bass, since they are
171     ;; not relative to the root.
172     (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
173                               complete-chord))
174     (if inversion
175         (set! complete-chord (process-inversion complete-chord)))
176     (if bass
177         (set! bass (pitch-octavated-strictly-below bass root)))
178     (if #f
179         (begin
180           (write-me "\n*******\n" flat-mods)
181           (write-me "root: " root)
182           (write-me "base chord: " base-chord)
183           (write-me "complete chord: " complete-chord)
184           (write-me "inversion: " inversion)
185           (write-me "bass: " bass)))
186     (if inversion
187         (make-chord-elements (cdr complete-chord) bass duration (car complete-chord)
188                              inversion)
189         (make-chord-elements complete-chord bass duration #f #f))))
190
191
192 (define (make-chord-elements pitches bass duration inversion original-inv-pitch)
193   "Make EventChord with notes corresponding to PITCHES, BASS and
194 DURATION, and INVERSION.  Notes above INVERSION are transposed downward
195 along with the inversion as long as they end up below at least one
196 non-inverted note."
197   (define (make-note-ev pitch . rest)
198     (apply make-music 'NoteEvent
199            'duration duration
200            'pitch pitch
201            rest))
202   (cond (inversion
203          (let* ((octavation (- (ly:pitch-octave inversion)
204                                (ly:pitch-octave original-inv-pitch)))
205                 (down (ly:make-pitch octavation 0 0)))
206            (define (invert p) (ly:pitch-transpose down p))
207            (define (make-inverted p . rest)
208              (apply make-note-ev (invert p) 'octavation octavation rest))
209            (receive (uninverted high)
210                     (span (lambda (p) (ly:pitch<? p original-inv-pitch))
211                           pitches)
212                     (receive (invertible rest)
213                              (if (null? uninverted)
214                                  ;; The following line caters for
215                                  ;; inversions "on the root", turning
216                                  ;; f/f into <f a' c''> rather than <f a c'>
217                                  ;; or <f' a' c''>
218                                  (values '() high)
219                                  (span (lambda (p)
220                                          (ly:pitch<? (invert p) (car uninverted)))
221                                        high))
222                              (cons (make-inverted original-inv-pitch 'inversion #t)
223                                    (append (if bass (list (make-note-ev bass 'bass #t)) '())
224                                            (map make-inverted invertible)
225                                            (map make-note-ev uninverted)
226                                            (map make-note-ev rest)))))))
227         (bass (cons (make-note-ev bass 'bass #t)
228                     (map make-note-ev pitches)))
229         (else (map make-note-ev pitches))))
230
231 ;;;;;;;;;;;;;;;;
232 ;; chord modifiers change the pitch list.
233
234 (define (aug-modifier pitches)
235   (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
236   (replace-step (ly:make-pitch 0 2 0) pitches))
237
238 (define (minor-modifier pitches)
239   (replace-step (ly:make-pitch 0 2 FLAT) pitches))
240
241 (define (maj7-modifier pitches)
242   (set! pitches (remove-step 7 pitches))
243   (cons (ly:make-pitch 0 6 0) pitches))
244
245 (define (dim-modifier pitches)
246   (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
247   (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
248   (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
249   pitches)
250
251 (define (sus-modifier pitches)
252   (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
253
254 (define-safe-public default-chord-modifier-list
255   `((m . ,minor-modifier)
256     (min . ,minor-modifier)
257     (aug . , aug-modifier)
258     (dim . , dim-modifier)
259     (maj . , maj7-modifier)
260     (sus . , sus-modifier)))
261
262 ;; canonical 13 chord.
263 (define the-canonical-chord
264   (map (lambda (n)
265          (define (nca x)
266            (if (= x 7) FLAT 0))
267
268          (if (>= n 8)
269              (ly:make-pitch 1 (- n 8) (nca n))
270              (ly:make-pitch 0 (- n 1) (nca n))))
271        '(1 3 5 7 9 11 13)))
272
273 (define (stack-thirds upper-step base)
274   "Stack thirds listed in BASE until we reach UPPER-STEP.  Add
275 UPPER-STEP separately."
276   (cond ((null? base) '())
277         ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
278          (cons (car base) (stack-thirds upper-step (cdr base))))
279         ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
280          (list upper-step))
281         (else '())))