]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
junk contents.
[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 (process-inversion complete-chord)
86       "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
87 Return (INVERSION . REST-OF-CHORD).
88
89 Side effect: put original pitch in INVERSION.
90 "
91       (let*
92           (
93            (root (car complete-chord))
94            (inv? (lambda (y)
95                    (= (ly:pitch-notename y)
96                       (ly:pitch-notename inversion))))
97            (rest-of-chord (filter-out-list inv? complete-chord))
98            (inversion-candidates (filter-list inv? complete-chord))
99            (down-inversion (ly:make-pitch
100                             (+
101                              (ly:pitch-octave root)
102                              (if (>= (ly:pitch-notename root)
103                                     (ly:pitch-notename inversion))
104                                  0 -1))
105                            (ly:pitch-notename inversion)
106                            (ly:pitch-alteration inversion)))
107            )
108
109         (if (pair? inversion-candidates)
110             (set! inversion (car inversion-candidates)))
111         
112         (cons down-inversion rest-of-chord)
113       ))
114
115     ;; root is always one octave too low.
116
117     ; something weird happens when this is removed,
118     ; every other chord is octavated. --hwn... hmmm. 
119     (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
120     
121     (if #f
122         (begin
123           (write-me "\n*******\n" flat-mods)
124           (write-me "root: " root)
125           (write-me "base: " base-chord)
126           (write-me "bass: " bass)))
127
128     ;; skip the leading : , we need some of the   stuff following it.
129     (if (pair? flat-mods)
130         (if (eq? (car flat-mods)  'chord-colon)
131             (set! flat-mods (cdr flat-mods))
132             (set! start-additions #f)
133         ))
134
135     ;; remember modifier
136     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
137         (begin
138           (set! lead-mod (car flat-mods))
139           (set! flat-mods (cdr flat-mods))
140           ))
141
142     ;; extract first  number if present, and build pitch list.
143     (if (and (pair? flat-mods)
144              (ly:pitch?  (car flat-mods))
145              (not (eq? lead-mod sus-modifier))
146              )
147         
148         (begin
149           (if (=  (pitch-step (car flat-mods)) 11)
150               (set! explicit-11  #t))
151           (set! base-chord
152                 (map (lambda (y) (ly:pitch-transpose y root))
153                      (stack-thirds (car flat-mods) the-canonical-chord)))
154           (set! flat-mods (cdr flat-mods))
155         ))
156
157     ;; apply modifier
158     (if (procedure? lead-mod)
159         (set! base-chord (lead-mod base-chord)))
160
161     
162     (set! complete-chord
163           (if start-additions
164            (interpret-additions base-chord flat-mods)
165            (interpret-removals base-chord flat-mods)
166            ))
167
168     
169     (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
170                               (sort complete-chord ly:pitch<?)))
171
172     ;; If natural 11 is present, but not given explicitly, we remove
173     ;; it.
174     (if (and (not explicit-11)
175              (get-step 11 complete-chord)
176              (= 0 (ly:pitch-alteration  (get-step 11 complete-chord))))
177              
178         (set! complete-chord (remove-step 11  complete-chord))
179         )
180
181     (if inversion
182         (begin
183           (set! complete-chord (process-inversion complete-chord))
184           (make-chord (cdr complete-chord) bass duration (car complete-chord)
185                       inversion
186                       ))
187         (make-chord complete-chord bass duration #f #f))
188   ))
189
190
191 (define (make-chord pitches bass duration inversion original-inv-pitch)
192   "Make EventChord with notes corresponding to PITCHES, BASS and
193 DURATION, and INVERSION."
194   (define (make-note-ev pitch)
195     (let*
196         (
197          (ev   (make-music-by-name 'NoteEvent))
198          )
199
200       (ly:set-mus-property! ev 'duration duration)
201       (ly:set-mus-property! ev 'pitch pitch)
202       ev      
203       ))
204   
205   (let*
206       (
207        (nots (map make-note-ev pitches))
208        (bass-note (if bass (make-note-ev bass) #f))
209        (inv-note (if inversion (make-note-ev inversion) #f))
210        )
211
212     
213     (if bass-note
214         (begin
215           (ly:set-mus-property! bass-note 'bass #t)
216           (set! nots (cons bass-note nots))))
217     
218     
219     (if inv-note
220         (begin
221           (ly:set-mus-property! inv-note 'inversion #t)
222           (ly:set-mus-property! inv-note 'original-pitch original-inv-pitch)
223           (set! nots (cons inv-note nots))))
224     
225     (make-event-chord nots)
226   ))
227
228
229 ;;;;;;;;;;;;;;;;
230 ; chord modifiers change the pitch list.
231
232 (define (aug-modifier  pitches)
233   (set! pitches  (replace-step (ly:make-pitch 0 4 1) pitches))
234   (replace-step (ly:make-pitch 0 2 0) pitches) 
235   )
236
237 (define (minor-modifier  pitches)
238   (replace-step (ly:make-pitch 0 2 -1) pitches)
239   )
240
241 (define (maj7-modifier  pitches)
242   (set! pitches (remove-step 7 pitches))
243   (cons  (ly:make-pitch 0 6 0) pitches)
244   )
245
246 (define (dim-modifier  pitches)
247   (set! pitches (replace-step (ly:make-pitch 0 2 -1) pitches))
248   (set! pitches (replace-step (ly:make-pitch 0 4 -1) pitches))
249   (set! pitches (replace-step (ly:make-pitch 0 6 -2) pitches))
250   pitches
251   )
252
253 (define (sus-modifier  pitches)
254    (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches)
255   )
256
257 (define-public default-chord-modifier-list
258   `((m . ,minor-modifier)
259     (min . ,minor-modifier)
260     (aug . , aug-modifier)
261     (dim . , dim-modifier)
262     (maj . , maj7-modifier)
263     (sus . , sus-modifier)
264     ))
265
266
267 ;; canonical 13 chord.
268 (define the-canonical-chord
269   (map
270    (lambda (n)
271      (define (nca x)
272        (if (= x 7) -1 0))
273      (if (>= n 8)
274          (ly:make-pitch 1 (- n 8) (nca n))
275          (ly:make-pitch 0 (- n 1) (nca n))))
276    '(1 3 5 7 9 11 13)))
277
278 (define (stack-thirds upper-step base)
279   "Stack thirds listed in BASE until we reach UPPER-STEP. Add
280 UPPER-STEP separately."
281    (cond
282     ((null? base) '())
283     ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
284      (cons (car base) (stack-thirds upper-step  (cdr base))))
285     ((= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
286      (list upper-step))
287     (else '())
288     ))
289