Debian Codegraph @

third_party/rust/coremidi/examples/send.rs

raw
1 extern crate coremidi;
2
3 use std::env;
4 use std::thread;
5 use std::time::Duration;
6
7 fn main() {
8 let destination_index = get_destination_index();
9 println!("Destination index: {}", destination_index);
10
11 let destination = coremidi::Destination::from_index(destination_index).unwrap();
12 println!(
13 "Destination display name: {}",
14 destination.display_name().unwrap()
15 );
16
17 let client = coremidi::Client::new("Example Client").unwrap();
18 let output_port = client.output_port("Example Port").unwrap();
19
20 let note_on = create_note_on(0, 64, 127);
21 let note_off = create_note_off(0, 64, 127);
22
23 for i in 0..10 {
24 println!("[{}] Sending note ...", i);
25
26 output_port.send(&destination, &note_on).unwrap();
27
28 thread::sleep(Duration::from_millis(1000));
29
30 output_port.send(&destination, &note_off).unwrap();
31 }
32 }
33
34 fn get_destination_index() -> usize {
35 let mut args_iter = env::args();
36 let tool_name = args_iter
37 .next()
38 .and_then(|path| {
39 path.split(std::path::MAIN_SEPARATOR)
40 .last()
41 .map(|v| v.to_string())
42 })
43 .unwrap_or_else(|| "send".to_string());
44
45 match args_iter.next() {
46 Some(arg) => match arg.parse::<usize>() {
47 Ok(index) => {
48 if index >= coremidi::Destinations::count() {
49 println!("Destination index out of range: {}", index);
50 std::process::exit(-1);
51 }
52 index
53 }
54 Err(_) => {
55 println!("Wrong destination index: {}", arg);
56 std::process::exit(-1);
57 }
58 },
59 None => {
60 println!("Usage: {} <destination-index>", tool_name);
61 println!();
62 println!("Available Destinations:");
63 print_destinations();
64 std::process::exit(-1);
65 }
66 }
67 }
68
69 fn print_destinations() {
70 for (i, destination) in coremidi::Destinations.into_iter().enumerate() {
71 if let Some(display_name) = destination.display_name() {
72 println!("[{}] {}", i, display_name)
73 }
74 }
75 }
76
77 fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
78 let data = &[0x90 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
79 coremidi::PacketBuffer::new(0, data)
80 }
81
82 fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
83 let data = &[0x80 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
84 coremidi::PacketBuffer::new(0, data)
85 }