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