mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
refactor: merge plugin, sampler -> mixer; transport -> sequencer; time -> core
This commit is contained in:
parent
6206a43b4a
commit
a659062dbc
46 changed files with 128 additions and 198 deletions
|
|
@ -9,12 +9,13 @@ pub use ratatui::prelude::{Rect, Style, Color, Buffer};
|
|||
pub use ratatui::style::Stylize;
|
||||
pub use clojure_reader::{edn::{read, Edn}, error::Error as EdnError};
|
||||
pub use once_cell::sync::Lazy;
|
||||
pub use std::sync::atomic::{Ordering, AtomicBool};
|
||||
|
||||
pub(crate) use std::error::Error;
|
||||
pub(crate) use std::io::{stdout};
|
||||
pub(crate) use std::thread::{spawn, JoinHandle};
|
||||
pub(crate) use std::time::Duration;
|
||||
pub(crate) use std::sync::atomic::{Ordering, AtomicBool};
|
||||
pub(crate) use atomic_float::*;
|
||||
//, LockResult, RwLockReadGuard, RwLockWriteGuard};
|
||||
//pub(crate) use std::path::PathBuf;
|
||||
//pub(crate) use std::fs::read_dir;
|
||||
|
|
@ -41,7 +42,12 @@ use crossterm::terminal::{
|
|||
}
|
||||
|
||||
submod! {
|
||||
exit render handle
|
||||
exit
|
||||
render
|
||||
handle
|
||||
time_base
|
||||
time_note
|
||||
time_tick
|
||||
}
|
||||
|
||||
/// EDN parsing helper.
|
||||
|
|
|
|||
|
|
@ -145,6 +145,15 @@ impl Render for () {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Render> Render for Option<T> {
|
||||
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||
match self {
|
||||
Some(widget) => widget.render(b, a),
|
||||
None => ().render(b, a),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Fn(&mut Buffer, Rect) -> Usually<Rect> + Send> Render for T {
|
||||
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||
(*self)(b, a)
|
||||
|
|
|
|||
103
crates/tek_core/src/time_base.rs
Normal file
103
crates/tek_core/src/time_base.rs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Keeps track of global time units.
|
||||
pub struct Timebase {
|
||||
/// Frames per second
|
||||
pub rate: AtomicF64,
|
||||
/// Beats per minute
|
||||
pub bpm: AtomicF64,
|
||||
/// Ticks per beat
|
||||
pub ppq: AtomicF64,
|
||||
}
|
||||
impl Default for Timebase {
|
||||
fn default () -> Self {
|
||||
Self {
|
||||
rate: 48000f64.into(),
|
||||
bpm: 150f64.into(),
|
||||
ppq: 96f64.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Timebase {
|
||||
pub fn new (rate: f64, bpm: f64, ppq: f64) -> Self {
|
||||
Self { rate: rate.into(), bpm: bpm.into(), ppq: ppq.into() }
|
||||
}
|
||||
|
||||
/// Frames per second
|
||||
#[inline] fn rate (&self) -> f64 {
|
||||
self.rate.load(Ordering::Relaxed)
|
||||
}
|
||||
#[inline] fn usec_per_frame (&self) -> f64 {
|
||||
1_000_000 as f64 / self.rate() as f64
|
||||
}
|
||||
#[inline] pub fn frame_to_usec (&self, frame: f64) -> f64 {
|
||||
frame * self.usec_per_frame()
|
||||
}
|
||||
|
||||
/// Beats per minute
|
||||
#[inline] pub fn bpm (&self) -> f64 {
|
||||
self.bpm.load(Ordering::Relaxed)
|
||||
}
|
||||
#[inline] pub fn set_bpm (&self, bpm: f64) {
|
||||
self.bpm.store(bpm, Ordering::Relaxed)
|
||||
}
|
||||
#[inline] fn usec_per_beat (&self) -> f64 {
|
||||
60_000_000f64 / self.bpm() as f64
|
||||
}
|
||||
#[inline] fn beat_per_second (&self) -> f64 {
|
||||
self.bpm() as f64 / 60000000.0
|
||||
}
|
||||
|
||||
/// Pulses per beat
|
||||
#[inline] pub fn ppq (&self) -> f64 {
|
||||
self.ppq.load(Ordering::Relaxed)
|
||||
}
|
||||
#[inline] pub fn pulse_per_frame (&self) -> f64 {
|
||||
self.usec_per_pulse() / self.usec_per_frame() as f64
|
||||
}
|
||||
#[inline] pub fn usec_per_pulse (&self) -> f64 {
|
||||
self.usec_per_beat() / self.ppq() as f64
|
||||
}
|
||||
#[inline] pub fn pulse_to_frame (&self, pulses: f64) -> f64 {
|
||||
self.pulse_per_frame() * pulses
|
||||
}
|
||||
#[inline] pub fn frame_to_pulse (&self, frames: f64) -> f64 {
|
||||
frames / self.pulse_per_frame()
|
||||
}
|
||||
#[inline] pub fn note_to_usec (&self, (num, den): (f64, f64)) -> f64 {
|
||||
4.0 * self.usec_per_beat() * num / den
|
||||
}
|
||||
#[inline] pub fn frames_per_pulse (&self) -> f64 {
|
||||
self.rate() as f64 / self.pulses_per_second()
|
||||
}
|
||||
#[inline] fn pulses_per_second (&self) -> f64 {
|
||||
self.beat_per_second() * self.ppq() as f64
|
||||
}
|
||||
|
||||
#[inline] pub fn note_to_frame (&self, note: (f64, f64)) -> f64 {
|
||||
self.usec_to_frame(self.note_to_usec(note))
|
||||
}
|
||||
#[inline] fn usec_to_frame (&self, usec: f64) -> f64 {
|
||||
usec * self.rate() / 1000.0
|
||||
}
|
||||
|
||||
#[inline] pub fn quantize (
|
||||
&self, step: (f64, f64), time: f64
|
||||
) -> (f64, f64) {
|
||||
let step = self.note_to_usec(step);
|
||||
(time / step, time % step)
|
||||
}
|
||||
|
||||
#[inline] pub fn quantize_into <E, T> (
|
||||
&self, step: (f64, f64), events: E
|
||||
) -> Vec<(f64, T)>
|
||||
where E: std::iter::Iterator<Item=(f64, T)> + Sized
|
||||
{
|
||||
let step = (step.0.into(), step.1.into());
|
||||
events
|
||||
.map(|(time, event)|(self.quantize(step, time).0, event))
|
||||
.collect()
|
||||
}
|
||||
|
||||
}
|
||||
59
crates/tek_core/src/time_note.rs
Normal file
59
crates/tek_core/src/time_note.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/// (pulses, name)
|
||||
pub const NOTE_DURATIONS: [(usize, &str);26] = [
|
||||
(1, "1/384"),
|
||||
(2, "1/192"),
|
||||
(3, "1/128"),
|
||||
(4, "1/96"),
|
||||
(6, "1/64"),
|
||||
(8, "1/48"),
|
||||
(12, "1/32"),
|
||||
(16, "1/24"),
|
||||
(24, "1/16"),
|
||||
(32, "1/12"),
|
||||
(48, "1/8"),
|
||||
(64, "1/6"),
|
||||
(96, "1/4"),
|
||||
(128, "1/3"),
|
||||
(192, "1/2"),
|
||||
(256, "2/3"),
|
||||
(384, "1/1"),
|
||||
(512, "4/3"),
|
||||
(576, "3/2"),
|
||||
(768, "2/1"),
|
||||
(1152, "3/1"),
|
||||
(1536, "4/1"),
|
||||
(2304, "6/1"),
|
||||
(3072, "8/1"),
|
||||
(3456, "9/1"),
|
||||
(6144, "16/1"),
|
||||
];
|
||||
|
||||
/// Returns the next shorter length
|
||||
pub fn prev_note_length (ppq: usize) -> usize {
|
||||
for i in 1..=16 {
|
||||
let length = NOTE_DURATIONS[16-i].0;
|
||||
if length < ppq {
|
||||
return length
|
||||
}
|
||||
}
|
||||
ppq
|
||||
}
|
||||
|
||||
/// Returns the next longer length
|
||||
pub fn next_note_length (ppq: usize) -> usize {
|
||||
for (length, _) in &NOTE_DURATIONS {
|
||||
if *length > ppq {
|
||||
return *length
|
||||
}
|
||||
}
|
||||
ppq
|
||||
}
|
||||
|
||||
pub fn ppq_to_name (ppq: usize) -> &'static str {
|
||||
for (length, name) in &NOTE_DURATIONS {
|
||||
if *length == ppq {
|
||||
return name
|
||||
}
|
||||
}
|
||||
""
|
||||
}
|
||||
108
crates/tek_core/src/time_tick.rs
Normal file
108
crates/tek_core/src/time_tick.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/// Defines frames 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 {
|
||||
TicksIterator(self.0, start, start, end)
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator that emits subsequent ticks within a range.
|
||||
pub struct TicksIterator(f64, usize, usize, usize);
|
||||
|
||||
impl Iterator for TicksIterator {
|
||||
type Item = (usize, usize);
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
if self.1 > self.3 {
|
||||
return None
|
||||
}
|
||||
let fpt = self.0;
|
||||
let frame = 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);
|
||||
if jitter > next_jitter { // at crossing:
|
||||
let time = (frame as usize) % (end as usize-start as usize);
|
||||
let tick = (frame / fpt) as usize;
|
||||
return Some((time, tick))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_frames_to_ticks () {
|
||||
let ticks = Ticks(12.3).between_frames(0, 100).collect::<Vec<_>>();
|
||||
println!("{ticks:?}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// (pulses, name)
|
||||
pub const NOTE_DURATIONS: [(usize, &str);26] = [
|
||||
(1, "1/384"),
|
||||
(2, "1/192"),
|
||||
(3, "1/128"),
|
||||
(4, "1/96"),
|
||||
(6, "1/64"),
|
||||
(8, "1/48"),
|
||||
(12, "1/32"),
|
||||
(16, "1/24"),
|
||||
(24, "1/16"),
|
||||
(32, "1/12"),
|
||||
(48, "1/8"),
|
||||
(64, "1/6"),
|
||||
(96, "1/4"),
|
||||
(128, "1/3"),
|
||||
(192, "1/2"),
|
||||
(256, "2/3"),
|
||||
(384, "1/1"),
|
||||
(512, "4/3"),
|
||||
(576, "3/2"),
|
||||
(768, "2/1"),
|
||||
(1152, "3/1"),
|
||||
(1536, "4/1"),
|
||||
(2304, "6/1"),
|
||||
(3072, "8/1"),
|
||||
(3456, "9/1"),
|
||||
(6144, "16/1"),
|
||||
];
|
||||
|
||||
/// Returns the next shorter length
|
||||
pub fn prev_note_length (ppq: usize) -> usize {
|
||||
for i in 1..=16 {
|
||||
let length = NOTE_DURATIONS[16-i].0;
|
||||
if length < ppq {
|
||||
return length
|
||||
}
|
||||
}
|
||||
ppq
|
||||
}
|
||||
|
||||
/// Returns the next longer length
|
||||
pub fn next_note_length (ppq: usize) -> usize {
|
||||
for (length, _) in &NOTE_DURATIONS {
|
||||
if *length > ppq {
|
||||
return *length
|
||||
}
|
||||
}
|
||||
ppq
|
||||
}
|
||||
|
||||
pub fn ppq_to_name (ppq: usize) -> &'static str {
|
||||
for (length, name) in &NOTE_DURATIONS {
|
||||
if *length == ppq {
|
||||
return name
|
||||
}
|
||||
}
|
||||
""
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue