Skip to content

Commit

Permalink
Floor function
Browse files Browse the repository at this point in the history
  • Loading branch information
vihdzp committed Sep 6, 2023
1 parent fbc1607 commit 9bbbb86
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/simple_melody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SAMPLE_RATE: SampleRate = SampleRate::CD;

fn main() {
// A quarter note.
let q = Time::from_sec(0.5, SAMPLE_RATE);
let q = Time::from_sec(0.5, SAMPLE_RATE).floor();
// The loop length.
let length = 16u8 * q;
// Release time for each note.
Expand Down Expand Up @@ -66,7 +66,7 @@ fn main() {
// Play as usual.
melody.next()
} else {
// Stop the loop, just play the inner signal instead.
// Stop the loop, just play the inner fading signal instead.
melody.sgn_mut().next()
}
},
Expand Down
5 changes: 5 additions & 0 deletions src/units/time/frac_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ impl FracInt {
pub fn frac_f64(self) -> f64 {
f64::from(self.frac_int()) / POW_TWO_F64
}

/// Rounds to the nearest integer down.
pub fn floor(self) -> Self {
Self::new_raw(self.0 & !((1 << 16) - 1))
}
}

/// Implements [`From`] for integer types.
Expand Down
7 changes: 7 additions & 0 deletions src/units/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ impl Time {
pub fn advance(&mut self) {
*self += Self::SAMPLE;
}

/// Rounds to the nearest sample down.
///
/// This can be useful if you want to force something into a neat length.
pub fn floor(self)->Self{
Self::new(self.samples.floor())
}
}

impl std::fmt::Display for Time {
Expand Down

0 comments on commit 9bbbb86

Please sign in to comment.