Feat: clean the codebase as much as possible
This commit is contained in:
64
src/midi.rs
64
src/midi.rs
@@ -1,8 +1,6 @@
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
|
||||
use midir::{MidiInput, MidiOutput};
|
||||
|
||||
use crate::model::CcAccess;
|
||||
|
||||
pub const MAX_MIDI_OUTPUTS: usize = 4;
|
||||
@@ -22,12 +20,12 @@ impl CcMemory {
|
||||
Self(Arc::new(Mutex::new([[[0u8; 128]; 16]; MAX_MIDI_DEVICES])))
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
fn inner(&self) -> &CcMemoryInner {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Set a CC value (for testing)
|
||||
#[allow(dead_code)]
|
||||
#[allow(dead_code)] // used by integration tests
|
||||
pub fn set_cc(&self, device: usize, channel: usize, cc: usize, value: u8) {
|
||||
let mut mem = self.0.lock();
|
||||
mem[device.min(MAX_MIDI_DEVICES - 1)][channel.min(15)][cc.min(127)] = value;
|
||||
@@ -52,8 +50,9 @@ pub struct MidiDeviceInfo {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub fn list_midi_outputs() -> Vec<MidiDeviceInfo> {
|
||||
let Ok(midi_out) = MidiOutput::new("cagire-probe") else {
|
||||
let Ok(midi_out) = midir::MidiOutput::new("cagire-probe") else {
|
||||
return Vec::new();
|
||||
};
|
||||
midi_out
|
||||
@@ -68,8 +67,14 @@ pub fn list_midi_outputs() -> Vec<MidiDeviceInfo> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
pub fn list_midi_outputs() -> Vec<MidiDeviceInfo> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub fn list_midi_inputs() -> Vec<MidiDeviceInfo> {
|
||||
let Ok(midi_in) = MidiInput::new("cagire-probe") else {
|
||||
let Ok(midi_in) = midir::MidiInput::new("cagire-probe") else {
|
||||
return Vec::new();
|
||||
};
|
||||
midi_in
|
||||
@@ -84,8 +89,15 @@ pub fn list_midi_inputs() -> Vec<MidiDeviceInfo> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
pub fn list_midi_inputs() -> Vec<MidiDeviceInfo> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
pub struct MidiState {
|
||||
#[cfg(feature = "cli")]
|
||||
output_conns: [Option<midir::MidiOutputConnection>; MAX_MIDI_OUTPUTS],
|
||||
#[cfg(feature = "cli")]
|
||||
input_conns: [Option<midir::MidiInputConnection<(CcMemoryInner, usize)>>; MAX_MIDI_INPUTS],
|
||||
pub selected_outputs: [Option<usize>; MAX_MIDI_OUTPUTS],
|
||||
pub selected_inputs: [Option<usize>; MAX_MIDI_INPUTS],
|
||||
@@ -101,7 +113,9 @@ impl Default for MidiState {
|
||||
impl MidiState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
#[cfg(feature = "cli")]
|
||||
output_conns: [None, None, None, None],
|
||||
#[cfg(feature = "cli")]
|
||||
input_conns: [None, None, None, None],
|
||||
selected_outputs: [None; MAX_MIDI_OUTPUTS],
|
||||
selected_inputs: [None; MAX_MIDI_INPUTS],
|
||||
@@ -109,11 +123,13 @@ impl MidiState {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub fn connect_output(&mut self, slot: usize, port_index: usize) -> Result<(), String> {
|
||||
if slot >= MAX_MIDI_OUTPUTS {
|
||||
return Err("Invalid output slot".to_string());
|
||||
}
|
||||
let midi_out = MidiOutput::new(&format!("cagire-out-{slot}")).map_err(|e| e.to_string())?;
|
||||
let midi_out =
|
||||
midir::MidiOutput::new(&format!("cagire-out-{slot}")).map_err(|e| e.to_string())?;
|
||||
let ports = midi_out.ports();
|
||||
let port = ports.get(port_index).ok_or("MIDI output port not found")?;
|
||||
let conn = midi_out
|
||||
@@ -124,6 +140,12 @@ impl MidiState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
pub fn connect_output(&mut self, _slot: usize, _port_index: usize) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub fn disconnect_output(&mut self, slot: usize) {
|
||||
if slot < MAX_MIDI_OUTPUTS {
|
||||
self.output_conns[slot] = None;
|
||||
@@ -131,11 +153,20 @@ impl MidiState {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
pub fn disconnect_output(&mut self, slot: usize) {
|
||||
if slot < MAX_MIDI_OUTPUTS {
|
||||
self.selected_outputs[slot] = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub fn connect_input(&mut self, slot: usize, port_index: usize) -> Result<(), String> {
|
||||
if slot >= MAX_MIDI_INPUTS {
|
||||
return Err("Invalid input slot".to_string());
|
||||
}
|
||||
let midi_in = MidiInput::new(&format!("cagire-in-{slot}")).map_err(|e| e.to_string())?;
|
||||
let midi_in =
|
||||
midir::MidiInput::new(&format!("cagire-in-{slot}")).map_err(|e| e.to_string())?;
|
||||
let ports = midi_in.ports();
|
||||
let port = ports.get(port_index).ok_or("MIDI input port not found")?;
|
||||
|
||||
@@ -165,6 +196,12 @@ impl MidiState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
pub fn connect_input(&mut self, _slot: usize, _port_index: usize) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub fn disconnect_input(&mut self, slot: usize) {
|
||||
if slot < MAX_MIDI_INPUTS {
|
||||
self.input_conns[slot] = None;
|
||||
@@ -172,6 +209,14 @@ impl MidiState {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
pub fn disconnect_input(&mut self, slot: usize) {
|
||||
if slot < MAX_MIDI_INPUTS {
|
||||
self.selected_inputs[slot] = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
fn send_message(&mut self, device: u8, message: &[u8]) {
|
||||
let slot = (device as usize).min(MAX_MIDI_OUTPUTS - 1);
|
||||
if let Some(conn) = &mut self.output_conns[slot] {
|
||||
@@ -179,6 +224,9 @@ impl MidiState {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cli"))]
|
||||
fn send_message(&mut self, _device: u8, _message: &[u8]) {}
|
||||
|
||||
pub fn send_note_on(&mut self, device: u8, channel: u8, note: u8, velocity: u8) {
|
||||
let status = 0x90 | (channel & 0x0F);
|
||||
self.send_message(device, &[status, note & 0x7F, velocity & 0x7F]);
|
||||
|
||||
Reference in New Issue
Block a user