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