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