summaryrefslogtreecommitdiff
path: root/src/pitch.rs
blob: 0944bbf2889d70f9a8d998663b7f13879cc6a3c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::fmt;
use std::f32;

#[derive(Debug, Clone, Copy)]
pub struct Pitch {
    pub hz: f32
}

impl Pitch {
    pub fn new(hz: f32) -> Pitch {
        Pitch {
            hz: hz
        }
    }
    
    fn midi_number(&self) -> f32 {
        69.0 + 12.0 * (self.hz / 440.0).log2()
    }

    pub fn cents_error(&self) -> f32 {
        if !self.hz.is_finite() {
            return f32::NAN;
        }
        
        let midi_number = self.midi_number();
        let cents = (midi_number - midi_number.floor()) * 100.0;
        if cents >= 50.0 {
            cents - 100.0
        }
        else {
            cents
        }
    }
}

impl fmt::Display for Pitch {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.hz <= 0.0 || !self.hz.is_finite() {
            write!(f, "")
        } else {
            let pitch_names = [
                "C",
                "C♯",
                "D",
                "E♭",
                "E",
                "F",
                "F♯",
                "G",
                "G♯",
                "A",
                "B♭",
                "B"
            ];

            //midi_number of 0 is C-1.
            let rounded_pitch = self.midi_number().round() as i32;
            let name = pitch_names[rounded_pitch as usize % pitch_names.len()];
            let octave = rounded_pitch / pitch_names.len() as i32 - 1;

            write!(f, "{: <2}{}", name, octave)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn a4_is_correct() {
        assert_eq!(format!("{}", Pitch::new(440.0)), "A 4");
    }

    #[test]
    fn a2_is_correct() {
        assert_eq!(format!("{}", Pitch::new(110.0)), "A 2");
    }

    #[test]
    fn c4_is_correct() {
        assert_eq!(format!("{}", Pitch::new(261.63)), "C 4");
    }

    #[test]
    fn f5_is_correct() {
        assert_eq!(format!("{}", Pitch::new(698.46)), "F 5");
    }
}