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