Skip to content

Instantly share code, notes, and snippets.

@karlthorssen
Created June 11, 2019 13:49
Show Gist options
  • Save karlthorssen/5f249b3864ad6cad2589ba0d59d8176b to your computer and use it in GitHub Desktop.
Save karlthorssen/5f249b3864ad6cad2589ba0d59d8176b to your computer and use it in GitHub Desktop.
(ns chords
(:require [clojure.java.io :as io]
[leipzig.melody :refer :all]
[leipzig.live :as lz]
[leipzig.scale :as scale]
[leipzig.melody :as melody]
[leipzig.chord :as chord]
[leipzig.temperament :as temperament]
[overtone.core :as overtone]
[overtone.inst.piano :as piano]
[overtone.inst.synth :as synth]
[overtone.inst.drum :as drum]
[contour.inst :refer [organ bass sing wobble-organ
dub2 reese string bass2 organ2]]
[overtone.live :refer :all :exclude [tap]]))
(swap! live-config assoc-in [:sc-args :max-buffers] 1024)
(def chosen-scale (comp scale/C scale/major))
(defn as-bass [phrase]
(->> phrase
(where :pitch chosen-scale)
(all :part :bass)
(all :amp 1)))
(defn as-organ [phrase]
(->> phrase
(where :pitch chosen-scale) ;; index->midi
(all :part :organ)
(all :amp 1)))
(defn as-piano [phrase]
(->> phrase
(where :pitch chosen-scale) ;; index->midi
(all :part :piano)))
(defn as-inst
"applies the given instrument key to the notes and converts note
indices to midi"
[inst phrase]
(->> phrase
(where :pitch chosen-scale)
(all :part inst)))
(def stick-sample (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/busta_rhymes_-_touch_it_(instrumental)-1.wav"))
(definst stick-inst []
(scaled-play-buf :num-channels 2 :buf-num (:id stick-sample)))
(def kick17-sample (load-sample "resources/tmp/Post Traumatic Samples and Presets V1/SAMPLE PACKS/KICKS/KICK17.aif"))
(definst kick17 []
(play-buf 2 kick17-sample))
(def perc3-sample (load-sample "resources/tmp/Post Traumatic Samples and Presets V1/SAMPLE PACKS/PERCS/PERC3.wav"))
(definst perc3 []
(play-buf 2 perc3-sample))
(def perc2-sample (load-sample "resources/tmp/Post Traumatic Samples and Presets V1/SAMPLE PACKS/PERCS/PERC2.wav"))
(definst perc2 []
(play-buf 2 perc2-sample))
(def kit {:fat-kick {:sound (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/FAT Kick.aif") :amp 1}
:close-hat {:sound (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/Close-Hat.aif") :amp 1}
:horn {:sound (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/Horn.aif") :amp 1}
:kick {:sound (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/Kick.aif") :amp 1}
:kick17 {:sound kick17}
:open-hat {:sound (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/Open-Hat.aif") :amp 1}
:snare {:sound (sample "resources/DA FUNK GROOVEMACHINE Project/DA FUNK Beats/Snare.aif") :amp 1}
:dub {:sound (fn [_ _] (dub2 :freq (-> -16 chosen-scale temperament/equal)))}
:stick {:sound stick-inst}
:perc3 {:sound perc3}
:perc2 {:sound perc2}})
(comment
(lz/play-note {:drum :kick17 :part :beat :amp 1})
(stick-inst)
(.invoke (-> kit :perc2 :sound))
(kick17)
(perc3)
)
(defmethod lz/play-note :beat [note]
(when-let [fn (-> (get kit (:drum note)) :sound)]
(fn :amp 0.6)))
(defmethod lz/play-note :bass [{:keys [pitch duration]}]
(synth/bass :freq (/ pitch 2) :t (* 2 duration)))
(defmethod lz/play-note :organ [{:keys [pitch duration]}]
(organ :freq (temperament/equal pitch) :dur duration))
(defmethod lz/play-note :piano [{:keys [pitch duration]}]
(when pitch
(piano/piano pitch :vel 70)))
(defmethod lz/play-note :reese [{:keys [pitch duration amp]}]
(reese :freq (/ (temperament/equal pitch) 4)
:amp (or amp 1)
:dur duration))
(defmethod lz/play-note :rest [_] nil)
(defmethod lz/play-note :dub-inst [{:keys [pitch]}]
(dub2 :freq (temperament/equal (- pitch 36))))
(defn tap [drum times length & {:keys [amp] :or {amp 1}}]
(map #(zipmap [:time :duration :drum :amp]
[%1 (- length %1) drum amp]) times))
(defn inst-phrase [inst times notes]
(as-inst inst (phrase times notes)))
(def hf 2)
(def qtr 1)
(def eth 1/2)
(def sth 1/4)
(def swup (partial * 1.3))
(def swbk (partial * 0.7))
(def swing-beat
(->>
(reduce with
[ ;; (tap :dub (range 8) 8)
(tap :fat-kick (range 16) 16)
(tap :snare (range 2 16 4) 16)
(tap :close-hat (range 0 16 qtr) 16)
(tap :close-hat (range (swup eth) 16 qtr) 16)])
(all :part :beat)))
(defonce track (atom nil))
(def chords {:i (chord/root chord/triad -7)
:v7 (-> chord/seventh (chord/root -3) (chord/inversion 3))
:iv (chord/root chord/triad -4)
:vi (-> chord/triad (chord/root -2) (chord/inversion 1))})
(def dings [eth sth eth sth eth sth eth sth eth eth])
(def kick-phrase (->> (phrase [hf eth eth qtr
(- hf sth) sth, eth sth (+ eth sth) eth ]
(repeat 0))
(all :part :beat)
(all :amp 1)
(all :drum :kick17)))
(reset! track
(let [
ticks (->> (phrase dings (repeat 0))
(all :part :beat)
(all :amp 1)
(all :drum :close-hat)
(times 2))
ticks2 (->> (reduce with
[(tap :close-hat (range 0 8 eth) 8)
(tap :perc2 [0 1/2 1 2 3 4 5 6 7] 8)
(tap :perc2 [(+ 1 sth) (- 3 sth) (- 5 sth) (+ 5 sth)] 8)])
(all :part :beat))
kicks (all :part :beat (tap :kick17 [0 4 5] 8))
perky-perc (with (times 2 kicks) (times 2 ticks2))
jesus-chords (inst-phrase :piano
(repeat 8 hf)
(map chords [:i :i :i :i :vi :vi :vi :vi]))
bass (->> (inst-phrase :reese (repeat 8 hf) [0 0 0 0 -2 -2 -2 -2])
(all :amp 0.5))
melody1 (inst-phrase :piano
(repeat 32 eth)
(take 32 (cycle [nil 2 7 4])))
melody2 (inst-phrase :piano
(repeat 64 eth)
(concat (take 28 (cycle [nil 0 2 4])) [4 3 4 3]
(take 12 (cycle [nil 0 2 4])) [3 5 6 7]
[7 9 7 9 7 7 5 4 3 4 3 4 2 0 -1 0] ))
saints-chords (inst-phrase :piano
(repeat 16 hf)
(map chords [:i :vi :i :vi
:i :i :i :v7
:i :i :i :iv
:i :i :v7 :i]))
outro (->>
(inst-phrase :piano
(repeat 8 hf)
(map chords [:i :i :i :i :vi :vi :v7 :i]))
(with (inst-phrase :piano
(concat (repeat 30 eth) [qtr])
(concat (take 24 (cycle [nil 2 7 4])) [4 3 1 -1 0 -1 0]))
(inst-phrase :reese
(concat (repeat 8 hf) [qtr])
[0 0 0 0 -2 -2 -1 0])))]
(->>
ticks2
(then (with jesus-chords melody1 perky-perc))
(then (with jesus-chords melody1 perky-perc bass))
(then (with melody2 saints-chords (times 2 perky-perc)))
(then (times 2 (with jesus-chords melody1 bass perky-perc)))
(then (with outro perky-perc))
(tempo (bpm 88)))))
(comment
(lz/play @track)
(lz/stop)
;; i ii iii iv v vi vii
;; 0 1 2 3 4 5 6
;; -7 -6 -5 -4 -3 -2 -1
(do
(recording-start "chords.wav")
@(lz/play @track)
(Thread/sleep 1000)
(recording-stop))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment