pub const MIDI_INPUT_PORT_PREFIX: &'static str = "nanoKONTROL2 SLIDER/KNOB";
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum TrackButton {
Left,
Right,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum MarkerButton {
Set,
Left,
Right,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Transport {
Rewind,
Fastforward,
Stop,
Play,
Record,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum ButtonRow {
Solo,
Mute,
Record,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum State {
On,
Off
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Strip { A, B, C, D, E, F, G, H }
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Event {
RotarySlider(Strip, u8),
VerticalSlider(Strip, u8),
Button(ButtonRow, Strip, State),
TrackButton(TrackButton, State),
CycleButton(State),
MarkerButton(MarkerButton, State),
TransportButton(Transport, State),
}
impl Strip {
fn from_u8(n: u8) -> Option<Self> {
match n {
0 => Some(Strip::A),
1 => Some(Strip::B),
2 => Some(Strip::C),
3 => Some(Strip::D),
4 => Some(Strip::E),
5 => Some(Strip::F),
6 => Some(Strip::G),
7 => Some(Strip::H),
_ => None,
}
}
}
impl Event {
pub fn from_midi(msg: &[u8]) -> Option<Self> {
match msg.len() {
3 => match (msg[0], msg[1], msg[2]) {
(176, n @ 16..=23, value) => {
let strip = Strip::from_u8(n - 16).unwrap();
Some(Event::RotarySlider(strip, value).into())
},
(176, n @ 0..=7, value) => {
let strip = Strip::from_u8(n).unwrap();
Some(Event::VerticalSlider(strip, value).into())
},
(176, n @ 58..=59, state) => {
let button = match n {
58 => TrackButton::Left,
59 => TrackButton::Right,
_ => unreachable!(),
};
let state = if state == 0 { State::Off } else { State::On };
Some(Event::TrackButton(button, state).into())
},
(176, 99, state) => {
let state = if state == 0 { State::Off } else { State::On };
Some(Event::CycleButton(state).into())
},
(176, n @ 60..=62, state) => {
let button = match n {
60 => MarkerButton::Set,
61 => MarkerButton::Left,
62 => MarkerButton::Right,
_ => unreachable!(),
};
let state = if state == 0 { State::Off } else { State::On };
Some(Event::MarkerButton(button, state).into())
},
(176, n @ 44..=47, state) => {
let transport = match n {
44 => {
if state == 0 { Transport::Rewind } else { Transport::Fastforward }
},
45 => Transport::Stop,
46 => Transport::Play,
47 => Transport::Record,
_ => unreachable!(),
};
let state = if state == 0 { State::Off } else { State::On };
Some(Event::TransportButton(transport, state).into())
},
(176, n @ 32..=39, state) => {
let strip = Strip::from_u8(n - 32).unwrap();
let state = if state == 0 { State::Off } else { State::On };
Some(Event::Button(ButtonRow::Solo, strip, state).into())
},
(176, n @ 48..=55, state) => {
let strip = Strip::from_u8(n - 48).unwrap();
let state = if state == 0 { State::Off } else { State::On };
Some(Event::Button(ButtonRow::Mute, strip, state).into())
},
(176, n @ 64..=71, state) => {
let strip = Strip::from_u8(n - 64).unwrap();
let state = if state == 0 { State::Off } else { State::On };
Some(Event::Button(ButtonRow::Record, strip, state).into())
},
_ => None,
},
_ => None,
}
}
}