Skip to content

Instantly share code, notes, and snippets.

@samaaron
Created September 8, 2010 14:18
Show Gist options
  • Save samaaron/570189 to your computer and use it in GitHub Desktop.
Save samaaron/570189 to your computer and use it in GitHub Desktop.
(ns boiingg.core
(:use
overtone.live
[clojure.contrib.seq-utils :only [indexed]])
(:require [monome-serial.monome :as monome]))
(refer-ugens)
(boot)
(defn wait-till-connected []
(while (not (connected?)) (java.lang.Thread/sleep 50)))
(def m (monome/connect "/dev/tty.usbserial-m64-0790"))
(wait-till-connected)
(defsynth beep [freq-mul 1]
(* (sin-osc (* 110 (+ 1 freq-mul))) (env-gen (perc 0.01 0.03) 1 1 0 1 :free)))
(def metro (metronome 880))
(def state (atom [nil nil nil nil nil nil nil nil]))
(defn play [col]
(beep col))
(defn schedule-notes
"If any of the state's columns have reached the 'ground' (i.e. row 7) then trigger the synth with the column id as a param to play at the supplied time"
[time]
(let [s @state]
(doall (for [col (indexed s)]
(if (= 7 (:current (nth col 1))) (at time (play (nth col 0))))))))
(defn bounce [col-state]
(let [max (:max col-state)
cur (:current col-state)
falling (:falling col-state)]
(cond (= 7 cur) (assoc col-state :current (dec cur) :falling false)
(= max cur) (assoc col-state :current (inc cur) :falling true)
falling (assoc col-state :current (inc cur) :falling true)
:else (assoc col-state :current (dec cur) :falling false))))
(defn update-state []
(reset! state (vec (map #(if % (bounce %)) @state))))
(defn draw-col [idx info]
(monome/led-on m idx (:current info) ))
(defn draw-state []
(let [idxdstate (indexed @state)]
(doall (map #(if (nth % 1) (draw-col (nth % 0) (nth % 1))) idxdstate))))
(defn go [beat]
(let [time (metro beat)]
(update-state)
(schedule-notes time)
(apply-at #(do
(monome/clear m)
(draw-state))
time)
(apply-before #'go (metro (inc beat)) (inc beat))))
(go (metro))
(defn start-falling [col row]
(if (= 7 row)
(swap! state assoc col nil)
(swap! state assoc col {:max row :current row :falling true})))
(monome/on-press m (fn [x y] (start-falling x y)))
;;;;;;;;;;;;;;
;; handy stuff
;;
;;(defn go [beat] nil)
;;
;;(reset! state [nil nil nil nil nil nil nil nil])
;;
;;(reset! state [{:max 3 :current 0 :falling true} nil {:max 5 :current 0 :falling true} nil {:max 4 :current 0 :falling true} nil {:max 6 :current 0 :falling true} nil])
;;
;;(reset! state [{:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} {:max 6 :current 6 :falling true} ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment