diff --git a/Cargo.toml b/Cargo.toml index a00b53c..8c1d7aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,14 @@ edition = "2021" [lib] +[[bin]] +name = "mmp-port" +path = "src/port/main.rs" +required-features = ["port"] + +[dependencies] +midi-msg = { version = "0.7", optional = true } +midir = { version = "0.10", optional = true } +[features] +default = [] +port = ["dep:midi-msg", "dep:midir"] diff --git a/src/port/main.rs b/src/port/main.rs new file mode 100644 index 0000000..d538736 --- /dev/null +++ b/src/port/main.rs @@ -0,0 +1,40 @@ +use std::error::Error; +use std::io::Read; + +use midi_msg::ControlChange; +use midir::os::unix::VirtualOutput; +use midir::MidiOutput; + +fn main() { + match run() { + Ok(_) => (), + Err(err) => println!("Error: {}", err), + } +} + +fn run() -> Result<(), Box> { + let Some(addr) = std::env::args().nth(1) else { + eprintln!("no address provided!"); + eprintln!("usage: mpp-port 192.168.1.1:8080"); + return Ok(()); + }; + + let midi_out = MidiOutput::new("mmp")?; + + let mut conn = midi_out.create_virtual("control")?; + + let msg = |control, value| midi_msg::MidiMsg::ChannelVoice { + channel: midi_msg::Channel::Ch1, + msg: midi_msg::ChannelVoiceMsg::ControlChange { + control: ControlChange::CC { control, value, } + } + }; + + let mut stream = std::net::TcpStream::connect(addr)?; + let mut buf = [0u8, 0u8]; + + loop { + stream.read_exact(&mut buf)?; + conn.send(&msg(buf[0], buf[1]).to_midi())?; + } +}