mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
i dont know why that worked
This commit is contained in:
parent
4ae62c5bc2
commit
b73aa8a0dc
17 changed files with 552 additions and 481 deletions
90
src/time.rs
Normal file
90
src/time.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/// Number of data frames in a second.
|
||||
pub struct Hz(pub u32);
|
||||
|
||||
/// One data frame.
|
||||
pub struct Frame(pub u32);
|
||||
|
||||
/// One microsecond.
|
||||
pub struct Usec(pub u32);
|
||||
|
||||
/// Beats per minute as `120000` = 120.000BPM
|
||||
pub struct Tempo(pub u32);
|
||||
|
||||
/// Time signature: N/Mths per bar.
|
||||
pub struct Signature(pub u32, pub u32);
|
||||
|
||||
/// NoteDuration in musical terms. Has definite usec value
|
||||
/// for given bpm and sample rate.
|
||||
pub enum NoteDuration {
|
||||
Nth(u16, u16),
|
||||
Dotted(Box<Self>),
|
||||
Tuplet(u16, Box<Self>),
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
#[inline]
|
||||
pub fn to_usec (&self, rate: &Hz) -> Usec {
|
||||
Usec((self.0 * 1000) / rate.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Usec {
|
||||
#[inline]
|
||||
pub fn to_frame (&self, rate: &Hz) -> Frame {
|
||||
Frame((self.0 * rate.0) / 1000)
|
||||
}
|
||||
}
|
||||
|
||||
impl Tempo {
|
||||
#[inline]
|
||||
pub fn to_usec_per_beat (&self) -> Usec {
|
||||
Usec((60_000_000_000 / self.0 as usize) as u32)
|
||||
}
|
||||
#[inline]
|
||||
pub fn to_usec_per_quarter (&self) -> Usec {
|
||||
Usec(self.to_usec_per_quarter().0 / 4)
|
||||
}
|
||||
#[inline]
|
||||
pub fn to_usec_per_tick (&self, ppq: u32) -> Usec {
|
||||
Usec(self.to_usec_per_quarter().0 / ppq)
|
||||
}
|
||||
#[inline]
|
||||
pub fn quantize (&self, step: &NoteDuration, time: Usec) -> Usec {
|
||||
let step = step.to_usec(self);
|
||||
let t = time.0 / step.0;
|
||||
Usec(step.0 * t)
|
||||
}
|
||||
#[inline]
|
||||
pub fn quantize_into <T, U> (&self, step: &NoteDuration, events: U)
|
||||
-> Vec<(Usec, T)>
|
||||
where U: std::iter::Iterator<Item=(Usec, T)> + Sized
|
||||
{
|
||||
events
|
||||
.map(|(time, event)|(self.quantize(step, time), event))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl NoteDuration {
|
||||
fn to_usec (&self, bpm: &Tempo) -> Usec {
|
||||
Usec(match self {
|
||||
Self::Nth(time, flies) =>
|
||||
bpm.to_usec_per_beat().0 * *time as u32 / *flies as u32,
|
||||
Self::Dotted(duration) =>
|
||||
duration.to_usec(bpm).0 * 3 / 2,
|
||||
Self::Tuplet(n, duration) =>
|
||||
duration.to_usec(bpm).0 * 2 / *n as u32,
|
||||
})
|
||||
}
|
||||
fn to_frame (&self, bpm: &Tempo, rate: &Hz) -> Frame {
|
||||
self.to_usec(bpm).to_frame(rate)
|
||||
}
|
||||
}
|
||||
|
||||
struct Loop<T> {
|
||||
repeat: bool,
|
||||
pre_start: T,
|
||||
start: T,
|
||||
end: T,
|
||||
post_end: T,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue