Skip to content

Instantly share code, notes, and snippets.

@forsakendaemon
Created January 27, 2012 12:12
Show Gist options
  • Save forsakendaemon/1688511 to your computer and use it in GitHub Desktop.
Save forsakendaemon/1688511 to your computer and use it in GitHub Desktop.
Potential additions to overtone.music.pitch to match chords.
(ns overtonehacking.pitch
(:use [overtone.music.pitch]
[clojure.test]
[clojure.pprint]
)
)
; Utility function to invert a map. Thanks to amalloy on stackexchange.com for help with this!
(defn map-invert-preserving-dups [m] (apply merge-with into (for [[k v] m] {v [k]})))
; Invert the NOTES map to get something that note positions can be matched against.
(def ALL-REVERSE-NOTES (map-invert-preserving-dups NOTES))
; Convert positions of notes in a chord into intervals between the notes.
(defn subchord [chord] (map - (rest chord) chord))
; Extend a chord to divisions of a complete octave.
(defn complete [chord] (concat chord (list (apply - (cons 12 chord)))))
; Collapse a chord to divisions of an octave.
(defn collapse-to-octave [chord] (sort (distinct (map #(rem % 12) chord))))
; Invert the CHORD map to get something that chords can be matched against.
(def ALL-REVERSE-CHORDS (apply merge-with into (for [[k v] CHORD] {(complete (subchord (collapse-to-octave v))) [k]})))
; Utility function to rotate a list.
(defn rotate [list] (cons (last list) (butlast list)))
; Match a normalised chord against the previously calculated map.
(defn match [chord inv]
(try
(if (= inv (+ 1 (count chord))) (throw (Exception. "Unable to Match")))
(if (ALL-REVERSE-CHORDS chord) (list inv (ALL-REVERSE-CHORDS chord)) (match (rotate chord) (+ 1 inv)))
(catch Exception e (prn e))))
; Calculate the root note from the supplied chord.
(defn getroot [notes matchlist] (cons (REVERSE-NOTES (rem (nth notes (mod (- (count notes) (first matchlist)) (count notes))) 12)) matchlist))
; Process a chord to get the chord type, root and inversion.
(defn procchord [chord] (getroot (sort (seq chord)) (match (complete (subchord chord)) 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment