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