mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
move formatting onto time traits
This commit is contained in:
parent
f26609ed62
commit
8fb5417c22
4 changed files with 44 additions and 27 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue