2 ;;; chord-name.scm -- Compile chord name
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 2000--2001 Jan Nieuwenhuizen <janneke@gnu.org>
18 ;; (octave notename accidental)
22 ;; text: scm markup text -- see font.scm and input/test/markup.ly
27 ;; * clean split of base/banter/american stuff
28 ;; * text definition is rather ad-hoc
29 ;; * do without format module
30 ;; * finish and check american names
31 ;; * make notename (tonic) configurable from lilypond
32 ;; * fix append/cons stuff in inner-name-banter
36 (define chord::names-alist-banter '())
37 (set! chord::names-alist-banter
43 (((0 . 0) (2 . 0)) . #f)
45 (((0 . 0) (2 . -1)) . ("m"))
47 (((0 . 0) (1 . 0) (4 . 0)) . (super "2"))
49 (((0 . 0) (3 . 0) (4 . 0)) . (super "4"))
51 (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
54 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (super "o"))
56 (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1)) . ("dim" (super "9")))
57 (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1) (3 . -1)) . ("dim" (super "11")))
59 chord::names-alist-banter))
62 ;; NOTE: Duplicates of chord names defined elsewhere occur in this list
63 ;; in order to prevent spurious superscripting of various chord names,
64 ;; such as maj7, maj9, etc.
66 ;; See input/test/american-chords.ly
68 ;; James Hammons, <jlhamm@pacificnet.net>
71 ;; DONT use non-ascii characters, even if ``it works'' in Windows
73 (define chord::names-alist-american '())
75 (set! chord::names-alist-american
79 (((0 . 0) (2 . 0)) . #f)
81 (((0 . 0) (4 . 0)) . ("5"))
83 (((0 . 0) (2 . -1)) . ("m"))
84 (((0 . 0) (3 . 0) (4 . 0)) . ("sus"))
85 (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
86 ;Alternate: (((0 . 0) (2 . -1) (4 . -1)) . ((super "o")))
87 (((0 . 0) (2 . 0) (4 . 1)) . ("aug"))
88 ;Alternate: (((0 . 0) (2 . 0) (4 . 1)) . ("+"))
89 (((0 . 0) (1 . 0) (4 . 0)) . ("2"))
90 ;; Common seventh chords
91 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (rows (super "o") "7"))
92 (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) . ("maj7"))
93 (((0 . 0) (2 . -1) (4 . 0) (6 . -1)) . ("m7"))
94 (((0 . 0) (2 . 0) (4 . 0) (6 . -1)) . ("7"))
95 (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . ("m(maj7)"))
96 ;jazz: the delta, see jazz-chords.ly
97 ;;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (super ((font-family . math) "N"))
98 ;; ugh, kludge slashed o
99 (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (rows ((raise . 1) "o") ((kern . -0.85) ((raise . 0.57) ((font-relative-size . -3) "/"))) "7")) ; slashed o
100 (((0 . 0) (2 . 0) (4 . 1) (6 . -1)) . ("aug7"))
101 (((0 . 0) (2 . 0) (4 . -1) (6 . 0)) . (rows "maj7" ((font-relative-size . -2) ((raise . 0.2) (music (named "accidentals--1")))) "5"))
102 (((0 . 0) (2 . 0) (4 . -1) (6 . -1)) . (rows "7" ((font-relative-size . -2) ((raise . 0.2) (music (named "accidentals--1")))) "5"))
103 (((0 . 0) (3 . 0) (4 . 0) (6 . -1)) . ("7sus4"))
104 ;; Common ninth chords
105 (((0 . 0) (2 . 0) (4 . 0) (5 . 0) (1 . 0)) . ("6/9")) ;; we don't want the '/no7'
106 (((0 . 0) (2 . 0) (4 . 0) (5 . 0)) . ("6"))
107 (((0 . 0) (2 . -1) (4 . 0) (5 . 0)) . ("m6"))
108 (((0 . 0) (2 . 0) (4 . 0) (1 . 0)) . ("add9"))
109 (((0 . 0) (2 . 0) (4 . 0) (6 . 0) (1 . 0)) . ("maj9"))
110 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0)) . ("9"))
111 (((0 . 0) (2 . -1) (4 . 0) (6 . -1) (1 . 0)) . ("m9"))
114 chord::names-alist-american))
116 ;; Jazz chords, by Atte Andr'e Jensen <atte@post.com>
117 ;; NBs: This uses the american list as a base.
118 ;; Some defs take up more than one line,
119 ;; be carefull when messing with ;'s!!
124 ;; This is getting out-of hand? Only exceptional chord names that
125 ;; cannot be generated should be here.
126 ;; Maybe we should have inner-jazz-name and inner-american-name functions;
130 ;; DONT use non-ascii characters, even if ``it works'' in Windows
132 (define chord::names-alist-jazz '())
133 (set! chord::names-alist-jazz
137 ; major sixth chord = 6
138 (((0 . 0) (2 . 0) (4 . 0) (5 . 0)) . (((raise . 0.5) "6")))
139 ; major seventh chord = triangle
140 ;; shouldn't this be a filled black triange, like this: ? --jcn
141 ;; (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) . (((raise . 0.5)((font-family . math) "N"))))
142 (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) . (((raise . 0.5)((font-family . math) "M"))))
143 ; major chord add nine = add9
144 (((0 . 0) (2 . 0) (4 . 0) (1 . 0)) . (((raise . 0.5) "add9")))
145 ; major sixth chord with nine = 6/9
146 (((0 . 0) (2 . 0) (4 . 0) (5 . 0) (1 . 0)) . (((raise . 0.5) "6/9")))
149 ; minor sixth chord = m6
150 (((0 . 0) (2 . -1) (4 . 0) (5 . 0)) . (rows("m")((raise . 0.5) "6")))
151 ;; minor major seventh chord = m triangle
152 ;; shouldn't this be a filled black triange, like this: ? --jcn
153 ;;(((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . (rows ("m") ((raise . 0.5)((font-family . math) "N"))))
154 (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . (rows ("m") ((raise . 0.5)((font-family . math) "M"))))
155 ; minor seventh chord = m7
156 (((0 . 0) (2 . -1) (4 . 0) (6 . -1)) . (rows("m")((raise . 0.5) "7")))
157 ; minor sixth nine chord = m6/9
158 (((0 . 0) (2 . -1) (4 . 0) (5 . 0) (1 . 0)) . (rows("m")((raise . 0.5) "6/9")))
159 ; minor with added nine chord = madd9
160 (((0 . 0) (2 . -1) (4 . 0) (1 . 0)) . (rows("m")((raise . 0.5) "add9")))
161 ; minor ninth chord = m9
162 (((0 . 0) (2 . -1) (4 . 0) (6 . -1) (1 . 0)) . (rows("m")((raise . 0.5) "9")))
165 ; dominant seventh = 7
166 (((0 . 0) (2 . 0) (4 . 0) (6 . -1)) . (((raise . 0.5) "7")))
167 ; augmented dominant = +7
168 ;(((0 . 0) (2 . 0) (4 . +1) (6 . -1)) . (((raise . 0.5) "+7"))) ; +7 with both raised
169 (((0 . 0) (2 . 0) (4 . +1) (6 . -1)) . (rows("+")((raise . 0.5) "7"))) ; +7 with 7 raised
170 ;(((0 . 0) (2 . 0) (4 . +1) (6 . -1)) . (rows((raise . 0.5) "7(")
171 ; ((raise . 0.3)(music (named ("accidentals-1"))))
172 ; ((raise . 0.5) "5)"))); 7(#5)
173 ; dominant flat 5 = 7(b5)
174 (((0 . 0) (2 . 0) (4 . -1) (6 . -1)) . (rows((raise . 0.5) "7(")
175 ((raise . 0.3)(music (named ("accidentals--1"))))
176 ((raise . 0.5) "5)")))
178 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0)) . (((raise . 0.8)"7(9)")))
179 ; dominant flat 9 = 7(b9)
180 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . -1)) . (
182 ((raise . 0.3)(music (named ("accidentals--1"))))
183 ((raise . 0.8)"9)")))
184 ; dominant sharp 9 = 7(#9)
185 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . +1)) . (
187 ((raise . 0.3)(music (named ("accidentals-1"))))
188 ((raise . 0.8)"9)")))
189 ; dominant 13 = 7(13)
190 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (5 . 0)) . (((raise . 0.8)"7(13)")))
191 ; dominant flat 13 = 7(b13)
192 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (5 . -1)) . (
194 ((raise . 0.3)(music (named ("accidentals--1"))))
195 ((raise . 0.8)"13)")))
196 ; dominant 9, 13 = 7(9,13)
197 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0) (5 . 0)) . (((raise . 0.8)"7(9, 13)")))
198 ; dominant flat 9, 13 = 7(b9,13)
199 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . -1) (5 . 0)) . (
201 ((raise . 0.3)(music (named ("accidentals--1"))))
202 ((raise . 0.8)"9, 13)")))
203 ; dominant sharp 9, 13 = 7(#9,13)
204 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . +1) (5 . 0)) . (
206 ((raise . 0.3)(music (named ("accidentals-1"))))
207 ((raise . 0.8)"9, 13)")))
208 ; dominant 9, flat 13 = 7(9,b13)
209 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0) (5 . -1)) . (
210 ((raise . 0.8)"7(9, ")
211 ((raise . 0.3)(music (named ("accidentals--1"))))
212 ((raise . 0.8)"13)")))
213 ; dominant flat 9, flat 13 = 7(b9,b13)
214 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . -1) (5 . -1)) . (
216 ((raise . 0.3)(music (named ("accidentals--1"))))
218 ((raise . 0.3)(music (named ("accidentals--1"))))
219 ((raise . 0.8)"13)")))
220 ; dominant sharp 9, flat 13 = 7(#9,b13)
221 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . +1) (5 . -1)) . (
223 ((raise . 0.3)(music (named ("accidentals-1"))))
225 ((raise . 0.3)(music (named ("accidentals--1"))))
226 ((raise . 0.8)"13)")))
228 ;; diminished chord(s)
229 ; diminished seventh chord = o
232 ;; DONT use non-ascii characters, even if ``it works'' in Windows
234 ;;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (((raise . 0.8)"o"))); works, but "o" is a little big
235 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . ((raise . 0.8) (size . -2) ("o")))
237 ;; half diminshed chords
238 ; half diminished seventh chord = slashed o
239 (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (((raise . 0.8)"/o")))
240 ; half diminished seventh chord with major 9 = slashed o cancelation 9
241 (((0 . 0) (2 . -1) (4 . -1) (6 . -1) (1 . 0)) . (
243 ((raise . 0.3)(music (named ("accidentals-0"))))
244 ((raise . 0.8)"9)")));
246 ;; Missing jazz chord definitions go here (note new syntax: see american for hints)
249 chord::names-alist-american))
254 (define (pitch->note-name pitch)
255 (cons (cadr pitch) (caddr pitch)))
257 (define (pitch->text pitch)
259 (make-string 1 (integer->char (+ (modulo (+ (cadr pitch) 2) 7) 65)))
260 (if (= (caddr pitch) 0)
267 (append '((font-relative-size . -2))
268 (list (append '((raise . 0.6))
270 (string-append "accidentals-"
271 (number->string (caddr pitch)))))))))))))))
273 ;;; Hooks to override chord names and note names,
274 ;;; see input/tricks/german-chords.ly
276 (define (pitch->text-banter pitch)
279 (define (pitch->chord-name-text-banter pitch)
280 (pitch->text-banter pitch))
282 (define (pitch->note-name-text-banter pitch)
283 (pitch->text-banter pitch))
285 (define (step->text pitch)
287 (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))
295 (define (step->text-banter pitch)
296 (if (= (cadr pitch) 6)
305 (define pitch::semitone-vec (list->vector '(0 2 4 5 7 9 11)))
307 (define (pitch::semitone pitch)
308 (+ (* (car pitch) 12)
309 (vector-ref pitch::semitone-vec (modulo (cadr pitch) 7))
312 (define (pitch::transpose pitch delta)
313 (let ((simple-octave (+ (car pitch) (car delta)))
314 (simple-notename (+ (cadr pitch) (cadr delta))))
315 (let ((octave (+ simple-octave (quotient simple-notename 7)))
316 (notename (modulo simple-notename 7)))
317 (let ((accidental (- (+ (pitch::semitone pitch) (pitch::semitone delta))
318 (pitch::semitone `(,octave ,notename 0)))))
319 `(,octave ,notename ,accidental)))))
321 (define (pitch::diff pitch tonic)
322 (let ((simple-octave (- (car pitch) (car tonic)))
323 (simple-notename (- (cadr pitch) (cadr tonic))))
324 (let ((octave (+ simple-octave (quotient simple-notename 7)
325 (if (< simple-notename 0) -1 0)))
326 (notename (modulo simple-notename 7)))
327 (let ((accidental (- (pitch::semitone pitch)
328 (pitch::semitone tonic)
329 (pitch::semitone `(,octave ,notename 0)))))
330 `(,octave ,notename ,accidental)))))
332 (define (pitch::note-pitch pitch)
333 (+ (* (car pitch) 7) (cadr pitch)))
335 (define (chord::step tonic pitch)
336 (- (pitch::note-pitch pitch) (pitch::note-pitch tonic)))
338 ;; text: list of word
339 ;; word: string + optional list of property
340 ;; property: align, kern, font (?), size
342 (define chord::minor-major-vec (list->vector '(0 -1 -1 0 -1 -1 0)))
344 ;; compute the relative-to-tonic pitch that goes with 'step'
345 (define (chord::step-pitch tonic step)
346 ;; urg, we only do this for thirds
347 (if (= (modulo step 2) 0)
349 (let loop ((i 1) (pitch tonic))
353 pitch `(0 2 ,(vector-ref chord::minor-major-vec
354 ;; -1 (step=1 -> vector=0) + 7 = 6
355 (modulo (+ i 6) 7)))))))))
357 ;; find the pitches that are not part of `normal' chord
358 (define (chord::additions chord-pitches)
359 (let ((tonic (car chord-pitches)))
360 ;; walk the chord steps: 1, 3, 5
361 (let loop ((step 1) (pitches chord-pitches) (additions '()))
363 (let* ((pitch (car pitches))
364 (p-step (+ (- (pitch::note-pitch pitch)
365 (pitch::note-pitch tonic))
367 ;; pitch is an addition if
369 ;; it comes before this step or
371 ;; its step is even or
372 (= (modulo p-step 2) 0)
373 ;; has same step, but different accidental or
375 (not (equal? pitch (chord::step-pitch tonic step))))
376 ;; is the last of the chord and not one of base thirds
378 (= (length pitches) 1)))
379 (loop step (cdr pitches) (cons pitch additions))
381 (loop step (cdr pitches) additions)
382 (loop (+ step 2) pitches additions))))
383 (reverse additions)))))
385 ;; find the pitches that are missing from `normal' chord
386 (define (chord::subtractions chord-pitches)
387 (let ((tonic (car chord-pitches)))
388 (let loop ((step 1) (pitches chord-pitches) (subtractions '()))
390 (let* ((pitch (car pitches))
391 (p-step (+ (- (pitch::note-pitch pitch)
392 (pitch::note-pitch tonic))
394 ;; pitch is an subtraction if
395 ;; a step is missing or
397 (loop (+ step 2) pitches
398 (cons (chord::step-pitch tonic step) subtractions))
399 ;; there are no pitches left, but base thirds are not yet done and
401 (= (length pitches) 1))
402 ;; present pitch is not missing step
404 (loop (+ step 2) pitches subtractions)
405 (loop (+ step 2) pitches
406 (cons (chord::step-pitch tonic step) subtractions)))
408 (loop (+ step 2) (cdr pitches) subtractions)
409 (loop step (cdr pitches) subtractions)))))
410 (reverse subtractions)))))
412 ;; combine tonic, user-specified chordname,
413 ;; additions, subtractions and base or inversion to chord name
415 (define (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)
418 (pitch->chord-name-text-banter tonic)
419 (if user-name user-name '())
420 ;; why does list->string not work, format seems only hope...
421 (if (and (string-match "super" (format "~s" user-name))
422 (or (pair? additions)
423 (pair? subtractions)))
426 (let loop ((from additions) (to '()))
428 (let ((p (car from)))
432 (list 'super (step->text-banter p))
433 (if (or (pair? (cdr from))
434 (pair? subtractions))
438 (let loop ((from subtractions) (to '()))
440 (let ((p (car from)))
445 (list 'super (step->text-banter p))
446 (if (pair? (cdr from))
450 (if (and (pair? base-and-inversion)
451 (or (car base-and-inversion)
452 (cdr base-and-inversion)))
454 (if (car base-and-inversion)
455 (pitch->note-name-text-banter
456 (car base-and-inversion))
457 (pitch->note-name-text-banter
458 (cdr base-and-inversion)))
463 (define (chord::name-banter tonic user-name pitches base-and-inversion)
464 (let ((additions (chord::additions pitches))
465 (subtractions (chord::subtractions pitches)))
466 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
468 ;; american chordnames use no "no",
469 ;; but otherwise very similar to banter for now
470 (define (chord::name-american tonic user-name pitches base-and-inversion)
471 (let ((additions (chord::additions pitches))
473 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
475 ;; Jazz style--basically similar to american with minor changes
476 (define (chord::name-jazz tonic user-name pitches base-and-inversion)
477 (let ((additions (chord::additions pitches))
479 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
481 (define (new-to-old-pitch p)
483 (list (pitch-octave p) (pitch-notename p) (pitch-alteration p))
491 ;; Check for each subset of chord, full chord first, if there's a
492 ;; user-override. Split the chord into user-overridden and to-be-done
493 ;; parts, complete the missing user-override matched part with normal
494 ;; chord to be name-calculated.
496 ;; CHORD: (pitches (base . inversion))
497 (define (default-chord-name-function style chord)
498 (let* ((style-string (symbol->string style))
499 (pitches (map new-to-old-pitch (car chord)))
500 (modifiers (cdr chord))
501 (base-and-inversion (if (pair? modifiers)
502 (cons (new-to-old-pitch (car modifiers))
503 (new-to-old-pitch (cdr modifiers)))
505 (diff (pitch::diff '(0 0 0) (car pitches)))
507 (ly-eval (string->symbol (string-append "chord::name-" style-string))))
509 (ly-eval (string->symbol (string-append "chord::names-alist-" style-string)))))
510 (let loop ((note-names (reverse pitches))
513 (if (pair? note-names)
517 (pitch->note-name (pitch::transpose x diff)))
521 ;; urg? found: break loop
522 (loop '() chord (cdr entry))
523 (loop (cdr note-names) (cons (car note-names) chord) #f)))
524 (let* ((transposed (if pitches
525 (map (lambda (x) (pitch::transpose x diff)) chord)
527 (matched (if (= (length chord) 0)
529 (- (length pitches) (length chord))))
531 (append (do ((i matched (- i 1))
532 (base '() (cons `(0 ,(* (- i 1) 2) 0) base)))
536 (name-func (car pitches) user-name completed base-and-inversion))))))