2 ;;; Generate chord names for the parser.
6 (define-public (construct-chord root duration modifications)
8 " Build a chord on root using modifiers in MODIFICATIONS. NoteEvent
9 have duration DURATION..
11 Notes: natural 11 is left from chord if not explicitly specified.
13 Entry point for the parser.
17 ((flat-mods (flatten-list modifications))
18 (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
26 (define (interpret-inversion chord mods)
27 "Read /FOO part. Side effect: INVERSION is set."
29 (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
31 (set! inversion (cadr mods))
32 (set! mods (cddr mods))))
34 (interpret-bass chord mods))
36 (define (interpret-bass chord mods)
37 "Read /+FOO part. Side effect: BASS is set."
39 (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
41 (set! bass (cadr mods))
42 (set! mods (cddr mods))))
45 (scm-error 'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f)
51 (define (interpret-removals chord mods)
52 (define (inner-interpret chord mods)
53 (if (and (pair? mods) (ly:pitch? (car mods)))
55 (remove-step (+ 1 (ly:pitch-steps (car mods))) chord)
57 (interpret-inversion chord mods))
60 (if (and (pair? mods) (eq? (car mods) 'chord-caret))
61 (inner-interpret chord (cdr mods))
62 (interpret-inversion chord mods))
66 (define (interpret-additions chord mods)
67 "Interpret additions. TODO: should restrict modifier use?"
71 ((ly:pitch? (car mods))
72 (if (= (pitch-step (car mods)) 11)
73 (set! explicit-11 #t))
75 (cons (car mods) (remove-step (pitch-step (car mods)) chord))
77 ((procedure? (car mods))
81 (else (interpret-removals chord mods))
84 (define (pitch-octavated-strictly-below p root)
85 "return P, but octavated, so it is below ROOT"
88 (ly:pitch-octave root)
89 (if (> (ly:pitch-notename root)
90 (ly:pitch-notename p))
93 (ly:pitch-alteration p)))
95 (define (process-inversion complete-chord)
96 "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
97 Return (INVERSION . REST-OF-CHORD).
99 Side effect: put original pitch in INVERSION.
100 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
106 (root (car complete-chord))
108 (and (= (ly:pitch-notename y)
109 (ly:pitch-notename inversion))
110 (= (ly:pitch-alteration y)
111 (ly:pitch-alteration inversion))
114 (rest-of-chord (remove inv? complete-chord))
115 (inversion-candidates (filter inv? complete-chord))
116 (down-inversion (pitch-octavated-strictly-below inversion root))
119 (if (pair? inversion-candidates)
120 (set! inversion (car inversion-candidates))
122 (set! bass inversion)
126 (cons down-inversion rest-of-chord)
131 ;; root is always one octave too low.
133 ; something weird happens when this is removed,
134 ; every other chord is octavated. --hwn... hmmm.
135 (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
137 ;; skip the leading : , we need some of the stuff following it.
138 (if (pair? flat-mods)
139 (if (eq? (car flat-mods) 'chord-colon)
140 (set! flat-mods (cdr flat-mods))
141 (set! start-additions #f)
145 (if (and (pair? flat-mods) (procedure? (car flat-mods)))
147 (set! lead-mod (car flat-mods))
148 (set! flat-mods (cdr flat-mods))
153 ;; extract first number if present, and build pitch list.
154 (if (and (pair? flat-mods)
155 (ly:pitch? (car flat-mods))
156 (not (eq? lead-mod sus-modifier))
160 (if (= (pitch-step (car flat-mods)) 11)
161 (set! explicit-11 #t))
163 (stack-thirds (car flat-mods) the-canonical-chord))
164 (set! flat-mods (cdr flat-mods))
168 (if (procedure? lead-mod)
169 (set! base-chord (lead-mod base-chord)))
174 (interpret-additions base-chord flat-mods)
175 (interpret-removals base-chord flat-mods)
178 (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
179 (sort complete-chord ly:pitch<?)))
181 ;; If natural 11 + natural 3 is present, but not given explicitly,
183 (if (and (not explicit-11)
184 (get-step 11 complete-chord)
185 (get-step 3 complete-chord)
186 (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
187 (= 0 (ly:pitch-alteration (get-step 3 complete-chord)))
190 (set! complete-chord (remove-step 11 complete-chord))
196 (set! complete-chord (process-inversion complete-chord)))
198 (set! bass (pitch-octavated-strictly-below bass root)))
202 (write-me "\n*******\n" flat-mods)
203 (write-me "root: " root)
204 (write-me "base chord: " base-chord)
205 (write-me "complete chord: " complete-chord)
206 (write-me "inversion: " inversion)
207 (write-me "bass: " bass)))
212 (make-chord (cdr complete-chord) bass duration (car complete-chord)
214 (make-chord complete-chord bass duration #f #f))
218 (define (make-chord pitches bass duration inversion original-inv-pitch)
219 "Make EventChord with notes corresponding to PITCHES, BASS and
220 DURATION, and INVERSION."
221 (define (make-note-ev pitch)
224 (ev (make-music-by-name 'NoteEvent))
227 (ly:set-mus-property! ev 'duration duration)
228 (ly:set-mus-property! ev 'pitch pitch)
234 (nots (map make-note-ev pitches))
235 (bass-note (if bass (make-note-ev bass) #f))
236 (inv-note (if inversion (make-note-ev inversion) #f))
242 (ly:set-mus-property! bass-note 'bass #t)
243 (set! nots (cons bass-note nots))))
248 (ly:set-mus-property! inv-note 'inversion #t)
249 (ly:set-mus-property! inv-note 'octavation
250 (- (ly:pitch-octave inversion)
251 (ly:pitch-octave original-inv-pitch))
253 (set! nots (cons inv-note nots))))
255 (make-event-chord nots)
260 ; chord modifiers change the pitch list.
262 (define (aug-modifier pitches)
263 (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
264 (replace-step (ly:make-pitch 0 2 0) pitches)
267 (define (minor-modifier pitches)
268 (replace-step (ly:make-pitch 0 2 FLAT) pitches)
271 (define (maj7-modifier pitches)
272 (set! pitches (remove-step 7 pitches))
273 (cons (ly:make-pitch 0 6 0) pitches)
276 (define (dim-modifier pitches)
277 (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
278 (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
279 (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
283 (define (sus-modifier pitches)
284 (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches)
287 (define-public default-chord-modifier-list
288 `((m . ,minor-modifier)
289 (min . ,minor-modifier)
290 (aug . , aug-modifier)
291 (dim . , dim-modifier)
292 (maj . , maj7-modifier)
293 (sus . , sus-modifier)
297 ;; canonical 13 chord.
298 (define the-canonical-chord
304 (ly:make-pitch 1 (- n 8) (nca n))
305 (ly:make-pitch 0 (- n 1) (nca n))))
308 (define (stack-thirds upper-step base)
309 "Stack thirds listed in BASE until we reach UPPER-STEP. Add
310 UPPER-STEP separately."
313 ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
314 (cons (car base) (stack-thirds upper-step (cdr base))))
315 ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))