diff --git a/.gitignore b/.gitignore index 3ca43ae..193d30e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9fb7c78 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rusty_midicommander" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +midir = "0.9.1" +clap = { version = "4.2.1", features = ["derive","color"] } diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..07ac577 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,32 @@ +// The commands module should define the commands that the program can send. +// This module should provide functions to send each command asynchronously. +// The functions should return immediately, without waiting for the command to complete. + +struct MidiData { + // Define your MIDI data struct here +} + +trait Command { + fn execute(&self, midi_data: &MidiData); +} + +struct CommandA; + +impl Command for CommandA { + fn execute(&self, midi_data: &MidiData) { + println!("Executing CommandA with MIDI data {:?}", midi_data); + } +} + +struct CommandB { + message: String, +} + +impl Command for CommandB { + fn execute(&self, midi_data: &MidiData) { + println!( + "Executing CommandB with message '{}' and MIDI data {:?}", + self.message, midi_data + ); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b7881f6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,78 @@ +// In this organization, src/main.rs is the entry point for the program. +// This file should set up the MIDI input and output devices, and then +// start a loop to receive and process MIDI messages. +// It should also use the commands module to send commands asynchronously. +// use crate::midi::print_midi_interfaces; +use clap::Parser; +use midir::MidiInput; +use midir::MidiInputPort; +use midir::MidiOutput; +use midir::MidiOutputPort; +// use midir::MidiOutput; +pub mod midi; + +/// Simple program to greet a person +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// The midi interface to select + midi_interface: Option, + + /// The port to use (default 0) + #[arg(short, long)] + port: Option, +} + +fn main() { + // Parse Arguments + let mut args = Args::parse(); + match args { + Args { + midi_interface: None, + port: _, + } => { + midi::print_midi_interfaces(); + return; + } + Args { + midi_interface: Some(_), + port: None, + } => args.port = Some(0), + Args { + midi_interface: Some(_), + port: Some(_), + } => {} + } + // println!("{:?}", args); + connect_with_callback(1, "Rusty MIDI Commander Input"); + loop {} +} + +pub fn connect_with_callback(port: usize, name: &str) +// where +// F: FnMut(u64, &[u8], &mut T) + Send + 'static, +{ + // Create MIDI input instance + let input_interface = MidiInput::new(name).unwrap(); + + // Get available MIDI input ports + let input_ports = input_interface.ports(); + + // Connect to port + let input_port = &input_ports[port]; + let _connection = input_interface + .connect( + input_port, + name, + move |_timestamp, message, _| { + // Handle MIDI messages here + println!("Received MIDI message: {:?}", message); + + // let conn = &midi_co + // .output_interface + // .connect(&midi_co.output_port.clone(), "Rusty MIDI Commander Input"); + }, + (), + ) + .unwrap(); +} diff --git a/src/midi.rs b/src/midi.rs new file mode 100644 index 0000000..464178f --- /dev/null +++ b/src/midi.rs @@ -0,0 +1,19 @@ +// The midi module should provide a high-level interface for receiving and sending MIDI messages. +// It should handle the low-level details of communicating with the MIDI devices. + +use crate::MidiInput; + +pub fn print_midi_interfaces() { + // Create MIDI input and output instances + let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap(); + + // Get available MIDI input and output ports + let input_ports = midi_in.ports(); + // let output_ports = midi_out.ports(); + + // Print available input ports + println!("No interface selected, please select one:"); + for (i, port) in input_ports.iter().enumerate() { + println!(" {}: {}", i, midi_in.port_name(port).unwrap()); + } +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..54f6aa7 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,2 @@ +// 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.