1 ;;;; chord-entry.scm -- Generate chord names for the parser.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 (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.
16 (let* ((flat-mods (flatten-list modifications))
17 (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
25 (define (interpret-inversion chord mods)
26 "Read /FOO part. Side effect: INVERSION is set."
27 (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
29 (set! inversion (cadr mods))
30 (set! mods (cddr mods))))
31 (interpret-bass chord mods))
33 (define (interpret-bass chord mods)
34 "Read /+FOO part. Side effect: BASS is set."
35 (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
37 (set! bass (cadr mods))
38 (set! mods (cddr mods))))
40 (scm-error 'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f))
43 (define (interpret-removals chord mods)
44 (define (inner-interpret chord mods)
45 (if (and (pair? mods) (ly:pitch? (car mods)))
46 (inner-interpret (remove-step (+ 1 (ly:pitch-steps (car mods))) chord)
48 (interpret-inversion chord mods)))
49 (if (and (pair? mods) (eq? (car mods) 'chord-caret))
50 (inner-interpret chord (cdr mods))
51 (interpret-inversion chord mods)))
53 (define (interpret-additions chord mods)
54 "Interpret additions. TODO: should restrict modifier use?"
55 (cond ((null? mods) chord)
56 ((ly:pitch? (car mods))
57 (if (= (pitch-step (car mods)) 11)
58 (set! explicit-11 #t))
59 (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
61 ((procedure? (car mods))
62 (interpret-additions ((car mods) chord)
64 (else (interpret-removals chord mods))))
66 (define (pitch-octavated-strictly-below p root)
67 "return P, but octavated, so it is below ROOT"
68 (ly:make-pitch (+ (ly:pitch-octave root)
69 (if (> (ly:pitch-notename root)
70 (ly:pitch-notename p))
73 (ly:pitch-alteration p)))
75 (define (process-inversion complete-chord)
76 "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
77 Return (INVERSION . REST-OF-CHORD).
79 Side effect: put original pitch in INVERSION.
80 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
84 (let* ((root (car complete-chord))
86 (and (= (ly:pitch-notename y)
87 (ly:pitch-notename inversion))
88 (= (ly:pitch-alteration y)
89 (ly:pitch-alteration inversion)))))
90 (rest-of-chord (remove inv? complete-chord))
91 (inversion-candidates (filter inv? complete-chord))
92 (down-inversion (pitch-octavated-strictly-below inversion root)))
93 (if (pair? inversion-candidates)
94 (set! inversion (car inversion-candidates))
99 (cons down-inversion rest-of-chord)
101 ;; root is always one octave too low.
102 ;; something weird happens when this is removed,
103 ;; every other chord is octavated. --hwn... hmmm.
104 (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
105 ;; skip the leading : , we need some of the stuff following it.
106 (if (pair? flat-mods)
107 (if (eq? (car flat-mods) 'chord-colon)
108 (set! flat-mods (cdr flat-mods))
109 (set! start-additions #f)))
111 (if (and (pair? flat-mods) (procedure? (car flat-mods)))
113 (set! lead-mod (car flat-mods))
114 (set! flat-mods (cdr flat-mods))))
115 ;; extract first number if present, and build pitch list.
116 (if (and (pair? flat-mods)
117 (ly:pitch? (car flat-mods))
118 (not (eq? lead-mod sus-modifier)))
120 (if (= (pitch-step (car flat-mods)) 11)
121 (set! explicit-11 #t))
123 (stack-thirds (car flat-mods) the-canonical-chord))
124 (set! flat-mods (cdr flat-mods))))
126 (if (procedure? lead-mod)
127 (set! base-chord (lead-mod base-chord)))
130 (interpret-additions base-chord flat-mods)
131 (interpret-removals base-chord flat-mods)))
132 (set! complete-chord (sort complete-chord ly:pitch<?))
133 ;; If natural 11 + natural 3 is present, but not given explicitly,
135 (if (and (not explicit-11)
136 (get-step 11 complete-chord)
137 (get-step 3 complete-chord)
138 (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
139 (= 0 (ly:pitch-alteration (get-step 3 complete-chord))))
140 (set! complete-chord (remove-step 11 complete-chord)))
141 ;; must do before processing inversion/bass, since they are
142 ;; not relative to the root.
143 (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
146 (set! complete-chord (process-inversion complete-chord)))
148 (set! bass (pitch-octavated-strictly-below bass root)))
151 (write-me "\n*******\n" flat-mods)
152 (write-me "root: " root)
153 (write-me "base chord: " base-chord)
154 (write-me "complete chord: " complete-chord)
155 (write-me "inversion: " inversion)
156 (write-me "bass: " bass)))
158 (make-chord (cdr complete-chord) bass duration (car complete-chord)
160 (make-chord complete-chord bass duration #f #f))))
163 (define (make-chord pitches bass duration inversion original-inv-pitch)
164 "Make EventChord with notes corresponding to PITCHES, BASS and
165 DURATION, and INVERSION."
166 (define (make-note-ev pitch)
167 (make-music 'NoteEvent
170 (let ((nots (map make-note-ev pitches))
171 (bass-note (if bass (make-note-ev bass) #f))
172 (inv-note (if inversion (make-note-ev inversion) #f)))
175 (set! (ly:music-property bass-note 'bass) #t)
176 (set! nots (cons bass-note nots))))
179 (set! (ly:music-property inv-note 'inversion) #t)
180 (set! (ly:music-property inv-note 'octavation)
181 (- (ly:pitch-octave inversion)
182 (ly:pitch-octave original-inv-pitch)))
183 (set! nots (cons inv-note nots))))
184 (make-event-chord nots)))
187 ; chord modifiers change the pitch list.
189 (define (aug-modifier pitches)
190 (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
191 (replace-step (ly:make-pitch 0 2 0) pitches))
193 (define (minor-modifier pitches)
194 (replace-step (ly:make-pitch 0 2 FLAT) pitches))
196 (define (maj7-modifier pitches)
197 (set! pitches (remove-step 7 pitches))
198 (cons (ly:make-pitch 0 6 0) pitches))
200 (define (dim-modifier pitches)
201 (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
202 (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
203 (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
206 (define (sus-modifier pitches)
207 (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
209 (define-safe-public default-chord-modifier-list
210 `((m . ,minor-modifier)
211 (min . ,minor-modifier)
212 (aug . , aug-modifier)
213 (dim . , dim-modifier)
214 (maj . , maj7-modifier)
215 (sus . , sus-modifier)))
217 ;; canonical 13 chord.
218 (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)))