Add regex midi device choosing
Add regex
This commit is contained in:
parent
55636839b8
commit
0d9e8a4571
3 changed files with 68 additions and 64 deletions
39
Cargo.lock
generated
39
Cargo.lock
generated
|
@ -17,6 +17,15 @@ version = "1.0.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alsa"
|
||||
version = "0.7.1"
|
||||
|
@ -571,6 +580,7 @@ dependencies = [
|
|||
"libpulse-binding",
|
||||
"midir",
|
||||
"mpris",
|
||||
"regex",
|
||||
"serde",
|
||||
"toml",
|
||||
]
|
||||
|
@ -630,6 +640,35 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
|
|
|
@ -11,6 +11,7 @@ itertools = "0.11.0"
|
|||
libpulse-binding = "2.28.1"
|
||||
midir = "0.9.1"
|
||||
mpris = "2.0.1"
|
||||
regex = "1.10.3"
|
||||
serde = {version ="1.0.188", features = ["derive"]}
|
||||
toml = "0.7.6"
|
||||
|
||||
|
|
|
@ -1,80 +1,44 @@
|
|||
use delay_map::HashMapDelay;
|
||||
use crate::config::NanoKeys;
|
||||
use midir::{Ignore, MidiInput};
|
||||
use mpris::PlayerFinder;
|
||||
use regex::Regex;
|
||||
use std::error::Error;
|
||||
use std::io::{stdin, stdout, Write};
|
||||
pub fn run() -> Result<(), Box<dyn Error>> {
|
||||
let mut input = String::new();
|
||||
|
||||
let mut midi_in = MidiInput::new("midir reading input")?;
|
||||
pub fn list_ports() -> () {
|
||||
let midi_in = MidiInput::new("PicoKontroller").expect("Creating Midi device failed");
|
||||
let in_ports = midi_in.ports();
|
||||
println!("All available midi devices:");
|
||||
for (_, p) in in_ports.iter().enumerate() {
|
||||
println!("{}", midi_in.port_name(p).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(port_name: &str) -> Result<(), Box<dyn Error>> {
|
||||
let port_filter = Regex::new(port_name).expect("Creating RegEx failed");
|
||||
|
||||
let mut midi_in = MidiInput::new("PicoKontroller").expect("Creating Midi device failed");
|
||||
midi_in.ignore(Ignore::None);
|
||||
|
||||
// Get an input port (read from console if multiple are available)
|
||||
let in_ports = midi_in.ports();
|
||||
let in_port = match in_ports.len() {
|
||||
0 => return Err("no input port found".into()),
|
||||
1 => {
|
||||
println!(
|
||||
"Choosing the only available input port: {}",
|
||||
midi_in.port_name(&in_ports[0]).unwrap()
|
||||
);
|
||||
&in_ports[0]
|
||||
}
|
||||
_ => {
|
||||
println!("\nAvailable input ports:");
|
||||
for (i, p) in in_ports.iter().enumerate() {
|
||||
println!("{}: {}", i, midi_in.port_name(p).unwrap());
|
||||
}
|
||||
print!("Please select input port: ");
|
||||
stdout().flush()?;
|
||||
let mut input = String::new();
|
||||
stdin().read_line(&mut input)?;
|
||||
in_ports
|
||||
.get(input.trim().parse::<usize>()?)
|
||||
.ok_or("invalid input port selected")?
|
||||
}
|
||||
};
|
||||
|
||||
println!("\nOpening connection");
|
||||
let in_port_name = midi_in.port_name(in_port)?;
|
||||
let in_port = in_ports
|
||||
.iter()
|
||||
.find(|p| port_filter.is_match(&midi_in.port_name(p).unwrap()))
|
||||
.expect("Couldn't find a port matching the supplied RegEx");
|
||||
|
||||
// _conn_in needs to be a named parameter, because it needs to be kept alive until the end of the scope
|
||||
let _conn_in = midi_in.connect(
|
||||
in_port,
|
||||
"midir-read-input",
|
||||
move |stamp, message, _| {
|
||||
println!(
|
||||
"{}: {:.3} (len = {})",
|
||||
stamp,
|
||||
message[2] as f64 / 127.0,
|
||||
message.len()
|
||||
);
|
||||
"PicoController Input",
|
||||
move |_, message, _| {
|
||||
let key = message[1];
|
||||
let volume = message[2] as f64 / 127.0;
|
||||
set_volume("LibreWolf", volume);
|
||||
println!(
|
||||
"Key: {:?} | State: {:.2}",
|
||||
NanoKeys::try_from(key).unwrap(),
|
||||
volume
|
||||
);
|
||||
},
|
||||
(),
|
||||
)?;
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
pub fn set_volume(player_id: &str, volume: f64) {
|
||||
PlayerFinder::new()
|
||||
.expect("Could not connect to D-Bus")
|
||||
.find_all()
|
||||
.unwrap_or_else(|_| Vec::new())
|
||||
.into_iter()
|
||||
.filter(|player| player.identity() == player_id)
|
||||
.for_each(|player| {
|
||||
let _ = player.set_volume(volume);
|
||||
});
|
||||
loop {}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue