]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-name.scm
ly- -> ly:
[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 ;; pitch = (octave notename accidental)
18 ;;
19 ;; note = (notename . accidental)
20 ;;
21 ;; text = scm markup text -- see font.scm and input/test/markup.ly
22
23
24 ;; TODO
25
26 ;; Ugh : naming chord::... ; this is scheme not C++
27 ;;
28 ;; * easier tweakability:
29 ;;    - split chord::names-alists up into logical bits,
30 ;;      such as chord::exceptions-delta, exceptions-oslash
31 ;;    - iso just the 'style parameter, use a list, eg:
32 ;;      \property ChordNames.ChordName \set
33 ;;        #'style = #'(jazz delta oslash german-tonic german-Bb)
34 ;;
35 ;; * fix FIXMEs
36 ;;
37 ;; * clean split/merge of bass/banter/american stuff
38 ;;
39 ;; * doc strings
40
41 (define-public chord::names-alist-banter '())
42 (set! chord::names-alist-banter
43       (append 
44         '(
45         ; C iso C.no3.no5
46         (((0 . 0)) . #f)
47         ; C iso C.no5
48         (((0 . 0) (2 . 0)) . #f)
49         ; Cm iso Cm.no5
50         (((0 . 0) (2 . -1)) . ("m"))
51         ; C2 iso C2.no3
52         (((0 . 0) (1 . 0) (4 . 0)) . ("" (super "2") " "))
53         ; C4 iso C4.no3
54         (((0 . 0) (3 . 0) (4 . 0)) . ("" (super "4") " " ))
55         ;; Cdim iso Cm5-
56         (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
57         ; URG: Simply C:m5-/maj7 iso Cdim maj7
58         (((0 . 0) (2 . -1) (4 . -1) (6 . 0)) . ("m" (super "5-/maj7" " ")))
59         ; URG: Simply C:m5-/7 iso Cdim7
60         (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . ("m" (super "5-/7" " ")))
61         ; Co iso C:m5-/7-
62         (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ("" (super "o") " "))
63         ; Cdim9
64         (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1)) . ("dim" (super "9") " "))
65         (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1) (3 . -1)) . ("dim" (super "11") " "))
66         )
67       chord::names-alist-banter))
68
69 ;;;;;;;;;;
70 (define simple-super
71 ;; duh, no docstrings for 
72 ;;  "No real superscript, just raised and small"
73   '((raise . 1) (font-relative-size . -2)))
74
75 (define (accidental->textp acc pos)
76   (if (= acc 0)
77       '()
78       (list '(music (font-relative-size . -2))
79                    (list pos (string-append "accidentals-" (number->string acc))))))
80
81 (define (accidental->text acc) (accidental->textp acc 'columns))
82 (define (accidental->text-super acc) (accidental->textp acc 'simple-super))
83 (define (accidental->text-sub acc) (accidental->textp acc 'sub))
84
85 (define (pitch->note-name pitch)
86   (cons (cadr pitch) (caddr pitch)))
87
88 (define (pitch->text pitch)
89   (text-append
90    (list
91     '(font-relative-size . 2)
92     (make-string 1 (integer->char (+ (modulo (+ (cadr pitch) 2) 7) 65))))
93    (accidental->text-super (caddr pitch))))
94
95
96 ;;; Hooks to override chord names and note names, 
97 ;;; see input/tricks/german-chords.ly
98
99 (define (pitch->text-banter pitch)
100   (pitch->text pitch))
101
102 ;; We need also steps, to allow for Cc name override,
103 ;; see input/test/Cc-chords.ly
104 (define (pitch->chord-name-text-banter pitch steps)
105   (pitch->text-banter pitch))
106
107 (define (pitch->note-name-text-banter pitch)
108   (pitch->text-banter pitch))
109
110 (define (step->text pitch)
111   (list (string-append
112     (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))
113     (case (caddr pitch)
114       ((-2) "--")
115       ((-1) "-")
116       ((0) "")
117       ((1) "+")
118       ((2) "++")))))
119   
120 (define (step->text-banter pitch)
121   (if (= (cadr pitch) 6)
122       (case (caddr pitch)
123         ((-2) '("7-"))
124         ((-1) '("7"))
125         ((0) '("maj7"))
126         ((1) '("7+"))
127         ((2) '("7+")))
128       (step->text pitch)))
129
130 (define pitch::semitone-vec (list->vector '(0 2 4 5 7 9 11)))
131
132 (define (pitch::semitone pitch)
133   (+ (* (car pitch) 12) 
134      (vector-ref pitch::semitone-vec (modulo (cadr pitch) 7)) 
135      (caddr pitch)))
136
137 (define (pitch::< l r)
138   (< (pitch::semitone l) (pitch::semitone r)))
139   
140 (define (pitch::transpose pitch delta)
141   (let ((simple-octave (+ (car pitch) (car delta)))
142         (simple-notename (+ (cadr pitch) (cadr delta))))
143     (let ((octave (+ simple-octave (quotient simple-notename 7)))
144            (notename (modulo simple-notename 7)))
145       (let ((accidental (- (+ (pitch::semitone pitch) (pitch::semitone delta))
146                            (pitch::semitone `(,octave ,notename 0)))))
147         `(,octave ,notename ,accidental)))))
148     
149 (define (pitch::diff pitch tonic)
150   (let ((simple-octave (- (car pitch) (car tonic)))
151         (simple-notename (- (cadr pitch) (cadr tonic))))
152     (let ((octave (+ simple-octave (quotient simple-notename 7)
153                      (if (< simple-notename 0) -1 0)))
154           (notename (modulo simple-notename 7)))
155       (let ((accidental (- (pitch::semitone pitch)
156                           (pitch::semitone tonic) 
157                           (pitch::semitone `(,octave ,notename 0)))))
158         `(,octave ,notename ,accidental)))))
159
160 (define (pitch::note-pitch pitch)
161   (+ (* (car pitch) 7) (cadr pitch)))
162
163 (define (chord::text? text)
164   (not (or (not text) (null? text) (unspecified? text))))
165
166 ;; FIXME: remove need for me, use text-append throughout
167 (define (chord::text-cleanup dirty)
168   "
169    Recursively remove '() #f, and #<unspecified> from markup text tree.
170    This allows us to leave else parts of (if # #) off.
171    Otherwise, you'd have to do (if # # '()), and you'd have to
172    filter-out the '() anyway.
173   "
174   (if (pair? dirty)
175       (let ((r (car dirty)))
176         (if (chord::text? r)
177             (cons (if (pair? r) (chord::text-cleanup r) r)
178                   (chord::text-cleanup (cdr dirty)))
179             (chord::text-cleanup (cdr dirty))))
180       (if (chord::text? dirty)
181           dirty
182           '())))
183
184 (define (text-append l . r)
185   (if (not (chord::text? r))
186       l
187       (if (not (chord::text? l))
188           r
189           (if (null? (cdr r))
190               (list 'columns l (car r))
191               (text-append (list 'columns l (car r)) (cdr r))))))
192            
193 (define (chord::step tonic pitch)
194  (- (pitch::note-pitch pitch) (pitch::note-pitch tonic)))
195
196 ;; text: list of word
197 ;; word: string + optional list of property
198 ;; property: align, kern, font (?), size
199
200 (define chord::minor-major-vec (list->vector '(0 -1 -1 0 -1 -1 0)))
201
202 ;; FIXME: unLOOP
203 ;; compute the relative-to-tonic pitch that goes with 'step'
204 (define (chord::step-pitch tonic step)
205   ;; urg, we only do this for thirds
206   (if (= (modulo step 2) 0)
207     '(0 0 0)
208     (let loop ((i 1) (pitch tonic))
209       (if (= i step) pitch
210         (loop (+ i 2) 
211               (pitch::transpose 
212                 pitch `(0 2 ,(vector-ref chord::minor-major-vec 
213                 ;; -1 (step=1 -> vector=0) + 7 = 6
214                 (modulo (+ i 6) 7)))))))))
215
216 (define (chord::additions steps)
217 " Return:
218    * any even step (2, 4, 6)
219    * any uneven step that is chromatically altered,
220      (where 7-- == -1, 7- == 0, 7 == +1)
221    * highest step
222
223 ?and jazz needs also:
224
225    * TODO: any uneven step that's lower than an uneven step which is
226      chromatically altered
227   "
228   (let ((evens (filter-list (lambda (x) (!= 0 (modulo (cadr x) 2))) steps))
229         (altered-unevens
230          (filter-list (lambda (x)
231                         (let ((n (cadr x)) (a (caddr x)))
232                           (or (and (= 6 n) (!= -1 a))
233                               (and (!= 6 n)
234                                    (= 0 (modulo n 2))
235                                    (!= 0 a)))))
236                       steps))
237         (highest (let ((h (car (last-pair steps))))
238                    (if (and (not (null? h))
239                             (or (> 4 (cadr h))
240                                 (!= 0 (caddr h))))
241                        (list (list h))
242                        '()))))
243     ;; Hmm, what if we have a step twice, can we ignore that?
244     (uniq-list (sort (apply append evens altered-unevens highest)
245                      pitch::<))))
246         
247      
248 ;; FIXME: unLOOP, see ::additions
249 ;; find the pitches that are missing from `normal' chord
250 (define (chord::subtractions chord-pitches)
251   (let ((tonic (car chord-pitches)))
252     (let loop ((step 1) (pitches chord-pitches) (subtractions '()))
253       (if (pair? pitches)
254         (let* ((pitch (car pitches))
255                (p-step (+ (- (pitch::note-pitch pitch)
256                              (pitch::note-pitch tonic))
257                           1)))
258           ;; pitch is an subtraction if 
259           ;; a step is missing or
260           (if (> p-step step)
261             (loop (+ step 2) pitches
262                 (cons (chord::step-pitch tonic step) subtractions))
263           ;; there are no pitches left, but base thirds are not yet done and
264           (if (and (<= step 5)
265                    (= (length pitches) 1))
266             ;; present pitch is not missing step
267             (if (= p-step step)
268               (loop (+ step 2) pitches subtractions)
269               (loop (+ step 2) pitches 
270                     (cons (chord::step-pitch tonic step) subtractions)))
271             (if (= p-step step)
272               (loop (+ step 2) (cdr pitches) subtractions)
273               (loop step (cdr pitches) subtractions)))))
274         (reverse subtractions)))))
275
276 (define (chord::additions->text-banter additions subtractions)
277   (if (pair? additions)
278       (text-append
279        (let ((step (step->text-banter (car additions))))
280          (if (or (pair? (cdr additions))
281                  (pair? subtractions))
282              (text-append step "/")
283              step))
284       (chord::additions->text-banter (cdr additions) subtractions))
285   '()))
286
287 (define (chord::subtractions->text-banter subtractions)  
288   (if (pair? subtractions)
289       (text-append
290        '("no")
291        (let ((step (step->text-jazz (car subtractions))))
292          (if (pair? (cdr subtractions))
293                         (text-append step "/")
294                         step))
295        (chord::subtractions->text-banter (cdr subtractions)))
296       '()))
297
298 (define (chord::bass-and-inversion->text-banter bass-and-inversion)
299   (if (and (pair? bass-and-inversion)
300            (or (car bass-and-inversion)
301                (cdr bass-and-inversion)))
302       (list "/" (if (car bass-and-inversion)
303                     (pitch->note-name-text-banter
304                      (car bass-and-inversion))
305                     (pitch->note-name-text-banter
306                      (cdr bass-and-inversion))))))
307
308 ;; FIXME: merge this function with inner-name-jazz, -american
309 ;;        iso using chord::bass-and-inversion->text-banter,
310 ;;        call (chord::restyle 'chord::bass-and-inversion->text- style)
311 ;;        See: chord::exceptions-lookup
312 ;;        
313 ;; Banter style
314 ;; Combine tonic, exception-part of chord name,
315 ;; additions, subtractions and bass or inversion into chord name
316 (define (chord::inner-name-banter tonic exception-part additions subtractions
317                                   bass-and-inversion steps)
318   (let* ((tonic-text (pitch->chord-name-text-banter tonic steps))
319          (except-text exception-part)
320          (sep-text (if (and (string-match "super" (format "~s" except-text))
321                             (or (pair? additions)
322                                 (pair? subtractions)))
323                        (list simple-super "/")))
324          (adds-text (chord::additions->text-banter additions subtractions))
325          (subs-text (chord::subtractions->text-banter subtractions))
326          (b+i-text (chord::bass-and-inversion->text-banter bass-and-inversion)))
327     (text-append
328      tonic-text except-text sep-text
329      ;;(list (list simple-super) adds-text subs-text)
330      (list (list '((raise . 1) (font-relative-size . -1))) adds-text subs-text)
331      b+i-text)))
332
333 (define (c++-pitch->scm p)
334   (if (ly:pitch? p)
335       (list (ly:pitch-octave p) (ly:pitch-notename p) (ly:pitch-alteration p))
336       #f))
337
338 (define-public (chord::name-banter tonic exception-part unmatched-steps
339                             bass-and-inversion steps)
340    (let ((additions (chord::additions unmatched-steps))
341          (subtractions (chord::subtractions unmatched-steps)))
342      (chord::inner-name-banter tonic exception-part additions subtractions
343                                bass-and-inversion steps)))
344
345
346 (define (chord::restyle name style)
347   (primitive-eval (string->symbol
348             (string-append (symbol->string name)
349                            (symbol->string style)))))
350
351 ;; check exceptions-alist for biggest matching part of try-steps
352 ;; return (MATCHED-EXCEPTION . UNMATCHED-STEPS)
353 (define (chord::exceptions-lookup-helper
354          exceptions-alist try-steps unmatched-steps exception-part)
355   (if (pair? try-steps)
356       ;; FIXME: junk '(0 . 0) from exceptions lists?
357       ;;        if so: how to handle first '((0 . 0) . #f) entry?
358       ;;
359       ;; FIXME: either format exceptions list as real pitches, ie,
360       ;;        including octave '((0 2 -1) ..), or drop octave
361       ;;        from rest of calculations, 
362       (let ((entry (assoc
363                     (map (lambda (x) (pitch->note-name x))
364                          (append '((0 0 0)) try-steps))
365                     exceptions-alist)))
366         (if entry
367             (chord::exceptions-lookup-helper
368              #f '() unmatched-steps (cdr entry))
369             (let ((r (reverse try-steps)))
370               (chord::exceptions-lookup-helper
371                exceptions-alist
372                (reverse (cdr r))
373                (cons (car r) unmatched-steps) #f))))
374       (cons exception-part unmatched-steps)))
375
376 ;; return (MATCHED-EXCEPTION . BASE-CHORD-WITH-UNMATCHED-STEPS)
377 ;; BASE-CHORD-WITH-UNMATCHED-STEPS always includes (tonic 3 5)
378 (define (chord::exceptions-lookup style steps)
379   (let* ((result (chord::exceptions-lookup-helper
380                   (chord::restyle 'chord::names-alist- style)
381                   steps '() #f))
382            (exception-part (car result))
383            (unmatched-steps (cdr result))
384            (matched-steps (if (= (length unmatched-steps) 0)
385                               3
386                               (+ 1 (- (length steps)
387                                       (length unmatched-steps)))))
388            (unmatched-with-1-3-5
389             (append (do ((i matched-steps (- i 1))
390                          (base '() (cons `(0 ,(* (- i 1) 2) 0) base)))
391                         ((= i 0) base)
392                       ())
393                     unmatched-steps)))
394     (list exception-part unmatched-with-1-3-5)))
395
396
397 (define (chord::name->text style tonic steps bass-and-inversion)
398   (let* ((lookup (chord::exceptions-lookup style steps))
399          (exception-part (car lookup))
400          (unmatched-steps (cadr lookup)))
401     (chord::text-cleanup
402      ((chord::restyle 'chord::name- style)
403       tonic exception-part unmatched-steps bass-and-inversion steps))))
404
405 ;; C++ entry point
406 ;; 
407 ;; Check for each subset of chord, full chord first, if there's a
408 ;; user-override.  Split the chord into user-overridden and to-be-done
409 ;; parts, complete the missing user-override matched part with normal
410 ;; chord to be name-calculated.
411 ;;
412 ;; CHORD: (pitches (bass . inversion))
413 (define (default-chord-name-function style chord)
414   (let* ((pitches (map c++-pitch->scm (car chord)))
415          (modifiers (cdr chord))
416          (bass-and-inversion (if (pair? modifiers)
417                                  (cons (c++-pitch->scm (car modifiers))
418                                        (c++-pitch->scm (cdr modifiers)))
419                                  '(() . ())))
420          (diff (pitch::diff '(0 0 0) (car pitches)))
421          (steps (if (cdr pitches) (map (lambda (x)
422                                          (pitch::transpose x diff))
423                                        (cdr pitches))
424                     '())))
425     (chord::name->text style (car pitches) steps bass-and-inversion)))
426
427 ;;;
428 ;;; American style
429 ;;;
430
431
432 ;; NOTE: Duplicates of chord names defined elsewhere occur in this list
433 ;; in order to prevent spurious superscripting of various chord names,
434 ;; such as maj7, maj9, etc.
435 ;;
436 ;; See input/test/american-chords.ly
437 ;;
438 ;; James Hammons, <jlhamm@pacificnet.net>
439 ;;
440
441 ;; DONT use non-ascii characters, even if ``it works'' in Windows
442
443 (define-public chord::names-alist-american '())
444
445 (set! chord::names-alist-american
446       (append 
447        '(
448          (((0 . 0)) . #f)
449          (((0 . 0) (2 . 0)) . #f)
450          ;; Root-fifth chord
451          (((0 . 0) (4 . 0)) . ("5"))
452          ;; Common triads
453          (((0 . 0) (2 . -1)) . ("m"))
454          (((0 . 0) (3 . 0) (4 . 0)) . ("sus"))
455          (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
456 ;Alternate:      (((0 . 0) (2 . -1) (4 . -1)) . ("" (super "o")))
457          (((0 . 0) (2 . 0) (4 . 1)) . ("aug"))
458 ;Alternate:      (((0 . 0) (2 . 0) (4 . 1)) . ("+"))
459          (((0 . 0) (1 . 0) (4 . 0)) . ("2"))
460          ;; Common seventh chords
461          (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ("" (super "o") " " "7"))
462          (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) . ("maj7"))
463          ;; urg! should use (0 . 0 2 . -1) -> "m", and add "7" to that!!
464          (((0 . 0) (2 . -1) (4 . 0) (6 . -1)) . ("m7"))
465          (((0 . 0) (2 . 0) (4 . 0) (6 . -1)) . ("7"))
466          (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . ("m(maj7)"))
467          ;jazz: the delta, see jazz-chords.ly
468          ;;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) .  (super ((font-family . math) "N"))
469          ;; slashed o
470          (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (columns (super (overstrike "o") "/") " " "7"))
471
472          (((0 . 0) (2 . 0) (4 . 1) (6 . -1)) . ("aug7"))
473          (((0 . 0) (2 . 0) (4 . -1) (6 . 0)) . (columns "maj7" ((font-relative-size . -2) ((raise . 0.2) (music (named "accidentals--1")))) "5"))
474          (((0 . 0) (2 . 0) (4 . -1) (6 . -1)) . (columns "7" ((font-relative-size . -2) ((raise . 0.2) (music (named "accidentals--1")))) "5"))
475          (((0 . 0) (3 . 0) (4 . 0) (6 . -1)) . ("7sus4"))
476          ;; Common ninth chords
477          (((0 . 0) (2 . 0) (4 . 0) (5 . 0) (1 . 0)) . ("6/9")) ;; we don't want the '/no7'
478          (((0 . 0) (2 . 0) (4 . 0) (5 . 0)) . ("6"))
479          (((0 . 0) (2 . -1) (4 . 0) (5 . 0)) . ("m6"))
480          (((0 . 0) (2 . 0) (4 . 0) (1 . 0)) . ("add9"))
481          (((0 . 0) (2 . 0) (4 . 0) (6 . 0) (1 . 0)) . ("maj9"))
482          (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0)) . ("9"))
483          (((0 . 0) (2 . -1) (4 . 0) (6 . -1) (1 . 0)) . ("m9"))
484
485          )
486       chord::names-alist-american))
487
488
489 ;; American style chordnames use no "no",
490 ;; but otherwise very similar to banter for now
491 (define-public (chord::name-american tonic exception-part unmatched-steps
492                               bass-and-inversion steps)
493   (let ((additions (chord::additions unmatched-steps))
494         (subtractions #f))
495     (chord::inner-name-banter tonic exception-part additions subtractions
496                               bass-and-inversion steps)))
497
498
499
500 ;;; 
501 ;;; Jazz style
502 ;;;
503
504
505
506 ;; Jazz chords, by Atte Andr'e Jensen <atte@post.com>
507 ;; NBs: This uses the american list as a bass.
508 ;;      Some defs take up more than one line,
509 ;; be carefull when messing with ;'s!!
510
511
512 ;; FIXME
513 ;;
514 ;; This is getting out-of hand?  Only exceptional chord names that
515 ;; cannot be generated should be here.
516 ;; Maybe we should have inner-name-jazz and inner-name-american functions;
517 ;; 
518 ;;       
519 ;;
520 ;; DONT use non-ascii characters, even if ``it works'' in Windows
521
522 (define-public chord::names-alist-jazz '())
523 (set! chord::names-alist-jazz
524       (append 
525       '(
526         ;; major chords
527         ; major sixth chord = 6
528         (((0 . 0) (2 . 0) (4 . 0) (5 . 0)) . (((raise . 0.5) "6")))
529         ; major seventh chord = triangle
530         ;; shouldn't this be a filled black triange, like this:  ? --jcn
531         ;; (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) .  (((raise . 0.5)((font-family . math) "N"))))
532         (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) .  (((raise . 0.5)((font-family . math) "M"))))
533         ; major chord add nine = add9
534         (((0 . 0) (2 . 0) (4 . 0) (1 . 0)) . (((raise . 0.5) "add9")))
535         ; major sixth chord with nine = 6/9
536         (((0 . 0) (2 . 0) (4 . 0) (5 . 0) (1 . 0)) . (((raise . 0.5) "6/9")))
537
538         ;; minor chords
539         ; minor sixth chord = m6
540         (((0 . 0) (2 . -1) (4 . 0) (5 . 0)) . (columns("m")((raise . 0.5) "6")))
541         ;; minor major seventh chord = m triangle
542         ;; shouldn't this be a filled black triange, like this:  ? --jcn
543         ;;(((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . (columns ("m") ((raise . 0.5)((font-family . math) "N"))))
544         (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . (columns ("m") ((raise . 0.5)((font-family . math) "M"))))
545         ; minor seventh chord = m7
546         (((0 . 0) (2 . -1) (4 . 0) (6 . -1)) . (columns("m")((raise . 0.5) "7")))
547         ; minor sixth nine chord = m6/9
548         (((0 . 0) (2 . -1) (4 . 0) (5 . 0) (1 . 0)) . (columns("m")((raise . 0.5) "6/9")))
549         ; minor with added nine chord = madd9
550         (((0 . 0) (2 . -1) (4 . 0) (1 . 0)) . (columns("m")((raise . 0.5) "add9")))
551         ; minor ninth chord = m9
552         (((0 . 0) (2 . -1) (4 . 0) (6 . -1) (1 . 0)) . (columns("m")((raise . 0.5) "9")))
553
554         ;; dominant chords
555         ; dominant seventh = 7
556         (((0 . 0) (2 . 0) (4 . 0) (6 . -1)) . (((raise . 0.5) "7")))
557         ; augmented dominant = +7
558         ;(((0 . 0) (2 . 0) (4 . +1) (6 . -1)) . (((raise . 0.5) "+7"))) ; +7 with both raised
559         (((0 . 0) (2 . 0) (4 . +1) (6 . -1)) . (columns("+")((raise . 0.5) "7"))) ; +7 with 7 raised
560         ;(((0 . 0) (2 . 0) (4 . +1) (6 . -1)) . (columns((raise . 0.5) "7(")
561         ;       ((raise . 0.3)(music (named ("accidentals-1"))))
562         ;       ((raise . 0.5) "5)"))); 7(#5)
563         ; dominant flat 5 = 7(b5)
564         (((0 . 0) (2 . 0) (4 . -1) (6 . -1)) . (columns((raise . 0.5) "7(")
565                 ((raise . 0.3)(music (named ("accidentals--1"))))
566                 ((raise . 0.5) "5)")))
567         ; dominant 9 = 7(9)
568         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0)) . (((raise . 0.8)"7(9)")))
569         ; dominant flat 9 = 7(b9)
570         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . -1)) . (
571                 ((raise . 0.8)"7(")
572                 ((raise . 0.3)(music (named ("accidentals--1"))))
573                 ((raise . 0.8)"9)")))
574         ; dominant sharp 9 = 7(#9)
575         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . +1)) . (
576                 ((raise . 0.8)"7(")
577                 ((raise . 0.3)(music (named ("accidentals-1"))))
578                 ((raise . 0.8)"9)")))
579         ; dominant 13 = 7(13)
580         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (5 . 0)) . (((raise . 0.8)"7(13)")))
581         ; dominant flat 13 = 7(b13)
582         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (5 . -1)) . (
583                 ((raise . 0.8)"7(")
584                 ((raise . 0.3)(music (named ("accidentals--1"))))
585                 ((raise . 0.8)"13)")))
586         ; dominant 9, 13 = 7(9,13)
587         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0) (5 . 0)) . (((raise . 0.8)"7(9, 13)")))
588         ; dominant flat 9, 13 = 7(b9,13)
589         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . -1) (5 . 0)) . (
590                 ((raise . 0.8)"7(")
591                 ((raise . 0.3)(music (named ("accidentals--1"))))
592                 ((raise . 0.8)"9, 13)")))
593         ; dominant sharp 9, 13 = 7(#9,13)
594         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . +1) (5 . 0)) . (
595                 ((raise . 0.8)"7(")
596                 ((raise . 0.3)(music (named ("accidentals-1"))))
597                 ((raise . 0.8)"9, 13)")))
598         ; dominant 9, flat 13 = 7(9,b13)
599         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0) (5 . -1)) . (
600                 ((raise . 0.8)"7(9, ")
601                 ((raise . 0.3)(music (named ("accidentals--1"))))
602                 ((raise . 0.8)"13)")))
603         ; dominant flat 9, flat 13 = 7(b9,b13)
604         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . -1) (5 . -1)) . (
605                 ((raise . 0.8)"7(")
606                 ((raise . 0.3)(music (named ("accidentals--1"))))
607                 ((raise . 0.8)"9, ")
608                 ((raise . 0.3)(music (named ("accidentals--1"))))
609                 ((raise . 0.8)"13)")))
610         ; dominant sharp 9, flat 13 = 7(#9,b13)
611         (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . +1) (5 . -1)) . (
612                 ((raise . 0.8)"7(")
613                 ((raise . 0.3)(music (named ("accidentals-1"))))
614                 ((raise . 0.8)"9, ")
615                 ((raise . 0.3)(music (named ("accidentals--1"))))
616                 ((raise . 0.8)"13)")))
617
618         ;; diminished chord(s)
619         ; diminished seventh chord =  o
620
621
622         ;; DONT use non-ascii characters, even if ``it works'' in Windows
623         
624         ;;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ((raise . 0.8) (size . -2) ("o")))
625         (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ("" (super "o")))
626
627         ;; half diminshed chords
628         ;; half diminished seventh chord = slashed o
629         ;; (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (((raise . 0.8) "/o")))
630         (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (columns (super (overstrike "o") "/") " " "7")) ; slashed o
631
632         ; half diminished seventh chord  with major 9 = slashed o cancelation 9
633         (((0 . 0) (2 . -1) (4 . -1) (6 . -1) (1 . 0)) . (
634                 ((raise . 0.8)"/o(")
635                 ((raise . 0.3)(music (named ("accidentals-0"))))
636                 ((raise . 0.8)"9)"))); 
637
638 ;; Missing jazz chord definitions go here (note new syntax: see american for hints)
639
640         )
641       chord::names-alist-american))
642
643 (define (step->text-alternate-jazz pitch)
644   (text-append
645    (accidental->text (caddr pitch))
646    (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))))
647
648 (define (step->text-jazz pitch)
649   (if (= (cadr pitch) 6)
650       (case (caddr pitch)
651         ;; sharp 7 only included for completeness?
652         ((-2) (text-append (accidental->text -1) '("7")))
653         ((-1) '("7"))
654         ((0) '("maj7"))
655         ((1) (text-append (accidental->text-super 1) '("7")))
656         ((2) (text-append (accidental->text-super 2) '("7"))))
657       (step->text-alternate-jazz pitch)))
658
659 (define (xchord::additions->text-jazz additions subtractions)
660   (if (pair? additions)
661       (text-append
662        (let ((step (step->text-jazz (car additions))))
663          (if (or (pair? (cdr additions))
664                  (pair? subtractions))
665              (text-append step "/")
666              step))
667       (chord::additions->text-jazz (cdr additions) subtractions))
668   '()))
669
670 (define (chord::>5? x)
671   (or (> (car x) 0)
672       (> (cadr x) 4)))
673
674
675 ;; FIXME:
676 ;; Perhaps all logic like this should be done earlier,
677 ;; so that in this text-construction printing phase
678 ;; we can just blindly create text from all additions.
679 ;;
680 ;; This depends maybe on the fact of code sharing,
681 ;; in this layout, we can share the functions chord::additions
682 ;; and chord::subtractions with banter.
683 (define (chord::additions->text-jazz additions subtractions)
684   (text-append
685    (chord::additions<=5->text-jazz (filter-out-list chord::>5? additions)
686                                    (filter-out-list chord::>5? subtractions))
687    (chord::additions>5->text-jazz (filter-list chord::>5? additions)
688                                   (filter-list chord::>5? subtractions))))
689
690 ;; FIXME
691 (define (chord::additions<=5->text-jazz additions subtractions)
692   (let ((sus (chord::sus-four-jazz additions)))
693     (if (pair? sus)
694         (text-append '("sus") (step->text-jazz (car sus)))
695         '())))
696
697 (define (chord::additions>5->text-jazz additions subtractions)
698   "
699 Compose text of all additions
700
701   * if there's a subtraction:
702     - add `add'
703     - list all up to highest
704   * list all steps that are below an chromatically altered step
705   "
706   (text-append
707    (if (not (null? subtractions)) "add" '())
708    (let ((radds (reverse additions)))
709      (reverse (chord::additions>5->text-jazz-helper
710                radds
711                subtractions
712                (if (or (null? subtractions) (null? radds))
713                    #f (car radds)))))))
714
715 (define (chord::additions>5->text-jazz-helper additions subtractions list-step)
716   "
717 Create texts for all additions
718 If list-step != #f, list all steps down to 5
719 If we encounter a chromatically altered step, turn on list-step
720 "
721
722   (if list-step
723       (if (not (member list-step subtractions))
724           (if (> 5 (cadr list-step))
725               (cons (step->text-jazz list-step)
726                     (chord::additions>5->text-jazz-helper
727                      additions
728                      subtractions
729                      (chord::get-create-step additions
730                                              (- (cadr list-step) 2))))
731               (step->text-jazz list-step))
732           (chord::get-create-step additions (- (cadr list-step) 2)))
733       (if (pair? additions)
734           (let ((step (car additions)))
735             (cons (step->text-jazz step)
736                   (chord::additions>5->text-jazz-helper
737                    (cdr additions)
738                    subtractions
739                    (if (or (and (!= 6 (cadr step)) (!= 0 (caddr step)))
740                            (and (= 6 (cadr step)) (!= -1 (caddr step))))
741                        (chord::get-create-step additions (- (cadr step) 2))
742                        #f))))
743           '())))
744
745 (define (chord::sus-four-jazz chord-pitches)
746   "List of pitches that are step 2 or step 4"
747   (filter-list (lambda (x)
748                  (and (= 0 (car x))
749                       (or (= 1 (cadr x)) (= 3 (cadr x))))) chord-pitches))
750
751 (define (chord::get-create-step steps n)
752   (let* ((i (if (< n 0) (+ n 7) n))
753          (found (filter-list (lambda (x) (= i (cadr x))) steps)))
754     (if (null? found)
755         (if (!= i 6)
756             (list 0 i 0)
757             (list 0 6 -1))
758         (car found))))
759   
760 (define (chord::subtractions->text-jazz subtractions)    
761   (if (pair? subtractions)
762       (text-append
763        (if (= 5 (cadr (car subtractions)))
764            (text-append
765             '("omit")
766             (let ((step (step->text-jazz (car subtractions))))
767               (if (pair? (cdr subtractions))
768                   (text-append step "/")
769                   step)))
770            '())
771        (chord::subtractions->text-jazz (cdr subtractions)))
772       '()))
773
774
775 ;; TODO: maybe merge with inner-name-banter
776 ;; Combine tonic, exception-part of chord name,
777 ;; additions, subtractions and bass or inversion into chord name
778 (define (chord::inner-name-jazz tonic exception-part additions subtractions
779                                   bass-and-inversion steps)
780     (text-append
781      (pitch->chord-name-text-banter tonic steps)
782      exception-part
783      ;; why does list->string not work, format seems only hope...
784      (if (and (string-match "super" (format "~s" exception-part))
785               (or (pair? additions)
786                   (pair? subtractions)))
787          (list simple-super "/"))
788      
789      (list `(,simple-super)
790            (chord::additions->text-jazz additions subtractions)
791            (chord::subtractions->text-jazz subtractions))
792      (chord::bass-and-inversion->text-banter bass-and-inversion)))
793
794 ;; Jazz style--basically similar to american with minor changes
795 ;;
796 ;; Consider Dm6.  When we get here:
797 ;;     tonic =  '(0 1 0) (note d=2)
798 ;;     steps =  '((0 0 0) '(0 2 -1) (0 4 0) (0 5 0))
799 ;;               steps are transposed for tonic c, octave 0,
800 ;;               so (car steps) is always (0 0 0)
801 ;;     except  = ("m")
802 ;;               assuming that the exceptions-alist has an entry
803 ;;               '(((0 . 0) (2 . -1)) . ("m"))
804 ;;               (and NOT the full chord, like std jazz list, ugh)
805 ;;     unmatch = '((0 0 0) (0 2 0) (0 4 0) (0 5 0))
806 ;;     subtract= '()
807 ;;
808 ;; You can look very easily what happens, if you add some write-me calls,
809 ;; and run lilypond on a simple file, eg, containing only the chord c:m6:
810 ;;
811 ;;   (let ((additions (write-me "adds: "
812 ;;                 (chord::additions (write-me "unmatched:"
813 ;;                 unmatched-steps))))
814 ;;
815 ;; If you set subtract #f, the chord::inner-name-jazz does not see any
816 ;; subtractions, ever, so they don't turn up in the chord name.
817 ;;
818 (define-public (chord::name-jazz tonic exception-part unmatched-steps
819                           bass-and-inversion steps)
820   (let ((additions (chord::additions unmatched-steps))
821         ;; get no 'omit' or 'no'
822         ;; (subtractions #f))
823         (subtractions (chord::subtractions unmatched-steps)))
824     (chord::inner-name-jazz tonic exception-part additions subtractions
825              bass-and-inversion steps)))
826
827 ;; wip (set! chord::names-alist-jazz
828 (define chord::names-alist-jazz
829       (append
830       '(
831         (((0 . 0) (2 . -1)) . ("m"))
832
833         ;; some fixups -- jcn
834         ; major seventh chord = triangle
835         (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) .  (((raise . 0.5)((font-family . math) "N"))))
836         ;; (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) .  (((raise . 0.5)((font-family . math) "M"))))
837
838         ;; minor major seventh chord = m triangle
839         (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . (columns ("m") ((raise . 0.5)((font-family . math) "N"))))
840         ;; (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . (columns ("m") ((raise . 0.5)((font-family . math) "M"))))
841         
842         )
843       ;; '()))
844       chord::names-alist-american))