Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Joe McCain III <jo3mccain@icloud.com>
  • Loading branch information
FL03 committed Aug 2, 2024
1 parent 9394a9e commit ebb339d
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 40 deletions.
4 changes: 2 additions & 2 deletions core/src/chords/dyad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ pub struct Dyad {
impl Dyad {
pub fn new(src: Note, tgt: Note) -> Self {
let chord = Pair::new(src, tgt);
let interval = Intervals::new(src, tgt);
let interval = Intervals::dist(src, tgt);
Self { chord, interval }
}

pub fn from_tuple((lhs, rhs): (Note, Note)) -> Self {
let chord = Pair::new(lhs, rhs);
let interval = Intervals::new(lhs, rhs);
let interval = Intervals::dist(lhs, rhs);
Self { chord, interval }
}

Expand Down
11 changes: 7 additions & 4 deletions core/src/intervals/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub enum Intervals {
}

impl Intervals {
pub fn dist(a: impl IntoPitch, b: impl IntoPitch) -> Self {
Self::new(a.into_pitch().absmod(), b.into_pitch().absmod())
}
pub fn new<A, B, C>(lhs: A, rhs: B) -> Self
where
A: core::ops::Sub<B, Output = C>,
Expand All @@ -50,8 +53,9 @@ impl Intervals {
/// Use the difference between two pitches to determine the interval.
pub fn from_value(value: impl IntoPitch) -> Self {
use Intervals::*;
let pitch = value.into_pitch();
match *pitch.absmod() {
let Pitch(pitch) = value.into_pitch();
match pitch.abs() % 12 {
0 => Octave,
1 => Semitone,
2 => Tone,
3 => Thirds(Third::Minor),
Expand All @@ -63,8 +67,7 @@ impl Intervals {
9 => Sevenths(Seventh::Diminished),
10 => Sevenths(Seventh::Minor),
11 => Sevenths(Seventh::Major),
12 => Octave,
_ => panic!("Invalid interval value: {}", pitch.get()),
_ => panic!("Invalid interval value: {}", pitch),
}
}
/// A convenience method for constructing a new instance of the [Octave](Intervals::Octave) variant.
Expand Down
2 changes: 0 additions & 2 deletions core/src/notes/impls/impl_note_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
use crate::{Intervals, Note};



macro_rules! impl_interval_ops {
(@assign $trait:ident.$call:ident) => {
paste::paste! {
Expand Down
14 changes: 13 additions & 1 deletion core/src/pitch/signs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
Contrib: FL03 <jo3mccain@icloud.com>
*/

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, strum::AsRefStr, strum::Display)]
#[derive(
Clone,
Copy,
Debug,
Default,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
strum::AsRefStr,
strum::Display,
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]

pub enum SymbolCount {
Expand Down
7 changes: 6 additions & 1 deletion neo/src/transform/lpr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::triad::store::BaseTriad;
use crate::triad::TriadKind;
use crate::Triad;

///
Expand Down Expand Up @@ -68,7 +69,11 @@ impl LPR {
}
/// Apply the current transformation to the given triad;
/// returns a [Triad] with the new notes and classification
pub fn apply<K>(self, triad: &mut Triad<K>) -> Triad<crate::triad::Triads> {
pub fn apply<K: TriadKind>(self, triad: Triad<K>) -> Triad<K::Rel> {
super::_transform(triad, self).expect("Transformation Error")
}

pub fn _apply<K>(self, triad: &mut Triad<K>) -> Triad<crate::triad::Triads> {
use rstmt::{
Intervals::{Semitone, Tone},
Third::*,
Expand Down
38 changes: 21 additions & 17 deletions neo/src/triad/impls/impl_triad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,48 @@
Appellation: impl_triad <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::triad::{Triad, TriadKind, Triads};
use crate::triad::Triad;
use rstmt::Note;

impl<K: TriadKind> Triad<K> {
/// Returns the [class](Triads) of the triad
pub fn class(&self) -> Triads {
K::class()
impl<K> AsRef<[Note]> for Triad<K> {
fn as_ref(&self) -> &[Note] {
&self.notes
}
}

impl<K: TriadKind> AsRef<[Note]> for Triad<K> {
fn as_ref(&self) -> &[Note] {
&self.notes
impl<K> AsMut<[Note]> for Triad<K> {
fn as_mut(&mut self) -> &mut [Note] {
&mut self.notes
}
}

impl<K: TriadKind> AsRef<[Note; 3]> for Triad<K> {
impl<K> AsRef<[Note; 3]> for Triad<K> {
fn as_ref(&self) -> &[Note; 3] {
&self.notes
}
}

impl<K: TriadKind> AsMut<[Note]> for Triad<K> {
fn as_mut(&mut self) -> &mut [Note] {
impl<K> AsMut<[Note; 3]> for Triad<K> {
fn as_mut(&mut self) -> &mut [Note; 3] {
&mut self.notes
}
}

impl<K: TriadKind> AsMut<[Note; 3]> for Triad<K> {
fn as_mut(&mut self) -> &mut [Note; 3] {
impl<K> core::ops::Deref for Triad<K> {
type Target = [Note; 3];

fn deref(&self) -> &Self::Target {
&self.notes
}
}

impl<K> core::ops::DerefMut for Triad<K> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.notes
}
}

impl<K> core::fmt::Display for Triad<K>
where
K: TriadKind,
{
impl<K> core::fmt::Display for Triad<K> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (root, third, fifth) = self.as_tuple();
write!(f, "({}, {}, {})", root, third, fifth)
Expand Down
8 changes: 4 additions & 4 deletions neo/src/triad/impls/impl_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ use crate::triad::{Triad, Triads};
impl<K: TriadKind> Triad<K> {
/// Checks if the triad is [augmented](Augmented).
pub fn is_augmented(&self) -> bool {
<K>::class().is_augmented()
self.class().is_augmented()
}
/// Checks if the triad is [diminished](Diminished).
pub fn is_diminished(&self) -> bool {
<K>::class().is_diminished()
self.class().is_diminished()
}
/// Checks if the triad is [major](Major).
pub fn is_major(&self) -> bool {
<K>::class().is_major()
self.class().is_major()
}
/// Checks if the triad is [minor](Minor).
pub fn is_minor(&self) -> bool {
<K>::class().is_minor()
self.class().is_minor()
}
}

Expand Down
2 changes: 1 addition & 1 deletion neo/src/triad/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<T> TriadData for (T, T, T) {
mod tests {
use super::store::BaseTriad;
use crate::transform::LPR;
use rstmt::{Note, IntervalOps};
use rstmt::{IntervalOps, Note};

#[test]
fn test_triad_store() {
Expand Down
16 changes: 14 additions & 2 deletions neo/src/triad/triad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ impl<K> Triad<K> {
notes: [root, third, fifth],
})
} else {

Err(TriadError::InvalidInterval(
"Failed to compute the required intervals...".into(),
))
Expand All @@ -100,6 +99,12 @@ impl<K> Triad<K> {
_class: PhantomData::<Triads>,
}
}
pub fn class(&self) -> Triads
where
K: TriadKind,
{
K::class()
}
/// Returns the name of the class
pub fn class_name(&self) -> &str
where
Expand All @@ -121,7 +126,14 @@ impl<K> Triad<K> {
where
K: TriadKind,
{
lpr.apply(&mut self)
lpr._apply(&mut self)
}

pub fn transform(self, lpr: LPR) -> Triad<K::Rel>
where
K: TriadKind,
{
lpr.apply(self)
}
}

Expand Down
9 changes: 3 additions & 6 deletions neo/tests/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rstmt::{IntervalOps, Note};
fn test_leading() {
use LPR::L;
let c_major = Triad::major(Note::from_pitch(0));
let next = c_major.apply(L);
let next = c_major.transform(L);
// Validate the resulting triad
assert_eq!(c_major.third(), next.third());
assert_ne!(c_major.root(), next.root());
Expand All @@ -23,12 +23,11 @@ fn test_leading() {
assert_ne!(c_major.third_to_fifth(), next.third_to_fifth());
}


#[test]
fn test_parallel() {
use LPR::P;
let c_major = Triad::major(Note::from_pitch(0));
let next = c_major.apply(P);
let next = c_major.transform(P);
// Validate the resulting triad
assert_ne!(c_major.third(), next.third());
assert_eq!(c_major.root(), next.root());
Expand All @@ -38,13 +37,12 @@ fn test_parallel() {
assert_ne!(c_major.root_to_third(), next.root_to_third());
}


#[ignore]
#[test]
fn test_relative() {
use LPR::R;
let c_major = Triad::major(Note::from_pitch(0));
let next = c_major.apply(R);
let next = c_major.transform(R);
// Validate the resulting triad
assert_eq!(c_major.root(), next.third());
assert_eq!(c_major.third(), next.fifth());
Expand All @@ -53,4 +51,3 @@ fn test_relative() {
// Compare the intervals between the two triads
assert_ne!(c_major.root_to_fifth(), next.root_to_fifth());
}

0 comments on commit ebb339d

Please sign in to comment.