Skip to content

Instantly share code, notes, and snippets.

@mrb
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrb/1a9f173044c12a58d69a to your computer and use it in GitHub Desktop.
Save mrb/1a9f173044c12a58d69a to your computer and use it in GitHub Desktop.
(ns chords.core
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :refer :all]
[clojure.core.logic.fd :as fd]
[clojure.core.logic.pldb :as pldb]
[fipp.edn :refer (pprint) :rename {pprint fipp}]))
(pldb/db-rel note-name ^:index p)
(pldb/db-rel octave ^:index p)
(pldb/db-rel note ^:index p1 ^:index p2)
(pldb/db-rel minor-third ^:index p1 ^:index p2)
(pldb/db-rel major-third ^:index p1 ^:index p2)
(pldb/db-rel fifth ^:index p1 ^:index p2)
(pldb/db-rel major-chord ^:index p1 ^:index p2 ^:index p3)
(def notes
(pldb/db
[note-name 'C]
[note-name 'C#/Db]
[note-name 'D]
[note-name 'D#/Eb]
[note-name 'E]
[note-name 'F]
[note-name 'F#/Gb]
[note-name 'G]
[note-name 'G#/Ab]
[note-name 'A]
[note-name 'A#/Bb]
[note-name 'B]
[minor-third 'C 'D#/Eb]
[major-third 'C 'E]
[fifth 'C 'F]
[minor-third 'C#/Db 'E]
[major-third 'C#/Db 'F]
[fifth 'C#/Db 'F#/Gb]
[minor-third 'D 'F]
[major-third 'D 'F#/Gb]
[fifth 'D 'G]
[minor-third 'D#/Eb 'F#/Gb]
[major-third 'D#/Eb 'G]
[fifth 'D#/Eb 'G#/Ab]
[minor-third 'E 'G]
[major-third 'E 'G#/Ab]
[fifth 'E 'A]
[minor-third 'F 'G#/Ab]
[major-third 'F 'A]
[fifth 'F 'A#/Bb]
[minor-third 'F#/Gb 'A]
[major-third 'F#/Gb 'A#/Bb]
[fifth 'F#/Gb 'B]
[minor-third 'G 'A#/Bb]
[major-third 'G 'B]
[fifth 'G 'C]
[minor-third 'G#/Ab 'B]
[major-third 'G#/Ab 'C]
[fifth 'G#/Ab 'C#/Db]
[minor-third 'A 'C]
[major-third 'A 'C#/Db]
[fifth 'A 'D]
[minor-third 'A#/Bb 'C#/Db]
[major-third 'A#/Bb 'D]
[fifth 'A#/Bb 'D#/Eb]
[minor-third 'B 'D]
[major-third 'B 'D#/Eb]
[fifth 'B 'E]))
(defn get-notes [db]
(pldb/with-db db
(run* [q]
(fresh [x]
(note-name x)
(== q [x])))))
(defn get-minor-third [db note]
(pldb/with-db db
(run* [q]
(fresh [x y]
(== x note)
(minor-third x y)
(== q [y])))))
(defn get-major-third [db note]
(pldb/with-db db
(run* [q]
(fresh [x y]
(== x note)
(major-third x y)
(== q [y])))))
(defn get-minor-chord [db note]
(pldb/with-db db
(run* [q]
(fresh [x y z]
(== x note)
(minor-third x y)
(fifth x z)
(== q [x y z])))))
(defn get-major-chord [db note]
(pldb/with-db db
(run* [q]
(fresh [x y z]
(== x note)
(major-third x y)
(fifth x z)
(== q [x y z])))))
(defn get-minor-seventh-chord [db note]
(pldb/with-db db
(run* [q]
(fresh [w x y z]
(== w note)
(minor-third w x)
(major-third x y)
(minor-third y z)
(== q [w x y z])))))
(def pp-chords
(fipp (map #(get-major-chord notes (first %)) (get-notes notes))))
; (([F A A#/Bb])
; ([D F#/Gb G])
; ([F#/Gb A#/Bb B])
; ([E G#/Ab A])
; ([C#/Db F F#/Gb])
; ([G B C])
; ([B D#/Eb E])
; ([A C#/Db D])
; ([G#/Ab C C#/Db])
; ([A#/Bb D D#/Eb])
; ([C E F])
; ([D#/Eb G G#/Ab]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment