]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-entry.scm
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / scm / chord-entry.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2009 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 MODIFICATIONS. NoteEvents
20 have duration DURATION.
21
22 Notes: natural 11 is left from chord if not explicitly specified.
23
24 Entry point for the parser.
25 "
26   (let* ((flat-mods (flatten-list modifications))
27          (base-chord (stack-thirds (ly:make-pitch 0 4 0) the-canonical-chord))
28          (complete-chord '())
29          (bass #f)
30          (inversion #f)
31          (lead-mod #f)
32          (explicit-11 #f)
33          (start-additions #t))
34
35     (define (interpret-inversion chord mods)
36       "Read /FOO part. Side effect: INVERSION is set."
37       (if (and (> (length mods) 1) (eq? (car mods) 'chord-slash))
38           (begin
39             (set! inversion (cadr mods))
40             (set! mods (cddr mods))))
41       (interpret-bass chord mods))
42
43     (define (interpret-bass chord mods)
44       "Read /+FOO part. Side effect: BASS is set."
45       (if (and (> (length mods) 1) (eq? (car mods) 'chord-bass))
46           (begin
47             (set! bass (cadr mods))
48             (set! mods (cddr mods))))
49       (if (pair? mods)
50           (scm-error 'chord-format "construct-chord" "Spurious garbage following chord: ~A" mods #f))
51       chord)
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 (remove-step (+ 1  (ly:pitch-steps (car mods))) chord)
57                              (cdr mods))
58             (interpret-inversion chord mods)))
59       (if (and (pair? mods) (eq? (car mods) 'chord-caret))
60           (inner-interpret chord (cdr mods))
61           (interpret-inversion chord mods)))
62
63     (define (interpret-additions chord mods)
64       "Interpret additions. TODO: should restrict modifier use?"
65       (cond ((null? mods) chord)
66             ((ly:pitch? (car mods))
67              (if (= (pitch-step (car mods)) 11)
68                  (set! explicit-11 #t))
69              (interpret-additions (cons (car mods) (remove-step (pitch-step (car mods)) chord))
70                                   (cdr mods)))
71             ((procedure? (car mods))
72              (interpret-additions ((car mods) chord)
73                                   (cdr mods)))
74             (else (interpret-removals chord mods))))
75
76     (define (pitch-octavated-strictly-below p root)
77       "return P, but octavated, so it is below  ROOT"
78       (ly:make-pitch (+ (ly:pitch-octave root)
79                         (if (> (ly:pitch-notename root)
80                                (ly:pitch-notename p))
81                             0 -1))
82                      (ly:pitch-notename p)
83                      (ly:pitch-alteration p)))
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 If INVERSION is not in COMPLETE-CHORD, it will be set as a BASS, overriding
91 the bass specified.
92
93 "
94       (let* ((root (car complete-chord))
95              (inv? (lambda (y)
96                      (and (= (ly:pitch-notename y)
97                              (ly:pitch-notename inversion))
98                           (= (ly:pitch-alteration y)
99                              (ly:pitch-alteration inversion)))))
100              (rest-of-chord (remove inv? complete-chord))
101              (inversion-candidates (filter inv? complete-chord))
102              (down-inversion (pitch-octavated-strictly-below inversion root)))
103         (if (pair? inversion-candidates)
104             (set! inversion (car inversion-candidates))
105             (begin
106               (set! bass inversion)
107               (set! inversion #f)))
108         (if inversion
109             (cons down-inversion rest-of-chord)
110             rest-of-chord)))
111     ;; root is always one octave too low.
112     ;; something weird happens when this is removed,
113     ;; every other chord is octavated. --hwn... hmmm.
114     (set! root (ly:pitch-transpose root (ly:make-pitch 1 0 0)))
115     ;; skip the leading : , we need some of the stuff following it.
116     (if (pair? flat-mods)
117         (if (eq? (car flat-mods) 'chord-colon)
118             (set! flat-mods (cdr flat-mods))
119             (set! start-additions #f)))
120     ;; remember modifier
121     (if (and (pair? flat-mods) (procedure? (car flat-mods)))
122         (begin
123           (set! lead-mod (car flat-mods))
124           (set! flat-mods (cdr flat-mods))))
125     ;; extract first number if present, and build pitch list.
126     (if (and (pair? flat-mods)
127              (ly:pitch?  (car flat-mods))
128              (not (eq? lead-mod sus-modifier)))
129         (begin
130           (if (= (pitch-step (car flat-mods)) 11)
131               (set! explicit-11 #t))
132           (set! base-chord
133                 (stack-thirds (car flat-mods) the-canonical-chord))
134           (set! flat-mods (cdr flat-mods))))
135     ;; apply modifier
136     (if (procedure? lead-mod)
137         (set! base-chord (lead-mod base-chord)))
138     (set! complete-chord
139           (if start-additions
140               (interpret-additions base-chord flat-mods)
141               (interpret-removals base-chord flat-mods)))
142     (set! complete-chord (sort complete-chord ly:pitch<?))
143     ;; If natural 11 + natural 3 is present, but not given explicitly,
144     ;; we remove the 11.
145     (if (and (not explicit-11)
146              (get-step 11 complete-chord)
147              (get-step 3 complete-chord)
148              (= 0 (ly:pitch-alteration (get-step 11 complete-chord)))
149              (= 0 (ly:pitch-alteration (get-step 3 complete-chord))))
150         (set! complete-chord (remove-step 11 complete-chord)))
151     ;; must do before processing inversion/bass, since they are
152     ;; not relative to the root.
153     (set! complete-chord (map (lambda (x) (ly:pitch-transpose x root))
154                               complete-chord))
155     (if inversion
156         (set! complete-chord (process-inversion complete-chord)))
157     (if bass
158         (set! bass (pitch-octavated-strictly-below bass root)))
159     (if #f
160         (begin
161           (write-me "\n*******\n" flat-mods)
162           (write-me "root: " root)
163           (write-me "base chord: " base-chord)
164           (write-me "complete chord: " complete-chord)
165           (write-me "inversion: " inversion)
166           (write-me "bass: " bass)))
167     (if inversion
168         (make-chord-elements (cdr complete-chord) bass duration (car complete-chord)
169                     inversion)
170         (make-chord-elements complete-chord bass duration #f #f))))
171
172
173 (define (make-chord-elements pitches bass duration inversion original-inv-pitch)
174   "Make EventChord with notes corresponding to PITCHES, BASS and
175 DURATION, and INVERSION."
176   (define (make-note-ev pitch)
177     (make-music 'NoteEvent
178                 'duration duration
179                 'pitch pitch))
180   (let ((nots (map make-note-ev pitches))
181         (bass-note (if bass (make-note-ev bass) #f))
182         (inv-note (if inversion (make-note-ev inversion) #f)))
183     (if bass-note
184         (begin
185           (set! (ly:music-property bass-note 'bass) #t)
186           (set! nots (cons bass-note nots))))
187     (if inv-note
188         (begin
189           (set! (ly:music-property inv-note 'inversion) #t)
190           (set! (ly:music-property inv-note 'octavation)
191                 (- (ly:pitch-octave inversion)
192                    (ly:pitch-octave original-inv-pitch)))
193           (set! nots (cons inv-note nots))))
194     nots))
195
196 ;;;;;;;;;;;;;;;;
197 ; chord modifiers change the pitch list.
198
199 (define (aug-modifier pitches)
200   (set! pitches (replace-step (ly:make-pitch 0 4 SHARP) pitches))
201   (replace-step (ly:make-pitch 0 2 0) pitches))
202
203 (define (minor-modifier pitches)
204   (replace-step (ly:make-pitch 0 2 FLAT) pitches))
205
206 (define (maj7-modifier pitches)
207   (set! pitches (remove-step 7 pitches))
208   (cons (ly:make-pitch 0 6 0) pitches))
209
210 (define (dim-modifier pitches)
211   (set! pitches (replace-step (ly:make-pitch 0 2 FLAT) pitches))
212   (set! pitches (replace-step (ly:make-pitch 0 4 FLAT) pitches))
213   (set! pitches (replace-step (ly:make-pitch 0 6 DOUBLE-FLAT) pitches))
214   pitches)
215
216 (define (sus-modifier pitches)
217   (remove-step (pitch-step (ly:make-pitch 0 2 0)) pitches))
218
219 (define-safe-public default-chord-modifier-list
220   `((m . ,minor-modifier)
221     (min . ,minor-modifier)
222     (aug . , aug-modifier)
223     (dim . , dim-modifier)
224     (maj . , maj7-modifier)
225     (sus . , sus-modifier)))
226
227 ;; canonical 13 chord.
228 (define the-canonical-chord
229   (map (lambda (n)
230          (define (nca x)
231            (if (= x 7) FLAT 0))
232          
233          (if (>= n 8)
234              (ly:make-pitch 1 (- n 8) (nca n))
235              (ly:make-pitch 0 (- n 1) (nca n))))
236        '(1 3 5 7 9 11 13)))
237
238 (define (stack-thirds upper-step base)
239   "Stack thirds listed in BASE until we reach UPPER-STEP. Add
240 UPPER-STEP separately."
241   (cond ((null? base) '())
242         ((> (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
243          (cons (car base) (stack-thirds upper-step (cdr base))))
244         ((<= (ly:pitch-steps upper-step) (ly:pitch-steps (car base)))
245          (list upper-step))
246         (else '())))