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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::cmp::Ordering;
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use super::{
calc,
DEFAULT_SCALE_WEIGHT,
Letter,
Hz,
Mel,
Perc,
ScaledPerc,
ScaleWeight,
Step,
hz_from_letter_octave,
mel_from_letter_octave,
perc_from_letter_octave,
scaled_perc_from_letter_octave,
step_from_letter_octave,
};
pub type Octave = i32;
#[derive(Debug, Copy, Clone, Hash)]
pub struct LetterOctave(pub Letter, pub Octave);
impl LetterOctave {
#[inline]
pub fn letter_octave(&self) -> (Letter, Octave) {
let LetterOctave(letter, octave) = *self;
(letter, octave)
}
#[inline]
pub fn letter(&self) -> Letter {
let LetterOctave(letter, _) = *self;
letter
}
#[inline]
pub fn octave(&self) -> Octave {
let LetterOctave(_, octave) = *self;
octave
}
#[inline]
pub fn hz(&self) -> calc::Hz {
let LetterOctave(letter, octave) = *self;
hz_from_letter_octave(letter, octave)
}
#[inline]
pub fn to_hz(&self) -> Hz {
Hz(self.hz())
}
#[inline]
pub fn mel(&self) -> calc::Mel {
let LetterOctave(letter, octave) = *self;
mel_from_letter_octave(letter, octave)
}
#[inline]
pub fn to_mel(&self) -> Mel {
Mel(self.mel())
}
#[inline]
pub fn perc(&self) -> calc::Perc {
let LetterOctave(letter, octave) = *self;
perc_from_letter_octave(letter, octave)
}
#[inline]
pub fn to_perc(&self) -> Perc {
Perc(self.perc())
}
#[inline]
pub fn scaled_perc_with_weight(&self, weight: ScaleWeight) -> calc::Perc {
let LetterOctave(letter, octave) = *self;
scaled_perc_from_letter_octave(letter, octave, weight)
}
#[inline]
pub fn scaled_perc(&self) -> calc::Perc {
self.scaled_perc_with_weight(DEFAULT_SCALE_WEIGHT)
}
#[inline]
pub fn to_scaled_perc_with_weight(&self, weight: ScaleWeight) -> ScaledPerc {
ScaledPerc(self.scaled_perc_with_weight(weight), weight)
}
#[inline]
pub fn to_scaled_perc(&self) -> ScaledPerc {
self.to_scaled_perc_with_weight(DEFAULT_SCALE_WEIGHT)
}
#[inline]
pub fn step(&self) -> calc::Step {
let LetterOctave(letter, octave) = *self;
step_from_letter_octave(letter, octave)
}
#[inline]
pub fn to_step(&self) -> Step {
Step(self.step())
}
}
impl Add for LetterOctave {
type Output = LetterOctave;
#[inline]
fn add(self, rhs: LetterOctave) -> LetterOctave {
(self.to_step() + rhs.to_step()).to_letter_octave()
}
}
impl Sub for LetterOctave {
type Output = LetterOctave;
#[inline]
fn sub(self, rhs: LetterOctave) -> LetterOctave {
(self.to_step() - rhs.to_step()).to_letter_octave()
}
}
impl Mul for LetterOctave {
type Output = LetterOctave;
#[inline]
fn mul(self, rhs: LetterOctave) -> LetterOctave {
(self.to_step() * rhs.to_step()).to_letter_octave()
}
}
impl Div for LetterOctave {
type Output = LetterOctave;
#[inline]
fn div(self, rhs: LetterOctave) -> LetterOctave {
(self.to_step() / rhs.to_step()).to_letter_octave()
}
}
impl Rem for LetterOctave {
type Output = LetterOctave;
#[inline]
fn rem(self, rhs: LetterOctave) -> LetterOctave {
(self.to_step() % rhs.to_step()).to_letter_octave()
}
}
impl Neg for LetterOctave {
type Output = LetterOctave;
#[inline]
fn neg(self) -> LetterOctave {
(-self.to_step()).to_letter_octave()
}
}
impl PartialEq for LetterOctave {
#[inline]
fn eq(&self, other: &LetterOctave) -> bool {
let LetterOctave(letter, octave) = *self;
let LetterOctave(other_letter, other_octave) = *other;
letter == other_letter && octave == other_octave
}
}
impl Eq for LetterOctave {}
impl PartialOrd for LetterOctave {
#[inline]
fn partial_cmp(&self, other: &LetterOctave) -> Option<Ordering> {
let LetterOctave(letter, octave) = *self;
let LetterOctave(other_letter, other_octave) = *other;
match octave.partial_cmp(&other_octave) {
Some(Ordering::Equal) => letter.partial_cmp(&other_letter),
ordering => ordering,
}
}
}
impl Ord for LetterOctave {
#[inline]
fn cmp(&self, other: &LetterOctave) -> Ordering {
self.partial_cmp(other).unwrap()
}
}