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
206
207
208
209
210
211
use crate::{Mode, Note};
use arrayvec::ArrayVec;
use std::fmt;
use strum::IntoEnumIterator;
#[derive(Debug, Copy, Clone)]
pub struct Key {
notes: [Note; 7],
mode: Mode,
}
impl Key {
pub fn new(root_note: Note, mode: Mode) -> Self {
let mut notes = ArrayVec::<[Note; 7]>::new();
notes.push(root_note);
for (i, interval) in mode.into_iter().enumerate() {
let previous_note = notes[i];
notes.push(previous_note + *interval);
}
Self {
notes: notes.into_inner().unwrap(),
mode,
}
}
pub fn notes_disregarding_octave(mut self) -> [Note; 7] {
for note in &mut self.notes {
*note = note.disregard_octave();
}
self.notes
}
}
#[cfg(test)]
#[test]
fn construction() {
assert_eq!(
Key::new(Note::new(0), Mode::Ionian).notes,
[
Note::new(0),
Note::new(2),
Note::new(4),
Note::new(5),
Note::new(7),
Note::new(9),
Note::new(11)
]
);
assert_eq!(
Key::new(Note::new(3), Mode::Aeolian).notes,
[
Note::new(3),
Note::new(5),
Note::new(6),
Note::new(8),
Note::new(10),
Note::new(11),
Note::new(13)
]
);
}
#[cfg(test)]
#[test]
fn octave_disregard() {
assert_eq!(
Key::new(Note::new(5), Mode::Ionian).notes_disregarding_octave(),
[
Note::new(5),
Note::new(7),
Note::new(9),
Note::new(10),
Note::new(0),
Note::new(2),
Note::new(4)
]
);
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
let num_notes = self.notes.len();
for (i, note) in self.notes.iter().enumerate() {
write!(f, "{}", note)?;
if i != num_notes - 1 {
f.write_str(" ")?;
}
}
Ok(())
} else {
write!(f, "{} {}", self.notes[0], self.mode)
}
}
}
#[cfg(test)]
mod display_tests {
use super::*;
#[test]
fn normal() {
assert_eq!(Key::new(Note::new(2), Mode::Dorian).to_string(), "D Dorian");
assert_eq!(
Key::new(Note::new(8), Mode::Mixolydian).to_string(),
"Ab Mixolydian"
);
}
#[test]
fn alternate() {
assert_eq!(
format!("{:#}", Key::new(Note::new(2), Mode::Dorian)),
"D E F G A B C"
);
assert_eq!(
format!("{:#}", Key::new(Note::new(8), Mode::Mixolydian)),
"Ab Bb C D Eb F G"
);
}
}
pub fn guess_keys(notes: Vec<Note>, root_note: Option<Note>) -> Vec<Key> {
let key_filter = |root_note, key_candidates: &mut Vec<_>| {
for mode in Mode::iter() {
let key = Key::new(root_note, mode);
if notes
.iter()
.all(|note| key.notes_disregarding_octave().contains(note))
{
key_candidates.push(key);
}
}
};
let mut key_candidates = Vec::new();
if let Some(root_note) = root_note {
key_filter(root_note, &mut key_candidates);
} else {
for root_note in Note::new(0).into_iter().take(12) {
key_filter(root_note, &mut key_candidates);
}
}
key_candidates
}