Skip to content

Commit

Permalink
Default config
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 26, 2023
1 parent 0dda4c9 commit f9e304f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ makey-midi --debug connect "Microsoft GS Wavetable Synth"
## Config

Below is an example `config.toml` file.
If no config file is found when running, makey-midi will fall back to the default config shown below.
The `channel` defined what channel the midi events are sent on.
The `keymap` is the main part where you map keyboard keys to midi notes.
A list of all possible key values is in the dropdown below, and a table of midi notes can be found [here](https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies).
Expand Down
30 changes: 23 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{args::Args, config::Config};
mod args;
mod config;

const DEFAULT_CONFIG: &str = include_str!("../config.toml");

struct MakeyMidi {
debug: bool,
config: Config,
Expand All @@ -19,25 +21,24 @@ struct MakeyMidi {

fn main() -> Result<()> {
let args = Args::parse();

let raw_config =
fs::read_to_string(args.config.unwrap_or_else(|| PathBuf::from("config.toml")))?;
let config = toml::from_str::<Config>(&raw_config)?;
let config = load_config(args.config.unwrap_or_else(|| PathBuf::from("config.toml")))?;
let output = args.midi.midi_device()?;

let mut app = MakeyMidi {
debug: args.debug,
config,
output: args.midi.midi_device()?,
output,
pressed: HashSet::new(),
};
if let Err(error) = listen(move |x| callback(&mut app, x)) {

if let Err(error) = listen(move |x| key_handler(&mut app, x)) {
println!("[E] Rdev error: {:?}", error)
}

Ok(())
}

fn callback(app: &mut MakeyMidi, event: Event) {
fn key_handler(app: &mut MakeyMidi, event: Event) {
let event = match event.event_type {
EventType::KeyPress(e) => {
if !app.pressed.insert(e) {
Expand Down Expand Up @@ -77,3 +78,18 @@ fn callback(app: &mut MakeyMidi, event: Event) {
let _ = app.output.send(&buf);
}
}

fn load_config(path: PathBuf) -> Result<Config> {
let raw_config = match fs::read_to_string(path) {
Ok(x) => x,
Err(error) => {
println!("[E] Failed to read config file: {:?}", error);
println!("[I] Using default config and writing to disk");
if let Err(e) = fs::write("config.toml", DEFAULT_CONFIG) {
println!("[E] Failed to write default config: {:?}", e);
}
DEFAULT_CONFIG.to_owned()
}
};
Ok(toml::from_str(&raw_config)?)
}

0 comments on commit f9e304f

Please sign in to comment.