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)
19 ;; word: string + optional list of property
20 ;; property: size, style, font, super, offset
25 ;; * clean split of base/banter/american stuff
26 ;; * text definition is rather ad-hoc
27 ;; * do without format module
28 ;; * finish and check american names
29 ;; * make notename (tonic) configurable from mudela
30 ;; * fix append/cons stuff in inner-name-banter
35 (define chord::names-alist-banter '())
36 (set! chord::names-alist-banter
42 (((0 . 0) (2 . 0)) . #f)
44 (((0 . 0) (2 . -1)) . ("m"))
46 (((0 . 0) (1 . 0) (4 . 0)) . (("2" (type . "super"))))
48 (((0 . 0) (3 . 0) (4 . 0)) . (("4" (type . "super"))))
50 (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
53 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (("o" (type . "super"))))
55 (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1)) . ("dim" ("9" (type . "super"))))
56 (((0 . 0) (2 . -1) (4 . -1) (6 . -2) (1 . -1) (3 . -1)) . ("dim" ("11" (type . "super"))))
58 chord::names-alist-banter))
61 (define chord::names-alist-american '())
62 (set! chord::names-alist-american
66 (((0 . 0) (2 . 0)) . #f)
67 (((0 . 0) (2 . -1)) . ("m"))
68 (((0 . 0) (2 . -1) (4 . -1)) . ("dim"))
69 (((0 . 0) (4 . 0)) . (("5" (type . "super"))))
70 (((0 . 0) (3 . 0) (4 . 0)) . ("sus"))
71 (((0 . 0) (2 . -1) (4 . -1)) . (("o" (type . "super"))))
72 (((0 . 0) (2 . -1) (4 . -1) (6 . -2)) . (("o7" (type . "super"))))
73 (((0 . 0) (2 . -1) (4 . -1) (6 . -1)) . (("x7" (type . "super"))))
75 (((0 . 0) (2 . 0) (4 . 1)) . ("aug"))
76 (((0 . 0) (2 . 0) (4 . 1) (6 . -1)) . (("aug" ("7" (type . "super")))))
78 (((0 . 0) (2 . 0) (4 . -1) (6 . 0)) . (("maj7" (type . "super")) ("accidentals--1" (font . "feta") (type . "super")) ("5" (type . "super"))))
80 (((0 . 0) (3 . 0) (4 . 0) (6 . -1)) . (("7sus4" (type . "super"))))
82 (((0 . 0) (2 . 0) (4 . 0) (5 . 0)) . (("maj6" (type . "super"))))
84 ;(((0 . 0) (2 . -1) (4 . 0) (5 . 0)) . ("m6" . ""))
87 ;;(((0 . 0) (2 . 0) (4 . 0) (8 . 0)) . ("add9" . ""))
88 ;;(((0 . 0) (2 . 0) (4 . 0) (1 . 0)) . ("" . (("script" . "add9"))))
90 ;; we don't want the '/no7'
91 ;;(((0 . 0) (2 . 0) (4 . 0) (5 . 0) (8 . 0)) . ("6/9" . ""))
92 ;;(((0 . 0) (2 . 0) (4 . 0) (5 . 0) (1 . 0)) . (("script" . "6/9"))))
95 ;;(((0 . 0) (2 . 0) (4 . 0) (6 . 0) (1 . 0)) . ("maj9" . ""))
98 chord::names-alist-american))
103 (define (pitch->note-name pitch)
104 (cons (cadr pitch) (caddr pitch)))
106 (define (pitch->text pitch)
108 (make-string 1 (integer->char (+ (modulo (+ (cadr pitch) 2) 7) 65)))
109 (if (= (caddr pitch) 0)
111 (list (list (string-append "accidentals-"
112 (number->string (caddr pitch)))
113 '(font . "feta"))))))
115 (define (step->text pitch)
117 (number->string (+ (cadr pitch) (if (= (car pitch) 0) 1 8)))
125 (define (pitch->text-banter pitch)
128 (define (step->text-banter pitch)
129 (if (= (cadr pitch) 6)
138 (define pitch::semitone-vec (list->vector '(0 2 4 5 7 9 11)))
140 (define (pitch::semitone pitch)
141 (+ (* (car pitch) 12)
142 (vector-ref pitch::semitone-vec (modulo (cadr pitch) 7))
145 (define (pitch::transpose pitch delta)
146 (let ((simple-octave (+ (car pitch) (car delta)))
147 (simple-notename (+ (cadr pitch) (cadr delta))))
148 (let ((octave (+ simple-octave (quotient simple-notename 7)))
149 (notename (modulo simple-notename 7)))
150 (let ((accidental (- (+ (pitch::semitone pitch) (pitch::semitone delta))
151 (pitch::semitone `(,octave ,notename 0)))))
152 `(,octave ,notename ,accidental)))))
154 (define (pitch::diff pitch tonic)
155 (let ((simple-octave (- (car pitch) (car tonic)))
156 (simple-notename (- (cadr pitch) (cadr tonic))))
157 (let ((octave (+ simple-octave (quotient simple-notename 7)
158 (if (< simple-notename 0) -1 0)))
159 (notename (modulo simple-notename 7)))
160 (let ((accidental (- (pitch::semitone pitch)
161 (pitch::semitone tonic)
162 (pitch::semitone `(,octave ,notename 0)))))
163 `(,octave ,notename ,accidental)))))
165 (define (pitch::note-pitch pitch)
166 (+ (* (car pitch) 7) (cadr pitch)))
168 (define (chord::step tonic pitch)
169 (- (pitch::note-pitch pitch) (pitch::note-pitch tonic)))
171 ;; text: list of word
172 ;; word: string + optional list of property
173 ;; property: align, kern, font (?), size
175 (define chord::minor-major-vec (list->vector '(0 -1 -1 0 -1 -1 0)))
177 ;; compute the relative-to-tonic pitch that goes with 'step'
178 (define (chord::step-pitch tonic step)
179 ;; urg, we only do this for thirds
180 (if (= (modulo step 2) 0)
182 (let loop ((i 1) (pitch tonic))
186 pitch `(0 2 ,(vector-ref chord::minor-major-vec
187 ;; -1 (step=1 -> vector=0) + 7 = 6
188 (modulo (+ i 6) 7)))))))))
190 ;; find the pitches that are not part of `normal' chord
191 (define (chord::additions chord-pitches)
192 (let ((tonic (car chord-pitches)))
193 ;; walk the chord steps: 1, 3, 5
194 (let loop ((step 1) (pitches chord-pitches) (additions '()))
196 (let* ((pitch (car pitches))
197 (p-step (+ (- (pitch::note-pitch pitch)
198 (pitch::note-pitch tonic))
200 ;; pitch is an addition if
202 ;; it comes before this step or
204 ;; its step is even or
205 (= (modulo p-step 2) 0)
206 ;; has same step, but different accidental or
208 (not (equal? pitch (chord::step-pitch tonic step))))
209 ;; is the last of the chord and not one of base thirds
211 (= (length pitches) 1)))
212 (loop step (cdr pitches) (cons pitch additions))
214 (loop step (cdr pitches) additions)
215 (loop (+ step 2) pitches additions))))
216 (reverse additions)))))
218 ;; find the pitches that are missing from `normal' chord
219 (define (chord::subtractions chord-pitches)
220 (let ((tonic (car chord-pitches)))
221 (let loop ((step 1) (pitches chord-pitches) (subtractions '()))
223 (let* ((pitch (car pitches))
224 (p-step (+ (- (pitch::note-pitch pitch)
225 (pitch::note-pitch tonic))
227 ;; pitch is an subtraction if
228 ;; a step is missing or
230 (loop (+ step 2) pitches
231 (cons (chord::step-pitch tonic step) subtractions))
232 ;; there are no pitches left, but base thirds are not yet done and
234 (= (length pitches) 1))
235 ;; present pitch is not missing step
237 (loop (+ step 2) pitches subtractions)
238 (loop (+ step 2) pitches
239 (cons (chord::step-pitch tonic step) subtractions)))
241 (loop (+ step 2) (cdr pitches) subtractions)
242 (loop step (cdr pitches) subtractions)))))
243 (reverse subtractions)))))
245 ;; combine tonic, user-specified chordname,
246 ;; additions, subtractions and base or inversion to chord name
248 (define (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)
249 (apply append (pitch->text-banter tonic)
250 (if user-name user-name '())
251 ;; why does list->string not work, format seems only hope...
252 (if (and (string-match "super" (format "~s" user-name))
253 (or (pair? additions)
254 (pair? subtractions)))
255 '(("/" (type . "super")))
257 (let loop ((from additions) (to '()))
259 (let ((p (car from)))
263 (cons (step->text-banter p) '((type . "super")))
264 (if (or (pair? (cdr from))
265 (pair? subtractions))
266 '(("/" (type . "super")))
269 (let loop ((from subtractions) (to '()))
271 (let ((p (car from)))
274 (cons '("no" (type . "super"))
276 (cons (step->text-banter p) '((type . "super")))
277 (if (pair? (cdr from))
278 '(("/" (type . "super")))
281 (if (and (pair? base-and-inversion)
282 (or (car base-and-inversion)
283 (cdr base-and-inversion)))
285 (if (car base-and-inversion)
287 (car base-and-inversion))
289 (cdr base-and-inversion)))
294 (define (chord::name-banter tonic user-name pitches base-and-inversion)
295 (let ((additions (chord::additions pitches))
296 (subtractions (chord::subtractions pitches)))
297 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
299 ;; american chordnames use no "no",
300 ;; but otherwise very similar to banter for now
301 (define (chord::name-american tonic user-name pitches base-and-inversion)
302 (let ((additions (chord::additions pitches))
304 (chord::inner-name-banter tonic user-name additions subtractions base-and-inversion)))
308 ;; Check for each subset of chord, full chord first, if there's a
309 ;; user-override. Split the chord into user-overridden and to-be-done
310 ;; parts, complete the missing user-override matched part with normal
311 ;; chord to be name-calculated.
313 (define (chord::user-name style pitches base-and-inversion)
314 ;(display "pitches:") (display pitches) (newline)
315 ;(display "style:") (display style) (newline)
316 ;(display "b&i:") (display base-and-inversion) (newline)
317 (let ((diff (pitch::diff '(0 0 0) (car pitches)))
319 (eval (string->symbol (string-append "chord::name-" style))))
321 (eval (string->symbol (string-append "chord::names-alist-" style)))))
322 (let loop ((note-names (reverse pitches))
325 (if (pair? note-names)
329 (pitch->note-name (pitch::transpose x diff)))
333 ;; urg? found: break loop
334 (loop '() chord (cdr entry))
335 (loop (cdr note-names) (cons (car note-names) chord) #f)))
336 (let* ((transposed (if pitches
337 (map (lambda (x) (pitch::transpose x diff)) chord)
339 (matched (if (= (length chord) 0)
341 (- (length pitches) (length chord))))
343 (append (do ((i matched (- i 1))
344 (base '() (cons `(0 ,(* (- i 1) 2) 0) base)))
348 (name-func (car pitches) user-name completed base-and-inversion))))))