Compare commits

...
This repository has been archived on 2026-09-13. You can view files and clone it, but cannot push or open issues or pull requests.

5 Commits

Author SHA1 Message Date
Dandaedre 0fb4185ecf More refactoring 2023-04-20 18:22:02 +02:00
Dandaedre 4cdb40ae4f Cleans up macro usage 2023-04-20 11:42:36 +02:00
Dandaedre dce5b8d883 Adds a cleanup for the macro 2023-04-20 11:25:11 +02:00
Dandaedre de9f497a00 Added a macro 2023-04-20 10:51:27 +02:00
Dandaedre 06e74b28c4 Fixed a couple of typos, too tired to continue 2023-04-19 15:32:19 +02:00
7 changed files with 129 additions and 45 deletions

View File

@ -12,6 +12,7 @@ actix-web = "4.3.1"
reqwest = { version = "0.11.16", features = ["blocking"] } reqwest = { version = "0.11.16", features = ["blocking"] }
base64 = "0.21.0" base64 = "0.21.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
toml = "0.7.3"
[[bin]] [[bin]]
name = "rusty_midicommander" name = "rusty_midicommander"

View File

@ -1,3 +1,8 @@
# RustyMIDICommander # RustyMIDICommander
Rusty MIDI Commander is a lightweight and flexible Rust program for sending asynchronous commands in response to MIDI input. Built on top of popular Rust MIDI libraries, it provides a simple and modular framework for defining MIDI commands and executing them asynchronously. Whether you need to control web APIs, hardware devices, or other MIDI-enabled software, Rusty MIDI Commander makes it easy to build powerful and responsive MIDI control systems. Rusty MIDI Commander is a lightweight and flexible Rust program for sending asynchronous commands in response to MIDI input. Built on top of popular Rust MIDI libraries, it provides a simple and modular framework for defining MIDI commands and executing them asynchronously. Whether you need to control web APIs, hardware devices, or other MIDI-enabled software, Rusty MIDI Commander makes it easy to build powerful and responsive MIDI control systems.
# Macro Reading
https://veykril.github.io/tlborm/syntax-extensions/expansion.html
https://doc.rust-lang.org/book/ch19-06-macros.html

8
config.toml Normal file
View File

@ -0,0 +1,8 @@
[general]
midi_name = "Rusty MIDI Commander"
[projector]
address = "127.0.0.1:8080"
login = "admin1"
password = "panasonic"

View File

@ -2,11 +2,11 @@
// This file should set up the MIDI input and output devices, and then // This file should set up the MIDI input and output devices, and then
// start a loop to receive and process MIDI messages. // start a loop to receive and process MIDI messages.
// It should also use the commands module to send commands asynchronously. // It should also use the commands module to send commands asynchronously.
// use crate::midi::print_midi_interfaces;
use crate::midi::cleanup; use crate::midi::cleanup;
use crate::midi::connect_loop_with_callback; use crate::midi::connect_loop_with_callback;
use crate::midi::create_output_interface; use crate::midi::create_output_interface;
use crate::midi::prepare;
use clap::Parser; use clap::Parser;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
@ -16,15 +16,18 @@ use midir::MidiInput;
use midir::MidiOutput; use midir::MidiOutput;
use midir::MidiOutputConnection; use midir::MidiOutputConnection;
// use midir::MidiOutput;
pub mod midi; pub mod midi;
mod projector; mod projector;
mod utils;
/// Simple program to greet a person /// Simple program to greet a person
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Args { struct Args {
/// The midi interface to select /// The midi interface to select
midi_interface: Option<usize>, midi_input: Option<usize>,
midi_output: Option<usize>,
config: Option<String>,
} }
fn main() { fn main() {
@ -32,48 +35,44 @@ fn main() {
let args = Args::parse(); let args = Args::parse();
match args { match args {
Args { Args {
midi_interface: None, midi_input: None,
midi_output: None,
config: _,
} => { } => {
midi::print_midi_interfaces(); midi::print_midi_interfaces(&"No interface selected, please select one:".into());
return; return;
} }
Args { Args {
midi_interface: Some(_), midi_input: Some(_),
midi_output: Some(_),
config: _,
} => {} } => {}
Args {
midi_input: _,
midi_output: None,
config: _,
} => {
midi::print_midi_interfaces(&"No interface selected, please select one:".into());
return;
}
_ => {}
} }
println!("{:?}", args);
// let config_text = &args.config;
println!("[INFO] Connecting with args: {:?}", args);
let proj = Arc::new(Mutex::new(projector::Projector::new( let proj = Arc::new(Mutex::new(projector::Projector::new(
"127.0.0.1:8080", "127.0.0.1:8080",
"admin1", "admin1",
"panasonic", "panasonic",
))); )));
let proj1 = Arc::clone(&proj);
let proj2 = Arc::clone(&proj);
// proj.lock().unwrap().shutter_on().unwrap();
// proj.lock().unwrap().shutter_off().unwrap();
// proj.lock().unwrap().get_osd().unwrap();
let mut output_interface = let mut output_interface =
create_output_interface(args.midi_interface.unwrap(), "Rusty MIDI Commander Output"); create_output_interface(args.midi_output, "Rusty MIDI Commander Output");
cleanup(&mut output_interface.lock().unwrap()); cleanup(&mut output_interface.lock().unwrap());
connect_loop_with_callback( connect_loop_with_callback(
args.midi_interface.unwrap(), args.midi_input.unwrap(),
"Rusty MIDI Commander Input", "Rusty MIDI Commander Input",
&mut output_interface, &mut output_interface,
vec![ list_actions!(proj, 3, shutter_on, 4, shutter_off),
(
3,
Box::new(move || {
proj1.lock().unwrap().shutter_on().unwrap();
}),
),
(
4,
Box::new(move || {
proj2.lock().unwrap().shutter_on().unwrap();
}),
),
],
); );
} }

View File

@ -12,18 +12,43 @@ lazy_static! {
static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)]; static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)];
} }
pub fn print_midi_interfaces() { pub fn print_midi_interfaces(error_message: &String) {
// Create MIDI input and output instances // Create MIDI input and output instances
let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap(); let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap();
let midi_out = MidiOutput::new("Rusty MIDI Commander Output").unwrap();
// Get available MIDI input and output ports // Get available MIDI input and output ports
let input_ports = midi_in.ports(); let input_ports = midi_in.ports();
// let output_ports = midi_out.ports(); let output_ports = midi_out.ports();
// Print available input ports // Print available input ports
println!("No interface selected, please select one:"); match (input_ports.len(), output_ports.len()) {
(0, 0) => {
println!(
"[ERROR] No MIDI interface was found, please ensure a MIDI interface is plugged in"
);
return;
}
(_, 0) => println!("No MIDI output interface was found, no feedback will be sent"),
(0, _) => {
println!(
"[ERROR] No MIDI input interface was found, please ensure a MIDI interface is plugged in"
);
return;
}
(_, _) => println!("{}", error_message),
}
println!("[Input]");
for (i, port) in input_ports.iter().enumerate() { for (i, port) in input_ports.iter().enumerate() {
println!(" {}: {}", i, midi_in.port_name(port).unwrap()); println!(" {}: {}", i, midi_in.port_name(port).unwrap());
}
if output_ports.len() == 0 {
return;
}
println!("[Output]");
for (i, port) in output_ports.iter().enumerate() {
println!(" {}: {}", i, midi_out.port_name(port).unwrap());
} }
} }
@ -40,6 +65,10 @@ pub fn connect_loop_with_callback(
let input_ports = input_interface.ports(); let input_ports = input_interface.ports();
// Connect to port // Connect to port
if port >= input_ports.len() {
print_midi_interfaces(&"[ERROR] Midi input port is out of range".to_string());
std::process::exit(1);
}
let input_port = &input_ports[port]; let input_port = &input_ports[port];
let conn_clone = Arc::clone(&out_port); let conn_clone = Arc::clone(&out_port);
// let fn_clone = Arc::clone(&calls); // let fn_clone = Arc::clone(&calls);
@ -81,24 +110,36 @@ pub fn connect_loop_with_callback(
loop {} loop {}
} }
pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> { // Currently doesn't accept virtual output or no output
let output_interface = MidiOutput::new(name).unwrap(); pub fn create_output_interface(
let output_port = &output_interface.ports()[port]; port: Option<usize>,
name: &str,
) -> Arc<Mutex<MidiOutputConnection>> {
let output_interface = match port {
None => MidiOutput::new(name).unwrap(),
Some(_) => MidiOutput::new(name).unwrap(),
};
if port.unwrap() >= output_interface.ports().len() {
print_midi_interfaces(
&"[ERROR] The midi output interface selected is out of bounds".into(),
);
std::process::exit(1);
}
let output_port = &output_interface.ports()[port.unwrap()];
Arc::new(Mutex::new( Arc::new(Mutex::new(
output_interface output_interface.connect(output_port, name).unwrap(),
.connect(output_port, "Rusty MIDI Commander Output")
.unwrap(),
)) ))
} }
pub fn cleanup( pub fn cleanup(
out_port: &mut MidiOutputConnection, /*, range: &dyn std::iter::Iterator<Item = usize>*/ out_port: &mut MidiOutputConnection, /*, range: &dyn std::iter::Iterator<Item = usize>*/
) { ) {
// let _messages = vec![[0xB0, 0x7B, 0x00], [0xB0, 0x79, 0x00]];
// for message in messages.iter() { // for message in messages.iter() {
// co.send(message).unwrap(); // co.send(message).unwrap();
// } // }
// out_port
// .send(&[0xF0, 0x47, 0x7F, 0x29, 0x60, 0x00, 0x40, 0x08, 0x00, 0xF7])
// .unwrap();
for n in 0..=71 { for n in 0..=71 {
send_note_on(out_port, n, 0); send_note_on(out_port, n, 0);
} }

View File

@ -1,10 +1,12 @@
#![allow(dead_code)]
use base64::{engine::general_purpose, Engine as _}; use base64::{engine::general_purpose, Engine as _};
use reqwest::blocking::{Client, RequestBuilder}; use reqwest::blocking::{Client, RequestBuilder};
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
static WAIT_TIME: f64 = 0.7; static WAIT_TIME: f64 = 0.5;
pub struct Projector { pub struct Projector {
ip: String, ip: String,

View File

@ -1,2 +1,30 @@
// The utils module should provide utility functions that are used throughout the program. // The utils module should provide utility functions that are used throughout the program.
// These functions could include functions to convert MIDI messages to and from human-readable formats, or to handle errors. // These functions could include functions to convert MIDI messages to and from human-readable formats, or to handle errors.
#[macro_export]
macro_rules! list_actions {
($proj:ident, $($id:literal, $action:ident),+ ) => {
{
let mut temp_vec:Vec<(u8, Box<(dyn Fn() + Send + 'static)>)> = Vec::new();
$(
let proj = Arc::clone(&$proj);
temp_vec.push((
$id,
Box::new(move || {
match proj.lock().unwrap().$action() {
Ok(_) => {}
Err(e) => {
println!(
"[ERROR] An error occurend while sending a ({}) message",
stringify!($action)
);
println!(" {}", e)
}
};
}),
));
)+
temp_vec
}
};
}