1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
18 (define-public (construct-chord-elements root duration modifications)
19 "Build a chord on root using modifiers in @var{modifications}.
20 @code{NoteEvents} have duration @var{duration}.
22 Notes: Natural 11 is left from chord if not explicitly specified.
24 Entry point for the parser."
25 (let* ((flat-mods (flatten-list modifications))
26 (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
34 (define (interpret-inversion chord mods)
35 "Read /FOO part. Side effect: INVERSION is set."
36 (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
38 (set! inversion (cadr mods))
39 (set! mods (cddr mods))))
40 (interpret-bass chord mods))
42 (define (interpret-bass chord mods)
43 "Read /+FOO part. Side effect: BASS is set."
44 (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
46 (set! bass (cadr mods))
47 (set! mods (cddr mods))))
49 (ly:warning (_ "Spurious garbage following chord: ~A") mods))
52 (define (interpret-removals chord mods)
53 (define (inner-interpret chord mods)
54 (if (and (pair? mods) (ly:pitch? (car mods)))
55 (inner-interpret (remove-step (+ 1 (ly:pitch-steps (car mods))) chord)
57 (interpret-inversion chord mods)))
58 (if (and (pair? mods) (eq? (car mods) 'chord-caret))
59 (inner-interpret chord (cdr mods))
60 (interpret-inversion chord mods)))
62 (define (interpret-additions chord mods)
63 "Interpret additions. TODO: should restrict modifier use?"
64 (cond ((null? mods) chord)
65 ((ly:pitch? (car mods))
66 (if (= (pitch-step (car mods)) 11)
67 (set! explicit-11 #t))
68 (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
70 ((procedure? (car mods))
71 (interpret-additions ((car mods) chord)
73 (else (interpret-removals chord mods))))
75 (define (pitch-octavated-strictly-below p root)
76 "return P, but octavated, so it is below ROOT"
77 (ly:make-pitch (+ (ly:pitch-octave root)
78 (if (> (ly:pitch-notename root)
79 (ly:pitch-notename p))
82 (ly:pitch-alteration p)))
84 (define (process-inversion complete-chord)
85 "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
86 Return (INVERSION . REST-OF-CHORD).
88 Side effect: put original pitch in INVERSION.
89 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
93 (let* ((root (car complete-chord))
95 (and (= (ly:pitch-notename y)
96 (ly:pitch-notename inversion))
97 (= (ly:pitch-alteration y)
98 (ly:pitch-alteration inversion)))))
99 (rest-of-chord (remove inv? complete-chord))
100 (inversion-candidates (filter inv? complete-chord))
101 (down-inversion (pitch-octavated-strictly-below inversion root)))
102 (if (pair? inversion-candidates)
103 (set! inversion (car inversion-candidates))
105 (set! bass inversion)
106 (set! inversion #f)))
108 (cons down-inversion rest-of-chord)
110 ;; root is always one octave too low.
111 ;; something weird happens when this is removed,
112 ;; every other chord is octavated. --hwn... hmmm.
113 (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
114 ;; skip the leading : , we need some of the stuff following it.
115 (if (pair? flat-mods)
116 (if (eq? (car flat-mods) 'chord-colon)
117 (set! flat-mods (cdr flat-mods))
118 (set! start-additions #f)))
120 (if (and (pair? flat-mods) (procedure? (car flat-mods)))
122 (set! lead-mod (car flat-mods))
123 (set! flat-mods (cdr flat-mods))))
124 ;; extract first number if present, and build pitch list.
125 (if (and (pair? flat-mods)
126 (ly:pitch? (car flat-mods))
127 (not (eq? lead-mod sus-modifier)))
129 (if (= (pitch-step (car flat-mods)) 11)
130 (set! explicit-11 #t))
132 (stack-thirds (car flat-mods) the-canonical-chord))
133 (set! flat-mods (cdr flat-mods))))
135 (if (procedure? lead-mod)
136 (set! base-chord (lead-mod base-chord)))
139 (interpret-additions base-chord flat-mods)
140 (interpret-removals base-chord flat-mods)))
141 (set! complete-chord (sort complete-chord ly:pitch<?))
142 ;; If natural 11 + natural 3 is present, but not given explicitly,
144 (if (and (not explicit-11)
145 (get-step 11 complete-chord)
146 (get-step 3 complete-chord)
147 (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
148 (= 0 (ly:pitch-alteration (get-step 3 complete-chord))))
149 (set! complete-chord (remove-step 11 complete-chord)))
150 ;; must do before processing inversion/bass, since they are
151 ;; not relative to the root.
152 (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
155 (set! complete-chord (process-inversion complete-chord)))
157 (set! bass (pitch-octavated-strictly-below bass root)))
160 (write-me "\n*******\n" flat-mods)
161 (write-me "root: " root)
162 (write-me "base chord: " base-chord)
163 (write-me "complete chord: " complete-chord)
164 (write-me "inversion: " inversion)
165 (write-me "bass: " bass)))
167 (make-chord-elements (cdr complete-chord) bass duration (car complete-chord)
169 (make-chord-elements complete-chord bass duration #f #f))))
172 (define (make-chord-elements pitches bass duration inversion original-inv-pitch)
173 "Make EventChord with notes corresponding to PITCHES, BASS and
174 DURATION, and INVERSION."
175 (define (make-note-ev pitch)
176 (make-music 'NoteEvent
179 (let ((nots (map make-note-ev pitches))
180 (bass-note (if bass (make-note-ev bass) #f))
181 (inv-note (if inversion (make-note-ev inversion) #f)))
184 (set! (ly:music-property bass-note 'bass) #t)
185 (set! nots (cons bass-note nots))))
188 (set! (ly:music-property inv-note 'inversion) #t)
189 (set! (ly:music-property inv-note 'octavation)
190 (- (ly:pitch-octave inversion)
191 (ly:pitch-octave original-inv-pitch)))
192 (set! nots (cons inv-note nots))))
196 ; chord modifiers change the pitch list.
198 (define (aug-modifier pitches)
199 (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
200 (replace-step (ly:make-pitch 0 2 0) pitches))
202 (define (minor-modifier pitches)
203 (replace-step (ly:make-pitch 0 2 FLAT) pitches))
205 (define (maj7-modifier pitches)
206 (set! pitches (remove-step 7 pitches))
207 (cons (ly:make-pitch 0 6 0) pitches))
209 (define (dim-modifier pitches)
210 (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
211 (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
212 (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
215 (define (sus-modifier pitches)
216 (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
218 (define-safe-public default-chord-modifier-list
219 `((m . ,minor-modifier)
220 (min . ,minor-modifier)
221 (aug . , aug-modifier)
222 (dim . , dim-modifier)
223 (maj . , maj7-modifier)
224 (sus . , sus-modifier)))
226 ;; canonical 13 chord.
227 (define the-canonical-chord
233 (ly:make-pitch 1 (- n 8) (nca n))
234 (ly:make-pitch 0 (- n 1) (nca n))))
237 (define (stack-thirds upper-step base)
238 "Stack thirds listed in BASE until we reach UPPER-STEP. Add
239 UPPER-STEP separately."
240 (cond ((null? base) '())
241 ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
242 (cons (car base) (stack-thirds upper-step (cdr base))))
243 ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))