]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
* lily/beam-quanting.cc: cleanup, separate counts for left and
[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   " Build a chord on root using modifiers in MODIFICATIONS. NoteEvent
8 have duration DURATION..
9
10 Notes: natural 11 is left from chord if not explicitly specified.
11
12 Entry point for the parser. 
13
14 "
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))
23
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))
31       
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)
41       
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)))
51     
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))))
64
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)))
73     
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).
77
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.  
81
82 "
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 (cdr complete-chord) bass duration (car complete-chord)
158                     inversion)
159         (make-chord complete-chord bass duration #f #f))))
160
161
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)
169       ev))
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-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 '())))