Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn ratios [start end]
- (map #(/ % start) (range start end)))
- (defn split-octaves [ratios]
- (loop [current-octave 1, acc [], ratios ratios]
- (if-not (seq ratios)
- acc
- (let [next-octave-ratio (Math/pow 2 current-octave)
- [octave-ratios rest] (split-with #(< % next-octave-ratio) ratios)]
- (recur (inc current-octave) (conj acc (vec octave-ratios)) rest)))))
- (defn normalize-ratios [octaves]
- (mapv (fn [octave] (mapv #(/ % (first octave)) octave)) octaves))
- (def natural-scale-ratios-map
- (sorted-map 1 :tonic
- 16/15 :minor-second
- 9/8 :major-second
- 6/5 :minor-third
- 5/4 :major-third
- 4/3 :forth
- 7/5 :tritone
- 3/2 :fifth
- 8/5 :minor-sixth
- 5/3 :major-sixth
- 16/9 :minor-seventh
- 15/8 :major-seventh
- 2 :octave))
- (def natural-scale-ratios
- (apply sorted-set (keys natural-scale-ratios-map)))
- (defn find-neighbors [ratio]
- (let [[xs ys] (split-with #(> ratio %) natural-scale-ratios)]
- [(last xs) (first ys)]))
- (defn describe-ratio [ratio]
- (if (natural-scale-ratios ratio)
- [ratio (str (natural-scale-ratios-map ratio))]
- [ratio (let [[lower upper] (find-neighbors ratio)
- neighbors-ratio (/ upper lower)
- third (Math/pow neighbors-ratio 1/3)
- max-lower (* lower third)
- min-upper (/ upper third)]
- (cond (< ratio max-lower) (str (natural-scale-ratios-map lower) "+")
- (> ratio min-upper) (str (natural-scale-ratios-map upper) "-")
- :else (str (natural-scale-ratios-map lower) " - " (natural-scale-ratios-map upper))))]))
- (defn describe-overtone-scale [start end]
- (let [ratios (ratios start end)
- octaves (normalize-ratios (split-octaves ratios))]
- (mapv #(mapv describe-ratio %) octaves)))
Advertisement
Add Comment
Please, Sign In to add comment