rstmt_core/traits/
intervals.rs

1/*
2    Appellation: intervals <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::intervals::{Intervals, IntoInterval, Seventh};
6
7/// [IntervalOps] is a trait for objects capable of basic arithmetic with intervals.
8///
9///
10///
11pub trait IntervalOps {
12    type Output;
13    /// Add an interval to the given object
14    fn add_interval<I: IntoInterval>(&self, interval: I) -> Self::Output;
15    /// Subtract an interval from the given object
16    fn sub_interval<I: IntoInterval>(&self, interval: I) -> Self::Output;
17
18    fn add_semitone(&self) -> Self::Output {
19        self.add_interval(Intervals::Semitone)
20    }
21
22    fn sub_semitone(&self) -> Self::Output {
23        self.sub_interval(Intervals::Semitone)
24    }
25
26    fn add_tone(&self) -> Self::Output {
27        self.add_interval(Intervals::Tone)
28    }
29
30    fn sub_tone(&self) -> Self::Output {
31        self.sub_interval(Intervals::Tone)
32    }
33
34    fn add_major_third(&self) -> Self::Output {
35        self.add_interval(Intervals::major_third())
36    }
37
38    fn sub_major_third(&self) -> Self::Output {
39        self.sub_interval(Intervals::major_third())
40    }
41
42    fn add_minor_third(&self) -> Self::Output {
43        self.add_interval(Intervals::minor_third())
44    }
45
46    fn sub_minor_third(&self) -> Self::Output {
47        self.sub_interval(Intervals::minor_third())
48    }
49
50    fn add_perfect_fifth(&self) -> Self::Output {
51        self.add_interval(Intervals::perfect_fifth())
52    }
53
54    fn sub_perfect_fifth(&self) -> Self::Output {
55        self.sub_interval(Intervals::perfect_fifth())
56    }
57
58    fn add_augmented_fifth(&self) -> Self::Output {
59        self.add_interval(Intervals::augmented_fifth())
60    }
61
62    fn sub_augmented_fifth(&self) -> Self::Output {
63        self.sub_interval(Intervals::augmented_fifth())
64    }
65
66    fn add_diminished_fifth(&self) -> Self::Output {
67        self.add_interval(Intervals::diminished_fifth())
68    }
69
70    fn sub_diminished_fifth(&self) -> Self::Output {
71        self.sub_interval(Intervals::diminished_fifth())
72    }
73
74    fn add_augmented_seventh(&self) -> Self::Output {
75        self.add_interval(Seventh::Augmented)
76    }
77
78    fn sub_augmented_seventh(&self) -> Self::Output {
79        self.sub_interval(Seventh::Augmented)
80    }
81
82    fn add_diminished_seventh(&self) -> Self::Output {
83        self.add_interval(Seventh::Diminished)
84    }
85
86    fn sub_diminished_seventh(&self) -> Self::Output {
87        self.sub_interval(Seventh::Diminished)
88    }
89
90    fn add_major_seventh(&self) -> Self::Output {
91        self.add_interval(Seventh::Major)
92    }
93
94    fn sub_major_seventh(&self) -> Self::Output {
95        self.sub_interval(Seventh::Major)
96    }
97
98    fn add_minor_seventh(&self) -> Self::Output {
99        self.add_interval(Seventh::Minor)
100    }
101
102    fn sub_minor_seventh(&self) -> Self::Output {
103        self.sub_interval(Seventh::Minor)
104    }
105
106    fn add_octave(&self) -> Self::Output {
107        self.add_interval(Intervals::Octave)
108    }
109
110    fn sub_octave(&self) -> Self::Output {
111        self.sub_interval(Intervals::Octave)
112    }
113}
114
115/*
116 ************* Implementations *************
117*/
118impl IntervalOps for crate::Note {
119    type Output = crate::Note;
120
121    fn add_interval<I: IntoInterval>(&self, interval: I) -> Self::Output {
122        self + interval.into_interval()
123    }
124
125    fn sub_interval<I: IntoInterval>(&self, interval: I) -> Self::Output {
126        self - interval.into_interval()
127    }
128}