diff options
Diffstat (limited to 'src/midi.clj')
| -rw-r--r-- | src/midi.clj | 108 |
1 files changed, 57 insertions, 51 deletions
diff --git a/src/midi.clj b/src/midi.clj index e330876..dabca7c 100644 --- a/src/midi.clj +++ b/src/midi.clj @@ -117,64 +117,70 @@ (m/? (m/via m/blk (.close receiver))) (log/log! {:level :info, :id :midi/rx-closed})))))) +(defn >bus + "Opens device named `name`. + + Device will consume `flow`, a flow of Message objects." + [name flow] + (let [device + (first + (select-devices (get-all-midi-device-info) + name false true))] + (with-device device + (fn [d] + (with-rx d flow))))) + +(defn <bus + "Opens device named `name`. + + Calls `flow-handler` with a flow of midi messages. + + `flow-handler` should return a flow." + [name flow-handler] + (let [device + (first + (select-devices (get-all-midi-device-info) + name true false))] + (with-device device + (fn [d] + (with-tx d + (fn [f] + (m/reduce {} nil (flow-handler f)))))))) + (defn echo - [tx-device rx-device from-ch to-ch] + "Echo test." + [name from-ch to-ch] (m/sp (let [rv (m/rdv)] (m/? (m/join vector - (with-device tx-device - (fn [d] - (println "Have tx device") - (m/sp - (m/? - (with-tx d - (fn [f] - (m/sp - (println "Have transmitter") - (m/? - (m/reduce prn nil - (m/ap - (let [v (m/?< f)] - (if (= (class v) ShortMessage) - (let [v ^ShortMessage v] - (if (and (= from-ch (.getChannel v)) - (#{ShortMessage/NOTE_ON ShortMessage/NOTE_OFF} (.getCommand v))) - (let [new-msg (ShortMessage. (.getCommand v) to-ch - (.getData1 v) - (.getData2 v))] - (m/? (rv new-msg))) - (m/amb))) - (m/amb))) - - (println "Value sent."))))))))))) - (with-device rx-device - (fn [d] - (println "Have rx device") - (m/sp - (m/? - (with-rx d + (<bus name + (fn [f] (m/ap - (m/amb= - (m/? m/never) - (loop [] - (println "Echo rx awaiting value...") - (m/amb - (m/? rv) - (recur))))))))))))))) + (let [v (m/?< f)] + (if (= (class v) ShortMessage) + (let [v ^ShortMessage v] + (if (and (= from-ch (.getChannel v)) + (#{ShortMessage/NOTE_ON ShortMessage/NOTE_OFF} (.getCommand v))) + (let [new-msg (ShortMessage. (.getCommand v) to-ch + (.getData1 v) + (.getData2 v))] + (m/? (rv new-msg))) + (m/amb))) + (m/amb))) + + (log/log! {:level :debug, :id :midi/value-sent})))) + (>bus name + (m/ap + (m/amb= + (m/? m/never) + (loop [] + (log/log! {:level :debug, :id :midi/echo-rx-awaiting-value}) + (m/amb + (m/? rv) + (recur))))))))))) (def run - (m/sp - (let [txd - (first - (select-devices (get-all-midi-device-info) - "CoreMIDI4J - IAC Bus" true false)) - - rxd - (first - (select-devices (get-all-midi-device-info) - "CoreMIDI4J - IAC Bus" false true))] - - (m/? (echo txd rxd 0 0))))) + (echo "CoreMIDI4J - IAC Bus" 0 0)) ;; CoreMidiSource is TX Device ;; CoreMidiDestination is RX Device |
