osynic_midi 0.1.0

A MIDI -> keyboard mapper for the Osynic
Documentation
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use clap::{Parser, Subcommand};
use enigo::{
    Direction::{Press, Release},
    Enigo, Key, Keyboard, Settings,
};
use inquire::Select;
use midir::MidiInput;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
use tokio::sync::mpsc;

#[derive(Parser)]
#[command(name = "osynic-midi")]
#[command(about = "MIDI to Keyboard mapper for Osynic", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Configuration file path
    #[arg(short, long)]
    config: Option<String>,

    /// Mapping mode: octaves or notes
    #[arg(short, long)]
    mode: Option<String>,
}

#[derive(Subcommand)]
enum Commands {
    /// List available MIDI input devices
    ListDevices,
    /// List available configuration files
    ListConfigs,
    /// Start MIDI mapping with interactive setup
    Start {
        /// Configuration file path (will prompt if not provided)
        #[arg(short, long)]
        config: Option<String>,

        /// Mapping mode: octaves or notes (will prompt if not provided)
        #[arg(short, long)]
        mode: Option<String>,
    },
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
enum MappingMode {
    Octaves,
    Notes,
}

#[derive(Debug, Serialize, Deserialize)]
struct Config {
    mapping_mode: Option<MappingMode>,
    octaves: HashMap<String, HashMap<String, String>>,
    velocity_threshold: u8,
    note_mappings: HashMap<u8, String>,
}

impl Config {
    fn load(path: &str) -> Result<Self, Box<dyn Error>> {
        let config_str = fs::read_to_string(path)?;
        let config: Config = serde_json::from_str(&config_str)?;
        Ok(config)
    }

    fn note_to_pitch(note: u8) -> (u8, String) {
        let octave = note / 12 - 1;
        let pitch = match note % 12 {
            0 => "C",
            1 => "C#/Db",
            2 => "D",
            3 => "D#/Eb",
            4 => "E",
            5 => "F",
            6 => "F#/Gb",
            7 => "G",
            8 => "G#/Ab",
            9 => "A",
            10 => "A#/Bb",
            11 => "B",
            _ => unreachable!(),
        };
        (octave, pitch.to_string())
    }

    fn get_key_for_note(&self, note: u8, mode: &MappingMode) -> Option<Key> {
        let key_str = match mode {
            MappingMode::Notes => self.note_mappings.get(&note).cloned(),
            MappingMode::Octaves => {
                let (octave, pitch) = Self::note_to_pitch(note);
                self.octaves
                    .get(&octave.to_string())
                    .and_then(|octave_map| octave_map.get(&pitch).cloned())
            }
        };

        key_str.and_then(|key_str| match key_str.as_str() {
            "," => Some(Key::Unicode(',')),
            "." => Some(Key::Unicode('.')),
            "/" => Some(Key::Unicode('/')),
            ";" => Some(Key::Unicode(';')),
            "'" => Some(Key::Unicode('\'')),
            "[" => Some(Key::Unicode('[')),
            "]" => Some(Key::Unicode(']')),
            "\\" => Some(Key::Unicode('\\')),
            "-" => Some(Key::Unicode('-')),
            "=" => Some(Key::Unicode('=')),
            "Space" => Some(Key::Space),
            "Left" => Some(Key::LeftArrow),
            "Right" => Some(Key::RightArrow),
            "A" => Some(Key::A),
            "B" => Some(Key::B),
            "C" => Some(Key::C),
            "D" => Some(Key::D),
            "E" => Some(Key::E),
            "F" => Some(Key::F),
            "G" => Some(Key::G),
            "H" => Some(Key::H),
            "I" => Some(Key::I),
            "J" => Some(Key::J),
            "K" => Some(Key::K),
            "L" => Some(Key::L),
            "M" => Some(Key::M),
            "N" => Some(Key::N),
            "O" => Some(Key::O),
            "P" => Some(Key::P),
            "Q" => Some(Key::Q),
            "R" => Some(Key::R),
            "S" => Some(Key::S),
            "T" => Some(Key::T),
            "U" => Some(Key::U),
            "V" => Some(Key::V),
            "W" => Some(Key::W),
            "X" => Some(Key::X),
            "Y" => Some(Key::Y),
            "Z" => Some(Key::Z),
            "RAlt" => Some(Key::Alt),
            _ => None,
        })
    }
}

#[derive(Debug)]
enum KeyEvent {
    NoteOn(u8, u8), // (note, velocity)
    NoteOff(u8),    // note
}

struct KeyboardMapper {
    config: Config,
    enigo: Arc<Mutex<Enigo>>,
    mode: MappingMode,
}

impl KeyboardMapper {
    fn new(config: Config, enigo: Arc<Mutex<Enigo>>, mode: MappingMode) -> Self {
        Self {
            config,
            enigo,
            mode,
        }
    }

    fn handle_event(&self, event: KeyEvent) -> Result<(), Box<dyn Error>> {
        if let Ok(mut enigo_guard) = self.enigo.lock() {
            match event {
                KeyEvent::NoteOn(note, velocity) => {
                    if velocity >= self.config.velocity_threshold {
                        if let Some(key) = self.config.get_key_for_note(note, &self.mode) {
                            enigo_guard.key(key, Press)?;
                        }
                    }
                }
                KeyEvent::NoteOff(note) => {
                    if let Some(key) = self.config.get_key_for_note(note, &self.mode) {
                        enigo_guard.key(key, Release)?;
                    }
                }
            }
        }
        Ok(())
    }
}

/// List available configuration files in the configs directory
fn list_configs() -> Result<Vec<String>, Box<dyn Error>> {
    let config_dir = "configs";
    let mut configs = Vec::new();

    if !Path::new(config_dir).exists() {
        println!("Configuration directory '{}' does not exist", config_dir);
        return Ok(configs);
    }

    for entry in fs::read_dir(config_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.extension().and_then(|s| s.to_str()) == Some("json") {
            if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
                configs.push(filename.to_string());
            }
        }
    }

    configs.sort();
    Ok(configs)
}

/// Display available configuration files
fn display_configs() -> Result<(), Box<dyn Error>> {
    println!("\nAvailable configuration files:");
    let configs = list_configs()?;

    if configs.is_empty() {
        println!("  No configuration files found in 'configs' directory");
        return Ok(());
    }

    for (i, config) in configs.iter().enumerate() {
        println!("  {}: {}", i, config);
    }

    Ok(())
}

/// List available MIDI input devices
fn display_devices() -> Result<(), Box<dyn Error>> {
    let midi_in = MidiInput::new("osynic-midi")?;
    let in_ports = midi_in.ports();

    println!("\nAvailable MIDI input devices:");
    if in_ports.is_empty() {
        println!("  No MIDI input devices found");
        return Ok(());
    }

    for (i, p) in in_ports.iter().enumerate() {
        println!("  {}: {}", i, midi_in.port_name(p)?);
    }

    Ok(())
}

/// Prompt user to select a configuration file
fn select_config() -> Result<String, Box<dyn Error>> {
    let configs = list_configs()?;
    if configs.is_empty() {
        return Err("No configuration files available".into());
    }

    println!();
    let selection = Select::new("Select configuration file:", configs.clone()).prompt()?;
    let selected = format!("configs/{}", selection);
    Ok(selected)
}

/// Prompt user to select a MIDI input device
fn select_device() -> Result<usize, Box<dyn Error>> {
    let midi_in = MidiInput::new("osynic-midi")?;
    let in_ports = midi_in.ports();

    if in_ports.is_empty() {
        return Err("No MIDI input devices found".into());
    }

    let mut device_names = Vec::new();
    for p in in_ports.iter() {
        device_names.push(midi_in.port_name(p)?);
    }

    println!();
    let selection = Select::new("Select MIDI input device:", device_names.clone()).prompt()?;

    let device_idx = device_names
        .iter()
        .position(|d| d == &selection)
        .ok_or("Device selection failed")?;

    Ok(device_idx)
}

/// Prompt user to select mapping mode
fn select_mode() -> Result<MappingMode, Box<dyn Error>> {
    let modes = vec![
        "Notes (individual note to key mapping)",
        "Octaves (octave-based mapping)",
    ];

    println!();
    let selection = Select::new("Select mapping mode:", modes).prompt()?;

    let mode = match selection {
        s if s.starts_with("Notes") => MappingMode::Notes,
        s if s.starts_with("Octaves") => MappingMode::Octaves,
        _ => MappingMode::Notes,
    };

    Ok(mode)
}

/// Start the MIDI to keyboard mapping
#[tokio::main]
async fn main() {
    let args = Cli::parse();

    let result = match args.command {
        Some(Commands::ListDevices) => display_devices(),
        Some(Commands::ListConfigs) => display_configs(),
        Some(Commands::Start { config, mode }) => start_mapping(config, mode).await,
        None => {
            // Default: start interactive setup
            start_mapping(args.config, args.mode).await
        }
    };

    if let Err(e) = result {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}

async fn start_mapping(
    config_path: Option<String>,
    mode_arg: Option<String>,
) -> Result<(), Box<dyn Error>> {
    // Select configuration file
    let config_path = match config_path {
        Some(path) => path,
        None => select_config()?,
    };

    // Load configuration
    println!("\nLoading configuration from: {}", config_path);
    let mut config = Config::load(&config_path)?;

    // Determine mapping mode
    let mode = match mode_arg {
        Some(m) => match m.to_lowercase().as_str() {
            "octaves" => MappingMode::Octaves,
            "notes" => MappingMode::Notes,
            _ => select_mode()?,
        },
        None => config
            .mapping_mode
            .take()
            .unwrap_or_else(|| select_mode().unwrap_or(MappingMode::Notes)),
    };

    println!("Using mapping mode: {:?}", mode);

    // Initialize Enigo
    let enigo = Arc::new(Mutex::new(Enigo::new(&Settings::default())?));
    let mapper = Arc::new(KeyboardMapper::new(config, Arc::clone(&enigo), mode));

    // Create channel for MIDI events
    let (tx, mut rx) = mpsc::channel::<KeyEvent>(32);

    // Select MIDI input device
    let device_idx = select_device()?;

    // Connect to MIDI device
    let midi_in = MidiInput::new("osynic-midi")?;
    let in_ports = midi_in.ports();
    let in_port = &in_ports[device_idx];
    let device_name = midi_in.port_name(in_port)?;

    println!("\nOpening MIDI connection: {}", device_name);

    let tx_clone = tx.clone();
    let _conn_in = midi_in.connect(
        in_port,
        "osynic-midi",
        move |_stamp, message, _| {
            if message.len() == 3 {
                let status = message[0];
                let note = message[1];
                let velocity = message[2];

                let event = if status == 0x90 && velocity > 0 {
                    Some(KeyEvent::NoteOn(note, velocity))
                } else if status == 0x80 || (status == 0x90 && velocity == 0) {
                    Some(KeyEvent::NoteOff(note))
                } else {
                    None
                };

                if let Some(event) = event {
                    let _ = tx_clone.try_send(event);
                }
            }
        },
        (),
    )?;

    // Create event handler task
    let event_handler = tokio::spawn({
        let mapper = Arc::clone(&mapper);
        async move {
            while let Some(event) = rx.recv().await {
                if let Err(e) = mapper.handle_event(event) {
                    eprintln!("Error handling event: {}", e);
                }
            }
        }
    });

    println!("\n✓ MIDI mapping is active!");
    println!("Press Ctrl+C to stop, or use the keyboard/MIDI device to interact.\n");

    // Wait indefinitely - user needs to press Ctrl+C to exit
    std::future::pending::<()>().await;

    println!("Shutting down...");
    drop(tx);
    event_handler.abort();

    Ok(())
}