 (defn midi-device-keys
   "Return a list of device event keys for the available MIDI devices"
   []
-  (map midi-mk-full-device-key (vals @midi-devices*)))
+  (map midi-mk-full-device-key (vals (connected-midi-devices))))
 
 (defn- midi-control-handler
   [state-atom handler mapping msg]

  devices rather than just general control events."
  [dev command control-id]
  (concat (midi-mk-full-device-event-key dev command) [control-id]))

(defn midi-event
  "Place incoming midi-event onto the global event stream."
  [dev msg & [ts]]
  (let [command       (:command msg)
        data2-f       (float (/ (:data2 msg) 127))
        msg           (assoc msg :data2-f data2-f :velocity-f data2-f)
        dev-key       (midi-mk-full-device-key dev)
        dev-event-key (midi-mk-full-device-event-key dev command)]
    (event [:midi command] msg)
    (event (midi-mk-full-device-key dev) msg)
    (event (midi-mk-full-control-event-key dev command (:data1 msg)) msg)
    (event dev-event-key msg)))

(defn- detect-midi-devices
  "Designed to run periodically and update the midi-devices* atom with
  the latest list of midi sources and add event handlers to new devices."
  []
  (try
    (let [old-devs     (set (keys @midi-devices*))
          devs         (midi-sources)
          devs         (filter #(not (old-devs (:device %))) devs)
          devs         (map #(assoc % ::dev-num (next-id (str (:vendor %) (:name %) (:description %)))) devs)
          devs         (map #(assoc % ::full-device-key (midi-mk-full-device-key %)) devs)
          receivers    (doall (filter
                               (fn [dev]
                                 (try
                                   (midi-handle-events (midi-in dev) #(midi-event dev %1))
                                   true
                                   (catch Exception e
                                     (log/warn "Can't listen to midi device: " dev "\n" e)
                                     false)))
                               devs))
          device-names (map :name devs)
          n-devs       (count device-names)
          dev-map      (apply hash-map (interleave (map :device devs) receivers))]
      (swap! midi-devices* merge dev-map)
      (when (pos? n-devs)
        (log/info "Connected " n-devs " midi devices: " device-names)))
    (catch Exception ex
      (println "Got exception in detect-midi-devices!" (.printStackTrace ex)))))

;; The rate at which we poll for new midi devices
(def MIDI-POLL-RATE 2000)

(defonce __DEVICE-POLLER__
  (detect-midi-devices))
;;  (every MIDI-POLL-RATE #'detect-midi-devices INTERNAL-POOL :desc "Check for new midi devices"))

(defn midi-poly-player
  "Sets up the event handlers and manages synth instances to easily play

                        (if (= java.util.regex.Pattern (type search))
                          (re-find search key-as-str)
                          (.contains key-as-str search))))]
    (filter filter-pred (connected-midi-devices))))

(defn midi-find-connected-device
  "Returns the first connected MIDI device found where the full device
