1 ;;;; chord-entry.scm -- Generate chord names for the parser.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 (define-public (construct-chord-elements root duration modifications)
8 " Build a chord on root using modifiers in MODIFICATIONS. NoteEvents
9 have duration DURATION.
11 Notes: natural 11 is left from chord if not explicitly specified.
13 Entry point for the parser.
15 (let* ((flat-mods (flatten-list modifications))
16 (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
24 (define (interpret-inversion chord mods)
25 "Read /FOO part. Side effect: INVERSION is set."
26 (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
28 (set! inversion (cadr mods))
29 (set! mods (cddr mods))))
30 (interpret-bass chord mods))
32 (define (interpret-bass chord mods)
33 "Read /+FOO part. Side effect: BASS is set."
34 (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
36 (set! bass (cadr mods))
37 (set! mods (cddr mods))))
39 (scm-error 'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f))
42 (define (interpret-removals chord mods)
43 (define (inner-interpret chord mods)
44 (if (and (pair? mods) (ly:pitch? (car mods)))
45 (inner-interpret (remove-step (+ 1 (ly:pitch-steps (car mods))) chord)
47 (interpret-inversion chord mods)))
48 (if (and (pair? mods) (eq? (car mods) 'chord-caret))
49 (inner-interpret chord (cdr mods))
50 (interpret-inversion chord mods)))
52 (define (interpret-additions chord mods)
53 "Interpret additions. TODO: should restrict modifier use?"
54 (cond ((null? mods) chord)
55 ((ly:pitch? (car mods))
56 (if (= (pitch-step (car mods)) 11)
57 (set! explicit-11 #t))
58 (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
60 ((procedure? (car mods))
61 (interpret-additions ((car mods) chord)
63 (else (interpret-removals chord mods))))
65 (define (pitch-octavated-strictly-below p root)
66 "return P, but octavated, so it is below ROOT"
67 (ly:make-pitch (+ (ly:pitch-octave root)
68 (if (> (ly:pitch-notename root)
69 (ly:pitch-notename p))
72 (ly:pitch-alteration p)))
74 (define (process-inversion complete-chord)
75 "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
76 Return (INVERSION . REST-OF-CHORD).
78 Side effect: put original pitch in INVERSION.
79 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
83 (let* ((root (car complete-chord))
85 (and (= (ly:pitch-notename y)
86 (ly:pitch-notename inversion))
87 (= (ly:pitch-alteration y)
88 (ly:pitch-alteration inversion)))))
89 (rest-of-chord (remove inv? complete-chord))
90 (inversion-candidates (filter inv? complete-chord))
91 (down-inversion (pitch-octavated-strictly-below inversion root)))
92 (if (pair? inversion-candidates)
93 (set! inversion (car inversion-candidates))
98 (cons down-inversion rest-of-chord)
100 ;; root is always one octave too low.
101 ;; something weird happens when this is removed,
102 ;; every other chord is octavated. --hwn... hmmm.
103 (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
104 ;; skip the leading : , we need some of the stuff following it.
105 (if (pair? flat-mods)
106 (if (eq? (car flat-mods) 'chord-colon)
107 (set! flat-mods (cdr flat-mods))
108 (set! start-additions #f)))
110 (if (and (pair? flat-mods) (procedure? (car flat-mods)))
112 (set! lead-mod (car flat-mods))
113 (set! flat-mods (cdr flat-mods))))
114 ;; extract first number if present, and build pitch list.
115 (if (and (pair? flat-mods)
116 (ly:pitch? (car flat-mods))
117 (not (eq? lead-mod sus-modifier)))
119 (if (= (pitch-step (car flat-mods)) 11)
120 (set! explicit-11 #t))
122 (stack-thirds (car flat-mods) the-canonical-chord))
123 (set! flat-mods (cdr flat-mods))))
125 (if (procedure? lead-mod)
126 (set! base-chord (lead-mod base-chord)))
129 (interpret-additions base-chord flat-mods)
130 (interpret-removals base-chord flat-mods)))
131 (set! complete-chord (sort complete-chord ly:pitch<?))
132 ;; If natural 11 + natural 3 is present, but not given explicitly,
134 (if (and (not explicit-11)
135 (get-step 11 complete-chord)
136 (get-step 3 complete-chord)
137 (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
138 (= 0 (ly:pitch-alteration (get-step 3 complete-chord))))
139 (set! complete-chord (remove-step 11 complete-chord)))
140 ;; must do before processing inversion/bass, since they are
141 ;; not relative to the root.
142 (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
145 (set! complete-chord (process-inversion complete-chord)))
147 (set! bass (pitch-octavated-strictly-below bass root)))
150 (write-me "\n*******\n" flat-mods)
151 (write-me "root: " root)
152 (write-me "base chord: " base-chord)
153 (write-me "complete chord: " complete-chord)
154 (write-me "inversion: " inversion)
155 (write-me "bass: " bass)))
157 (make-chord-elements (cdr complete-chord) bass duration (car complete-chord)
159 (make-chord-elements complete-chord bass duration #f #f))))
162 (define (make-chord-elements pitches bass duration inversion original-inv-pitch)
163 "Make EventChord with notes corresponding to PITCHES, BASS and
164 DURATION, and INVERSION."
165 (define (make-note-ev pitch)
166 (make-music 'NoteEvent
169 (let ((nots (map make-note-ev pitches))
170 (bass-note (if bass (make-note-ev bass) #f))
171 (inv-note (if inversion (make-note-ev inversion) #f)))
174 (set! (ly:music-property bass-note 'bass) #t)
175 (set! nots (cons bass-note nots))))
178 (set! (ly:music-property inv-note 'inversion) #t)
179 (set! (ly:music-property inv-note 'octavation)
180 (- (ly:pitch-octave inversion)
181 (ly:pitch-octave original-inv-pitch)))
182 (set! nots (cons inv-note nots))))
186 ; chord modifiers change the pitch list.
188 (define (aug-modifier pitches)
189 (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
190 (replace-step (ly:make-pitch 0 2 0) pitches))
192 (define (minor-modifier pitches)
193 (replace-step (ly:make-pitch 0 2 FLAT) pitches))
195 (define (maj7-modifier pitches)
196 (set! pitches (remove-step 7 pitches))
197 (cons (ly:make-pitch 0 6 0) pitches))
199 (define (dim-modifier pitches)
200 (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
201 (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
202 (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
205 (define (sus-modifier pitches)
206 (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
208 (define-safe-public default-chord-modifier-list
209 `((m . ,minor-modifier)
210 (min . ,minor-modifier)
211 (aug . , aug-modifier)
212 (dim . , dim-modifier)
213 (maj . , maj7-modifier)
214 (sus . , sus-modifier)))
216 ;; canonical 13 chord.
217 (define the-canonical-chord
223 (ly:make-pitch 1 (- n 8) (nca n))
224 (ly:make-pitch 0 (- n 1) (nca n))))
227 (define (stack-thirds upper-step base)
228 "Stack thirds listed in BASE until we reach UPPER-STEP. Add
229 UPPER-STEP separately."
230 (cond ((null? base) '())
231 ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
232 (cons (car base) (stack-thirds upper-step (cdr base))))
233 ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))