mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 12:46:42 +01:00
wip: correct cycle timings (jitter eats notes)
This commit is contained in:
parent
e83802e1fd
commit
665885f6ff
5 changed files with 90 additions and 76 deletions
|
|
@ -35,6 +35,10 @@ impl Timebase {
|
|||
#[inline] pub fn frames_usecs (&self, frame: f64) -> f64 {
|
||||
frame * self.frame_usec()
|
||||
}
|
||||
/// Frames to usecs
|
||||
#[inline] pub fn usecs_frames (&self, usec: f64) -> f64 {
|
||||
usec / self.frame_usec()
|
||||
}
|
||||
|
||||
/// Beats per minute
|
||||
#[inline] pub fn bpm (&self) -> f64 {
|
||||
|
|
@ -53,6 +57,10 @@ impl Timebase {
|
|||
#[inline] fn pulse_usec (&self) -> f64 {
|
||||
self.beat_usec() / self.ppq() as f64
|
||||
}
|
||||
/// Usecs to pulses
|
||||
#[inline] pub fn pulses_usecs (&self, pulses: f64) -> f64 {
|
||||
self.pulse_usec() * pulses
|
||||
}
|
||||
/// Pulses per frame
|
||||
#[inline] pub fn pulse_frame (&self) -> f64 {
|
||||
self.pulse_usec() / self.frame_usec() as f64
|
||||
|
|
@ -110,49 +118,6 @@ impl Timebase {
|
|||
#[inline] pub fn frames_per_tick (&self) -> f64 {
|
||||
self.rate() as f64 / self.ticks_per_second()
|
||||
}
|
||||
pub fn frames_to_ticks (
|
||||
&self,
|
||||
start: f64,
|
||||
end: f64,
|
||||
repeat: f64,
|
||||
) -> Vec<(usize, usize)> {
|
||||
let start_frame = start % repeat;
|
||||
let end_frame = end % repeat;
|
||||
let fpt = self.pulse_frame();
|
||||
//panic!("{start_frame} {end_frame} {fpt}");
|
||||
let mut ticks = vec![];
|
||||
let mut add_frame = |frame: f64|{
|
||||
let jitter = frame.rem_euclid(fpt);
|
||||
let last_jitter = (frame - 1.0).max(0.0) % fpt;
|
||||
let next_jitter = frame + 1.0 % fpt;
|
||||
if jitter <= last_jitter && jitter <= next_jitter {
|
||||
ticks.push((frame as usize % (end as usize-start as usize), (frame / fpt) as usize));
|
||||
};
|
||||
};
|
||||
if start_frame < end_frame {
|
||||
for frame in start_frame as usize..end_frame as usize {
|
||||
add_frame(frame as f64);
|
||||
}
|
||||
} else {
|
||||
let mut frame = start_frame as usize;
|
||||
loop {
|
||||
add_frame(frame as f64);
|
||||
frame = frame + 1;
|
||||
if frame >= repeat as usize {
|
||||
frame = 0;
|
||||
loop {
|
||||
add_frame(frame as f64);
|
||||
frame = frame + 1;
|
||||
if frame >= (end_frame as usize).saturating_sub(1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
ticks
|
||||
}
|
||||
}
|
||||
#[cfg(test)] mod test {
|
||||
use super::*;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue