]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
(process_music): use octavation
[lilypond.git] / scm / chord-entry.scm
1 ;;;
2 ;;; Generate chord names for the parser.
3 ;;;
4 ;;;
5
6 (define-public (construct-chord root duration modifications)
7
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*
17       (
18        (flat-mods (flatten-list modifications))
19        (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
20        (complete-chord '())
21        (bass #f)
22        (inversion #f)
23        (lead-mod #f)
24        (explicit-11 #f)
25        (start-additions #t)
26        )
27
28     (define (interpret-inversion chord mods)
29       "Read /FOO   part. Side effect: INVERSION is set."
30       
31       (if (and (>  (length mods) 1) (eq? (car mods) 'chord-slash))
32           (begin
33             (set! inversion (cadr mods))
34             (set! mods (cddr mods))))
35       
36       (interpret-bass chord mods))
37       
38     (define (interpret-bass chord mods)
39       "Read /+FOO   part. Side effect: BASS is set."
40       
41       (if (and (>  (length mods) 1) (eq? (car mods) 'chord-bass))
42           (begin
43             (set! bass (cadr mods))
44             (set! mods (cddr mods))))
45
46       (if (pair? mods)
47           (scm-error  'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f) 
48           )
49       
50       chord
51       )
52       
53     (define (interpret-removals  chord mods)
54       (define (inner-interpret chord mods)
55         (if (and (pair? mods) (ly:pitch? (car mods)))
56             (inner-interpret
57              (remove-step (+ 1  (ly:pitch-steps (car mods))) chord)
58              (cdr mods))
59             (interpret-inversion chord mods))
60             )
61         
62       (if (and (pair? mods) (eq? (car mods) 'chord-caret))
63           (inner-interpret chord (cdr mods))
64           (interpret-inversion chord mods))
65       
66       )
67     
68     (define (interpret-additions  chord mods)
69       "Interpret additions. TODO: should restrict modifier use?"
70       (cond
71        ((null? mods) chord)
72        ((ly:pitch? (car mods))
73         (if (= (ly:pitch-steps (car mods)) 11)
74             (set! explicit-11 #t))
75         (interpret-additions
76          (cons (car mods) (remove-step (pitch-step (car mods)) chord))
77          (cdr mods)))
78        ((procedure? (car mods))
79         (interpret-additions  
80          ((car mods)  chord)
81          (cdr mods)))
82        (else (interpret-removals  chord mods))
83       ))
84
85     (define (pitch-octavated-strictly-below p root)
86       "return P, but octavated, so it is below  ROOT"
87       (ly:make-pitch
88        (+
89         (ly:pitch-octave root)
90         (if (> (ly:pitch-notename root)
91                 (ly:pitch-notename p))
92             0 -1))
93        (ly:pitch-notename p)
94        (ly:pitch-alteration p)))
95     
96     (define (process-inversion complete-chord)
97       "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
98 Return (INVERSION . REST-OF-CHORD).
99
100 Side effect: put original pitch in INVERSION.
101 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
102 the bass specified.  
103
104 "
105       (let*
106           (
107            (root (car complete-chord))
108            (inv? (lambda (y)
109                    (and (= (ly:pitch-notename y)
110                            (ly:pitch-notename inversion))
111                         (= (ly:pitch-alteration y)
112                            (ly:pitch-alteration inversion))
113                         )))
114                  
115            (rest-of-chord (filter-out-list inv? complete-chord))
116            (inversion-candidates (filter-list inv? complete-chord))
117            (down-inversion (pitch-octavated-strictly-below inversion root))
118            )
119
120         (if (pair? inversion-candidates)
121             (set! inversion (car inversion-candidates))
122             (begin
123               (set! bass inversion)
124               (set! inversion #f))
125             )
126         (if inversion
127             (cons down-inversion rest-of-chord)
128             rest-of-chord
129             )
130       ))
131
132     ;; root is always one octave too low.
133
134     ; something weird happens when this is removed,
135     ; every other chord is octavated. --hwn... hmmm. 
136     (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
137
138     ;; skip the leading : , we need some of the stuff following it.
139     (if (pair? flat-mods)
140         (if (eq? (car flat-mods)  'chord-colon)
141             (set! flat-mods (cdr flat-mods))
142             (set! start-additions #f)
143         ))
144
145     ;; remember modifier
146     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
147         (begin
148           (set! lead-mod (car flat-mods))
149           (set! flat-mods (cdr flat-mods))
150           ))
151
152     ;; extract first  number if present, and build pitch list.
153     (if (and (pair? flat-mods)
154              (ly:pitch?  (car flat-mods))
155              (not (eq? lead-mod sus-modifier))
156              )
157         
158         (begin
159           (if (=  (pitch-step (car flat-mods)) 11)
160               (set! explicit-11  #t))
161           (set! base-chord
162                 (stack-thirds (car flat-mods) the-canonical-chord))
163           (set! flat-mods (cdr flat-mods))
164         ))
165
166     ;; apply modifier
167     (if (procedure? lead-mod)
168         (set! base-chord (lead-mod base-chord)))
169
170     
171     (set! complete-chord
172           (if start-additions
173            (interpret-additions base-chord flat-mods)
174            (interpret-removals base-chord flat-mods)
175            ))
176
177     
178     (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
179                               (sort complete-chord ly:pitch<?)))
180
181     ;; If natural 11 + natural 3 is present, but not given explicitly,
182     ;; we remove the 11.
183     (if (and (not explicit-11)
184              (get-step 11 complete-chord)
185              (get-step 3 complete-chord)
186              (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
187              (= 0 (ly:pitch-alteration (get-step 3 complete-chord)))
188              )
189              
190         (set! complete-chord (remove-step 11  complete-chord))
191         )
192
193     (if inversion
194         (set! complete-chord (process-inversion complete-chord)))
195     (if bass
196         (set! bass (pitch-octavated-strictly-below bass root)))
197     
198     (if #f
199         (begin
200           (write-me "\n*******\n" flat-mods)
201           (write-me "root: " root)
202           (write-me "base chord: " base-chord)
203           (write-me "complete  chord: " complete-chord)
204           (write-me "inversion: " inversion)
205           (write-me "bass: " bass)))
206
207
208     
209     (if inversion
210         (make-chord (cdr complete-chord) bass duration (car complete-chord)
211                     inversion)
212         (make-chord complete-chord bass duration #f #f))
213   ))
214
215
216 (define (make-chord pitches bass duration inversion original-inv-pitch)
217   "Make EventChord with notes corresponding to PITCHES, BASS and
218 DURATION, and INVERSION."
219   (define (make-note-ev pitch)
220     (let*
221         (
222          (ev   (make-music-by-name 'NoteEvent))
223          )
224
225       (ly:set-mus-property! ev 'duration duration)
226       (ly:set-mus-property! ev 'pitch pitch)
227       ev      
228       ))
229   
230   (let*
231       (
232        (nots (map make-note-ev pitches))
233        (bass-note (if bass (make-note-ev bass) #f))
234        (inv-note (if inversion (make-note-ev inversion) #f))
235        )
236
237     
238     (if bass-note
239         (begin
240           (ly:set-mus-property! bass-note 'bass #t)
241           (set! nots (cons bass-note nots))))
242     
243     
244     (if inv-note
245         (begin
246           (ly:set-mus-property! inv-note 'inversion #t)
247           (ly:set-mus-property! inv-note 'octavation
248                                 (- (ly:pitch-octave inversion)
249                                    (ly:pitch-octave original-inv-pitch))
250                                 )
251           (set! nots (cons inv-note nots))))
252     
253     (make-event-chord nots)
254   ))
255
256
257 ;;;;;;;;;;;;;;;;
258 ; chord modifiers change the pitch list.
259
260 (define (aug-modifier  pitches)
261   (set! pitches  (replace-step (ly:make-pitch 0 4 1) pitches))
262   (replace-step (ly:make-pitch 0 2 0) pitches) 
263   )
264
265 (define (minor-modifier  pitches)
266   (replace-step (ly:make-pitch 0 2 -1) pitches)
267   )
268
269 (define (maj7-modifier  pitches)
270   (set! pitches (remove-step 7 pitches))
271   (cons  (ly:make-pitch 0 6 0) pitches)
272   )
273
274 (define (dim-modifier  pitches)
275   (set! pitches (replace-step (ly:make-pitch 0 2 -1) pitches))
276   (set! pitches (replace-step (ly:make-pitch 0 4 -1) pitches))
277   (set! pitches (replace-step (ly:make-pitch 0 6 -2) pitches))
278   pitches
279   )
280
281 (define (sus-modifier  pitches)
282    (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches)
283   )
284
285 (define-public default-chord-modifier-list
286   `((m . ,minor-modifier)
287     (min . ,minor-modifier)
288     (aug . , aug-modifier)
289     (dim . , dim-modifier)
290     (maj . , maj7-modifier)
291     (sus . , sus-modifier)
292     ))
293
294
295 ;; canonical 13 chord.
296 (define the-canonical-chord
297   (map
298    (lambda (n)
299      (define (nca x)
300        (if (= x 7) -1 0))
301      (if (>= n 8)
302          (ly:make-pitch 1 (- n 8) (nca n))
303          (ly:make-pitch 0 (- n 1) (nca n))))
304    '(1 3 5 7 9 11 13)))
305
306 (define (stack-thirds upper-step base)
307   "Stack thirds listed in BASE until we reach UPPER-STEP. Add
308 UPPER-STEP separately."
309    (cond
310     ((null? base) '())
311     ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
312      (cons (car base) (stack-thirds upper-step  (cdr base))))
313     ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
314      (list upper-step))
315     (else '())
316     ))
317