]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
Merge branch 'web-experimental' into lilypond/translation
[lilypond.git] / scm / chord-entry.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 (define-public (construct-chord-elements root duration modifications)
19   "Build a chord on root using modifiers in @var{modifications}.
20 @code{NoteEvents} have duration @var{duration}.
21
22 Notes: Natural 11 is left from chord if not explicitly specified.
23
24 Entry point for the parser."
25   (let* ((flat-mods (flatten-list modifications))
26          (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
27          (complete-chord '())
28          (bass #f)
29          (inversion #f)
30          (lead-mod #f)
31          (explicit-11 #f)
32          (start-additions #t))
33
34     (define (interpret-inversion chord mods)
35       "Read /FOO part.  Side effect: INVERSION is set."
36       (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
37           (begin
38             (set! inversion (cadr mods))
39             (set! mods (cddr mods))))
40       (interpret-bass chord mods))
41
42     (define (interpret-bass chord mods)
43       "Read /+FOO part.  Side effect: BASS is set."
44       (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
45           (begin
46             (set! bass (cadr mods))
47             (set! mods (cddr mods))))
48       (if (pair? mods)
49           (ly:warning (_ "Spurious garbage following chord: ~A") mods))
50       chord)
51
52     (define (interpret-removals  chord mods)
53       (define (inner-interpret chord mods)
54         (if (and (pair? mods) (ly:pitch? (car mods)))
55             (inner-interpret (remove-step (+ 1  (ly:pitch-steps (car mods))) chord)
56                              (cdr mods))
57             (interpret-inversion chord mods)))
58       (if (and (pair? mods) (eq? (car mods) 'chord-caret))
59           (inner-interpret chord (cdr mods))
60           (interpret-inversion chord mods)))
61
62     (define (interpret-additions chord mods)
63       "Interpret additions.  TODO: should restrict modifier use?"
64       (cond ((null? mods) chord)
65             ((ly:pitch? (car mods))
66              (if (= (pitch-step (car mods)) 11)
67                  (set! explicit-11 #t))
68              (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
69                                   (cdr mods)))
70             ((procedure? (car mods))
71              (interpret-additions ((car mods) chord)
72                                   (cdr mods)))
73             (else (interpret-removals chord mods))))
74
75     (define (pitch-octavated-strictly-below p root)
76       "return P, but octavated, so it is below  ROOT"
77       (ly:make-pitch (+ (ly:pitch-octave root)
78                         (if (> (ly:pitch-notename root)
79                                (ly:pitch-notename p))
80                             0 -1))
81                      (ly:pitch-notename p)
82                      (ly:pitch-alteration p)))
83
84     (define (process-inversion complete-chord)
85       "Take out inversion from COMPLETE-CHORD, and put it at the bottom.
86 Return (INVERSION . REST-OF-CHORD).
87
88 Side effect: put original pitch in INVERSION.
89 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
90 the bass specified.
91
92 "
93       (let* ((root (car complete-chord))
94              (inv? (lambda (y)
95                      (and (= (ly:pitch-notename y)
96                              (ly:pitch-notename inversion))
97                           (= (ly:pitch-alteration y)
98                              (ly:pitch-alteration inversion)))))
99              (rest-of-chord (remove inv? complete-chord))
100              (inversion-candidates (filter inv? complete-chord))
101              (down-inversion (pitch-octavated-strictly-below inversion root)))
102         (if (pair? inversion-candidates)
103             (set! inversion (car inversion-candidates))
104             (begin
105               (set! bass inversion)
106               (set! inversion #f)))
107         (if inversion
108             (cons down-inversion rest-of-chord)
109             rest-of-chord)))
110     ;; root is always one octave too low.
111     ;; something weird happens when this is removed,
112     ;; every other chord is octavated. --hwn... hmmm.
113     (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
114     ;; skip the leading : , we need some of the stuff following it.
115     (if (pair? flat-mods)
116         (if (eq? (car flat-mods) 'chord-colon)
117             (set! flat-mods (cdr flat-mods))
118             (set! start-additions #f)))
119     ;; remember modifier
120     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
121         (begin
122           (set! lead-mod (car flat-mods))
123           (set! flat-mods (cdr flat-mods))))
124     ;; extract first number if present, and build pitch list.
125     (if (and (pair? flat-mods)
126              (ly:pitch?  (car flat-mods))
127              (not (eq? lead-mod sus-modifier)))
128         (begin
129           (if (= (pitch-step (car flat-mods)) 11)
130               (set! explicit-11 #t))
131           (set! base-chord
132                 (stack-thirds (car flat-mods) the-canonical-chord))
133           (set! flat-mods (cdr flat-mods))))
134     ;; apply modifier
135     (if (procedure? lead-mod)
136         (set! base-chord (lead-mod base-chord)))
137     (set! complete-chord
138           (if start-additions
139               (interpret-additions base-chord flat-mods)
140               (interpret-removals base-chord flat-mods)))
141     (set! complete-chord (sort complete-chord ly:pitch<?))
142     ;; If natural 11 + natural 3 is present, but not given explicitly,
143     ;; we remove the 11.
144     (if (and (not explicit-11)
145              (get-step 11 complete-chord)
146              (get-step 3 complete-chord)
147              (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
148              (= 0 (ly:pitch-alteration (get-step 3 complete-chord))))
149         (set! complete-chord (remove-step 11 complete-chord)))
150     ;; must do before processing inversion/bass, since they are
151     ;; not relative to the root.
152     (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
153                               complete-chord))
154     (if inversion
155         (set! complete-chord (process-inversion complete-chord)))
156     (if bass
157         (set! bass (pitch-octavated-strictly-below bass root)))
158     (if #f
159         (begin
160           (write-me "\n*******\n" flat-mods)
161           (write-me "root: " root)
162           (write-me "base chord: " base-chord)
163           (write-me "complete chord: " complete-chord)
164           (write-me "inversion: " inversion)
165           (write-me "bass: " bass)))
166     (if inversion
167         (make-chord-elements (cdr complete-chord) bass duration (car complete-chord)
168                     inversion)
169         (make-chord-elements complete-chord bass duration #f #f))))
170
171
172 (define (make-chord-elements pitches bass duration inversion original-inv-pitch)
173   "Make EventChord with notes corresponding to PITCHES, BASS and
174 DURATION, and INVERSION."
175   (define (make-note-ev pitch)
176     (make-music 'NoteEvent
177                 'duration duration
178                 'pitch pitch))
179   (let ((nots (map make-note-ev pitches))
180         (bass-note (if bass (make-note-ev bass) #f))
181         (inv-note (if inversion (make-note-ev inversion) #f)))
182     (if bass-note
183         (begin
184           (set! (ly:music-property bass-note 'bass) #t)
185           (set! nots (cons bass-note nots))))
186     (if inv-note
187         (begin
188           (set! (ly:music-property inv-note 'inversion) #t)
189           (set! (ly:music-property inv-note 'octavation)
190                 (- (ly:pitch-octave inversion)
191                    (ly:pitch-octave original-inv-pitch)))
192           (set! nots (cons inv-note nots))))
193     nots))
194
195 ;;;;;;;;;;;;;;;;
196 ; chord modifiers change the pitch list.
197
198 (define (aug-modifier pitches)
199   (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
200   (replace-step (ly:make-pitch 0 2 0) pitches))
201
202 (define (minor-modifier pitches)
203   (replace-step (ly:make-pitch 0 2 FLAT) pitches))
204
205 (define (maj7-modifier pitches)
206   (set! pitches (remove-step 7 pitches))
207   (cons (ly:make-pitch 0 6 0) pitches))
208
209 (define (dim-modifier pitches)
210   (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
211   (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
212   (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
213   pitches)
214
215 (define (sus-modifier pitches)
216   (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
217
218 (define-safe-public default-chord-modifier-list
219   `((m . ,minor-modifier)
220     (min . ,minor-modifier)
221     (aug . , aug-modifier)
222     (dim . , dim-modifier)
223     (maj . , maj7-modifier)
224     (sus . , sus-modifier)))
225
226 ;; canonical 13 chord.
227 (define the-canonical-chord
228   (map (lambda (n)
229          (define (nca x)
230            (if (= x 7) FLAT 0))
231          
232          (if (>= n 8)
233              (ly:make-pitch 1 (- n 8) (nca n))
234              (ly:make-pitch 0 (- n 1) (nca n))))
235        '(1 3 5 7 9 11 13)))
236
237 (define (stack-thirds upper-step base)
238   "Stack thirds listed in BASE until we reach UPPER-STEP.  Add
239 UPPER-STEP separately."
240   (cond ((null? base) '())
241         ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
242          (cons (car base) (stack-thirds upper-step (cdr base))))
243         ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
244          (list upper-step))
245         (else '())))