#[cfg(feature = "serde")]
use super::short;
use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Category {
Lead,
Pluck,
Pad,
Chord,
Bell,
Keys,
Vocal,
Strings,
Guitar,
Bass,
Kick,
Snare,
Hat,
Crash,
Tom,
Fx,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Metric {
LoudDb,
PeakDb,
AttackMs,
Sustain,
TailS,
Low,
High,
Harm,
MonoLossDb,
Width,
}
impl Metric {
pub const fn name(self) -> &'static str {
match self {
Metric::LoudDb => "loud_db",
Metric::PeakDb => "peak_db",
Metric::AttackMs => "attack_ms",
Metric::Sustain => "sustain",
Metric::TailS => "tail_s",
Metric::Low => "low",
Metric::High => "high",
Metric::Harm => "harm",
Metric::MonoLossDb => "mono_loss_db",
Metric::Width => "width",
}
}
pub fn value(self, m: &ToneMetrics) -> Option<f32> {
Some(match self {
Metric::LoudDb => m.loud_db,
Metric::PeakDb => m.peak_db,
Metric::AttackMs => m.attack_ms as f32,
Metric::Sustain => m.sustain,
Metric::TailS => m.tail_s,
Metric::Low => m.low,
Metric::High => m.high,
Metric::Harm => return m.harm,
Metric::MonoLossDb => m.mono_loss_db,
Metric::Width => m.width,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Rule {
pub metric: Metric,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Option::is_none", serialize_with = "short::opt")
)]
pub min: Option<f32>,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Option::is_none", serialize_with = "short::opt")
)]
pub max: Option<f32>,
}
const fn rule(metric: Metric, min: Option<f32>, max: Option<f32>) -> Rule {
Rule { metric, min, max }
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Deviation {
pub metric: Metric,
#[cfg_attr(feature = "serde", serde(serialize_with = "short::one"))]
pub value: f32,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Option::is_none", serialize_with = "short::opt")
)]
pub min: Option<f32>,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Option::is_none", serialize_with = "short::opt")
)]
pub max: Option<f32>,
}
pub const ALL: [Category; 16] = [
Category::Lead,
Category::Pluck,
Category::Pad,
Category::Chord,
Category::Bell,
Category::Keys,
Category::Vocal,
Category::Strings,
Category::Guitar,
Category::Bass,
Category::Kick,
Category::Snare,
Category::Hat,
Category::Crash,
Category::Tom,
Category::Fx,
];
const PEAK: Rule = rule(Metric::PeakDb, None, Some(-1.0));
impl Category {
pub const fn name(self) -> &'static str {
match self {
Category::Lead => "lead",
Category::Pluck => "pluck",
Category::Pad => "pad",
Category::Chord => "chord",
Category::Bell => "bell",
Category::Keys => "keys",
Category::Vocal => "vocal",
Category::Strings => "strings",
Category::Guitar => "guitar",
Category::Bass => "bass",
Category::Kick => "kick",
Category::Snare => "snare",
Category::Hat => "hat",
Category::Crash => "crash",
Category::Tom => "tom",
Category::Fx => "fx",
}
}
pub fn from_name(name: &str) -> Option<Self> {
ALL.into_iter().find(|c| c.name() == name)
}
pub const fn phrase(self) -> &'static str {
match self {
Category::Lead | Category::Vocal => "o5 l8 c d e g a4 g4 e2",
Category::Pluck => "o5 l16 c e g > c < e g > c e < c4",
Category::Pad => "o4 'c e g'1",
Category::Chord => "o4 l8 'c e g' r 'c e g' 'c e g' r 'c e g' r 'c e g'",
Category::Strings => "o4 l8 c d e g a4 g4 e2",
Category::Guitar => "o3 l8 e e g e a4 g4 e2",
Category::Bell => "o5 l8 c e g > c < g e c4",
Category::Keys => "o4 l4 'c e g' 'f a > c' l8 > c e c4",
Category::Bass => "o2 l8 c c g c d f g c4",
Category::Kick => "o1 l4 a a a a",
Category::Snare => "o1 l4 r a r a",
Category::Hat => "o1 l8 a a a a l16 a a a a a a a a",
Category::Crash => "o1 a16",
Category::Tom => "o1 l16 a a a a l8 a a a4",
Category::Fx => "o4 l1 c c c c",
}
}
pub const fn fundamental_hz(self) -> Option<f64> {
match self {
Category::Lead | Category::Vocal => Some(659.255), Category::Strings => Some(329.628), Category::Guitar => Some(164.814), Category::Pluck | Category::Bell | Category::Keys => Some(523.251), Category::Pad | Category::Chord | Category::Fx => Some(261.626), Category::Bass => Some(65.406), Category::Kick | Category::Snare | Category::Hat | Category::Crash | Category::Tom => {
None
}
}
}
pub fn rules(self) -> Vec<Rule> {
use Metric::*;
let loud = |c: f32, w: f32| rule(LoudDb, Some(c - w), Some(c + w));
let mut r = match self {
Category::Lead => vec![
loud(-15.0, 3.0),
rule(AttackMs, None, Some(400.0)),
rule(Sustain, Some(0.4), None),
rule(TailS, Some(0.5), Some(1.5)),
rule(Harm, Some(1.4), None),
],
Category::Pluck => vec![
loud(-18.0, 3.0),
rule(AttackMs, None, Some(20.0)),
rule(Sustain, None, Some(0.2)),
rule(TailS, None, Some(0.8)),
rule(MonoLossDb, Some(-1.5), None),
],
Category::Pad => vec![
loud(-18.0, 3.0),
rule(AttackMs, Some(200.0), None),
rule(Sustain, Some(0.4), None),
rule(TailS, Some(0.8), None),
rule(MonoLossDb, Some(-1.5), None),
],
Category::Chord => vec![loud(-18.0, 3.0), rule(MonoLossDb, Some(-1.5), None)],
Category::Bell => vec![
loud(-16.0, 3.0),
rule(AttackMs, None, Some(20.0)),
rule(Sustain, None, Some(0.3)),
],
Category::Keys => vec![loud(-16.0, 3.0), rule(AttackMs, None, Some(30.0))],
Category::Vocal => vec![loud(-15.0, 3.0)],
Category::Strings => vec![
loud(-15.0, 3.0),
rule(Sustain, Some(0.3), None),
rule(TailS, Some(0.3), Some(1.5)),
],
Category::Guitar => vec![loud(-15.0, 3.0)],
Category::Bass => vec![
loud(-10.0, 3.0),
rule(TailS, None, Some(0.3)),
rule(Low, Some(0.9), None),
rule(Width, None, Some(0.05)),
],
Category::Kick => vec![
loud(-12.0, 3.0),
rule(AttackMs, None, Some(30.0)),
rule(Low, Some(0.5), None),
],
Category::Snare => vec![loud(-18.0, 3.0), rule(AttackMs, None, Some(30.0))],
Category::Hat => vec![
loud(-24.0, 3.0),
rule(AttackMs, None, Some(10.0)),
rule(High, Some(0.8), None),
],
Category::Crash => vec![
loud(-24.0, 3.0),
rule(AttackMs, None, Some(10.0)),
rule(TailS, Some(0.8), None),
rule(High, Some(0.8), None),
],
Category::Tom => vec![loud(-16.0, 3.0), rule(AttackMs, None, Some(30.0))],
Category::Fx => vec![loud(-20.0, 6.0)],
};
r.push(PEAK);
r
}
pub fn target_loud_db(self) -> f32 {
self.rules()
.iter()
.find(|r| r.metric == Metric::LoudDb)
.and_then(|r| Some((r.min? + r.max?) / 2.0))
.unwrap_or(-18.0)
}
pub fn check(self, m: &ToneMetrics) -> Vec<Deviation> {
self.rules()
.into_iter()
.filter_map(|r| {
let v = r.metric.value(m)?;
let out = r.min.is_some_and(|lo| v < lo) || r.max.is_some_and(|hi| v > hi);
out.then_some(Deviation {
metric: r.metric,
value: v,
min: r.min,
max: r.max,
})
})
.collect()
}
}