Skip to content

Instantly share code, notes, and snippets.

@threedaymonk
Created August 16, 2012 00:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threedaymonk/3364821 to your computer and use it in GitHub Desktop.
Save threedaymonk/3364821 to your computer and use it in GitHub Desktop.
Hacking Chris Ford's Overtone code to play Indian scales.
(ns noize.core
(:use [overtone.live :exclude [midi->hz sharp flat scale run pitch shift]]))
(definst harps# [freq 440]
(let [duration 1]
(*
(line:kr 1 1 duration FREE)
(pluck (* (white-noise) (env-gen (perc 0.001 5) :action FREE)) 1 1 (/ 1 freq) (* duration 2) 0.25))))
(def midi-0 8.1757989156)
(defn scale->hz [sc note]
(let [length (count sc)
octave-multiplier (java.lang.Math/pow 2 (quot note length))
offset (/ (nth sc (mod note length)) 1200)]
(* midi-0 octave-multiplier (+ 1 offset))))
(def indian-diatonic-scale
[0 204 386 498 702 884 1088])
(def indian-diatonic-scale-alt
[0 204 386 498 702 906 1088])
(def indian-22-note-scale
[ 0 90 112 182 204 294 316 386 408 498 520
590 610 702 792 814 884 906 996 1018 1088 1110])
(def current-scale indian-diatonic-scale-alt)
(defn note->hz [note]
(scale->hz current-scale note))
(defn ding! [note] (harps# (note->hz note)))
(defn play [notes]
(let [start (now)
play-at (fn [[ms note]] (at (+ ms start) (ding! note)))]
(->> notes (map play-at) dorun)
notes))
(defn even-melody [pitches]
(let [times (reductions + (repeat 400))
notes (map vector times pitches)]
(play notes)))
(defn play-scale []
(let [length (count current-scale)
start (* 6 length)
end (+ start length 1)]
(even-melody (range start end))))
;(play-scale)
(defn start-from [base] (partial + base))
(defn sum-n [series n] (reduce + (take n series)))
(defn scale [intervals]
(fn [degree]
(if-not (neg? degree)
(sum-n (cycle intervals) degree)
((comp - (scale (reverse intervals)) -) degree))))
(def dia (scale [1 1 1 1 1 1]))
(def C (start-from (* 6 (count current-scale))))
(def row-row-row-your-boat
(let [pitches
[0 0 0 1 2
; Row, row, row your boat,
2 1 2 3 4
; Gently down the stream,
7 7 7 4 4 4 2 2 2 0 0 0
; (take 4 (repeat "merrily"))
4 3 2 1 0]
; Life is but a dream!
durations
[1 1 2/3 1/3 1
2/3 1/3 2/3 1/3 2
1/3 1/3 1/3 1/3 1/3 1/3 1/3 1/3 1/3 1/3 1/3 1/3
2/3 1/3 2/3 1/3 2]
times (reductions + 0 durations)]
(map vector times pitches)))
(defn bpm [beats] (fn [beat] (-> beat (/ beats) (* 60) (* 1000))))
(play
(map
(fn [[beat degree]]
[((bpm 90) beat) ((comp C dia) degree)])
row-row-row-your-boat)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment