-
Notifications
You must be signed in to change notification settings - Fork 30
/
scale.rs
118 lines (107 loc) · 3.59 KB
/
scale.rs
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
use crate::interval::Interval;
use crate::note::{Note, Notes, PitchClass};
use crate::scale::errors::ScaleError;
use crate::scale::{Mode, ScaleType};
use strum_macros::Display;
/// The direction of the scale; up or down.
#[derive(Display, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Ascending,
Descending,
}
/// A scale.
#[derive(Debug, Clone)]
pub struct Scale {
/// The root note of the scale.
pub tonic: PitchClass,
/// The octave of the root note of the scale.
pub octave: u8,
/// The type of scale (diatonic, melodic minor, harmonic minor).
pub scale_type: ScaleType,
/// The mode of the scale.
pub mode: Option<Mode>,
/// The list of intervals in the scale.
pub intervals: Vec<Interval>,
/// The direction of the scale, ascending or descending.
pub direction: Direction,
}
impl Scale {
/// Create a new scale with a given direction.
pub fn new(
scale_type: ScaleType,
tonic: PitchClass,
octave: u8,
mode: Option<Mode>,
direction: Direction,
) -> Result<Self, ScaleError> {
let intervals = match scale_type {
ScaleType::Diatonic => Interval::from_semitones(&[2, 2, 1, 2, 2, 2, 1]),
ScaleType::HarmonicMinor => Interval::from_semitones(&[2, 1, 2, 2, 1, 3, 1]),
ScaleType::MelodicMinor => Interval::from_semitones(&[2, 1, 2, 2, 2, 2, 1]),
}?;
Ok(Scale {
tonic,
octave,
scale_type,
mode,
intervals,
direction,
})
}
/// Parse a scale from a regex.
pub fn from_regex_in_direction(string: &str, direction: Direction) -> Result<Self, ScaleError> {
let (tonic, tonic_match) = PitchClass::from_regex(&string.trim())?;
let mode_string = &string[tonic_match.end()..].trim();
let (mode, _) = Mode::from_regex(mode_string)?;
let scale_type = ScaleType::from_mode(mode);
let octave = 4;
let scale = Scale::new(scale_type, tonic, octave, Some(mode), direction)?;
Ok(scale)
}
pub fn from_regex(string: &str) -> Result<Self, ScaleError> {
Self::from_regex_in_direction(string, Direction::Ascending)
}
}
impl Notes for Scale {
fn notes(&self) -> Vec<Note> {
use Direction::*;
use Mode::*;
let root_note = Note {
octave: self.octave,
pitch_class: self.tonic,
};
let mut intervals_clone = self.intervals.clone();
// shift the scale based on the mode
match &self.mode {
None => {}
Some(mode) => {
match mode {
Ionian => {}
Dorian => intervals_clone.rotate_left(1),
Phrygian => intervals_clone.rotate_left(2),
Lydian => intervals_clone.rotate_left(3),
Mixolydian => intervals_clone.rotate_left(4),
Aeolian => intervals_clone.rotate_right(2),
Locrian => intervals_clone.rotate_right(1),
_ => {}
};
}
};
match &self.direction {
Ascending => Interval::to_notes(root_note, intervals_clone),
Descending => Interval::to_notes_reverse(root_note, intervals_clone),
}
}
}
impl Default for Scale {
fn default() -> Self {
Scale {
tonic: PitchClass::C,
octave: 0,
scale_type: ScaleType::Diatonic,
mode: Some(Mode::Ionian),
intervals: vec![],
direction: Direction::Ascending,
}
}
}