pitch_detector/
core.rs

1use std::fmt;
2
3pub mod constants;
4pub mod fft_space;
5pub mod utils;
6
7#[cfg(any(test, feature = "test_utils"))]
8pub mod test_utils;
9
10#[derive(Debug, Clone, PartialEq, PartialOrd)]
11pub enum NoteName {
12    A,
13    ASharp,
14    B,
15    C,
16    CSharp,
17    D,
18    DSharp,
19    E,
20    F,
21    FSharp,
22    G,
23    GSharp,
24}
25
26impl From<&str> for NoteName {
27    fn from(s: &str) -> Self {
28        match s {
29            "A" => NoteName::A,
30            "A#" => NoteName::ASharp,
31            "B" => NoteName::B,
32            "C" => NoteName::C,
33            "C#" => NoteName::CSharp,
34            "D" => NoteName::D,
35            "D#" => NoteName::DSharp,
36            "E" => NoteName::E,
37            "F" => NoteName::F,
38            "F#" => NoteName::FSharp,
39            "G" => NoteName::G,
40            "G#" => NoteName::GSharp,
41            _ => panic!("Invalid pitch"),
42        }
43    }
44}
45
46impl fmt::Display for NoteName {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        match *self {
49            NoteName::A => write!(f, "A"),
50            NoteName::ASharp => write!(f, "A#"),
51            NoteName::B => write!(f, "B"),
52            NoteName::C => write!(f, "C"),
53            NoteName::CSharp => write!(f, "C#"),
54            NoteName::D => write!(f, "D"),
55            NoteName::DSharp => write!(f, "D#"),
56            NoteName::E => write!(f, "E"),
57            NoteName::F => write!(f, "F"),
58            NoteName::FSharp => write!(f, "F#"),
59            NoteName::G => write!(f, "G"),
60            NoteName::GSharp => write!(f, "G#"),
61        }
62    }
63}
64
65#[derive(Debug, Clone, PartialEq)]
66pub struct FftBin {
67    pub bin: usize,
68    pub magnitude: f64,
69}
70
71impl Default for FftBin {
72    fn default() -> Self {
73        Self {
74            bin: 0,
75            magnitude: 0.0,
76        }
77    }
78}
79
80impl PartialOrd for FftBin {
81    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
82        self.magnitude.partial_cmp(&other.magnitude)
83    }
84}
85
86#[derive(Debug, Clone, PartialEq, PartialOrd)]
87pub struct FftPoint {
88    pub x: f64,
89    pub y: f64,
90}
91
92impl Default for FftPoint {
93    fn default() -> Self {
94        Self { x: 0.0, y: 0.0 }
95    }
96}
97
98impl From<FftBin> for FftPoint {
99    fn from(bin: FftBin) -> Self {
100        Self {
101            x: bin.bin as f64,
102            y: bin.magnitude,
103        }
104    }
105}