Skip to content

Commit

Permalink
Medium-level: Add DurationInQuarterNotes
Browse files Browse the repository at this point in the history
helgoboss committed Sep 23, 2024
1 parent d521564 commit 198be85
Showing 4 changed files with 63 additions and 0 deletions.
36 changes: 36 additions & 0 deletions main/common-types/src/duration_in_quarter_notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use nutype::nutype;

/// This represents a duration expressed as positive amount of quarter notes.
#[nutype(
new_unchecked,
validate(finite, greater_or_equal = 0.0),
derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Default,
Display,
FromStr,
Into,
TryFrom,
Serialize,
Deserialize
),
default = 0.0
)]
pub struct DurationInQuarterNotes(f64);

impl DurationInQuarterNotes {
/// The minimum duration (zero, empty).
pub const ZERO: DurationInQuarterNotes = unsafe { DurationInQuarterNotes::new_unchecked(0.0) };

/// The maximum possible duration (highest possible floating-point number).
pub const MAX: DurationInQuarterNotes =
unsafe { DurationInQuarterNotes::new_unchecked(f64::MAX) };

nutype_additions!(f64);
}
2 changes: 2 additions & 0 deletions main/common-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ pub use hex_literal::hex;
mod bpm;
mod db;
mod duration_in_beats;
mod duration_in_quarter_notes;
mod duration_in_seconds;
mod hz;
mod linear_volume_value;
@@ -22,6 +23,7 @@ mod semitones;
pub use bpm::*;
pub use db::*;
pub use duration_in_beats::*;
pub use duration_in_quarter_notes::*;
pub use duration_in_seconds::*;
pub use hz::*;
pub use linear_volume_value::*;
24 changes: 24 additions & 0 deletions main/common-types/src/position_in_quarter_notes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::DurationInQuarterNotes;
use nutype::nutype;
use std::ops::{Add, Neg};

/// This represents a position expressed as an amount of quarter notes.
///
@@ -32,3 +34,25 @@ impl PositionInQuarterNotes {

nutype_additions!(f64);
}

impl From<DurationInQuarterNotes> for PositionInQuarterNotes {
fn from(v: DurationInQuarterNotes) -> Self {
PositionInQuarterNotes::new_panic(v.get())
}
}

impl Add<DurationInQuarterNotes> for PositionInQuarterNotes {
type Output = Self;

fn add(self, rhs: DurationInQuarterNotes) -> Self {
PositionInQuarterNotes::new_panic(self.get() + rhs.get())
}
}

impl Neg for PositionInQuarterNotes {
type Output = Self;

fn neg(self) -> Self {
Self::new_panic(-self.get())
}
}
1 change: 1 addition & 0 deletions main/medium/src/misc_newtypes.rs
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ use std::num::NonZeroI32;
pub use reaper_common_types::Bpm;
pub use reaper_common_types::Db;
pub use reaper_common_types::DurationInBeats;
pub use reaper_common_types::DurationInQuarterNotes;
pub use reaper_common_types::DurationInSeconds;
pub use reaper_common_types::Hz;
pub use reaper_common_types::LinearVolumeValue as ReaperVolumeValue;

0 comments on commit 198be85

Please sign in to comment.