You can use this Rust program to make any interaction with your MIDI controller (like an Akai LPD8) trigger various actions on your system, like controlling the audio volume or start a mail sync. You will need midir (https://crates.io/crates/midir) for this to work. This approach is tested on a Linux based OS only.
Last active
October 22, 2021 19:30
-
-
Save mschwld/289bbe5a3a892c72e409d3b305505d55 to your computer and use it in GitHub Desktop.
Use an Akai LPD8 to launch executables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "midicon" | |
version = "0.1" | |
authors = ["Alice <alice@server.com>"] | |
edition = "2018" | |
[dependencies] | |
midir = "0.7.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate midir; | |
use std::io::{stdin}; | |
use std::process::Command; | |
use std::thread; | |
use std::error::Error; | |
use midir::{MidiInput, Ignore}; | |
fn run() -> Result<(), Box<dyn Error>> { | |
/* Connect to device */ | |
let mut input = String::new(); | |
let mut midi_in = MidiInput::new("midir reading input")?; | |
midi_in.ignore(Ignore::None); | |
let in_ports = midi_in.ports(); | |
let in_port = &in_ports[1]; | |
println!("\nOpening connection"); | |
let in_port_name = midi_in.port_name(in_port)?; | |
let _conn_in = midi_in.connect(in_port, "midir-read-input", move |stamp, message, _| { | |
/* Print midi data, use this to find out how to address buttons and knobs */ | |
/* println!("{}: {:?} (len = {})", stamp, message, message.len()); */ | |
/* Button 1 */ | |
if message[1] == 1 && message[0] == 137 { | |
thread::spawn(|| { | |
Command::new("/foo/bar/") | |
.arg("--alice") | |
.arg("--bob") | |
.output() | |
.expect("failed to execute process"); | |
}); | |
/* Button 2 */ | |
} else if message[1] == 2 && message[0] == 137 { | |
thread::spawn(|| { | |
Command::new("/foo/bar") | |
.arg("--bob") | |
.arg("--alice") | |
.output() | |
.expect("failed to execute process"); | |
}); | |
/* Knob 1 */ | |
} else if message[1] == 9 { | |
/* do sth. here ... */ | |
} | |
}, ())?; | |
println!("Connection open, reading input from '{}' (press enter to exit) ...", in_port_name); | |
input.clear(); | |
stdin().read_line(&mut input)?; // wait for next enter key press | |
println!("Closing connection"); | |
Ok(()) | |
} | |
fn main() { | |
match run() { | |
Ok(_) => (), | |
Err(err) => println!("Error: {}", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment