]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / scm / chord-entry.scm
1 ;;;; chord-entry.scm -- Generate chord names for the parser.
2 ;;;;
3 ;;;; source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7 (define-public (construct-chord root duration modifications)
8   " Build a chord on root using modifiers in MODIFICATIONS. NoteEvent
9 have duration DURATION..
10
11 Notes: natural 11 is left from chord if not explicitly specified.
12
13 Entry point for the parser.
14
15 "
16   (let* ((flat-mods (flatten-list modifications))
17          (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
18          (complete-chord '())
19          (bass #f)
20          (inversion #f)
21          (lead-mod #f)
22          (explicit-11 #f)
23          (start-additions #t))
24
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))
28           (begin
29             (set! inversion (cadr mods))
30             (set! mods (cddr mods))))
31       (interpret-bass chord mods))
32
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))
36           (begin
37             (set! bass (cadr mods))
38             (set! mods (cddr mods))))
39       (if (pair? mods)
40           (scm-error 'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f))
41       chord)
42
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)
47                              (cdr mods))
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)))
52
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))
60                                   (cdr mods)))
61             ((procedure? (car mods))
62              (interpret-additions ((car mods) chord)
63                                   (cdr mods)))
64             (else (interpret-removals chord mods))))
65
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))
71                             0 -1))
72                      (ly:pitch-notename p)
73                      (ly:pitch-alteration p)))
74
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).
78
79 Side effect: put original pitch in INVERSION.
80 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
81 the bass specified.
82
83 "
84       (let* ((root (car complete-chord))
85              (inv? (lambda (y)
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))
95             (begin
96               (set! bass inversion)
97               (set! inversion #f)))
98         (if inversion
99             (cons down-inversion rest-of-chord)
100             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)))
110     ;; remember modifier
111     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
112         (begin
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)))
119         (begin
120           (if (= (pitch-step (car flat-mods)) 11)
121               (set! explicit-11 #t))
122           (set! base-chord
123                 (stack-thirds (car flat-mods) the-canonical-chord))
124           (set! flat-mods (cdr flat-mods))))
125     ;; apply modifier
126     (if (procedure? lead-mod)
127         (set! base-chord (lead-mod base-chord)))
128     (set! complete-chord
129           (if start-additions
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,
134     ;; we remove the 11.
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))
144                               complete-chord))
145     (if inversion
146         (set! complete-chord (process-inversion complete-chord)))
147     (if bass
148         (set! bass (pitch-octavated-strictly-below bass root)))
149     (if #f
150         (begin
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)))
157     (if inversion
158         (make-chord (cdr complete-chord) bass duration (car complete-chord)
159                     inversion)
160         (make-chord complete-chord bass duration #f #f))))
161
162
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
168                 'duration duration
169                 '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)))
173     (if bass-note
174         (begin
175           (set! (ly:music-property bass-note 'bass) #t)
176           (set! nots (cons bass-note nots))))
177     (if inv-note
178         (begin
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)))
185
186 ;;;;;;;;;;;;;;;;
187 ; chord modifiers change the pitch list.
188
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))
192
193 (define (minor-modifier pitches)
194   (replace-step (ly:make-pitch 0 2 FLAT) pitches))
195
196 (define (maj7-modifier pitches)
197   (set! pitches (remove-step 7 pitches))
198   (cons (ly:make-pitch 0 6 0) pitches))
199
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))
204   pitches)
205
206 (define (sus-modifier pitches)
207   (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
208
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)))
216
217 ;; canonical 13 chord.
218 (define the-canonical-chord
219   (map (lambda (n)
220          (define (nca x)
221            (if (= x 7) FLAT 0))
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)))
226
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 '())))