File: send.rs

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (75 lines) | stat: -rw-r--r-- 2,307 bytes parent folder | download | duplicates (3)
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
use coremidi::{Client, Destination, Destinations, EventBuffer, Protocol};
use std::env;
use std::thread;
use std::time::Duration;

fn main() {
    let destination_index = get_destination_index();
    println!("Destination index: {}", destination_index);

    let destination = Destination::from_index(destination_index).unwrap();
    println!(
        "Destination display name: {}",
        destination.display_name().unwrap()
    );

    let client = Client::new("Example Client").unwrap();
    let output_port = client.output_port("Example Port").unwrap();

    let note_on = EventBuffer::new(Protocol::Midi10).with_packet(0, &[0x2090407f]);

    let note_off = EventBuffer::new(Protocol::Midi10).with_packet(0, &[0x2080407f]);

    for i in 0..10 {
        println!("[{}] Sending note ...", i);

        output_port.send(&destination, &note_on).unwrap();
        thread::sleep(Duration::from_millis(1000));

        output_port.send(&destination, &note_off).unwrap();
        thread::sleep(Duration::from_millis(100));
    }
}

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_else(|| "send".to_string());

    match args_iter.next() {
        Some(arg) => match arg.parse::<usize>() {
            Ok(index) => {
                if index >= 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 Destinations.into_iter().enumerate() {
        if let Some(display_name) = destination.display_name() {
            println!("[{}] {}", i, display_name)
        }
    }
}