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
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use crate::Step;
use crate::Tpc;
#[derive(Clone, Copy, Debug, PartialEq, FromPrimitive)]
#[must_use]
#[allow(missing_docs)]
pub enum Key {
Cb = -7,
Gb,
Db,
Ab,
Eb,
Bb,
F,
C,
G,
D,
A,
E,
B,
Fs,
Cs,
}
impl Default for Key {
fn default() -> Self {
Self::C
}
}
impl Key {
pub const MIN: Key = Key::Cb;
pub const MAX: Key = Key::Cs;
pub const NUM_OF: isize = Self::MAX as isize - Self::MIN as isize + 1;
pub const DELTA_ENHARMONIC: isize = 12;
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,
}
}
pub fn root(self) -> Tpc {
FromPrimitive::from_i8(self as i8).unwrap()
}
pub fn scale_degree(self, degree: isize) -> Tpc {
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));
}
}