From bfbf0e62c0f665f72831e1d9afaf7dd7c1b44884 Mon Sep 17 00:00:00 2001
From: Nicole Tietz-Sokolskaya <me@ntietz.com>
Date: Wed, 1 Jan 2025 15:11:14 -0500
Subject: [PATCH] start on proper system for routing

---
 Cargo.toml         |   1 +
 src/bin/main.rs    |   3 +
 src/midi.rs        |   2 +
 src/midi/daemon.rs | 138 +++++++++++++++++++++++++++++++++++++++++++++
 src/ui.rs          |  75 ++++++++++++++++++++++--
 5 files changed, 213 insertions(+), 6 deletions(-)
 create mode 100644 src/midi/daemon.rs

diff --git a/Cargo.toml b/Cargo.toml
index 100a2aa..970c60e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,7 @@ edition = "2021"
 
 [dependencies]
 anyhow = "1.0.86"
+crossbeam = "0.8.4"
 eframe = "0.29.1"
 egui = "0.29.1"
 enigo = { version = "0.2.1", features = ["serde"] }
diff --git a/src/bin/main.rs b/src/bin/main.rs
index 1abcf2e..f41ba18 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -13,11 +13,14 @@ fn main() {
     //enigo.text("echo \"hello world\"").unwrap();
     //enigo.key(Key::Return, Direction::Press).unwrap();
 
+    midi_keys::midi::daemon::run_daemon();
     midi_keys::ui::run();
 
 }
 
 pub fn run() -> Result<()> {
+    midi_keys::midi::daemon::run_daemon();
+
     let midi_in = MidiInput::new("keyboard")?;
 
     let midi_device = match find_first_midi_device(&midi_in) {
diff --git a/src/midi.rs b/src/midi.rs
index 46ffd53..1fd9a68 100644
--- a/src/midi.rs
+++ b/src/midi.rs
@@ -1,3 +1,5 @@
+pub mod daemon;
+
 #[derive(PartialEq, Eq, Debug, Clone)]
 pub enum Message {
     Voice(VoiceMessage),
diff --git a/src/midi/daemon.rs b/src/midi/daemon.rs
new file mode 100644
index 0000000..fe070f0
--- /dev/null
+++ b/src/midi/daemon.rs
@@ -0,0 +1,138 @@
+use std::{
+    collections::HashMap,
+    sync::mpsc::{self, Receiver, Sender},
+};
+
+use midir::{MidiInput, MidiInputConnection, MidiInputPort};
+
+use crate::parser::parse_message;
+
+use super::Message;
+
+enum Category {
+    Drums,
+    WindSynth,
+    Piano,
+    Ignore,
+}
+
+/// Architecture for the MIDI connectivity:
+///
+/// State is what's held by the daemon, and is used in an event loop.
+/// - queues are passed in which are what we use to route messages
+/// - this state can only be accessed directly by the event loop, as it owns the state
+/// - does a `select!` (crossbeam) on the timer tick and on other channels
+///     - each tick of the timer (on a timer rx channel), call `state.refresh_ports()` and assign
+///       ports if they're mapped automatically (drums / windsynth / ignore) for anything that
+///       isn't yet mapped (users can override in UI)
+///     - listens to a queue of routing updates ("set connectionid to drums / windsynth / ignore")
+///       from the UI
+///     - receive from the message_receive queue and route the messages
+///
+/// ideal would be to do the port refresh on a separate thread. but! it's relatively cheap (250
+/// microseconds per refresh) so it's not a big deal
+
+type ConnectionId = String;
+type Timestamp = u64;
+type TimestampedMessage = (Timestamp, Message);
+
+pub fn run_daemon() {
+    let mut state = State::new();
+
+    loop {
+        state.refresh_ports();
+        std::thread::sleep_ms(250);
+    }
+}
+
+pub struct State {
+    midi: MidiInput,
+    ports: Vec<MidiInputPort>,
+    connections: HashMap<
+        String,
+        MidiInputConnection<(ConnectionId, Sender<(ConnectionId, TimestampedMessage)>)>,
+    >,
+
+    message_receive: Receiver<(ConnectionId, TimestampedMessage)>,
+    message_send: Sender<(ConnectionId, TimestampedMessage)>,
+}
+
+impl State {
+    pub fn new() -> State {
+        let (tx, rx) = mpsc::channel();
+
+        State {
+            midi: MidiInput::new("midi-keys daemon").expect("could not connect to system MIDI"),
+            ports: vec![],
+            connections: HashMap::new(),
+            message_receive: rx,
+            message_send: tx,
+        }
+    }
+
+    pub fn refresh_ports(&mut self) {
+        let start = std::time::Instant::now();
+
+        let ports = self.midi.ports();
+        for port in ports.iter() {
+            self.refresh_port(port);
+        }
+        self.ports = ports;
+
+        let duration = std::time::Instant::now().duration_since(start).as_micros();
+        println!("refreshing ports took {} microseconds", duration);
+    }
+
+    pub fn refresh_port(&mut self, port: &MidiInputPort) {
+        let start = std::time::Instant::now();
+        let connected = self.connections.contains_key(&port.id());
+
+        println!("checking conn took {} microseconds", std::time::Instant::now().duration_since(start).as_micros());
+        let name = match self.midi.port_name(port) {
+            Ok(s) => s,
+            Err(_) => {
+                // this case happens if we're using a MidiInputPort which has been disconnected,
+                // so we will remove the device from our connections list. we can try this even
+                // if it's not already in the map, as this is idempotent.
+                self.connections.remove(&port.id());
+                return;
+            }
+        };
+
+        println!("getting name took {} microseconds", std::time::Instant::now().duration_since(start).as_micros());
+
+        if connected {
+            return;
+        }
+
+        let midi = MidiInput::new("midi-keys").expect("could not connect to system MIDI");
+        println!("midi input init took {} microseconds", std::time::Instant::now().duration_since(start).as_micros());
+        let send_queue = self.message_send.clone();
+        println!("cloning queue took {} microseconds", std::time::Instant::now().duration_since(start).as_micros());
+
+        if let Ok(conn) = midi.connect(
+            &port,
+            &name,
+            |timestamp, bytes, (conn_id, send_queue)| {
+                handle_message(timestamp, bytes, &conn_id, &send_queue);
+            },
+            (port.id(), send_queue),
+        ) {
+            self.connections.insert(port.id(), conn);
+        }
+        println!("midi conn took {} microseconds", std::time::Instant::now().duration_since(start).as_micros());
+    }
+}
+
+fn handle_message(
+    ts: Timestamp,
+    bytes: &[u8],
+    conn_id: &ConnectionId,
+    tx: &Sender<(ConnectionId, TimestampedMessage)>,
+) {
+    if let Ok((_rem, msg)) = parse_message(bytes) {
+        let _ = tx.send((conn_id.clone(), (ts, msg)));
+    }
+}
+// TODO: chain of handlers? -> handle event as keypress, send to UI
+
diff --git a/src/ui.rs b/src/ui.rs
index b984491..552d06b 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -3,6 +3,8 @@ use std::{sync::Arc, thread::JoinHandle};
 use egui::{mutex::Mutex, Color32, Frame, Rounding, SelectableLabel};
 use midir::{MidiInput, MidiInputPort};
 
+use crate::midi::Message;
+
 /// Launches the UI and runs it until it's done executing.
 pub fn run() {
     let native_options = eframe::NativeOptions::default();
@@ -15,8 +17,65 @@ pub fn run() {
     );
 }
 
+const MAX_MESSAGES: usize = 1_000;
+type Timestamp = u64;
+
+struct MidiMessageBuffer {
+    messages: Vec<(Timestamp, Message)>,
+    start: usize,
+    max: usize,
+}
+
+impl MidiMessageBuffer {
+    pub fn new(max: usize) -> MidiMessageBuffer {
+        Self {
+            messages: vec![],
+            start: 0,
+            max,
+        }
+    }
+
+    pub fn insert(&mut self, m: (Timestamp, Message)) {
+        if self.messages.len() < self.max {
+            self.messages.push(m);
+        } else {
+            self.messages[self.start] = m;
+            self.start = (self.start + 1) % self.messages.len();
+        }
+    }
+
+    pub fn iter(&self) -> BufferIter {
+        BufferIter {
+            elems: self.messages.as_slice(),
+            start: self.start,
+            index: 0,
+        }
+    }
+}
+
+struct BufferIter<'a> {
+    elems: &'a [(Timestamp, Message)],
+    start: usize,
+    index: usize,
+}
+
+impl<'a> Iterator for BufferIter<'a> {
+    type Item = (Timestamp, Message);
+
+    fn next(&mut self) -> Option<Self::Item> {
+        if self.index >= self.elems.len() {
+            None
+        } else {
+            let elem = self.elems[(self.start + self.index) % self.elems.len()].clone();
+            self.index += 1;
+            Some(elem)
+        }
+    }
+}
+
 struct MidiKeysApp {
     midi_input_ports: Arc<Mutex<Vec<MidiInputPort>>>,
+    midi_messages: Arc<Mutex<MidiMessageBuffer>>,
     midi_in: MidiInput,
 }
 
@@ -28,11 +87,14 @@ impl MidiKeysApp {
             MidiInput::new("midi-keys").expect("could not connect to system MIDI");
         let midi_input_ports = Arc::new(Mutex::new(Vec::new()));
 
+        let midi_messages = Arc::new(Mutex::new(MidiMessageBuffer::new(MAX_MESSAGES)));
+
         // TODO: have a way to shut down the midi daemon?
         let _ = launch_midi_daemon(midi_input_ports.clone());
 
         MidiKeysApp {
             midi_input_ports,
+            midi_messages,
             midi_in,
         }
     }
@@ -88,10 +150,16 @@ impl eframe::App for MidiKeysApp {
 
                 frame.end(ui);
             }
+
+            ctx.request_repaint();
         });
     }
 }
 
+struct MidiDaemon {
+
+}
+
 pub fn launch_midi_daemon(target_field: Arc<Mutex<Vec<MidiInputPort>>>) -> JoinHandle<()> {
     let daemon_handle = std::thread::spawn(move || midi_daemon(target_field));
 
@@ -102,13 +170,8 @@ pub fn midi_daemon(target_field: Arc<Mutex<Vec<MidiInputPort>>>) {
     let midi_in: MidiInput = MidiInput::new("midi-keys").expect("could not connect to system MIDI");
 
     loop {
-        let midi_input_ports = get_connnected_midi_devices(&midi_in);
-        *target_field.lock() = midi_input_ports;
+        *target_field.lock() = midi_in.ports();
 
         std::thread::sleep(std::time::Duration::from_millis(100));
     }
 }
-
-pub fn get_connnected_midi_devices(midi_in: &MidiInput) -> Vec<MidiInputPort> {
-    midi_in.ports()
-}