Fast draft, 50% ?
This commit is contained in:
parent
e91358e61f
commit
4078b602ec
81
src/main.rs
81
src/main.rs
|
|
@ -4,10 +4,15 @@
|
|||
// 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 std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use midir::MidiOutput;
|
||||
use midir::MidiOutputPort;
|
||||
use midir::MidiOutputConnection;
|
||||
|
||||
// use midir::MidiOutput;
|
||||
pub mod midi;
|
||||
|
||||
|
|
@ -17,38 +22,38 @@ pub mod midi;
|
|||
struct Args {
|
||||
/// The midi interface to select
|
||||
midi_interface: Option<usize>,
|
||||
|
||||
/// The port to use (default 0)
|
||||
#[arg(short, long)]
|
||||
port: Option<usize>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Parse Arguments
|
||||
let mut args = Args::parse();
|
||||
let 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 {}
|
||||
let mut output_interface =
|
||||
create_output_interface(args.midi_interface.unwrap(), "Rusty MIDI Commander Output");
|
||||
cleanup(&mut output_interface.lock().unwrap());
|
||||
connect_with_callback(
|
||||
args.midi_interface.unwrap(),
|
||||
"Rusty MIDI Commander Input",
|
||||
&mut output_interface,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn connect_with_callback(port: usize, name: &str)
|
||||
pub fn connect_with_callback(
|
||||
port: usize,
|
||||
name: &str,
|
||||
out_port: &mut Arc<Mutex<MidiOutputConnection>>,
|
||||
)
|
||||
// where
|
||||
// F: FnMut(u64, &[u8], &mut T) + Send + 'static,
|
||||
{
|
||||
|
|
@ -60,6 +65,7 @@ pub fn connect_with_callback(port: usize, name: &str)
|
|||
|
||||
// Connect to port
|
||||
let input_port = &input_ports[port];
|
||||
let conn_clone = Arc::clone(&out_port);
|
||||
let _connection = input_interface
|
||||
.connect(
|
||||
input_port,
|
||||
|
|
@ -67,12 +73,47 @@ pub fn connect_with_callback(port: usize, name: &str)
|
|||
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");
|
||||
conn_clone.lock().unwrap().send(message).unwrap();
|
||||
},
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
loop {}
|
||||
}
|
||||
|
||||
pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> {
|
||||
let output_interface = MidiOutput::new(name).unwrap();
|
||||
let output_port = &output_interface.ports()[port];
|
||||
Arc::new(Mutex::new(
|
||||
output_interface
|
||||
.connect(output_port, "Rusty MIDI Commander Output")
|
||||
.unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn cleanup(
|
||||
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() {
|
||||
// co.send(message).unwrap();
|
||||
// }
|
||||
for n in 0..=63 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
for n in 82..=89 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_note_on(out_port: &mut MidiOutputConnection, pitch: u8, velocity: u8) {
|
||||
let msg = &[0x90, pitch, velocity];
|
||||
out_port.send(msg).unwrap();
|
||||
}
|
||||
|
||||
pub fn prepare(out_port: &mut MidiOutputConnection, notes_values: Vec<(u8, u8)>) {
|
||||
for (note, val) in notes_values.iter() {
|
||||
send_note_on(out_port, *note, *val);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
74
src/midi.rs
74
src/midi.rs
|
|
@ -2,6 +2,11 @@
|
|||
// It should handle the low-level details of communicating with the MIDI devices.
|
||||
|
||||
use crate::MidiInput;
|
||||
use crate::MidiOutput;
|
||||
use crate::MidiOutputConnection;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub fn print_midi_interfaces() {
|
||||
// Create MIDI input and output instances
|
||||
|
|
@ -17,3 +22,72 @@ pub fn print_midi_interfaces() {
|
|||
println!(" {}: {}", i, midi_in.port_name(port).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn connect_with_callback(
|
||||
port: usize,
|
||||
name: &str,
|
||||
out_port: &mut Arc<Mutex<MidiOutputConnection>>,
|
||||
)
|
||||
// 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 conn_clone = Arc::clone(&out_port);
|
||||
let _connection = input_interface
|
||||
.connect(
|
||||
input_port,
|
||||
name,
|
||||
move |_timestamp, message, _| {
|
||||
// Handle MIDI messages here
|
||||
println!("Received MIDI message: {:?}", message);
|
||||
conn_clone.lock().unwrap().send(message).unwrap();
|
||||
},
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
loop {}
|
||||
}
|
||||
|
||||
pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> {
|
||||
let output_interface = MidiOutput::new(name).unwrap();
|
||||
let output_port = &output_interface.ports()[port];
|
||||
Arc::new(Mutex::new(
|
||||
output_interface
|
||||
.connect(output_port, "Rusty MIDI Commander Output")
|
||||
.unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn cleanup(
|
||||
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() {
|
||||
// co.send(message).unwrap();
|
||||
// }
|
||||
for n in 0..=63 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
for n in 82..=89 {
|
||||
send_note_on(out_port, n, 0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_note_on(out_port: &mut MidiOutputConnection, pitch: u8, velocity: u8) {
|
||||
let msg = &[0x90, pitch, velocity];
|
||||
out_port.send(msg).unwrap();
|
||||
}
|
||||
|
||||
pub fn prepare(out_port: &mut MidiOutputConnection, notes_values: Vec<(u8, u8)>) {
|
||||
for (note, val) in notes_values.iter() {
|
||||
send_note_on(out_port, *note, *val);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue