summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJake Zerrer <him@jakezerrer.com>2025-11-04 09:30:49 -0500
committerJake Zerrer <him@jakezerrer.com>2025-11-05 13:28:55 -0500
commitbc14d6a631a6305ae1647c7e45f98af35930052b (patch)
treecdbc5eed90bc83cae1d6eae79a75a246a9fddc5b
parent29b97f57ebfdebbe904017b997e3be7e303c615a (diff)
Adding basic midi filter functions
-rw-r--r--.nrepl-port2
-rw-r--r--src/midi.clj36
-rw-r--r--src/scratch.clj36
3 files changed, 67 insertions, 7 deletions
diff --git a/.nrepl-port b/.nrepl-port
index 6afab2c..9a32db3 100644
--- a/.nrepl-port
+++ b/.nrepl-port
@@ -1 +1 @@
-50636 \ No newline at end of file
+52896 \ No newline at end of file
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 <bus] :as midi]
+ [missionary.core :as m]))
+
+#_(print-all-midi-devices)
+
+(def midi-keyboard "CoreMIDI4J - Minilab3 MIDI")
+
+(def run
+ (<bus
+ midi-keyboard
+ #_
+ (fn [f] (m/ap
+ (let [[i] ]
+ (m/group-by #(.getCh %) ))
+ (println (m/?< f))))
+ (fn [f]
+ (m/ap
+ (let [[ch vs]
+ (m/?> 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)
+