| 1 |
extern crate midir; |
| 2 |
|
| 3 |
fn main() { |
| 4 |
match example::run() { |
| 5 |
Ok(_) => (), |
| 6 |
Err(err) => println!("Error: {}", err) |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
#[cfg(not(any(windows, target_arch = "wasm32")))] // virtual ports are not supported on Windows nor on Web MIDI |
| 11 |
mod example { |
| 12 |
|
| 13 |
use std::thread::sleep; |
| 14 |
use std::time::Duration; |
| 15 |
use std::error::Error; |
| 16 |
|
| 17 |
use midir::{MidiInput, MidiOutput, Ignore}; |
| 18 |
use midir::os::unix::VirtualInput; |
| 19 |
|
| 20 |
const LARGE_SYSEX_SIZE: usize = 5572; // This is the maximum that worked for me |
| 21 |
|
| 22 |
pub fn run() -> Result<(), Box<dyn Error>> { |
| 23 |
let mut midi_in = MidiInput::new("My Test Input")?; |
| 24 |
midi_in.ignore(Ignore::None); |
| 25 |
let midi_out = MidiOutput::new("My Test Output")?; |
| 26 |
|
| 27 |
let previous_count = midi_out.port_count(); |
| 28 |
|
| 29 |
println!("Creating virtual input port ..."); |
| 30 |
let conn_in = midi_in.create_virtual("midir-test", |stamp, message, _| { |
| 31 |
println!("{}: {:?} (len = {})", stamp, message, message.len()); |
| 32 |
}, ())?; |
| 33 |
|
| 34 |
assert_eq!(midi_out.port_count(), previous_count + 1); |
| 35 |
|
| 36 |
let out_ports = midi_out.ports(); |
| 37 |
let new_port = out_ports.last().unwrap(); |
| 38 |
println!("Connecting to port '{}' ...", midi_out.port_name(&new_port).unwrap()); |
| 39 |
let mut conn_out = midi_out.connect(&new_port, "midir-test")?; |
| 40 |
println!("Starting to send messages ..."); |
| 41 |
//sleep(Duration::from_millis(2000)); |
| 42 |
println!("Sending NoteOn message"); |
| 43 |
conn_out.send(&[144, 60, 1])?; |
| 44 |
sleep(Duration::from_millis(200)); |
| 45 |
println!("Sending small SysEx message ..."); |
| 46 |
conn_out.send(&[0xF0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xF7])?; |
| 47 |
sleep(Duration::from_millis(200)); |
| 48 |
println!("Sending large SysEx message ..."); |
| 49 |
let mut v = Vec::with_capacity(LARGE_SYSEX_SIZE); |
| 50 |
v.push(0xF0u8); |
| 51 |
for _ in 1..LARGE_SYSEX_SIZE-1 { |
| 52 |
v.push(0u8); |
| 53 |
} |
| 54 |
v.push(0xF7u8); |
| 55 |
assert_eq!(v.len(), LARGE_SYSEX_SIZE); |
| 56 |
conn_out.send(&v)?; |
| 57 |
sleep(Duration::from_millis(200)); |
| 58 |
// FIXME: the following doesn't seem to work with ALSA |
| 59 |
println!("Sending large SysEx message (chunked)..."); |
| 60 |
for ch in v.chunks(4) { |
| 61 |
conn_out.send(ch)?; |
| 62 |
} |
| 63 |
sleep(Duration::from_millis(200)); |
| 64 |
println!("Sending small SysEx message ..."); |
| 65 |
conn_out.send(&[0xF0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xF7])?; |
| 66 |
sleep(Duration::from_millis(200)); |
| 67 |
println!("Closing output ..."); |
| 68 |
conn_out.close(); |
| 69 |
println!("Closing virtual input ..."); |
| 70 |
conn_in.close().0; |
| 71 |
Ok(()) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// needed to compile successfully |
| 76 |
#[cfg(any(windows, target_arch = "wasm32"))] mod example { |
| 77 |
use std::error::Error; |
| 78 |
pub fn run() -> Result<(), Box<dyn Error>> { Ok(()) } |
| 79 |
} |