Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / scm / chord-entry.scm
blob45c395ba4e6081d929580f0450a1825bcf98102d
1 ;;;; chord-entry.scm -- Generate chord names for the parser.
2 ;;;;
3 ;;;; source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2004--2009 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))
17          (complete-chord '())
18          (bass #f)
19          (inversion #f)
20          (lead-mod #f)
21          (explicit-11 #f)
22          (start-additions #t))
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))
27           (begin
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))
35           (begin
36             (set! bass (cadr mods))
37             (set! mods (cddr mods))))
38       (if (pair? mods)
39           (scm-error 'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f))
40       chord)
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)
46                              (cdr mods))
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))
59                                   (cdr mods)))
60             ((procedure? (car mods))
61              (interpret-additions ((car mods) chord)
62                                   (cdr mods)))
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))
70                             0 -1))
71                      (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
80 the bass specified.
83       (let* ((root (car complete-chord))
84              (inv? (lambda (y)
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))
94             (begin
95               (set! bass inversion)
96               (set! inversion #f)))
97         (if inversion
98             (cons down-inversion rest-of-chord)
99             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)))
109     ;; remember modifier
110     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
111         (begin
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)))
118         (begin
119           (if (= (pitch-step (car flat-mods)) 11)
120               (set! explicit-11 #t))
121           (set! base-chord
122                 (stack-thirds (car flat-mods) the-canonical-chord))
123           (set! flat-mods (cdr flat-mods))))
124     ;; apply modifier
125     (if (procedure? lead-mod)
126         (set! base-chord (lead-mod base-chord)))
127     (set! complete-chord
128           (if start-additions
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,
133     ;; we remove the 11.
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))
143                               complete-chord))
144     (if inversion
145         (set! complete-chord (process-inversion complete-chord)))
146     (if bass
147         (set! bass (pitch-octavated-strictly-below bass root)))
148     (if #f
149         (begin
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)))
156     (if inversion
157         (make-chord-elements (cdr complete-chord) bass duration (car complete-chord)
158                     inversion)
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
167                 'duration duration
168                 'pitch pitch))
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)))
172     (if bass-note
173         (begin
174           (set! (ly:music-property bass-note 'bass) #t)
175           (set! nots (cons bass-note nots))))
176     (if inv-note
177         (begin
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))))
183     nots))
185 ;;;;;;;;;;;;;;;;
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))
203   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
218   (map (lambda (n)
219          (define (nca x)
220            (if (= x 7) FLAT 0))
221          
222          (if (>= n 8)
223              (ly:make-pitch 1 (- n 8) (nca n))
224              (ly:make-pitch 0 (- n 1) (nca n))))
225        '(1 3 5 7 9 11 13)))
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)))
234          (list upper-step))
235         (else '())))