Skip to content

Commit 90dcad8

Browse files
committed
update
Signed-off-by: Joe McCain III <jo3mccain@icloud.com>
1 parent de5ce2a commit 90dcad8

File tree

19 files changed

+348
-182
lines changed

19 files changed

+348
-182
lines changed

core/src/intervals/interval.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
Appellation: interval <module>
33
Contrib: FL03 <jo3mccain@icloud.com>
44
*/
5+
use super::IntervalLevel;
56

67
pub struct Interval<Q> {
78
pub level: IntervalLevel,
89
pub quality: Option<Q>,
910
pub value: i8,
1011
}
1112

12-
pub enum IntervalLevel {
13-
Semitone,
14-
Tone,
15-
Third,
16-
Fourth,
17-
Fifth,
18-
Sixth,
19-
Seventh,
20-
Octave,
21-
}
2213

23-
pub trait IntervalQuality {
24-
fn level(&self) -> IntervalLevel;
14+
15+
pub trait Quality<T> {
16+
type Group;
17+
18+
fn group(&self) -> Self::Group;
2519

2620
fn name(&self) -> &str;
2721

28-
fn value(&self) -> i8;
22+
fn value(&self) -> T;
23+
}
24+
25+
pub struct Variant<K, V> {
26+
pub group: K,
27+
pub name: &'static str,
28+
pub value: V,
2929
}

core/src/intervals/kinds.rs

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Appellation: kinds <module>
33
Contrib: FL03 <jo3mccain@icloud.com>
44
*/
5+
#[doc(inline)]
6+
pub use self::variants::*;
7+
8+
pub(crate) mod variants;
9+
510
use crate::{IntoPitch, Pitch};
611

712
/// [Intervals] enumerates the various intervals used within music theory.
@@ -121,7 +126,7 @@ impl Intervals {
121126
}
122127
}
123128

124-
macro_rules! new_interval {
129+
macro_rules! impl_new_interval {
125130
(@impl $name:ident::$variant:ident.$call:ident($($T:ident)::*)) => {
126131
pub fn $call() -> Self {
127132
$name::$variant($($T)::*)
@@ -136,7 +141,7 @@ macro_rules! new_interval {
136141

137142
($name:ident {$($variant:ident.$call:ident$(($($T:ident)::*))?),* $(,)?}) => {
138143
impl $name {
139-
$(new_interval!(@impl $name::$variant.$call$(($($T)::*))?);)*
144+
$(impl_new_interval!(@impl $name::$variant.$call$(($($T)::*))?);)*
140145
}
141146
};
142147
}
@@ -156,7 +161,7 @@ macro_rules! impl_from_value {
156161
};
157162
}
158163

159-
new_interval! {
164+
impl_new_interval! {
160165
Intervals {
161166
Thirds.minor_third(Third::Minor),
162167
Thirds.major_third(Third::Major),
@@ -188,61 +193,3 @@ impl_from_value! {
188193
Sevenths(Seventh),
189194
}
190195
}
191-
192-
interval! {
193-
default: Major;
194-
pub enum Third {
195-
Minor = 3,
196-
Major = 4,
197-
}
198-
}
199-
200-
interval! {
201-
default: Perfect;
202-
pub enum Fourth {
203-
Perfect = 5,
204-
}
205-
}
206-
207-
interval! {
208-
default: Perfect;
209-
pub enum Fifth {
210-
Diminished = 6,
211-
Perfect = 7,
212-
Augmented = 8,
213-
}
214-
}
215-
216-
interval! {
217-
default: Diminished;
218-
pub enum Seventh {
219-
Diminished = 9,
220-
Minor = 10,
221-
Major = 11,
222-
Augmented = 12,
223-
}
224-
}
225-
226-
impl Fifth {
227-
pub fn augmented() -> Self {
228-
Fifth::Augmented
229-
}
230-
231-
pub fn diminished() -> Self {
232-
Fifth::Diminished
233-
}
234-
235-
pub fn perfect() -> Self {
236-
Fifth::Perfect
237-
}
238-
239-
pub fn from_thirds(lhs: Third, rhs: Third) -> Self {
240-
let value = lhs as i8 + rhs as i8;
241-
match value {
242-
6 => Fifth::Diminished,
243-
7 => Fifth::Perfect,
244-
8 => Fifth::Augmented,
245-
_ => panic!("Invalid fifth value: {}", value),
246-
}
247-
}
248-
}

core/src/intervals/kinds/variants.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Appellation: variants <module>
3+
Contrib: FL03 <jo3mccain@icloud.com>
4+
*/
5+
6+
interval! {
7+
default: Major;
8+
pub enum Third {
9+
Minor = 3,
10+
Major = 4,
11+
}
12+
}
13+
14+
interval! {
15+
default: Perfect;
16+
pub enum Fourth {
17+
Perfect = 5,
18+
}
19+
}
20+
21+
interval! {
22+
default: Perfect;
23+
pub enum Fifth {
24+
Diminished = 6,
25+
Perfect = 7,
26+
Augmented = 8,
27+
}
28+
}
29+
30+
interval! {
31+
default: Diminished;
32+
pub enum Seventh {
33+
Diminished = 9,
34+
Minor = 10,
35+
Major = 11,
36+
Augmented = 12,
37+
}
38+
}
39+
40+
/*
41+
************* Implementations *************
42+
*/
43+
impl Third {
44+
pub fn minor() -> Self {
45+
Third::Minor
46+
}
47+
48+
pub fn major() -> Self {
49+
Third::Major
50+
}
51+
}
52+
53+
impl Fourth {
54+
pub fn perfect() -> Self {
55+
Fourth::Perfect
56+
}
57+
}
58+
59+
impl Fifth {
60+
pub fn augmented() -> Self {
61+
Fifth::Augmented
62+
}
63+
64+
pub fn diminished() -> Self {
65+
Fifth::Diminished
66+
}
67+
68+
pub fn perfect() -> Self {
69+
Fifth::Perfect
70+
}
71+
72+
pub fn from_thirds(lhs: Third, rhs: Third) -> Self {
73+
let value = lhs as i8 + rhs as i8;
74+
match value {
75+
6 => Fifth::Diminished,
76+
7 => Fifth::Perfect,
77+
8 => Fifth::Augmented,
78+
_ => panic!("Invalid fifth value: {}", value),
79+
}
80+
}
81+
}
82+
83+
impl Seventh {
84+
pub fn augmented() -> Self {
85+
Seventh::Augmented
86+
}
87+
88+
pub fn diminished() -> Self {
89+
Seventh::Diminished
90+
}
91+
92+
pub fn major() -> Self {
93+
Seventh::Major
94+
}
95+
96+
pub fn minor() -> Self {
97+
Seventh::Minor
98+
}
99+
}

core/src/intervals/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
Contrib: FL03 <jo3mccain@icloud.com>
44
*/
55
#[doc(inline)]
6-
pub use self::kinds::*;
6+
pub use self::{kinds::*, types::*};
77

88
pub(crate) mod kinds;
9+
pub(crate) mod types;
910

1011
#[doc(hidden)]
1112
pub mod interval;
12-
#[doc(hidden)]
13-
pub mod qualities;
1413

1514
pub(crate) mod prelude {
1615
pub use super::kinds::*;
16+
pub use super::types::IntervalLevel;
1717
pub use super::IntervalKind;
1818
}
1919

2020
pub(crate) type IntervalTy = i8;
2121

22-
pub trait IntervalT {
22+
pub trait Intervallic {
2323
private!();
2424

2525
fn as_i8(&self) -> i8;

core/src/intervals/types/level.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Appellation: level <module>
3+
Contrib: FL03 <jo3mccain@icloud.com>
4+
*/
5+
#[derive(
6+
Clone,
7+
Copy,
8+
Debug,
9+
Default,
10+
Eq,
11+
Hash,
12+
Ord,
13+
PartialEq,
14+
PartialOrd,
15+
strum::AsRefStr,
16+
strum::Display,
17+
strum::EnumCount,
18+
strum::EnumIs,
19+
strum::EnumIter,
20+
strum::EnumString,
21+
strum::VariantNames,
22+
strum::VariantArray,
23+
)]
24+
#[cfg_attr(
25+
feature = "serde",
26+
derive(serde::Deserialize, serde::Serialize),
27+
serde(rename_all = "lowercase")
28+
)]
29+
#[strum(serialize_all = "lowercase")]
30+
pub enum IntervalLevel {
31+
#[default]
32+
Semitone = 1,
33+
Tone = 2,
34+
Third = 4,
35+
Fourth = 5,
36+
Fifth = 7,
37+
Sixth,
38+
Seventh,
39+
Octave = 12,
40+
}
41+
42+
impl IntervalLevel {
43+
pub const MOD: i8 = 12;
44+
45+
pub fn from_i8(value: i8) -> Self {
46+
use IntervalLevel::*;
47+
match value.abs() % 12 {
48+
1 => Semitone,
49+
2 => Tone,
50+
3..=4 => Third,
51+
5 => Fourth,
52+
6..=8 => Fifth,
53+
9..=11 => Seventh,
54+
0 => Octave,
55+
_ => unreachable!(),
56+
}
57+
}
58+
pub fn semitone() -> Self {
59+
IntervalLevel::Semitone
60+
}
61+
62+
pub fn tone() -> Self {
63+
IntervalLevel::Tone
64+
}
65+
66+
pub fn third() -> Self {
67+
IntervalLevel::Third
68+
}
69+
70+
pub fn fourth() -> Self {
71+
IntervalLevel::Fourth
72+
}
73+
74+
pub fn fifth() -> Self {
75+
IntervalLevel::Fifth
76+
}
77+
78+
pub fn sixth() -> Self {
79+
IntervalLevel::Sixth
80+
}
81+
82+
pub fn seventh() -> Self {
83+
IntervalLevel::Seventh
84+
}
85+
}

core/src/intervals/types/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Appellation: types <module>
3+
Contrib: FL03 <jo3mccain@icloud.com>
4+
*/
5+
#[doc(inline)]
6+
pub use self::{level::*, qualities::*};
7+
8+
pub(crate) mod level;
9+
pub(crate) mod qualities;
10+
11+
#[allow(unused_imports)]
12+
#[doc(hidden)]
13+
pub(crate) mod prelude {
14+
pub use super::level::IntervalLevel;
15+
// pub use super::qualities::*;
16+
}

core/src/intervals/qualities.rs renamed to core/src/intervals/types/qualities.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Appellation: qualities <module>
33
Contrib: FL03 <jo3mccain@icloud.com>
44
*/
5-
/// Musically speaking, an [interval quality](Quality) is used to identify the different versions of various musical
5+
/// Musically speaking, the [quality](Quality) of an interval is used to denote the particular
6+
/// variation, or [level](super::IntervalLevel), of the interval.
67
/// objects.
78
///
89
/// With respect to intervals, the quality is used to identify the different versions of the interval.

0 commit comments

Comments
 (0)