From bc14d6a631a6305ae1647c7e45f98af35930052b Mon Sep 17 00:00:00 2001 From: Jake Zerrer Date: Tue, 4 Nov 2025 09:30:49 -0500 Subject: Adding basic midi filter functions --- src/midi.clj | 36 ++++++++++++++++++++++++++++++------ src/scratch.clj | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/scratch.clj (limited to 'src') diff --git a/src/midi.clj b/src/midi.clj index fcd9d64..bfe075e 100644 --- a/src/midi.clj +++ b/src/midi.clj @@ -7,12 +7,13 @@ (defn get-all-midi-device-info [] (CoreMidiDeviceProvider/getMidiDeviceInfo)) +;; Move to tools.repl (defn print-all-midi-devices "Prints the names of all MIDI devices attached to the computer." [] (doseq [^MidiDevice$Info device-info (get-all-midi-device-info)] (println (.getName device-info)))) -(print-all-midi-devices) + (defn select-devices "Given device info list `devices`, return seq where device name is `device-name`. If tx? is true, returned devices will have unlimited transmitters. @@ -186,15 +187,10 @@ (m/amb (m/? rv) (recur))))))))))) -(def run - (echo "CoreMIDI4J - IAC Bus" 0 0)) ;; CoreMidiSource is TX Device ;; CoreMidiDestination is RX Device -(def close (run prn prn)) -(close) - #_(defn >midi-messages->ch-stream [>midi-messages] (m/signal @@ -213,10 +209,38 @@ (swap! (first (get device channel)) disj data-1))) :else :other)) (m/amb))))))) +(defn >midi-messages->ch-stream + [>midi-messages] + (m/ap + (let [device (atom #{}) + v (m/?< >midi-messages)] + (cond (instance? ShortMessage v) + (let [channel (.getChannel ^ShortMessage v) + command (.getCommand ^ShortMessage v) + data-1 (.getData1 ^ShortMessage v)] + (cond (= command ShortMessage/NOTE_ON) + (swap! device conj data-1) + (= command ShortMessage/NOTE_OFF) + (swap! device disj data-1))) + :else (m/amb))))) + +(defn |short-messages + "Filter down to midi short messages" + [>messages] + (m/eduction (filter #(instance? ShortMessage %)) >messages)) + +(defn |channels + [>messages] + (m/group-by #(.getChannel ^ShortMessage %) >messages)) #_(defn >ch-stream [>device ch] (m/cp (m/?< (second (get >device ch))))) +#_{ch {:notes {note aftertouch} + :pitch v + :control {controller value} + :program program}} + ;; Goal: ;; Create function called `(receive-from-bus bus-name)`. ;; `bus-name` is the name of a midi bus on this machine. If a bus with that name diff --git a/src/scratch.clj b/src/scratch.clj new file mode 100644 index 0000000..77eb609 --- /dev/null +++ b/src/scratch.clj @@ -0,0 +1,36 @@ +(ns scratch + (:require [midi :refer [print-all-midi-devices >bus 128 (midi/|channels (midi/|short-messages f)))] + (println "CH" ch (m/?< vs))) + #_ + (let [[ch >ch-messages] + (m/?> 128 (midi/|channels (midi/|short-messages f))) + #_#_ + [note >note-messages] + (m/?> 128 (midi/|notes >ch-messages))] + (println "CH" ch) + #_#_ + (println "HELLO") + [ch note (m/?< >note-messages)]))))) + +(def cancel + (run prn prn)) +(cancel) + -- cgit v1.2.3