]> git.donarmstrong.com Git - lilypond.git/blob - scm/chords-ignatzek.scm
* scm/chords-ignatzek.scm (ignatzek-chord-names): robustness fix.
[lilypond.git] / scm / chords-ignatzek.scm
1 (define (natural-chord-alteration p)
2   "Return the natural alteration for step P."
3   (if (= (ly:pitch-steps p) 6)
4       -1
5       0))
6
7 (define (accidental->markup alteration)
8   "Return accidental markup for ALTERATION."
9   (if (= alteration 0)
10       (make-line-markup (list empty-markup))
11       (conditional-kern-before
12        (make-smaller-markup
13         (make-raise-markup
14         (if (= alteration -1)
15             0.3
16             0.6)
17         (make-musicglyph-markup
18          (string-append "accidentals-" (number->string alteration)))))
19        (= alteration -1) 0.2
20        )))
21
22 (define (pitch->markup pitch)
23   "Return pitch markup for PITCH."
24   (make-line-markup
25    (list
26     (make-simple-markup
27      (vector-ref #("C" "D" "E" "F" "G" "A" "B") (ly:pitch-notename pitch)))
28     (make-normal-size-super-markup
29      (accidental->markup (ly:pitch-alteration pitch))))))
30
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;;
33
34 (define-public (sequential-music-to-chord-exceptions seq)
35   "Transform sequential music of <<a b c>>-\markup{ foobar } type to
36  (cons ABC-PITCHES FOOBAR-MARKUP)
37  "
38   
39   (define (is-req-chord? m)
40     (and
41      (memq 'event-chord (ly:get-mus-property m 'types))
42      (not (equal? (ly:make-moment 0 1) (ly:get-music-length m)))
43     ))
44
45   (define (chord-to-exception-entry m)
46     (let*
47         (
48          (elts   (ly:get-mus-property m 'elements))
49          (pitches (map
50                    (lambda (x)
51                      (ly:get-mus-property x 'pitch)
52                      )
53                    (filter-list
54                     (lambda (y) (memq 'note-event (ly:get-mus-property y 'types)))
55                     elts)))
56          (sorted  (sort pitches ly:pitch<? ))
57          (root (car sorted))
58          (non-root (map (lambda (x) (ly:pitch-diff x root)) (cdr sorted)))
59          (texts (map
60                  (lambda (x)
61                    (ly:get-mus-property x 'text)
62                    )
63                  
64                  (filter-list
65                   (lambda (y)
66                     (memq 'text-script-event
67                           (ly:get-mus-property y 'types))) elts)
68                  ))
69          (text (if (null? texts)
70                    #f
71                    (car texts)))
72
73          )
74       (cons non-root text)
75     ))
76
77   (let*
78     (
79      (elts (filter-list is-req-chord? (ly:get-mus-property seq 'elements)))
80      (alist (map chord-to-exception-entry elts))
81      )
82     (filter-list (lambda (x) (cdr x)) alist)
83   ))
84
85
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 ;;
89 ;; jazz-part 2
90 ;;
91 ;; after Klaus Ignatzek,   Die Jazzmethode fuer Klavier 1.
92 ;; 
93 ;; The idea is: split chords into
94 ;;  
95 ;;  ROOT PREFIXES MAIN-NAME ALTERATIONS SUFFIXES ADDITIONS
96 ;;
97 ;; and put that through a layout routine.
98 ;; 
99 ;; the split is a procedural process, with lots of set!. 
100 ;;
101
102 (define-public (ignatzek-chord-names
103                 in-pitches bass inversion
104                 context)
105   
106   (define (get-step x ps)
107     "Does PS have the X step? Return that step if it does."
108     (if (null? ps)
109         #f
110         (if (= (- x 1) (ly:pitch-steps (car ps)))
111             (car ps) 
112             (get-step x (cdr ps)))
113         ))
114
115
116   (define (remove-step x ps)
117     "Copy PS, but leave out the Xth step."
118     (if (null? ps)
119         '()
120         (let*
121             (
122              (t (remove-step x (cdr ps)))
123              )
124
125           (if (= (- x 1) (ly:pitch-steps (car ps)))
126               t
127               (cons (car ps) t)
128               ))
129         ))
130
131   (define (remove-uptil-step x ps)
132     "Copy PS, but leave out everything below the Xth step."
133     (if (null? ps)
134         '()
135         (if (< (ly:pitch-steps (car ps)) (- x 1))
136             (remove-uptil-step x (cdr ps))
137             ps)
138         )
139     )
140
141   (define (pitch-step p)
142     "Musicological notation for an interval. Eg. C to D is 2."
143     (+ 1 (ly:pitch-steps p)))
144   
145
146   (define (is-natural-alteration? p)
147     (= (natural-chord-alteration p)  (ly:pitch-alteration p))
148     )
149   
150   
151   (define (ignatzek-format-chord-name
152            root
153            prefix-modifiers
154            main-name
155            alteration-pitches
156            addition-pitches
157            suffix-modifiers
158            )
159
160     "Format for the given (lists of) pitches. This is actually more
161 work than classifying the pitches."
162     
163     (define (filter-main-name p)
164     "The main name: don't print anything for natural 5 or 3."
165     (if
166      (or (not (ly:pitch? p))
167          (and (is-natural-alteration? p)
168           (or (= (pitch-step p) 5)
169               (= (pitch-step p) 3))))
170      '()
171      (list (name-step p))
172      ))
173
174     (define (glue-word-to-step word x)
175       (make-line-markup 
176        (list
177         (make-simple-markup word)
178         (name-step x)))
179       )
180     
181     (define (suffix-modifier->markup mod)
182       (if (or (= 4 (pitch-step mod))
183               (= 2 (pitch-step mod)))
184           (glue-word-to-step "sus" mod)
185           (glue-word-to-step "huh" mod)
186           ))
187     
188     (define (prefix-modifier->markup mod)
189       (if (and (= 3 (pitch-step mod))
190                (= -1 (ly:pitch-alteration mod)))
191           (make-simple-markup "m")
192           (make-simple-markup "huh")
193           ))
194     
195     (define (filter-alterations alters)
196       "Filter out uninteresting (natural) pitches from ALTERS."
197       
198       (define (altered? p)
199         (not (is-natural-alteration? p)))
200       
201       (if
202        (null? alters)
203        '()
204        (let*
205            (
206             (l (filter-list altered? alters))
207             (lp (last-pair alters))
208             )
209
210          ;; we want the highest also if unaltered
211          (if (and (not (altered? (car lp)))
212                   (> (pitch-step (car lp)) 5))
213              (append l (last-pair alters))
214              l)
215          )))
216
217     (define (name-step pitch)
218       (define (step-alteration pitch)
219         (- (ly:pitch-alteration pitch)
220            (natural-chord-alteration pitch)
221            ))
222
223       (let*
224           (
225            (num-markup (make-simple-markup
226                         (number->string (pitch-step pitch))))
227            (args (list num-markup))
228            (total (if (= (ly:pitch-alteration pitch) 0)
229                       (if (= (pitch-step pitch) 7)
230                           (list (ly:get-context-property context 'majorSevenSymbol))
231                           args)
232                       (cons (accidental->markup (step-alteration pitch)) args)
233                       ))
234            )
235         
236         (make-line-markup total)))
237
238     (let*
239         (
240          (sep (ly:get-context-property context 'chordNameSeparator))
241          (root-markup (pitch->markup root))
242          (add-markups (map (lambda (x)
243                              (glue-word-to-step "add" x))
244                            addition-pitches))
245          (filtered-alterations (filter-alterations alteration-pitches))
246          (alterations (map name-step filtered-alterations))
247          (suffixes (map suffix-modifier->markup suffix-modifiers))
248          (prefixes (map prefix-modifier->markup prefix-modifiers))
249          (main-markups (filter-main-name main-name))
250          (to-be-raised-stuff (markup-join
251                               (append
252                                main-markups
253                                alterations
254                                suffixes
255                                add-markups) sep))
256          )
257       
258       (make-line-markup
259        (list
260         root-markup
261         (markup-join prefixes sep)
262         (make-super-markup to-be-raised-stuff))
263        )))
264
265   (let*
266       (
267        (root (car in-pitches))
268        (pitches (map (lambda (x) (ly:pitch-diff x root)) (cdr in-pitches)))
269        (exceptions (ly:get-context-property context 'chordNameExceptions))
270        (exception (assoc-get-default pitches exceptions #f))
271        (prefixes '())
272        (suffixes '())
273        (add-steps '())
274        (main-name #f)
275        (alterations '())
276        )
277
278     (if
279      exception
280      (make-line-markup
281       (list (pitch->markup root) exception))
282      
283      (begin                             ; no exception.
284        
285        ; handle sus4 and sus2 suffix: if there is a 3 together with
286        ; sus2 or sus4, then we explicitly say  add3.
287        (map
288         (lambda (j)
289           (if (get-step j pitches)
290               (begin
291                 (if (get-step 3 pitches)
292                     (begin
293                       (set! add-steps (cons (get-step 3 pitches) add-steps))
294                       (set! pitches (remove-step 3 pitches))
295                       ))
296                 (set! suffixes  (cons (get-step j pitches) suffixes))
297                 )
298               )
299           ) '(2 4) )
300
301        ;; do minor-3rd modifier.
302        (if (and (get-step 3 pitches)
303                 (= (ly:pitch-alteration (get-step 3 pitches)) -1))
304            (set! prefixes (cons (get-step 3 pitches) prefixes))
305            )
306        
307        ;; lazy bum. Should write loop.
308        (cond
309         ((get-step 7 pitches) (set! main-name (get-step 7 pitches)))
310         ((get-step 6 pitches) (set! main-name (get-step 6 pitches)))
311         ((get-step 5 pitches) (set! main-name (get-step 5 pitches)))
312         ((get-step 4 pitches) (set! main-name (get-step 4 pitches)))
313         ((get-step 3 pitches) (set! main-name (get-step 3 pitches)))
314         )
315        
316        (let*
317            (
318             (3-diff? (lambda (x y)
319                        (= (- (pitch-step y) (pitch-step x)) 2)))
320             (split (split-at 3-diff? (remove-uptil-step 5 pitches)))
321             )
322          (set! alterations (append alterations (car split)))
323          (set! add-steps (append add-steps (cdr split)))
324          
325          (set! alterations (delq main-name alterations))
326          (set! add-steps (delq main-name add-steps))
327
328
329          ;; chords with natural (5 7 9 11 13) or leading subsequence.
330          ;; etc. are named by the top pitch, without any further
331          ;; alterations.
332          (if (and
333               (ly:pitch? main-name)
334               (= 7 (pitch-step main-name))
335               (is-natural-alteration? main-name)
336               (pair? (remove-uptil-step 7 alterations))
337               (reduce (lambda (x y) (and x y))
338                       (map is-natural-alteration? alterations)))
339              (begin
340                (set! main-name (tail alterations))
341                (set! alterations '())
342                ))
343          
344          (ignatzek-format-chord-name root prefixes main-name alterations add-steps suffixes)
345          )
346        ))))
347