]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-name.scm
comments
[lilypond.git] / scm / chord-name.scm
1 ;;;
2 ;;; chord-name.scm -- Compile chord name
3 ;;;
4 ;;; source file of the GNU LilyPond music typesetter
5 ;;; 
6 ;;; (c) 2000--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;
8
9
10 (use-modules
11    (ice-9 debug)
12    (ice-9 format)
13    (ice-9 regex)
14    (ice-9 string-fun)
15    )
16
17
18 (define-public (write-me x)
19   "Write and return X. For debugging purposes. "
20   (write x) (newline) x)
21
22 ;(define (dbg x) (write-me x))
23 (define (dbg x) x)
24
25 ;;(define (write-me x) (write x) (newline) x)
26 ;;(define (write-me-2 x y) (write "FOO") (write x) (write y) (newline) y)
27
28
29 "
30 TODO:
31
32   * Use lilypond Pitch objects -- SCM pitch objects lead to
33     duplication. LilyPond pitch objects force meaningful names
34     (i.e. (ly:pitch-octave PITCH) )
35
36   * Pitches are musical objects. The pitches -> markup step should
37 happen earlier (during interpreting), brew-molecule () should only
38 dump reinterpret the markup as a molecule.
39
40   *  chord:: prefix is a poor-man's namespace device.
41   We have a module system to prevent export to userland.
42   Nested functions can take care of many other clashes. --hwn.
43
44    * easier tweakability:
45
46     - split chord::names-alists up into logical bits,
47       such as chord::exceptions-delta, exceptions-oslash
48     - iso just the 'style parameter, use a list, eg:
49       \property ChordNames.ChordName \set
50         #'style = #'(jazz delta oslash german-tonic german-Bb)
51
52  * clean split/merge of bass/banter/american stuff.
53    GET RID OF code duplication.
54
55  * fix FIXMEs
56
57  * doc strings
58
59 "
60
61 ;; " hey Emacs: string has ended
62
63
64 ;; pitch = (octave notename alteration)
65 ;;
66 ;; note = (notename . alteration)
67 ;;
68 ;; markup = markup text -- see font.scm and input/test/markup.ly
69
70
71 (define-public chord::exception-alist-banter
72        `(
73         ; C iso C.no3.no5
74         (((0 . 0)) . ,empty-markup)
75         ; C iso C.no5
76         (((0 . 0) (2 . 0)) . ,empty-markup)
77         ; Cm iso Cm.no5
78         (((0 . 0) (2 . -1)) . ,(make-simple-markup "m"))
79         ; C2 iso C2.no3
80         (((0 . 0) (1 . 0) (4 . 0))
81          . ,(make-normal-size-super-markup (make-simple-markup "2 ")))
82         ; C4 iso C4.no3
83         (((0 . 0) (3 . 0) (4 . 0))
84          . ,(make-normal-size-super-markup (make-simple-markup "4 ")))
85         ;; Cdim iso Cm5-
86         (((0 . 0) (2 . -1) (4 . -1)) . ,(make-simple-markup "dim"))
87         ; URG: Simply C:m5-/maj7 iso Cdim maj7
88         (((0 . 0) (2 . -1) (4 . -1) (6 . 0))
89          . ,(make-line-markup
90              (list
91               (make-simple-markup "m")
92               (make-normal-size-super-markup (make-simple-markup "5-/maj7 ")))))
93         ; URG: Simply C:m5-/7 iso Cdim7
94         (((0 . 0) (2 . -1) (4 . -1) (6 . -1))
95          . ,(make-line-markup
96              (list
97               (make-simple-markup "m")
98               (make-normal-size-super-markup (make-simple-markup "5-/7 ")))))
99         ; Co iso C:m5-/7-
100         (((0 . 0) (2 . -1) (4 . -1) (6 . -2))
101          . ,(make-super-markup (make-simple-markup "o")))
102         ; Cdim9
103         (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1))
104          . ,(make-line-markup
105              (list (make-simple-markup "dim")
106                    (make-normal-size-super-markup (make-simple-markup "9 ")))))
107         (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1) (3 . -1))
108          . ,(make-line-markup
109              (list (make-simple-markup "dim")
110                                 (make-normal-size-super-markup
111                                  (make-simple-markup "11 ")))))
112         
113         ))
114
115 ; pitch->note-name: drops octave
116 (define (pitch->note-name pitch)
117   (cons (cadr pitch) (caddr pitch)))
118
119 (define (accidental->markup acc)
120   "ACC is an int, return a markup making an accidental."
121   (if (= acc 0)
122       (make-line-markup (list empty-markup))
123       (make-smaller-markup (make-musicglyph-markup
124                             (string-append "accidentals-"
125                                            (number->string acc))))))
126
127 ; unused.
128 (define (accidental->markupp acc pos)
129
130   (if (= acc 0)
131       empty-markup
132       (let ((acc-markup (make-musicglyph-markup
133                          (string-append "accidentals-"
134                                         (number->string acc)))))
135         
136         (if (equal? pos 'columns)
137             (make-line-markup (list (make-smaller-markup acc-markup)))
138           (if (equal? pos 'super)
139               (make-line-markup (list (make-super-markup acc-markup)))
140               ;; not 'super or 'columns must be 'sub...
141               (make-line-markup (list (make-sub-markup acc-markup))))))))
142
143
144 ; unused.
145
146 ;; FIXME: possibly to be used for american/jazz style
147 ;; However, only pos == columns is used, which seems to do
148 ;; exactly what accidental->markup does...
149 (define (amy-accidental->text acc) (accidental->textp acc 'columns))
150
151
152 ;; These not used
153 ;;;(define (accidental->text-super acc) (accidental->textp acc 'simple-super))
154 ;;(define (accidental->text-super acc) (accidental->textp acc 'super))
155 ;;(define (accidental->text-sub acc) (accidental->textp acc 'sub))
156
157
158 ;;
159 ;; TODO: invent sensible way to make note name tweaking possible?
160 ;;
161 (define (pitch->markup pitch)
162   (make-line-markup
163    (list
164     (make-simple-markup
165      (vector-ref #("C" "D" "E" "F" "G" "A" "B")  (cadr pitch)))
166     (make-normal-size-super-markup
167      (accidental->markup (caddr pitch))))))
168   
169 ;;; Hooks to override chord names and note names, 
170 ;;; see input/tricks/german-chords.ly
171
172 (define pitch->markup-banter pitch->markup)
173
174 ;; We need also steps, to allow for Cc name override,
175 ;; see input/test/Cc-chords.ly
176 (define (pitch->chord-name-markup-banter pitch steps)
177   (pitch->markup-banter pitch))
178
179 (define pitch->note-name-markup-banter pitch->markup-banter)
180
181 (define (step->markup pitch)
182   (string-append
183    (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))
184    (case (caddr pitch)
185       ((-2) "--")
186       ((-1) "-")
187       ((0) "")
188       ((1) "+")
189       ((2) "++"))))
190   
191 (define (step->markup-banter pitch)
192   (make-simple-markup
193    (if (= (cadr pitch) 6)
194        (case (caddr pitch)
195          ((-2)  "7-")
196          ((-1) "7")
197          ((0)  "maj7")
198          ((1)  "7+")
199          ((2)  "7+"))
200        (step->markup pitch))))
201
202 (define (step->markup-previously-alternate-jazz pitch)
203   (make-line-markup
204    (list
205     (accidental->markup (caddr pitch))
206     (make-simple-markup
207      (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))))))
208
209 (define (step->markup-previously-jazz pitch)
210   (if (= (cadr pitch) 6)
211       (case (caddr pitch)
212         ;; sharp 7 only included for completeness?
213         ((-2) (make-line-markup
214                (list
215                 (accidental->markup  -1)
216                 (make-simple-markup "7"))))
217         ((-1) (make-simple-markup "7"))
218         ((0) (make-simple-markup "maj7"))
219         ;;((0) (make-line-markup
220         ;;      (list (make-simple-markup "maj7"))))
221         ((1) (make-line-markup
222               (list
223                (accidental->markup 1) (make-simple-markup "7"))))
224         ((2) (make-line-markup
225               (list (accidental->markup 1)
226                     (make-simple-markup "7")))))
227       (step->markup-previously-alternate-jazz pitch)))
228
229
230 (define pitch::semitone-vec #(0 2 4 5 7 9 11))
231
232 (define (pitch::semitone pitch)
233   (+ (* (car pitch) 12) 
234      (vector-ref pitch::semitone-vec (modulo (cadr pitch) 7)) 
235      (caddr pitch)))
236
237 (define (pitch::< l r)
238   (< (pitch::semitone l) (pitch::semitone r)))
239   
240 (define (pitch::transpose pitch delta)
241   (let ((simple-octave (+ (car pitch) (car delta)))
242         (simple-notename (+ (cadr pitch) (cadr delta))))
243     (let ((octave (+ simple-octave (quotient simple-notename 7)))
244            (notename (modulo simple-notename 7)))
245       (let ((accidental (- (+ (pitch::semitone pitch) (pitch::semitone delta))
246                            (pitch::semitone `(,octave ,notename 0)))))
247         `(,octave ,notename ,accidental)))))
248     
249 (define (pitch::diff pitch tonic)
250   (let ((simple-octave (- (car pitch) (car tonic)))
251         (simple-notename (- (cadr pitch) (cadr tonic))))
252     (let ((octave (+ simple-octave (quotient simple-notename 7)
253                      (if (< simple-notename 0) -1 0)))
254           (notename (modulo simple-notename 7)))
255       (let ((accidental (- (pitch::semitone pitch)
256                           (pitch::semitone tonic) 
257                           (pitch::semitone `(,octave ,notename 0)))))
258         `(,octave ,notename ,accidental)))))
259
260 (define (pitch::note-pitch pitch)
261   (+ (* (car pitch) 7) (cadr pitch)))
262
263
264 ; what's this? 
265 (define chord::minor-major-vec #(0 -1 -1 0 -1 -1 0))
266
267 ;; FIXME: unLOOP
268 ;; compute the relative-to-tonic pitch that goes with 'step'
269 (define (chord::step-pitch tonic step)
270   ;; urg, we only do this for thirds
271   (if (= (modulo step 2) 0)
272     '(0 0 0)
273     (let loop ((i 1) (pitch tonic))
274       (if (= i step) pitch
275         (loop (+ i 2) 
276               (pitch::transpose 
277                 pitch `(0 2 ,(vector-ref chord::minor-major-vec 
278                 ;; -1 (step=1 -> vector=0) + 7 = 6
279                 (modulo (+ i 6) 7)))))))))
280
281 (define (chord::additions steps)
282 " Return:
283    * any even step (2, 4, 6)
284    * any uneven step that is chromatically altered,
285      (where 7-- == -1, 7- == 0, 7 == +1)
286    * highest step
287
288 ?and jazz needs also:
289
290    * TODO: any uneven step that's lower than an uneven step which is
291      chromatically altered
292   "
293   (let ((evens (filter-list (lambda (x) (!= 0 (modulo (cadr x) 2))) steps))
294         (altered-unevens
295          (filter-list (lambda (x)
296                         (let ((n (cadr x)) (a (caddr x)))
297                           (or (and (= 6 n) (!= -1 a))
298                               (and (!= 6 n)
299                                    (= 0 (modulo n 2))
300                                    (!= 0 a)))))
301                       steps))
302         (highest (let ((h (car (last-pair steps))))
303                    (if (and (not (null? h))
304                             (or (> 4 (cadr h))
305                                 (!= 0 (caddr h))))
306                        (list (list h))
307                        '()))))
308     ;; Hmm, what if we have a step twice, can we ignore that?
309     (uniq-list (sort (apply append evens altered-unevens highest)
310                      pitch::<))))
311         
312      
313 ;; FIXME: unLOOP, see ::additions
314 ;; find the pitches that are missing from `normal' chord
315 (define (chord::subtractions chord-pitches)
316   (let ((tonic (car chord-pitches)))
317     (let loop ((step 1) (pitches chord-pitches) (subtractions '()))
318       (if (pair? pitches)
319         (let* ((pitch (car pitches))
320                (p-step (+ (- (pitch::note-pitch pitch)
321                              (pitch::note-pitch tonic))
322                           1)))
323           ;; pitch is an subtraction if 
324           ;; a step is missing or
325           (if (> p-step step)
326             (loop (+ step 2) pitches
327                 (cons (chord::step-pitch tonic step) subtractions))
328           ;; there are no pitches left, but base thirds are not yet done and
329           (if (and (<= step 5)
330                    (= (length pitches) 1))
331             ;; present pitch is not missing step
332             (if (= p-step step)
333               (loop (+ step 2) pitches subtractions)
334               (loop (+ step 2) pitches 
335                     (cons (chord::step-pitch tonic step) subtractions)))
336             (if (= p-step step)
337               (loop (+ step 2) (cdr pitches) subtractions)
338               (loop step (cdr pitches) subtractions)))))
339         (reverse subtractions)))))
340
341 (define (chord::additions->markup-banter additions subtractions)
342   (if (pair? additions)
343       (make-line-markup
344        (list
345         (let ((step (step->markup-banter (car additions))))
346           (if (or (pair? (cdr additions))
347                   (pair? subtractions))
348               (make-line-markup
349                (list step (make-simple-markup "/")))
350               step))
351         (chord::additions->markup-banter (cdr additions) subtractions)))
352       empty-markup))
353
354 (define (chord::subtractions->markup-banter subtractions)
355   (if (pair? subtractions)
356       (make-line-markup
357        (list
358         (make-simple-markup "no")
359         (let ((step (step->markup-previously-jazz
360                      (car subtractions))))
361           (if (pair? (cdr subtractions))
362               (make-line-markup
363                (list step (make-simple-markup "/")))
364               step))
365         (chord::subtractions->markup-banter (cdr subtractions))))
366       empty-markup))
367
368 (define (chord::bass-and-inversion->markup-banter bass-and-inversion)
369   (if (and (pair? bass-and-inversion)
370            (or (car bass-and-inversion)
371                (cdr bass-and-inversion)))
372       (make-line-markup
373        (list
374         (make-simple-markup "/")
375         (pitch->note-name-markup-banter 
376          (if (car bass-and-inversion)
377              (car bass-and-inversion)
378              (cdr bass-and-inversion)))))
379       empty-markup))
380
381 ;; FIXME: merge this function with inner-name-jazz, -american
382 ;;        iso using chord::bass-and-inversion->markup-banter,
383 ;;        See: chord::exceptions-lookup
384 (define (chord::inner-name-banter tonic exception-part additions subtractions
385                                   bass-and-inversion steps)
386   "
387         
388  Banter style
389  Combine tonic, exception-part of chord name,
390  additions, subtractions and bass or inversion into chord name
391
392 "
393   (let* ((tonic-markup (pitch->chord-name-markup-banter tonic steps))
394          (except-markup (if exception-part exception-part empty-markup))
395          (sep-markup (if (and exception-part
396                               (let ((s (format "~s" except-markup)))
397                                 (and
398                                  (string-match "super" s)
399                                  ;; ugh ugh
400                                  ;; python: `except_markup`[-5:] != '"o"))'
401                                  (not (equal?
402                                        "\"o\"))"
403                                        (substring s
404                                                   (- (string-length s) 5))))))
405                               (or (pair? additions)
406                                   (pair? subtractions)))
407                          (make-super-markup (make-simple-markup "/"))
408                          empty-markup))
409          (adds-markup (chord::additions->markup-banter additions subtractions))
410          (subs-markup (chord::subtractions->markup-banter subtractions))
411          (b+i-markup (chord::bass-and-inversion->markup-banter
412                       bass-and-inversion)))
413     
414     (make-line-markup
415      (list
416       tonic-markup
417       except-markup
418       sep-markup
419       (make-normal-size-super-markup
420        (make-line-markup (list adds-markup subs-markup)))
421       b+i-markup))))
422
423 (define (c++-pitch->scm p)
424   (if (ly:pitch? p)
425       (list (ly:pitch-octave p) (ly:pitch-notename p) (ly:pitch-alteration p))
426       #f))
427
428 (define (chord::name-banter tonic exception-part unmatched-steps
429                             bass-and-inversion steps)
430   (let ((additions (chord::additions unmatched-steps))
431         (subtractions (chord::subtractions unmatched-steps)))
432     
433     (chord::inner-name-banter tonic exception-part additions subtractions
434                               bass-and-inversion steps)))
435
436
437 ;; see above.
438 (define (chord::exceptions-lookup exceptions steps)
439   "
440    return (MATCHED-EXCEPTION . BASE-CHORD-WITH-UNMATCHED-STEPS)
441    BASE-CHORD-WITH-UNMATCHED-STEPS always includes (tonic 3 5)
442
443 "
444   ;; this is unintelligible.
445   ;;
446   (define (chord::exceptions-lookup-helper
447            exception-alist try-steps unmatched-steps exception-part)
448     "
449
450  check exception-alist for biggest matching part of try-steps
451  return (MATCHED-EXCEPTION . UNMATCHED-STEPS)
452
453 "
454     (if (pair? try-steps)
455         ;; FIXME: junk '(0 . 0) from exceptions lists?
456         ;;        if so: how to handle first '((0 . 0) . #f) entry?
457         ;;
458         ;; FIXME: either format exceptions list as real pitches, ie,
459         ;;        including octave '((0 2 -1) ..), or drop octave
460         ;;        from rest of calculations, 
461         (let ((entry (assoc
462                       (map (lambda (x) (pitch->note-name x))
463                            (append '((0 0 0)) try-steps))
464                       exception-alist)))
465           (if entry
466               (chord::exceptions-lookup-helper
467                #f '() unmatched-steps (cdr entry))
468               (let ((r (reverse try-steps)))
469                 (chord::exceptions-lookup-helper
470                  exception-alist
471                  (reverse (cdr r))
472                  (cons (car r) unmatched-steps) #f))))
473         (cons exception-part unmatched-steps)))
474
475   (let* ((result (chord::exceptions-lookup-helper
476                   exceptions
477                   steps '() #f))
478            (exception-part (car result))
479            (unmatched-steps (cdr result))
480            (matched-steps (if (= (length unmatched-steps) 0)
481                               3
482                               (+ 1 (- (length steps)
483                                       (length unmatched-steps)))))
484            (unmatched-with-1-3-5
485             (append (do ((i matched-steps (- i 1))
486                          (base '() (cons `(0 ,(* (- i 1) 2) 0) base)))
487                         ((= i 0) base)
488                       ())
489                     unmatched-steps)))
490     (list exception-part unmatched-with-1-3-5)))
491
492
493
494 ;;; American style
495 ;;;
496
497 ;; See input/test/american-chords.ly
498 ;;
499 ;; Original Version by James Hammons, <jlhamm@pacificnet.net>
500 ;; Complete rewrite by Amelie Zapf, <amy@loueymoss.com>
501
502 ;; DONT use non-ascii characters, even if ``it works'' in Windows
503
504 ;;a white triangle
505 (define mathm-markup-object
506   (make-override-markup '(font-family . math) (make-simple-markup "M")))
507
508 ;a black triangle
509 (define mathn-markup-object
510   (make-override-markup '(font-family . math) (make-simple-markup "N")))
511
512 (define (step->markup-accidental pitch)
513     (case (caddr pitch)
514       ((-2) (accidental->markup -2))
515       ((-1) (accidental->markup -1))
516       ((0) empty-markup)
517       ((1) (accidental->markup 1))
518       ((2) (accidental->markup 2)))
519     (make-simple-markup (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))))
520
521 (define-public chord::exception-alist-american 
522   `(
523     (((0 . 0)) . ,empty-markup)
524     (((0 . 0) (2 . -1)) . ,(make-simple-markup "m"))
525     
526     ;; these should probably be normal-size?  --jcn
527     ;;(((0 . 0) (4 . 0)) . ,(make-super-markup (make-simple-markup "5 ")))
528     ;;(((0 . 0) (1 . 0) (4 . 0)) . ,(make-super-markup (make-simple-markup "2 ")))
529     
530     (((0 . 0) (4 . 0)) . ,(make-normal-size-super-markup (make-simple-markup "5 ")))
531     (((0 . 0) (1 . 0) (4 . 0)) . ,(make-normal-size-super-markup (make-simple-markup "2 ")))
532     
533     ;;choose your symbol for the fully diminished chord
534     (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ,(make-simple-markup "dim"))
535     ;;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ,(make-line-markup (list empty-markup (make-super-markup (make-simple-markup "o")))))
536     ))
537
538 (define (step->markup-american pitch)
539   (case (cadr pitch)
540     ((6) (case (caddr pitch)
541            ((-2) (make-line-markup (list (accidental->markup -1) (make-simple-markup "7"))))
542            ((-1) (make-simple-markup "7"))
543            ((0) (make-simple-markup "maj7"))
544            ((1) (make-line-markup (list (accidental->markup 1) (make-simple-markup "7"))))
545            ((2) (make-line-markup (list (accidental->markup 2) (make-simple-markup "7"))))))
546     ((4) (case (caddr pitch)
547            ((-2) (make-line-markup (list (accidental->markup -2) (make-simple-markup "5"))))
548            ;;choose your symbol for the diminished fifth
549            ((-1) (make-simple-markup "-5"))
550            ;;((-1) (make-line-markup (list (accidental->markup -1) (make-simple-markup "5")))))
551            ((0) empty-markup)
552            ;;choose your symbol for the augmented fifth
553            ;;((1) (make-simple-markup "aug"))
554            ;;((1) (make-line-markup (list (accidental->markup 1) (make-simple-markup "5")))))
555            ((1) (make-simple-markup "+5"))
556            ((2) (make-line-markup (list (accidental->markup 2) (make-simple-markup "5"))))))
557     (else (if (and (= (car pitch) 0)
558                    (= (cadr pitch) 3)
559                    (= (caddr pitch) 0))
560               (make-simple-markup "sus4")
561               (step->markup-accidental pitch)))))
562   
563 (define (chord::additions->markup-american additions subtractions)
564   (if (pair? additions)
565       ;; I don't like all this reasoning here, when we're actually typesetting.
566       (if(and(pair? (cdr additions)) ;a further addition left over
567              (or(and(= 0 (caddr(car additions))) ;this addition natural
568                     (not(= 6 (cadr(car additions)))))
569                 (and(= -1 (caddr(car additions)))
570                     (= 6 (cadr(car additions)))))
571              (or(and(= 0 (caddr(cadr additions))) ;the following addition natural
572                     (not(= 6 (cadr(cadr additions)))))
573                 (and(= -1 (caddr(cadr additions)))
574                     (= 6 (cadr(cadr additions)))))
575              (or(and(= (car(car additions)) (car(cadr additions))) ;both a third apart
576                     (= 2 (- (cadr(cadr additions)) (cadr(car additions)))))
577                 (and(= 1 (- (car(cadr additions)) (car(car additions))))
578                     (= 5 (- (cadr(car additions)) (cadr(cadr additions))))))
579              (or(null? subtractions) ;this or clause protects the "adds"
580                 (and (pair? subtractions)
581                      (or (< (car(cadr additions)) (car(car subtractions)))
582                          (and(= (car(cadr additions)) (car(car subtractions)))
583                              (< (cadr(cadr additions)) (cadr(car subtractions))))))))
584          (chord::additions->markup-american (cdr additions) subtractions)
585          (make-line-markup
586           (list
587            (let ((step (step->markup-american (car additions))))
588              (if (or (pair? (cdr additions))
589                      (pair? subtractions))
590                  (if (and (pair? (cdr additions))
591                           (or(< 3 (- (cadr(cadr additions)) (cadr(car additions))))
592                              (and(< 0 (- (car(cadr additions)) (car(car additions))))
593                                  (> 4 (- (cadr(car additions)) (cadr(cadr additions)))))))
594                      (make-line-markup (list step (make-simple-markup " add")))
595                      ;; tweak your favorite separator here
596                      ;; (make-line-markup (list step (make-simple-markup "/")))
597                      (make-line-markup (list step (make-simple-markup " "))))
598                  step))
599            (chord::additions->markup-american (cdr additions) subtractions))))
600       empty-markup))
601
602 (define (chord::inner-name-american tonic exception-part additions subtractions
603                                   bass-and-inversion steps)
604   (let* ((tonic-markup (pitch->chord-name-markup-banter tonic steps))
605          (except-markup (if exception-part exception-part empty-markup))
606          (sep-markup (if (and (string-match "super" (format "~s" except-markup))
607                               (or (pair? additions)
608                                   (pair? subtractions)))
609                          (make-super-markup (make-simple-markup "/"))
610                          empty-markup))
611          ;;this list contains all the additions that go "in line"
612          (prefixes
613           (filter-list
614            (lambda (x)
615              (let ((o (car x)) (n (cadr x)) (a (caddr x)))
616                (and (not (and (= 0 o) (= 2 n))) ;gets rid of unwanted thirds
617                     ;;change this if you want it differently
618                     (not (and (= 0 o) (= 3 n) (= 0 a))) ;sus4
619                     (not (and (= 0 o) (= 4 n) (!= 0 a)))))) ;alt5
620            additions))
621          ;;this list contains all the additions that are patched onto the end
622          ;;of the chord symbol, usually sus4 and altered 5ths.
623          (suffixes
624           ;;take out the reverse if it bothers you in a pathological chord
625           (reverse
626            (filter-list
627             (lambda (x)
628               (let ((o (car x)) (n (cadr x)) (a (caddr x)))
629                 (and(not (and (= 0 o) (= 2 n))) ;gets rid of unwanted thirds
630                     ;;change this correspondingly
631                     (or(and (= 0 o) (= 3 n) (= 0 a)) ;sus4
632                        (and (= 0 o) (= 4 n) (!= 0 a)))))) ;alt5
633             additions)))
634          (relevant-subs (filter-list
635                          (lambda (x) ;catches subtractions higher than 5th
636                            (let((o (car x)) (n (cadr x)))
637                              (or (> o 0)
638                                  (> n 4))))
639                          subtractions))
640          (pref-markup (chord::additions->markup-american prefixes relevant-subs))
641          (suff-markup (chord::additions->markup-american suffixes relevant-subs))
642          (b+i-markup (chord::bass-and-inversion->markup-banter bass-and-inversion)))
643     (make-line-markup
644      (list
645       tonic-markup except-markup sep-markup
646       (make-normal-size-super-markup
647        (make-line-markup (list pref-markup suff-markup)))
648       b+i-markup))))
649
650 (define (chord::additions-american steps)
651   (let ((evens (filter-list (lambda (x) (!= 0 (modulo (cadr x) 2))) steps))
652         ;we let all the unevens pass for now, we'll fix that later.
653         (unevens
654          (filter-list (lambda (x)
655                         (let ((n (cadr x)) (a (caddr x)))
656                           (or (and (= 6 n) (!= -1 a))
657                               (and (< 3 n)
658                                    (= 0 (modulo n 2))))))
659                       steps))
660         (highest (let ((h (car (last-pair steps))))
661                    (if (and (not (null? h))
662                             (or (> 4 (cadr h))
663                                 (!= 0 (caddr h))))
664                        (list (list h))
665                        '()))))
666     (uniq-list (sort (apply append evens unevens highest)
667                      pitch::<))))
668
669   ;; American style chordnames use no "no",
670   ;; but otherwise very similar to banter for now
671   (define-public (chord::name-american tonic exception-part unmatched-steps
672                               bass-and-inversion steps)
673   (let ((additions (chord::additions-american unmatched-steps))
674         (subtractions (chord::subtractions unmatched-steps)))
675     (chord::inner-name-american tonic exception-part additions subtractions
676                               bass-and-inversion steps)))
677
678   ;;; Jazz style
679   ;;;
680 ;; Jazz chords, by Atte Andr'e Jensen <atte@post.com>
681 ;; Complete rewrite by Amelie Zapf (amy@loueymoss.com)
682
683 ;; FIXME: identical to chord::exception-alist-american, apart from commented
684 ;;        dim chord.  should merge.
685 (define-public chord::exception-alist-jazz 
686   `(
687     (((0 . 0)) . ,empty-markup)
688     (((0 . 0) (2 . -1)) . ,(make-simple-markup "m"))
689
690     ;; these should probably be normal-size?  --jcn
691     ;;(((0 . 0) (4 . 0)) . ,(make-super-markup (make-simple-markup "5 ")))
692     ;;(((0 . 0) (1 . 0) (4 . 0)) . ,(make-super-markup (make-simple-markup "2 ")))
693     
694     (((0 . 0) (4 . 0)) . ,(make-normal-size-super-markup (make-simple-markup "5 ")))
695     (((0 . 0) (1 . 0) (4 . 0)) . ,(make-normal-size-super-markup (make-simple-markup "2 ")))
696     
697     ;;choose your symbol for the fully diminished chord
698     ;;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ,(make-simple-markup "dim"))
699     (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ,(make-line-markup (list (make-simple-markup "") (make-super-markup (make-simple-markup "o")))))
700     ))
701
702 ;; FIXME: rather similar to step->markup-american.  should merge.
703 (define (step->markup-jazz pitch)
704   (case (cadr pitch)
705     ((6) (case (caddr pitch)
706            ((-2) (make-line-markup (list (accidental->markup -1) (make-simple-markup "7"))))
707            ((-1) (make-simple-markup "7"))
708            ;;Pick your favorite maj7
709            ((0) mathm-markup-object)  ;;a white triangle
710            ;;((0) mathn-markup-object) ;;a black triangle
711            ;;((0) (make-simple-markup "maj7")) ;;good old maj7
712            ((1) (make-line-markup (list (accidental->markup 1) (make-simple-markup "7"))))
713            ((2) (make-line-markup (list (accidental->markup 2) (make-simple-markup "7"))))))
714     ((4) (case (caddr pitch)
715            ((-2) (make-line-markup (list (accidental->markup -2) (make-simple-markup "5"))))
716            ;;choose your symbol for the diminished fifth
717            ;;((-1) (make-simple-markup "-5"))
718            ((-1) (make-line-markup (list (accidental->markup -1) (make-simple-markup "5"))))
719            ((0) empty-markup)
720            ;;choose your symbol for the augmented fifth
721            ;;((1) (make-simple-markup "aug"))
722            ((1) (make-line-markup (list (accidental->markup 1) (make-simple-markup "5"))))
723            ;;((1) (make-simple-markup "+5"))
724            ((2) (make-line-markup (list (accidental->markup 2) (make-simple-markup "5"))))))
725     (else (if (and (= (car pitch) 0)
726                    (= (cadr pitch) 3)
727                    (= (caddr pitch) 0))
728               (make-simple-markup "sus4")
729               (step->markup-accidental pitch)))))
730
731 ;; FIXME: identical to chord::additions->markup-american,
732 ;; except for -jazz / -american suffixes on calls
733 (define (chord::additions->markup-jazz additions subtractions)
734   (if (pair? additions)
735       ;; I don't like all this reasoning here, when we're actually typesetting.
736       (if(and(pair? (cdr additions)) ;a further addition left over
737              (or(and(= 0 (caddr(car additions))) ;this addition natural
738                     (not(= 6 (cadr(car additions)))))
739                 (and(= -1 (caddr(car additions)))
740                     (= 6 (cadr(car additions)))))
741              (or(and(= 0 (caddr(cadr additions))) ;the following addition natural
742                     (not(= 6 (cadr(cadr additions)))))
743                 (and(= -1 (caddr(cadr additions)))
744                     (= 6 (cadr(cadr additions)))))
745              (or(and(= (car(car additions)) (car(cadr additions))) ;both a third apart
746                     (= 2 (- (cadr(cadr additions)) (cadr(car additions)))))
747                 (and(= 1 (- (car(cadr additions)) (car(car additions))))
748                     (= 5 (- (cadr(car additions)) (cadr(cadr additions))))))
749              (or(null? subtractions) ;this or clause protects the "adds"
750                 (and (pair? subtractions)
751                      (or (< (car(cadr additions)) (car(car subtractions)))
752                          (and(= (car(cadr additions)) (car(car subtractions)))
753                              (< (cadr(cadr additions)) (cadr(car subtractions))))))))
754          (chord::additions->markup-jazz (cdr additions) subtractions)
755          (make-line-markup
756           (list
757            (let ((step (step->markup-jazz (car additions))))
758              (if (or (pair? (cdr additions))
759                      (pair? subtractions))
760                  (if (and (pair? (cdr additions))
761                           (or(< 3 (- (cadr(cadr additions)) (cadr(car additions))))
762                              (and(< 0 (- (car(cadr additions)) (car(car additions))))
763                                  (> 4 (- (cadr(car additions)) (cadr(cadr additions)))))))
764                      (make-line-markup (list step (make-simple-markup " add")))
765                      ;; tweak your favorite separator here
766                      ;; (make-line-markup (list step (make-simple-markup "/")))
767                      (make-line-markup (list step (make-simple-markup " "))))
768                  step))
769            (chord::additions->markup-jazz (cdr additions) subtractions))))
770       empty-markup))
771
772 ;; FIXME: identical to chord::additions->markup-american.
773 ;; except for -jazz / -american suffixes on calls
774 (define (chord::inner-name-jazz tonic exception-part additions subtractions
775                                 bass-and-inversion steps)
776   (let* ((tonic-markup (pitch->chord-name-markup-banter tonic steps))
777          (except-markup (if exception-part exception-part empty-markup))
778          (sep-markup (if (and (string-match "super" (format "~s" except-markup))
779                               (or (pair? additions)
780                                   (pair? subtractions)))
781                          (make-super-markup (make-simple-markup "/"))
782                          empty-markup))
783          ;;this list contains all the additions that go "in line"
784          (prefixes
785           (filter-list
786            (lambda (x)
787              (let ((o (car x)) (n (cadr x)) (a (caddr x)))
788                (and (not (and (= 0 o) (= 2 n))) ;gets rid of unwanted thirds
789                     ;;change this if you want it differently
790                     (not (and (= 0 o) (= 3 n) (= 0 a))) ;sus4
791                     (not (and (= 0 o) (= 4 n) (!= 0 a)))))) ;alt5
792            additions))
793          ;;this list contains all the additions that are patched onto the end
794          ;;of the chord symbol, usually sus4 and altered 5ths.
795          (suffixes
796           ;;take out the reverse if it bothers you in a pathological chord
797           (reverse
798            (filter-list
799             (lambda (x)
800               (let ((o (car x)) (n (cadr x)) (a (caddr x)))
801                 (and(not (and (= 0 o) (= 2 n))) ;gets rid of unwanted thirds
802                     ;;change this correspondingly
803                     (or(and (= 0 o) (= 3 n) (= 0 a)) ;sus4
804                        (and (= 0 o) (= 4 n) (!= 0 a)))))) ;alt5
805             additions)))
806          (relevant-subs (filter-list
807                          (lambda (x) ;catches subtractions higher than 5th
808                            (let((o (car x)) (n (cadr x)))
809                              (or (> o 0)
810                                  (> n 4))))
811                          subtractions))
812          (pref-markup (chord::additions->markup-jazz prefixes relevant-subs))
813          (suff-markup (chord::additions->markup-jazz suffixes relevant-subs))
814          (b+i-markup (chord::bass-and-inversion->markup-banter bass-and-inversion)))
815     (make-line-markup
816      (list
817       tonic-markup except-markup sep-markup
818       (make-normal-size-super-markup
819        (make-line-markup (list pref-markup suff-markup)))
820       b+i-markup))))
821
822 (define (chord::name-jazz tonic exception-part unmatched-steps
823                           bass-and-inversion steps)
824   (let ((additions (chord::additions-american unmatched-steps))
825         (subtractions (chord::subtractions unmatched-steps)))
826     (chord::inner-name-jazz tonic exception-part additions subtractions
827                             bass-and-inversion steps)))
828
829
830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
831
832
833 (define-public (new-chord->markup func ly-pitches bass inversion exceptions)
834   "Entry point for New_chord_name_engraver. See chord-name.scm for the
835 signature of FUNC.  LY-PITCHES, BASS and INVERSION are lily
836 pitches. EXCEPTIONS is an alist (see scm file).
837  "
838   
839   (let* ((pitches (map c++-pitch->scm ly-pitches))
840          (bass-and-inversion 
841           (cons (c++-pitch->scm bass)
842                 (c++-pitch->scm inversion)))
843          (diff (pitch::diff '(0 0 0) (car pitches)))
844          (steps (if (cdr pitches) (map (lambda (x)
845                                          (pitch::transpose x diff))
846                                        (cdr pitches))
847                     '()))
848          (lookup (dbg (chord::exceptions-lookup exceptions steps)))
849          (exception-part (dbg (car lookup)))
850          (unmatched-steps (cadr lookup))
851          (tonic (car pitches))   
852          )
853
854       (func tonic exception-part unmatched-steps bass-and-inversion steps)
855       ))
856     
857 (define-public (chord->markup-jazz . args)
858   (apply new-chord->markup (cons chord::name-jazz args))
859   )
860
861 (define-public (chord->markup-american . args)
862   (apply new-chord->markup (cons chord::name-american args))
863   )
864
865 (define-public (chord->markup-banter . args)
866   (apply new-chord->markup (cons chord::name-banter args))
867   )
868
869 (define-public (new-chord-name-brew-molecule grob)
870   (let*
871       (
872        (ws (ly:get-grob-property grob 'word-space))
873        (markup (ly:get-grob-property grob 'text))
874        (molecule (interpret-markup grob
875                                    (cons '((word-space . 0.0))
876                                          (Font_interface::get_property_alist_chain grob))
877                                    markup))
878        )
879
880     ;;
881     ;; chord names aren't in staffs, so WS is in global staff space.
882     (if (number? ws)
883         (ly:combine-molecule-at-edge
884          molecule
885          X RIGHT (ly:make-molecule "" (cons 0 ws) '(-1 . 1) )
886          0.0)
887         molecule)
888     ))
889
890 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
891
892 (define-public (set-chord-name-style sym)
893   "Return music expressions that set the chord naming style. For
894 inline use in .ly file"
895   
896   (define (chord-name-style-setter function exceptions)
897     (context-spec-music
898      (make-sequential-music 
899       (list (make-property-set 'chordNameFunction function)
900             (make-property-set 'chordNameExceptions exceptions)))
901      "ChordNames"
902      )
903     )
904
905   (ly:export
906    (case sym
907      ((jazz)
908       (chord-name-style-setter chord->markup-jazz
909                                chord::exception-alist-jazz))
910      ((banter)
911       (chord-name-style-setter chord->markup-banter
912                                chord::exception-alist-banter))
913      ((american)
914       (chord-name-style-setter chord->markup-american
915                                chord::exception-alist-american))
916      )))
917
918