move formatting onto time traits

This commit is contained in:
🪞👃🪞 2024-10-26 20:46:07 +03:00
parent f26609ed62
commit 8fb5417c22
4 changed files with 44 additions and 27 deletions

View file

@ -2,10 +2,10 @@ use crate::*;
use std::iter::Iterator;
/// Any numeric type that represents time
pub trait TimeUnit: PartialEq + Copy + Display
pub trait TimeUnit: Copy + Display + PartialOrd + PartialEq
+ Add<Self, Output=Self> + Mul<Self, Output=Self>
+ Div<Self, Output=Self> + Rem<Self, Output=Self> {}
impl<T> TimeUnit for T where T: PartialEq + Copy + Display
impl<T> TimeUnit for T where T: Copy + Display + PartialOrd + PartialEq
+ Add<Self, Output=Self> + Mul<Self, Output=Self>
+ Div<Self, Output=Self> + Rem<Self, Output=Self> {}
@ -121,6 +121,18 @@ pub trait PulsesPerQuaver<U: TimeUnit> {
{
self.sr() / self.pulses_per_second()
}
#[inline] fn format_beats (&self, pulse: U) -> String where U: TimeInteger {
let ppq = self.ppq();
let (beats, pulses) = if ppq > U::from(0) {
(pulse / ppq, pulse % ppq)
} else {
(U::from(0), U::from(0))
};
let bars = (beats / U::from(4)) + U::from(1);
let beats = (beats % U::from(4)) + U::from(1);
format!("{bars}.{beats}.{pulses:02}")
}
}
pub trait FramePosition<U: TimeUnit> {
@ -131,11 +143,22 @@ pub trait FramePosition<U: TimeUnit> {
pub trait PulsePosition<U: TimeUnit> {
fn pulse (&self) -> U;
fn set_pulse (&self, pulse: U);
#[inline] fn format_current_pulse (&self) -> String
where Self: PulsesPerQuaver<U>, U: TimeInteger
{
self.format_beats(self.pulse())
}
}
pub trait UsecPosition<U: TimeUnit> {
fn usec (&self) -> U;
fn set_usec (&self, usec: U);
#[inline] fn format_current_usec (&self) -> String where U: From<usize> {
let usecs = self.usec();
let (seconds, msecs) = (usecs / U::from(1000000), usecs / U::from(1000) % U::from(1000));
let (minutes, seconds) = (seconds / U::from(60), seconds % U::from(60));
format!("{minutes}:{seconds:02}:{msecs:03}")
}
}
pub trait LaunchSync<U: TimeUnit> {