1 ;;; chord.scm -- to be included in/to replace chord-name.scm
2 ;;; 2000 janneke@gnu.org
7 ;; urg, these two only to guess if a '/' is needed to separate
8 ;; user-chord-name and additions/subtractions
14 ;; (octave notename accidental)
18 ;; text: scm markup text -- see font.scm and input/test/markup.ly
23 ;; * clean split of base/banter/american stuff
24 ;; * text definition is rather ad-hoc
25 ;; * do without format module
26 ;; * finish and check american names
27 ;; * make notename (tonic) configurable from mudela
28 ;; * fix append/cons stuff in inner-name-banter
33 (define chord::names-alist-banter '())
34 (set! chord::names-alist-banter
40 (((0 . 0) (2 . 0)) . #f)
42 (((0 . 0) (2 . -1)) . ("m"))
44 (((0 . 0) (1 . 0) (4 . 0)) . (super "2"))
46 (((0 . 0) (3 . 0) (4 . 0)) . (super "4"))
48 (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
51 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (super "o"))
53 (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1)) . ("dim" (super "9")))
54 (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1) (3 . -1)) . ("dim" (super "11")))
56 chord::names-alist-banter))
59 ;; NOTE: Duplicates of chord names defined elsewhere occur in this list
60 ;; in order to prevent spurious superscripting of various chord names,
61 ;; such as maj7, maj9, etc.
63 ;; See input/test/american-chords.ly
65 ;; James Hammons, <jlhamm@pacificnet.net>
67 (define chord::names-alist-american '())
68 (set! chord::names-alist-american
72 (((0 . 0) (2 . 0)) . #f)
74 (((0 . 0) (4 . 0)) . ("5"))
76 (((0 . 0) (2 . -1)) . ("m"))
77 (((0 . 0) (3 . 0) (4 . 0)) . ("sus"))
78 (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
79 ;Alternate: (((0 . 0) (2 . -1) (4 . -1)) . ((super "o")))
80 (((0 . 0) (2 . 0) (4 . 1)) . ("aug"))
81 ;Alternate: (((0 . 0) (2 . 0) (4 . 1)) . ("+"))
82 (((0 . 0) (1 . 0) (4 . 0)) . ("2"))
83 ;; Common seventh chords
84 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (rows (super "o") "7"))
85 (((0 . 0) (2 . 0) (4 . 0) (6 . 0)) . ("maj7"))
86 (((0 . 0) (2 . -1) (4 . 0) (6 . -1)) . ("m7"))
87 (((0 . 0) (2 . 0) (4 . 0) (6 . -1)) . ("7"))
88 (((0 . 0) (2 . -1) (4 . 0) (6 . 0)) . ("m(maj7)"))
89 ;jazz: the delta, see jazz-chords.ly
90 ;(((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (super ((family . "math") "N"))
91 (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (rows (super "o") ((kern . -0.5) ((size . "-3") "/")) "7")) ; slashed o
92 (((0 . 0) (2 . 0) (4 . 1) (6 . -1)) . ("aug7"))
93 (((0 . 0) (2 . 0) (4 . -1) (6 . 0)) . (rows "maj7" (music (named ("accidentals--1"))) "5"))
94 (((0 . 0) (3 . 0) (4 . 0) (6 . -1)) . ("7sus4"))
95 ;; Common ninth chords
96 (((0 . 0) (2 . 0) (4 . 0) (5 . 0) (1 . 0)) . ("6/9")) ;; we don't want the '/no7'
97 (((0 . 0) (2 . 0) (4 . 0) (5 . 0)) . ("maj6"))
98 (((0 . 0) (2 . -1) (4 . 0) (5 . 0)) . ("m6"))
99 (((0 . 0) (2 . 0) (4 . 0) (1 . 0)) . ("add9"))
100 (((0 . 0) (2 . 0) (4 . 0) (6 . 0) (1 . 0)) . ("maj9"))
101 (((0 . 0) (2 . 0) (4 . 0) (6 . -1) (1 . 0)) . ("9"))
102 (((0 . 0) (2 . -1) (4 . 0) (6 . -1) (1 . 0)) . ("m9"))
105 chord::names-alist-american))
110 (define (pitch->note-name pitch)
111 (cons (cadr pitch) (caddr pitch)))
113 (define (pitch->text pitch)
115 (make-string 1 (integer->char (+ (modulo (+ (cadr pitch) 2) 7) 65)))
116 (if (= (caddr pitch) 0)
123 (string-append "accidentals-"
124 (number->string (caddr pitch)))))))))))
127 (define (step->text pitch)
129 (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))
137 (define (pitch->text-banter pitch)
140 (define (step->text-banter pitch)
141 (if (= (cadr pitch) 6)
150 (define pitch::semitone-vec (list->vector '(0 2 4 5 7 9 11)))
152 (define (pitch::semitone pitch)
153 (+ (* (car pitch) 12)
154 (vector-ref pitch::semitone-vec (modulo (cadr pitch) 7))
157 (define (pitch::transpose pitch delta)
158 (let ((simple-octave (+ (car pitch) (car delta)))
159 (simple-notename (+ (cadr pitch) (cadr delta))))
160 (let ((octave (+ simple-octave (quotient simple-notename 7)))
161 (notename (modulo simple-notename 7)))
162 (let ((accidental (- (+ (pitch::semitone pitch) (pitch::semitone delta))
163 (pitch::semitone `(,octave ,notename 0)))))
164 `(,octave ,notename ,accidental)))))
166 (define (pitch::diff pitch tonic)
167 (let ((simple-octave (- (car pitch) (car tonic)))
168 (simple-notename (- (cadr pitch) (cadr tonic))))
169 (let ((octave (+ simple-octave (quotient simple-notename 7)
170 (if (< simple-notename 0) -1 0)))
171 (notename (modulo simple-notename 7)))
172 (let ((accidental (- (pitch::semitone pitch)
173 (pitch::semitone tonic)
174 (pitch::semitone `(,octave ,notename 0)))))
175 `(,octave ,notename ,accidental)))))
177 (define (pitch::note-pitch pitch)
178 (+ (* (car pitch) 7) (cadr pitch)))
180 (define (chord::step tonic pitch)
181 (- (pitch::note-pitch pitch) (pitch::note-pitch tonic)))
183 ;; text: list of word
184 ;; word: string + optional list of property
185 ;; property: align, kern, font (?), size
187 (define chord::minor-major-vec (list->vector '(0 -1 -1 0 -1 -1 0)))
189 ;; compute the relative-to-tonic pitch that goes with 'step'
190 (define (chord::step-pitch tonic step)
191 ;; urg, we only do this for thirds
192 (if (= (modulo step 2) 0)
194 (let loop ((i 1) (pitch tonic))
198 pitch `(0 2 ,(vector-ref chord::minor-major-vec
199 ;; -1 (step=1 -> vector=0) + 7 = 6
200 (modulo (+ i 6) 7)))))))))
202 ;; find the pitches that are not part of `normal' chord
203 (define (chord::additions chord-pitches)
204 (let ((tonic (car chord-pitches)))
205 ;; walk the chord steps: 1, 3, 5
206 (let loop ((step 1) (pitches chord-pitches) (additions '()))
208 (let* ((pitch (car pitches))
209 (p-step (+ (- (pitch::note-pitch pitch)
210 (pitch::note-pitch tonic))
212 ;; pitch is an addition if
214 ;; it comes before this step or
216 ;; its step is even or
217 (= (modulo p-step 2) 0)
218 ;; has same step, but different accidental or
220 (not (equal? pitch (chord::step-pitch tonic step))))
221 ;; is the last of the chord and not one of base thirds
223 (= (length pitches) 1)))
224 (loop step (cdr pitches) (cons pitch additions))
226 (loop step (cdr pitches) additions)
227 (loop (+ step 2) pitches additions))))
228 (reverse additions)))))
230 ;; find the pitches that are missing from `normal' chord
231 (define (chord::subtractions chord-pitches)
232 (let ((tonic (car chord-pitches)))
233 (let loop ((step 1) (pitches chord-pitches) (subtractions '()))
235 (let* ((pitch (car pitches))
236 (p-step (+ (- (pitch::note-pitch pitch)
237 (pitch::note-pitch tonic))
239 ;; pitch is an subtraction if
240 ;; a step is missing or
242 (loop (+ step 2) pitches
243 (cons (chord::step-pitch tonic step) subtractions))
244 ;; there are no pitches left, but base thirds are not yet done and
246 (= (length pitches) 1))
247 ;; present pitch is not missing step
249 (loop (+ step 2) pitches subtractions)
250 (loop (+ step 2) pitches
251 (cons (chord::step-pitch tonic step) subtractions)))
253 (loop (+ step 2) (cdr pitches) subtractions)
254 (loop step (cdr pitches) subtractions)))))
255 (reverse subtractions)))))
257 ;; combine tonic, user-specified chordname,
258 ;; additions, subtractions and base or inversion to chord name
260 (define (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)
263 (pitch->text-banter tonic)
264 (if user-name user-name '())
265 ;; why does list->string not work, format seems only hope...
266 (if (and (string-match "super" (format "~s" user-name))
267 (or (pair? additions)
268 (pair? subtractions)))
271 (let loop ((from additions) (to '()))
273 (let ((p (car from)))
277 (list 'super (step->text-banter p))
278 (if (or (pair? (cdr from))
279 (pair? subtractions))
283 (let loop ((from subtractions) (to '()))
285 (let ((p (car from)))
290 (list 'super (step->text-banter p))
291 (if (pair? (cdr from))
295 (if (and (pair? base-and-inversion)
296 (or (car base-and-inversion)
297 (cdr base-and-inversion)))
299 (if (car base-and-inversion)
301 (car base-and-inversion))
303 (cdr base-and-inversion)))
308 (define (chord::name-banter tonic user-name pitches base-and-inversion)
309 (let ((additions (chord::additions pitches))
310 (subtractions (chord::subtractions pitches)))
311 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
313 ;; american chordnames use no "no",
314 ;; but otherwise very similar to banter for now
315 (define (chord::name-american tonic user-name pitches base-and-inversion)
316 (let ((additions (chord::additions pitches))
318 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
322 ;; Check for each subset of chord, full chord first, if there's a
323 ;; user-override. Split the chord into user-overridden and to-be-done
324 ;; parts, complete the missing user-override matched part with normal
325 ;; chord to be name-calculated.
327 (define (default-chord-name-function style pitches base-and-inversion)
328 ;(display "pitches:") (display pitches) (newline)
329 ;(display "style:") (display style) (newline)
330 ;(display "b&i:") (display base-and-inversion) (newline)
331 (let ((diff (pitch::diff '(0 0 0) (car pitches)))
333 (ly-eval (string->symbol (string-append "chord::name-" style))))
335 (ly-eval (string->symbol (string-append "chord::names-alist-" style)))))
336 (let loop ((note-names (reverse pitches))
339 (if (pair? note-names)
343 (pitch->note-name (pitch::transpose x diff)))
347 ;; urg? found: break loop
348 (loop '() chord (cdr entry))
349 (loop (cdr note-names) (cons (car note-names) chord) #f)))
350 (let* ((transposed (if pitches
351 (map (lambda (x) (pitch::transpose x diff)) chord)
353 (matched (if (= (length chord) 0)
355 (- (length pitches) (length chord))))
357 (append (do ((i matched (- i 1))
358 (base '() (cons `(0 ,(* (- i 1) 2) 0) base)))
362 (name-func (car pitches) user-name completed base-and-inversion))))))