diff --git a/Cargo.toml b/Cargo.toml index be55609..6f9e8a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,6 @@ repository = "https://github.com/tsionyx/musik" [dependencies] num-rational = "0.4" +num-integer = "0.1" enum-map ="2.6" enum-iterator = "1.4" diff --git a/examples/hsom-exercises/ch4.rs b/examples/hsom-exercises/ch4.rs index af7d7d8..9eda634 100644 --- a/examples/hsom-exercises/ch4.rs +++ b/examples/hsom-exercises/ch4.rs @@ -130,7 +130,7 @@ fn child_song_6() -> Music { + M::B(oc4, Dur::HN), ]; let main_voice = v1.times(3) + M::line(v2); - let t = (Dur::DHN.into_rational() / Dur::QN.into_rational()) * Ratio::new(69, 120); + let t = (Dur::DHN.into_ratio() / Dur::QN.into_ratio()) * Ratio::new(69, 120); (bass_line | main_voice) .with_tempo(t) .with_instrument(StandardMidiInstrument::RhodesPiano) diff --git a/src/music/duration.rs b/src/music/duration.rs index a9ab8cd..4415adb 100644 --- a/src/music/duration.rs +++ b/src/music/duration.rs @@ -16,7 +16,7 @@ impl PartialOrd for Dur { impl Ord for Dur { fn cmp(&self, other: &Self) -> Ordering { - self.into_rational().cmp(&other.into_rational()) + self.into_ratio::().cmp(&other.into_ratio()) } } @@ -29,8 +29,11 @@ impl Dur { Self(num, denom) } - pub fn into_rational(self) -> Ratio { - Ratio::new(self.0, self.1) + pub fn into_ratio(self) -> Ratio + where + T: From + Clone + num_integer::Integer, + { + Ratio::new(T::from(self.0), T::from(self.1)) } pub const ZERO: Self = Self::from_integer(0); @@ -117,7 +120,7 @@ impl Add for Dur { type Output = Self; fn add(self, rhs: Self) -> Self::Output { - (self.into_rational() + rhs.into_rational()).into() + (self.into_ratio() + rhs.into_ratio()).into() } } @@ -125,7 +128,7 @@ impl Sub for Dur { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { - (self.into_rational() - rhs.into_rational()).into() + (self.into_ratio() - rhs.into_ratio()).into() } } @@ -133,7 +136,7 @@ impl Mul for Dur { type Output = Self; fn mul(self, rhs: u8) -> Self::Output { - (self.into_rational() * rhs).into() + (self.into_ratio() * rhs).into() } } @@ -141,7 +144,7 @@ impl Mul> for Dur { type Output = Self; fn mul(self, rhs: Ratio) -> Self::Output { - (self.into_rational() * rhs).into() + (self.into_ratio() * rhs).into() } } @@ -149,7 +152,7 @@ impl Div for Dur { type Output = Self; fn div(self, rhs: u8) -> Self::Output { - (self.into_rational() / rhs).into() + (self.into_ratio() / rhs).into() } } @@ -157,6 +160,6 @@ impl Div> for Dur { type Output = Self; fn div(self, rhs: Ratio) -> Self::Output { - (self.into_rational() / rhs).into() + (self.into_ratio() / rhs).into() } }