 (defonce midi-devices* (atom {}))
 (defonce midi-control-agents* (atom {}))
 (defonce poly-players* (atom {}))
-(defonce seen-midi-device-keys* (atom #{}))
 
 (def EXCLUDED-DEVICES #{"Real Time Sequencer" "Java Sound Synthesizer"})
 

    (def dinger (midi-poly-player ding))
  "
  ([play-fn] (midi-poly-player play-fn ::midi-poly-player))
  ([play-fn key]
     (let [notes*  (atom {})
           on-key  [key :note-on]
           off-key [key :note-off]]
       (on-event [:midi :note-on] (fn [{note :note velocity :velocity}]
                                    (let [amp (float (/ velocity 127))]
                                      (swap! notes* assoc note (play-fn :note note :amp amp :velocity velocity))))
                 on-key)

       (on-event [:midi :note-off] (fn [{note :note velocity :velocity}]
                                     (let [velocity (float (/ velocity 127 ))]
                                       (when-let [n (get @notes* note)]
                                         (node-control n [:gate 0 :after-touch velocity])
                                         (swap! notes* dissoc note))))
                 off-key)

       ;; TODO listen for '/n_end' event for nodes that free themselves
       ;; before recieving a note-off message.
       (let [player {:notes* notes*
                     :on-key on-key
                     :off-key off-key
                     :playing? (atom true)}]
         (swap! poly-players* assoc key player)))))

(defn- midi-control-handler
  [state-atom handler mapping msg]


(defn midi-player-stop
  ([]
     (remove-handler [::midi-poly-player :midi :note-on])
     (remove-handler [::midi-poly-player :midi :note-off]))
  ([player-or-key]
     (if (keyword? player-or-key)
       (midi-player-stop (get @poly-players* player-or-key))
       (let [player player-or-key]
         (when-not (= ::midi-poly-player (type player))
           (throw (IllegalArgumentException. (str "Expected a midi-poly-player. Got: " (prn-str (type player))))))
         (remove-handler (:on-key player))
         (remove-handler (:off-key player))
         (reset! (:playing? player) false)
         (swap! poly-players* dissoc (:player-key player))
         player))))


(defn midi-capture-next-control-input
