Skip to content

Commit

Permalink
list nano-pad mapping to midi-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinthepa committed Jul 8, 2012
1 parent 401c799 commit deee621
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 57 deletions.
82 changes: 40 additions & 42 deletions src/hackbrett/core.clj
Original file line number Diff line number Diff line change
@@ -1,90 +1,88 @@
(ns hackbrett.core
(:use compojure.core
[ring.middleware.multipart-params :only [wrap-multipart-params]]
[ring.middleware.params :only [wrap-params]]
[ring.util.response :only [redirect]]
; [ring.middleware.reload :only [wrap-reload]]
[clojure.tools.logging :only [error]]
)
(:require [compojure.route :as route]
(:require :reload
[compojure.route :as route]
[compojure.handler :as handler]
[hackbrett.sound :as sound]
[hackbrett.mongo :as mongo]
[hackbrett.mongo :as db]
[hackbrett.mapping :as mapping]
[cheshire.core :as json]))
[cheshire.core :as cheshire]))

(def json cheshire/generate-string)

(defn toi [x]
(Integer/parseInt x)) ;; TODO error handling

(defroutes main-routes
(POST ["/midi-key/:midi-key" :midi-key #"[0-9]+"]
[midi-key]
(sound/play-note (toi midi-key))
(sound/play-note (toi midi-key))
"")

(POST ["/midi-key/:midi-key/sample/:sample-name"
:midi-key #"[0-9]+"
:sample-name #"[^/]+"]
[midi-key sample-name]
(mapping/bind-sample (toi midi-key) sample-name))
(mapping/bind-sample (toi midi-key) sample-name)
"")

(GET "/" [] (redirect "/pad"))
(GET "/pad" []
(json (mapping/get-scenes))
)
(GET ["/pad/scene/:scene"
:scene #"[0-9+]"]
[scene]
(json (mapping/get-scene (toi scene))))
(GET ["/pad/scene/:scene/button/:button"
:scene #"[0-9]+"
:button #"[0-9]+"]
[scene button]
(json (mapping/get-button (toi scene) (toi button))))

; (GET "/" [] (redirect "/pad"))
; (GET "/pad" []
; (json/generate-string (sound/list-bindings)))
; (GET ["/pad/scene/:scene"
; :scene #"[0-9+]"]
; [scene]
; (json/generate-string (sound/list-bindings (toi scene))))
; (GET ["/pad/scene/:scene/button/:button"
; :scene #"[0-9]+"
; :button #"[0-9]+"]
; [scene button]
; (json/generate-string (sound/list-bindings (toi scene)
; (toi button))))
;
; (GET "/sample" []
; (json/generate-string (sound/list-samples)))
; (json (mapping/list-samples)))
;
; (POST ["/pad/scene/:scene/button/:button"
; :scene #"[0-9]+"
; :button #"[0-9]+"]
; [scene button]
; (sound/play-sound (toi scene)
; (mapping/play-sound (toi scene)
; (toi button)))
;
; (POST ["/pad/scene/:scene/button/:button/sample/:sampleid"
; :scene #"[0-9]+"
; :button #"[0-9]+"
; :sampleid #"[0-9]+"]
; [scene button sampleid]
; (sound/bind-sample (toi scene)
; (mapping/bind-sample (toi scene)
; (toi button)
; (toi sampleid)))
;
; TODO check if this is a wav file, error or conversion (xuggle.com or mplayer)
(wrap-multipart-params ;; TODO needed? get the filename from the multipart-params instead?
(PUT ["/sample/:filename" :filename #"[^/]+"] ;; TODO GET on same url..
{{filename :filename play :play} :params
body :body
:as request}
(let [real-filename (mapping/add-file body filename)
;; TODO free-sample if the sample replaced another of the same name
_ (sound/load-sample real-filename)]
(if play
(sound/play-file real-filename))
(json/generate-string "OK")))) ;; TODO stupid, use locator for sample info instead
(PUT ["/sample/:filename" :filename #"[^/]+"] ;; TODO GET on same url..
{{filename :filename play :play} :params
body :body
:as request}
(let [real-filename (mapping/add-file body filename)
;; TODO free-sample if the sample replaced another of the same name
_ (sound/load-sample real-filename)]
(if play
(sound/play-file real-filename))
"OK")) ;; TODO stupid, use locator for sample info instead

(route/resources "/") ;; TODO
(route/not-found "404 - Oh noes, there's nothing here!") ;; TODO something cooler
)

;; TODO look at foreclojure.ring/wrap-json

(def app
(->
(handler/site main-routes)
wrap-params
(-> main-routes
handler/api
))

(defn init []
(mongo/init)
(db/init)
(sound/init))
31 changes: 24 additions & 7 deletions src/hackbrett/mapping.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns hackbrett.mapping
(:use hackbrett.util)
(:use clojure.pprint)
(:require [somnium.congomongo :as mongo]) ;; TODO consider moving to mongo ns
(:use [clojure.tools.logging :only [error]])
(:use [clojure.java.io :only [copy output-stream delete-file]])
(:import com.eaio.uuid.UUID)
)
(:use hackbrett.util
clojure.pprint
[clojure.tools.logging :only [error]]
[clojure.java.io :only [copy output-stream delete-file]]
[somnium.congomongo :as mongo]
[hackbrett.mongo :as db] ;; TODO consider moving to mongo ns
)
(:import com.eaio.uuid.UUID))

(defn get-samples []
(mongo/fetch :samples))
Expand All @@ -18,6 +19,22 @@
:where {:id-filename id-filename})]
(:real-filename sample)))

(defn find-first [coll key val]
(first (filter #(= (% key) val) coll)))

;; request handlers
(defn get-scenes []
(:scenes (db/get-nano-pad)))

(defn get-scene [scene]
(-> (get-scenes)
(find-first :scene scene)))

(defn get-button [scene button]
(-> (get-scene scene)
:buttons
(find-first :button button)))

(defn add-file [body filename]
(let [path (str "sounds/uploaded/" (UUID.) ".wav")
_ (with-open [s (output-stream path)]
Expand Down
69 changes: 61 additions & 8 deletions src/hackbrett/mongo.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
(ns hackbrett.mongo
(:use somnium.congomongo))

(defn get-nano-pad []
(fetch-one :controllers :where {:name :nano-pad}))

(defn init-nano-pad []
(when (zero? (fetch-count
:controllers
:where {:name :nano-pad}))
(insert! :controllers
{:name :nano-pad
:scenes
[{:scene 1 :buttons [{:button 1, :midi-key 39}
{:button 2, :midi-key 48}
{:button 3, :midi-key 45}
{:button 4, :midi-key 43}
{:button 5, :midi-key 51}
{:button 6, :midi-key 49}
{:button 7, :midi-key 36}
{:button 8, :midi-key 38}
{:button 9, :midi-key 40}
{:button 10, :midi-key 42}
{:button 11, :midi-key 44}
{:button 12, :midi-key 46}]}
{:scene 2 :buttons [{:button 1, :midi-key 60}
{:button 2, :midi-key 61}
{:button 3, :midi-key 62}
{:button 4, :midi-key 63}
{:button 5, :midi-key 64}
{:button 6, :midi-key 65}
{:button 7, :midi-key 66}
{:button 8, :midi-key 67}
{:button 9, :midi-key 68}
{:button 10, :midi-key 69}
{:button 11, :midi-key 70}
{:button 12, :midi-key 71}]}
{:scene 3 :buttons [{:button 1, :midi-key 72}
{:button 2, :midi-key 73}
{:button 3, :midi-key 74}
{:button 4, :midi-key 75}
{:button 5, :midi-key 76}
{:button 6, :midi-key 77}
{:button 7, :midi-key 78}
{:button 8, :midi-key 79}
{:button 9, :midi-key 80}
{:button 10, :midi-key 81}
{:button 11, :midi-key 82}
{:button 12, :midi-key 83}]}
{:scene 4 :buttons [{:button 1, :midi-key 84}
{:button 2, :midi-key 85}
{:button 3, :midi-key 86}
{:button 4, :midi-key 87}
{:button 5, :midi-key 88}
{:button 6, :midi-key 89}
{:button 7, :midi-key 90}
{:button 8, :midi-key 91}
{:button 9, :midi-key 92}
{:button 10, :midi-key 93}
{:button 11, :midi-key 94}
{:button 12, :midi-key 95}]}]})))

(defn init []
(mongo! :host "127.0.0.1" :db "hackbrett"))
(mongo! :host "127.0.0.1" :db "hackbrett")
(init-nano-pad))

;; samples
;; {
Expand All @@ -15,10 +75,3 @@
;; :midi-key
;; :id-filename
;; }
;;
;; buttons
;; {
;; :scene
;; :button
;; :midi-key
;; }

0 comments on commit deee621

Please sign in to comment.