 (ns midi
   (:import (javax.sound.midi MidiSystem
-                             ShortMessage))
+                             ShortMessage
+                             MidiUnavailableException))
   (:refer-clojure :exclude [send]))
 
 (defn note-on

  [re]
  (let [infos (MidiSystem/getMidiDeviceInfo)]
    (map #(MidiSystem/getMidiDevice %)
         (filter #(re-find (re-pattern re) (.getName %))
                 infos))))

(defn get-receiver

  (let [devices (find-devices re)
        devices (filter #(not (= 0 (.getMaxReceivers %))) devices)
        device (first devices)]
    (when (not device)
      (throw (MidiUnavailableException.
              (str "No MIDI receiver matching /" re "/"))))
    (when (not (.isOpen device))
      (.open device))
    (.getReceiver device)))
      

(defn send
  "Send a message to a receiver"
