Adding Chords to cl-musicman

2006-02-24 16:49:00


CL-USER> (format t "~:{~2A minor: ~3A ~3A ~3A~&~}" 
     (mapcar #'(lambda (x) (cons x (show-sharps (minor-chord x))))
        (show-sharps *notes*)))
A  minor: A   C   E  
A# minor: A#  C#  F  
B  minor: B   D   F# 
C  minor: C   D#  G  
C# minor: C#  E   G# 
D  minor: D   F   A  
D# minor: D#  F#  A# 
E  minor: E   G   B  
F  minor: F   G#  C  
F# minor: F#  A   C# 
G  minor: G   A#  D  
G# minor: G#  B   D# 
NIL

I had a little time to work on my music lisp file. It now understands some chords. Above is an example of all of the minor chords. Below is an example of the chord definitions from the lisp file.


(make-chord major-chord             '(1st 3rd 5th))
(make-chord minor-chord             '(1st flat-3rd 5th))
(make-chord power-chord             '(1st 5th))
(make-chord augmented-chord         '(1st 3rd sharp-5th))
(make-chord diminshed-chord         '(1st 4th 5th))
(make-chord suspended-4th-chord     '(1st 4th 5th))
(make-chord suspended-2nd-chord     '(1st 2nd 5th))
(make-chord major-add-9th-chord     '(1st 3rd 5th 9th))
...
(make-chord major-13th-chord        '(1st 3rd 5th 7th 9th 13th))
(make-chord minor-13th-chord        '(1st flat-3rd 5th flat-7th 9th 11th 13th))
(make-chord 13th-chord              '(1st 3rd 5th flat-7th 9th 13th))     

1st 3rd 5th ext. Are actually functions. We take the major scale, apply each function from the list, and cons it all together.


CL-USER> (macroexpand-1 '(make-chord minor-chord '(1st flat-3rd 5th)))
(DEFUN MINOR-CHORD (KEY)
 (LABELS
  ((BUILD-CHORD (F CHORD)
    (COND ((NULL (CAR F)) (NREVERSE CHORD))
     (T (BUILD-CHORD (CDR F) (CONS (FUNCALL (CAR F) KEY) CHORD))))))
  (BUILD-CHORD '(1ST FLAT-3RD 5TH) 'NIL)))
T

Here is what the definition of flat-3rd looks like.


CL-USER> (macroexpand-1 '(make-scale-pos flat-3rd         3 'flat  major-scale))
(DEFUN FLAT-3RD (KEY)
 (LET ((NOTE (NTH (MOD (1- 3) 7) (MAJOR-SCALE KEY))))
  (FUNCALL (MAKE-SCALE-HELPER 'FLAT) NOTE)))
T

Make-scale-helper understands flat, sharp and double-flat and it raises or lowers the key accordingly.