From 5263395c504d5376eae3cb2b09d35c69a8b6ec29 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 9 Nov 2019 16:42:08 -0500 Subject: [PATCH] Add helper function to get system MIDI devices --- src/midi-playground/midi.clj | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/midi-playground/midi.clj b/src/midi-playground/midi.clj index 0eb6669..83822e5 100644 --- a/src/midi-playground/midi.clj +++ b/src/midi-playground/midi.clj @@ -1,5 +1,12 @@ (ns midi-playground.midi - (:import [javax.sound.midi MidiSystem ShortMessage Receiver Sequencer Synthesizer])) + (:import [javax.sound.midi + MidiSystem + ShortMessage + Receiver + Sequencer + Synthesizer + Transmitter] + [com.sun.media.sound MidiInDevice MidiOutDevice])) (defn midi-message [command channel data1 data2] {:command command @@ -12,11 +19,25 @@ (def default-receiver (MidiSystem/getReceiver)) -(defn get-device-named [name] - (MidiSystem/getMidiDevice - (first - (filter #(= name (.getName %)) - (MidiSystem/getMidiDeviceInfo))))) +(defn list-system-devices [] + (vec (map (fn [info] + (let [device (MidiSystem/getMidiDevice info)] + {:type (class device) + :name (.getName info) + :device device})) + (MidiSystem/getMidiDeviceInfo)))) + +(defn get-devices-of-type [type] + (filter #(instance? type (:device %)) + (list-system-devices))) + +(defn get-system-device [type name] + (when-let [device-info (first + (filter #(and (instance? type (:device %)) + (= (:name %) name)) + (list-system-devices)))] + (:device device-info))) + (defn send-midi [rcvr midi-message] (.send rcvr (map->ShortMessage midi-message) -1))