2 ;;; Generate chord names for the parser.
6 (define-public (construct-chord root duration modifications)
7 " Build a chord on root using modifiers in MODIFICATIONS. NoteEvent
8 have duration DURATION..
10 Notes: natural 11 is left from chord if not explicitly specified.
12 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 (cdr complete-chord) bass duration (car complete-chord)
159 (make-chord complete-chord bass duration #f #f))))
162 (define (make-chord 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 (let ((ev (make-music-by-name 'NoteEvent)))
167 (set! (ly:music-property ev 'duration) duration)
168 (set! (ly:music-property ev 'pitch) pitch)
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-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)))