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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use self::Letter::{Ab, Ash, Bb, Csh, Db, Dsh, Eb, Fsh, Gb, Gsh, A, B, C, D, E, F, G};
use crate::utils::modulo;
use num::PrimInt as Int;
use num::{FromPrimitive, ToPrimitive};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

pub const TOTAL_LETTERS: u8 = 12;

/// The letter representation for each step in the 12-tone, equal temperament, chromatic scale.
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Letter {
    C,
    Csh,
    Db,
    D,
    Dsh,
    Eb,
    E,
    F,
    Fsh,
    Gb,
    G,
    Gsh,
    Ab,
    A,
    Ash,
    Bb,
    B,
}

impl PartialOrd for Letter {
    fn partial_cmp(&self, other: &Letter) -> Option<Ordering> {
        let other_u8 = other.to_u8().unwrap();
        self.to_u8().unwrap().partial_cmp(&other_u8)
    }
}

impl Ord for Letter {
    fn cmp(&self, other: &Letter) -> Ordering {
        let other_u8 = other.to_u8().unwrap();
        self.to_u8().unwrap().cmp(&other_u8)
    }
}

impl PartialEq for Letter {
    fn eq(&self, other: &Letter) -> bool {
        self.to_u8().unwrap() == other.to_u8().unwrap()
    }
}

impl Eq for Letter {}

impl Letter {
    /// Returns whether or not the note would be a black key on a standard piano or keyboard.
    pub fn is_black_key(self) -> bool {
        use self::Letter::*;
        match self {
            Csh | Db | Dsh | Eb | Fsh | Gb | Gsh | Ab | Ash | Bb => true,
            C | D | E | F | G | A | B => false,
        }
    }
}

impl FromPrimitive for Letter {
    fn from_i64(n: i64) -> Option<Letter> {
        match modulo(n, 12) {
            0 => Some(C),
            1 => Some(Csh),
            2 => Some(D),
            3 => Some(Dsh),
            4 => Some(E),
            5 => Some(F),
            6 => Some(Fsh),
            7 => Some(G),
            8 => Some(Gsh),
            9 => Some(A),
            10 => Some(Ash),
            11 => Some(B),
            _ => None,
        }
    }
    fn from_u64(n: u64) -> Option<Letter> {
        match modulo(n, 12) {
            0 => Some(C),
            1 => Some(Csh),
            2 => Some(D),
            3 => Some(Dsh),
            4 => Some(E),
            5 => Some(F),
            6 => Some(Fsh),
            7 => Some(G),
            8 => Some(Gsh),
            9 => Some(A),
            10 => Some(Ash),
            11 => Some(B),
            _ => None,
        }
    }
}

impl ToPrimitive for Letter {
    fn to_i64(&self) -> Option<i64> {
        match *self {
            C => Some(0),
            Csh | Db => Some(1),
            D => Some(2),
            Dsh | Eb => Some(3),
            E => Some(4),
            F => Some(5),
            Fsh | Gb => Some(6),
            G => Some(7),
            Gsh | Ab => Some(8),
            A => Some(9),
            Ash | Bb => Some(10),
            B => Some(11),
        }
    }
    fn to_u64(&self) -> Option<u64> {
        match *self {
            C => Some(0),
            Csh | Db => Some(1),
            D => Some(2),
            Dsh | Eb => Some(3),
            E => Some(4),
            F => Some(5),
            Fsh | Gb => Some(6),
            G => Some(7),
            Gsh | Ab => Some(8),
            A => Some(9),
            Ash | Bb => Some(10),
            B => Some(11),
        }
    }
}

impl ::rand::Rand for Letter {
    fn rand<R: ::rand::Rng>(rng: &mut R) -> Letter {
        rng.gen_range(0, 12).to_letter()
    }
}

/// A trait to be implemented for all primitives for easy conversion to the Letter type.
pub trait ToLetter {
    /// Cast a primitive type to a Letter.
    fn to_letter(&self) -> Letter;
}

impl<T: Int> ToLetter for T {
    fn to_letter(&self) -> Letter {
        FromPrimitive::from_i64(self.to_i64().unwrap()).unwrap()
    }
}

impl<T: Int> ::std::ops::Add<T> for Letter {
    type Output = Letter;
    fn add(self, rhs: T) -> Letter {
        let semitones = modulo(rhs.to_i64().unwrap(), 12).to_i16().unwrap();
        FromPrimitive::from_i16(modulo(self.to_i16().unwrap() + semitones, 12)).unwrap()
    }
}

impl<T: Int> ::std::ops::Sub<T> for Letter {
    type Output = Letter;
    fn sub(self, rhs: T) -> Letter {
        let semitones = modulo(rhs.to_i64().unwrap(), 12).to_i16().unwrap();
        FromPrimitive::from_i16(modulo(self.to_i16().unwrap() - semitones, 12)).unwrap()
    }
}

impl ::std::ops::Add for Letter {
    type Output = Letter;
    fn add(self, rhs: Letter) -> Letter {
        self + rhs.to_u8().unwrap()
    }
}

impl ::std::ops::Sub for Letter {
    type Output = Letter;
    fn sub(self, rhs: Letter) -> Letter {
        self - rhs.to_i16().unwrap()
    }
}