use crate::Instrument;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Preset {
Sine,
Square,
Triangle,
Pluck,
Bass,
Pad,
Lead,
BassDrum,
Tom,
Snare,
HiHat,
PercussionKit,
}
const AVAILABLE: [Preset; 12] = [
Preset::Sine,
Preset::Square,
Preset::Triangle,
Preset::Pluck,
Preset::Bass,
Preset::Pad,
Preset::Lead,
Preset::BassDrum,
Preset::Tom,
Preset::Snare,
Preset::HiHat,
Preset::PercussionKit,
];
impl Preset {
#[must_use]
pub const fn available() -> &'static [Self] {
&AVAILABLE
}
#[must_use]
pub const fn id(self) -> &'static str {
match self {
Self::Sine => "sine",
Self::Square => "square",
Self::Triangle => "triangle",
Self::Pluck => "pluck",
Self::Bass => "bass",
Self::Pad => "pad",
Self::Lead => "lead",
Self::BassDrum => "bass-drum",
Self::Tom => "tom",
Self::Snare => "snare",
Self::HiHat => "hi-hat",
Self::PercussionKit => "percussion-kit",
}
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Sine => "Sine",
Self::Square => "Square",
Self::Triangle => "Triangle",
Self::Pluck => "Pluck",
Self::Bass => "Bass",
Self::Pad => "Pad",
Self::Lead => "Lead",
Self::BassDrum => "Bass Drum",
Self::Tom => "Tom",
Self::Snare => "Snare",
Self::HiHat => "Hi-Hat",
Self::PercussionKit => "Percussion Kit",
}
}
#[must_use]
pub fn from_id(id: &str) -> Option<Self> {
Self::available()
.iter()
.copied()
.find(|preset| preset.id() == id)
}
#[must_use]
pub const fn instrument(self, midi_note: u8) -> Instrument {
match self {
Self::Sine => Instrument::Sine,
Self::Square => Instrument::Square,
Self::Triangle => Instrument::Triangle,
Self::Pluck => Instrument::Pluck,
Self::Bass => Instrument::Bass,
Self::Pad => Instrument::Pad,
Self::Lead => Instrument::Lead,
Self::BassDrum => Instrument::BassDrum,
Self::Tom => Instrument::Tom,
Self::Snare => Instrument::Snare,
Self::HiHat => Instrument::HiHat,
Self::PercussionKit => percussion_instrument(midi_note),
}
}
pub(crate) const fn uses_midi_pitch(self) -> bool {
matches!(
self,
Self::Sine
| Self::Square
| Self::Triangle
| Self::Pluck
| Self::Bass
| Self::Pad
| Self::Lead
)
}
}
const fn percussion_instrument(note: u8) -> Instrument {
match note {
0..=37 => Instrument::BassDrum,
38..=40 => Instrument::Snare,
42 | 44 | 46 => Instrument::HiHat,
41 | 43 | 45 | 47..=u8::MAX => Instrument::Tom,
}
}
#[cfg(test)]
mod tests {
use super::Preset;
use crate::Instrument;
#[test]
fn runtime_catalog_has_stable_unique_ids() {
let presets = Preset::available();
assert_eq!(presets.len(), 12);
for (index, preset) in presets.iter().copied().enumerate() {
assert_eq!(Preset::from_id(preset.id()), Some(preset));
assert!(!preset.name().is_empty());
assert!(
presets[..index]
.iter()
.all(|other| other.id() != preset.id())
);
}
assert_eq!(Preset::from_id("future-preset"), None);
}
#[test]
fn percussion_kit_uses_general_midi_keys_and_fills_gaps() {
let kit = Preset::PercussionKit;
assert_eq!(kit.instrument(35), Instrument::BassDrum);
assert_eq!(kit.instrument(36), Instrument::BassDrum);
assert_eq!(kit.instrument(37), Instrument::BassDrum);
assert_eq!(kit.instrument(38), Instrument::Snare);
assert_eq!(kit.instrument(39), Instrument::Snare);
assert_eq!(kit.instrument(40), Instrument::Snare);
assert_eq!(kit.instrument(41), Instrument::Tom);
assert_eq!(kit.instrument(42), Instrument::HiHat);
assert_eq!(kit.instrument(43), Instrument::Tom);
assert_eq!(kit.instrument(44), Instrument::HiHat);
assert_eq!(kit.instrument(45), Instrument::Tom);
assert_eq!(kit.instrument(46), Instrument::HiHat);
assert_eq!(kit.instrument(50), Instrument::Tom);
assert_eq!(kit.instrument(127), Instrument::Tom);
}
}