modularize core

This commit is contained in:
🪞👃🪞 2024-06-30 22:47:17 +03:00
parent a4061535b5
commit 2837ffff4a
43 changed files with 629 additions and 770 deletions

View file

@ -1,146 +0,0 @@
use crate::prelude::*;
pub struct Timebase {
/// Frames per second
pub rate: AtomicUsize,
/// Beats per minute
pub tempo: AtomicUsize,
/// Ticks per beat
pub ppq: AtomicUsize,
}
enum QuantizeMode {
Forward,
Backward,
Nearest,
}
struct Loop<T> {
repeat: bool,
pre_start: T,
start: T,
end: T,
post_end: T,
}
/// NoteDuration in musical terms. Has definite usec value
/// for given bpm and sample rate.
pub enum NoteDuration {
Nth(usize, usize),
Dotted(Box<Self>),
Tuplet(usize, Box<Self>),
}
impl Timebase {
#[inline] pub fn rate (&self) -> usize {
self.rate.load(Ordering::Relaxed)
}
#[inline] pub fn tempo (&self) -> usize {
self.tempo.load(Ordering::Relaxed)
}
#[inline] pub fn ppq (&self) -> usize {
self.ppq.load(Ordering::Relaxed)
}
#[inline] fn beats_per_second (&self) -> f64 {
self.tempo() as f64 / 60000.0
}
#[inline] fn frames_per_second (&self) -> usize {
self.rate()
}
#[inline] pub fn frames_per_beat (&self) -> f64 {
self.frames_per_second() as f64 / self.beats_per_second()
}
#[inline] pub fn frames_per_tick (&self) -> f64 {
self.frames_per_second() as f64 / self.ticks_per_second()
}
#[inline] fn frames_per_loop (&self, steps: f64, steps_per_beat: f64) -> f64 {
self.frames_per_beat() * steps / steps_per_beat
}
#[inline] fn ticks_per_beat (&self) -> f64 {
self.ppq.load(Ordering::Relaxed) as f64
}
#[inline] fn ticks_per_second (&self) -> f64 {
self.beats_per_second() * self.ticks_per_beat()
}
#[inline] pub fn frame_to_usec (&self, frame: usize) -> usize {
frame * 1000000 / self.rate()
}
#[inline] pub fn usec_to_frame (&self, usec: usize) -> usize {
usec * self.rate() / 1000
}
#[inline] pub fn usec_per_bar (&self, beats_per_bar: usize) -> usize {
self.usec_per_beat() * beats_per_bar
}
#[inline] pub fn usec_per_beat (&self) -> usize {
60_000_000_000 / self.tempo()
}
#[inline] pub fn usec_per_step (&self, divisor: usize) -> usize {
self.usec_per_beat() / divisor
}
#[inline] pub fn usec_per_tick (&self) -> usize {
self.usec_per_beat() / self.ppq()
}
#[inline] pub fn note_to_usec (&self, note: &NoteDuration) -> usize {
match note {
NoteDuration::Nth(time, flies) =>
self.usec_per_beat() * *time / *flies,
NoteDuration::Dotted(note) =>
self.note_to_usec(note) * 3 / 2,
NoteDuration::Tuplet(n, note) =>
self.note_to_usec(note) * 2 / *n,
}
}
#[inline] pub fn note_to_frame (&self, note: &NoteDuration) -> usize {
self.usec_to_frame(self.note_to_usec(note))
}
pub fn frames_to_ticks (&self, start: usize, end: usize, fpl: usize) -> Vec<(usize, usize)> {
let start_frame = start % fpl;
let end_frame = end % fpl;
let fpt = self.frames_per_tick();
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-start), (frame / fpt) as usize));
};
};
if start_frame < end_frame {
for frame in start_frame..end_frame {
add_frame(frame as f64);
}
} else {
let mut frame = start_frame;
loop {
add_frame(frame as f64);
frame = frame + 1;
if frame >= fpl {
frame = 0;
loop {
add_frame(frame as f64);
frame = frame + 1;
if frame >= end_frame.saturating_sub(1) {
break
}
}
break
}
}
}
ticks
}
pub fn quantize (&self, step: &NoteDuration, time: usize) -> (usize, usize) {
let step = self.note_to_usec(step);
let time = time / step;
let offset = time % step;
(time, offset)
}
pub fn quantize_into <T, U> (&self, step: &NoteDuration, events: U) -> Vec<(usize, T)>
where U: std::iter::Iterator<Item=(usize, T)> + Sized
{
events
.map(|(time, event)|(self.quantize(step, time).0, event))
.collect()
}
}