Skip to content

Commit

Permalink
Medium-level: Allow Bpm values < 1 and > 960
Browse files Browse the repository at this point in the history
Such Bpm values can be produced by REAPER with certain time signatures
  • Loading branch information
helgoboss committed Sep 23, 2024
1 parent 1d431d0 commit d521564
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions main/common-types/src/bpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nutype::nutype;
/// Represents a tempo measured in beats per minute.
#[nutype(
new_unchecked,
validate(finite, greater_or_equal = 1.0, less_or_equal = 960.0),
validate(finite, greater_or_equal = f64::EPSILON),
derive(
Copy,
Clone,
Expand All @@ -25,11 +25,11 @@ use nutype::nutype;
pub struct Bpm(f64);

impl Bpm {
/// The minimum possible value.
pub const MIN: Self = unsafe { Self::new_unchecked(1.0) };
/// The "soft minimum" tempo.
pub const ONE_BPM: Self = unsafe { Self::new_unchecked(1.0) };

/// The maximum possible value.
pub const MAX: Self = unsafe { Self::new_unchecked(960.0) };
/// The "soft maximum" tempo.
pub const NINE_HUNDRED_SIXTY_BPM: Self = unsafe { Self::new_unchecked(960.0) };

nutype_additions!(f64);
}
Expand All @@ -40,21 +40,21 @@ mod tests {

#[test]
fn basics() {
assert!(Bpm::new(0.5).is_err());
assert!(Bpm::new(1000.0).is_err());
assert!(Bpm::new(0.0).is_err());
assert!(Bpm::new(-1.0).is_err());
assert!(Bpm::new(60.0).unwrap() < Bpm::new(120.0).unwrap());
assert_eq!(Bpm::default(), Bpm::new(1.0).unwrap());
assert_eq!(
serde_json::from_str::<Bpm>("5").unwrap(),
Bpm::new(5.0).unwrap()
);
assert!(serde_json::from_str::<Bpm>("0.5").is_err());
assert!(serde_json::from_str::<Bpm>("-0.5").is_err());
assert_eq!(Bpm::new(756.5).unwrap().to_string(), "756.5");
assert_eq!(format!("{:?}", Bpm::new(756.5).unwrap()), "Bpm(756.5)");
assert_eq!(Bpm::MIN.into_inner(), 1.0);
const _ADDITION: f64 = Bpm::MIN.get() + Bpm::MAX.get();
assert_eq!(Bpm::MIN.get(), 1.0);
assert_eq!(Bpm::MAX.get(), 960.0);
assert_eq!(Bpm::ONE_BPM.into_inner(), 1.0);
const _ADDITION: f64 = Bpm::ONE_BPM.get() + Bpm::NINE_HUNDRED_SIXTY_BPM.get();
assert_eq!(Bpm::ONE_BPM.get(), 1.0);
assert_eq!(Bpm::NINE_HUNDRED_SIXTY_BPM.get(), 960.0);
unsafe {
assert_eq!(Bpm::new_unchecked(5.0).get(), 5.0);
}
Expand All @@ -67,6 +67,6 @@ mod tests {
#[test]
#[should_panic]
fn new_panic() {
Bpm::new_panic(0.5);
Bpm::new_panic(0.0);
}
}
6 changes: 3 additions & 3 deletions main/high/src/tempo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ impl Tempo {
pub fn from_normalized_value(normalized_value: f64) -> Tempo {
assert!(is_normalized_value(normalized_value));
Tempo(Bpm::new_panic(
Bpm::MIN.get() + normalized_value * bpm_span(),
Bpm::ONE_BPM.get() + normalized_value * bpm_span(),
))
}

pub fn normalized_value(self) -> f64 {
(self.0.get() - Bpm::MIN.get()) / bpm_span()
(self.0.get() - Bpm::ONE_BPM.get()) / bpm_span()
}

pub fn bpm(self) -> Bpm {
Expand All @@ -26,7 +26,7 @@ impl Tempo {
}

fn bpm_span() -> f64 {
Bpm::MAX.get() - Bpm::MIN.get()
Bpm::NINE_HUNDRED_SIXTY_BPM.get() - Bpm::ONE_BPM.get()
}

#[cfg(test)]
Expand Down

0 comments on commit d521564

Please sign in to comment.