use crate::midi::Note;
const CHORD_COUNT: u8 = 16;
const CHORDS: [(&str, &[u8]); CHORD_COUNT as usize] = [
("Maj", &[0, 4, 7]),
("Min", &[0, 3, 7]),
("Maj7", &[0, 4, 7, 11]),
("m7", &[0, 3, 7, 10]),
("7", &[0, 4, 7, 10]),
("Sus2", &[0, 2, 7]),
("Sus4", &[0, 5, 7]),
("Dim", &[0, 3, 6]),
("Aug", &[0, 4, 8]),
("6", &[0, 4, 7, 9]),
("m6", &[0, 3, 7, 9]),
("Dim7", &[0, 3, 6, 9]),
("m7b5", &[0, 3, 6, 10]),
("mMaj7", &[0, 3, 7, 11]),
("Add9", &[0, 4, 7, 14]),
("mAdd9", &[0, 3, 7, 14]),
];
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Chord(u8);
impl Chord {
pub const MAJOR: Self = Self(0);
#[must_use]
pub fn name(self) -> &'static str {
CHORDS[usize::from(self.0)].0
}
#[must_use]
pub fn turn(self, delta: i8) -> Self {
let i = i16::from(self.0).saturating_add(i16::from(delta));
Self(i.rem_euclid(i16::from(CHORD_COUNT)).to_le_bytes()[0])
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum VoiceMode {
#[default]
Poly,
Chord(Chord),
Unison,
}
#[derive(Clone, Copy, Debug)]
pub struct Keyboard {
pub mode: VoiceMode,
held: [Option<Note>; 4],
}
impl Keyboard {
pub const EMPTY: Self = Self {
mode: VoiceMode::Poly,
held: [None; 4],
};
pub fn press(&mut self, note: Note) {
let index = self
.held
.iter()
.position(|n| n.is_some_and(|n| n.number == note.number))
.unwrap_or(3);
self.held[..=index].rotate_right(1);
self.held[0] = Some(note);
}
pub fn release(&mut self, note: u8) -> bool {
let Some(index) = self
.held
.iter()
.position(|n| n.is_some_and(|n| n.number == note))
else {
return false;
};
self.held[index..].rotate_left(1);
self.held[3] = None;
index == 0
}
pub fn clear(&mut self) {
self.held.fill(None);
}
#[must_use]
pub fn voice(&self, slot: usize) -> Option<Note> {
let root = self.held[0]?;
match self.mode {
VoiceMode::Poly => self.held.get(slot).copied().flatten(),
VoiceMode::Unison => (slot < 4).then_some(root),
VoiceMode::Chord(chord) => {
let offset = *CHORDS[usize::from(chord.0)].1.get(slot)?;
let number = root.number.checked_add(offset)?;
(number < 128).then_some(Note { number, ..root })
}
}
}
#[must_use]
pub fn step(&self, slot: usize, base: u32) -> u32 {
if self.mode != VoiceMode::Unison {
return base;
}
let step = match slot {
0 => base.saturating_sub(base >> 8),
1 => base.saturating_sub(base >> 10),
2 => base.saturating_add(base >> 10),
_ => base.saturating_add(base >> 8),
};
if step < (1 << 31) { step } else { 0 }
}
}