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
90
91
92
93
94
95
//! Key signatures
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

use crate::Step;
use crate::Tpc;

/// Keys represent a number of fixed sharps or flats.
#[derive(Clone, Copy, Debug, PartialEq, FromPrimitive)]
#[must_use]
#[allow(missing_docs)]
pub enum Key {
    Cb = -7,
    Gb,
    Db,
    Ab,
    Eb,
    Bb,
    F,
    C, // == 0
    G,
    D,
    A,
    E,
    B,
    Fs,
    Cs,
}

impl Default for Key {
    fn default() -> Self {
        Self::C
    }
}

impl Key {
    /// The flattest key is C flat with seven flats
    pub const MIN: Key = Key::Cb;

    /// The sharpest key is C sharp with seven sharps
    pub const MAX: Key = Key::Cs;

    /// The number of different keys
    pub const NUM_OF: isize = Self::MAX as isize - Self::MIN as isize + 1;

    /// Steps along the line of fifths to end up at an enharmonic key.
    pub const DELTA_ENHARMONIC: isize = 12;

    /// The root of the key's major scale
    pub fn root_step(self) -> Step {
        match (self as i8).rem_euclid(7) {
            0 => Step::C,
            1 => Step::G,
            2 => Step::D,
            3 => Step::A,
            4 => Step::E,
            5 => Step::B,
            _ => Step::F,
        }
    }

    /// The root of this key's major scale
    /// ```
    /// # use tonality::{Key, Tpc};
    /// assert_eq!(Tpc::G, Key::G.root());
    /// ```
    pub fn root(self) -> Tpc {
        FromPrimitive::from_i8(self as i8).unwrap()
    }

    /// Zero-indexed scale degrees: 0 is root, 4 is fifth
    pub fn scale_degree(self, degree: isize) -> Tpc {
        /// Each scale degree's distance from the root, in fifths
        const OFFSETS: [i8; 7] = [0, 2, 4, -1, 1, 3, 5];
        let value = self as i8 + OFFSETS[degree.rem_euclid(7) as usize];
        FromPrimitive::from_i8(value).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_root() {
        assert_eq!(Tpc::Cs, Key::Cs.root());
        assert_eq!(Tpc::C, Key::C.root());
    }

    #[test]
    fn test_scale_step() {
        assert_eq!(Tpc::Bb, Key::Bb.scale_degree(0));
        assert_eq!(Tpc::Es, Key::Cs.scale_degree(2));
    }
}