mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
rename frame to sample everywhere
This commit is contained in:
parent
063706017e
commit
d77fe325b0
7 changed files with 40 additions and 38 deletions
|
|
@ -9,7 +9,7 @@ impl<T> TimeUnit for T where T: PartialEq + Copy
|
|||
+ Add<Self, Output=Self> + Mul<Self, Output=Self>
|
||||
+ Div<Self, Output=Self> + Rem<Self, Output=Self> {}
|
||||
|
||||
/// Integer time unit, such as frames, pulses, or microseconds
|
||||
/// Integer time unit, such as samples, pulses, or microseconds
|
||||
pub trait TimeInteger: TimeUnit + From<usize> + Into<usize> + Copy {}
|
||||
impl<T> TimeInteger for T where T: TimeUnit + From<usize> + Into<usize> + Copy {}
|
||||
|
||||
|
|
@ -51,12 +51,12 @@ pub trait BeatsPerMinute<U: TimeFloat> {
|
|||
#[inline] fn note_to_usec (&self, (num, den): (U, U)) -> U {
|
||||
U::from(4.0) * self.usec_per_beat() * num / den
|
||||
}
|
||||
/// Return the number of frames corresponding to a note of the given duration
|
||||
#[inline] fn note_to_frame (&self, note: (U, U)) -> U where Self: SampleRate<U> {
|
||||
self.usec_to_frame(self.note_to_usec(note))
|
||||
/// Return the number of samples corresponding to a note of the given duration
|
||||
#[inline] fn note_to_samples (&self, note: (U, U)) -> U where Self: SampleRate<U> {
|
||||
self.usec_to_sample(self.note_to_usec(note))
|
||||
}
|
||||
/// Return the number of frames corresponding to the given number of microseconds
|
||||
#[inline] fn usec_to_frame (&self, usec: U) -> U where Self: SampleRate<U> {
|
||||
/// Return the number of samples corresponding to the given number of microseconds
|
||||
#[inline] fn usec_to_sample (&self, usec: U) -> U where Self: SampleRate<U> {
|
||||
usec * self.sr() / U::from(1000f64)
|
||||
}
|
||||
/// Return the quantized position of a moment in time given a step
|
||||
|
|
@ -118,8 +118,8 @@ pub trait PulsesPerQuaver<U: TimeUnit> {
|
|||
}
|
||||
|
||||
pub trait FramePosition<U: TimeUnit> {
|
||||
fn frame (&self) -> U;
|
||||
fn set_frame (&self, frame: U);
|
||||
fn sample (&self) -> U;
|
||||
fn set_sample (&self, sample: U);
|
||||
}
|
||||
|
||||
pub trait PulsePosition<U: TimeUnit> {
|
||||
|
|
@ -135,13 +135,13 @@ pub trait UsecPosition<U: TimeUnit> {
|
|||
pub trait LaunchSync<U: TimeUnit> {
|
||||
fn sync (&self) -> U;
|
||||
fn set_sync (&self, sync: U);
|
||||
#[inline] fn next_launch_frame (&self) -> U where U: TimeInteger, Self: FramePosition<U> {
|
||||
#[inline] fn next_launch_sample (&self) -> U where U: TimeInteger, Self: FramePosition<U> {
|
||||
let sync = self.sync();
|
||||
let frame = self.frame();
|
||||
if frame % sync == U::from(0) {
|
||||
frame
|
||||
let sample = self.sample();
|
||||
if sample % sync == U::from(0) {
|
||||
sample
|
||||
} else {
|
||||
(frame / sync) * sync + U::from(1)
|
||||
(sample / sync) * sync + U::from(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -231,12 +231,12 @@ pub fn pulses_to_name (pulses: usize) -> &'static str {
|
|||
""
|
||||
}
|
||||
|
||||
/// Defines frames per tick.
|
||||
/// Defines samples per tick.
|
||||
pub struct Ticks(pub f64);
|
||||
|
||||
impl Ticks {
|
||||
/// Iterate over ticks between start and end.
|
||||
pub fn between_frames (&self, start: usize, end: usize) -> TicksIterator {
|
||||
pub fn between_samples (&self, start: usize, end: usize) -> TicksIterator {
|
||||
TicksIterator(self.0, start, start, end)
|
||||
}
|
||||
}
|
||||
|
|
@ -252,16 +252,16 @@ impl Iterator for TicksIterator {
|
|||
return None
|
||||
}
|
||||
let fpt = self.0;
|
||||
let frame = self.1 as f64;
|
||||
let sample = self.1 as f64;
|
||||
let start = self.2;
|
||||
let end = self.3;
|
||||
self.1 = self.1 + 1;
|
||||
//println!("{fpt} {frame} {start} {end}");
|
||||
let jitter = frame.rem_euclid(fpt); // ramps
|
||||
let next_jitter = (frame + 1.0).rem_euclid(fpt);
|
||||
//println!("{fpt} {sample} {start} {end}");
|
||||
let jitter = sample.rem_euclid(fpt); // ramps
|
||||
let next_jitter = (sample + 1.0).rem_euclid(fpt);
|
||||
if jitter > next_jitter { // at crossing:
|
||||
let time = (frame as usize) % (end as usize-start as usize);
|
||||
let tick = (frame / fpt) as usize;
|
||||
let time = (sample as usize) % (end as usize-start as usize);
|
||||
let tick = (sample / fpt) as usize;
|
||||
return Some((time, tick))
|
||||
}
|
||||
}
|
||||
|
|
@ -273,8 +273,8 @@ mod test {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_frames_to_ticks () {
|
||||
let ticks = Ticks(12.3).between_frames(0, 100).collect::<Vec<_>>();
|
||||
fn test_samples_to_ticks () {
|
||||
let ticks = Ticks(12.3).between_samples(0, 100).collect::<Vec<_>>();
|
||||
println!("{ticks:?}");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue