Skip to content

Instantly share code, notes, and snippets.

@mschwld
Last active October 22, 2021 19:30
Show Gist options
  • Save mschwld/289bbe5a3a892c72e409d3b305505d55 to your computer and use it in GitHub Desktop.
Save mschwld/289bbe5a3a892c72e409d3b305505d55 to your computer and use it in GitHub Desktop.
Use an Akai LPD8 to launch executables

About

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.

[package]
name = "midicon"
version = "0.1"
authors = ["Alice <alice@server.com>"]
edition = "2018"
[dependencies]
midir = "0.7.0"
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