wip: tying it together...

This commit is contained in:
🪞👃🪞 2024-11-01 02:15:51 +02:00
parent bbafb72e9b
commit ad2f75bee6
7 changed files with 76 additions and 115 deletions

View file

@ -1,6 +1,5 @@
use crate::*;
use std::iter::Iterator;
/// Any numeric type that represents time
pub trait TimeUnit: Copy + Display + PartialOrd + PartialEq
+ Add<Self, Output=Self> + Mul<Self, Output=Self>
@ -8,15 +7,35 @@ pub trait TimeUnit: Copy + Display + PartialOrd + PartialEq
impl<T> TimeUnit for T where T: Copy + Display + PartialOrd + PartialEq
+ Add<Self, Output=Self> + Mul<Self, Output=Self>
+ Div<Self, Output=Self> + Rem<Self, Output=Self> {}
/// 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 {}
/// Floating time unit, such as beats or seconds
pub trait TimeFloat: TimeUnit + From<f64> + Into<f64> + Copy {}
impl<T> TimeFloat for T where T: TimeUnit + From<f64> + Into<f64> + Copy {}
/// Defines global temporal resolutions.
#[derive(Debug)] pub struct Timebase {
/// Audio samples per second
sr: AtomicF64,
/// MIDI beats per minute
bpm: AtomicF64,
/// MIDI ticks per beat
ppq: AtomicF64,
}
/// Represents a point in time in all scales
#[derive(Debug, Default)] pub struct Instant {
pub timebase: Arc<Timebase>,
/// Current time in microseconds
usec: AtomicUsize,
/// Current time in audio samples
sample: AtomicUsize,
/// Current time in MIDI pulses
pulse: AtomicF64,
}
/// Defines samples per tick.
pub struct Ticks(pub f64);
/// Iterator that emits subsequent ticks within a range.
pub struct TicksIterator(f64, usize, usize, usize);
/// Trait for struct that defines a sample rate in hertz (samples per second)
pub trait SampleRate<U: TimeUnit> {
/// Get the sample rate
@ -40,7 +59,6 @@ pub trait SampleRate<U: TimeUnit> {
usecs * self.sample_per_usec()
}
}
/// Trait for struct that defines a tempo in beats per minute
pub trait BeatsPerMinute<U: TimeFloat> {
/// Get the tempo
@ -79,7 +97,6 @@ pub trait BeatsPerMinute<U: TimeFloat> {
events.map(|(time, event)|(self.quantize(step, time).0, event)).collect()
}
}
/// Trait for struct that defines a MIDI resolution in pulses per quaver (beat)
pub trait PulsesPerQuaver<U: TimeUnit> {
const DEFAULT_PPQ: U;
@ -141,7 +158,6 @@ pub trait PulsesPerQuaver<U: TimeUnit> {
{
self.sr() / self.pulses_per_second()
}
#[inline] fn format_beats (&self, pulse: U) -> String where U: TimeInteger {
let ppq = self.ppq();
let (beats, pulses) = if ppq > U::from(0) {
@ -154,22 +170,19 @@ pub trait PulsesPerQuaver<U: TimeUnit> {
format!("{bars}.{beats}.{pulses:02}")
}
}
pub trait SamplePosition<U: TimeUnit> {
fn sample (&self) -> U;
fn set_sample (&self, sample: U);
}
pub trait PulsePosition<U: TimeUnit> {
fn pulse (&self) -> U;
fn set_pulse (&self, pulse: U);
#[inline] fn format_current_pulse (&self) -> String
#[inline] fn format_beat (&self) -> String
where Self: PulsesPerQuaver<U>, U: TimeInteger
{
self.format_beats(self.pulse())
}
}
pub trait UsecPosition<U: TimeUnit> {
fn usec (&self) -> U;
fn set_usec (&self, usec: U);
@ -180,7 +193,6 @@ pub trait UsecPosition<U: TimeUnit> {
format!("{minutes}:{seconds:02}:{msecs:03}")
}
}
pub trait LaunchSync<U: TimeUnit> {
fn sync (&self) -> U;
fn set_sync (&self, sync: U);
@ -194,27 +206,19 @@ pub trait LaunchSync<U: TimeUnit> {
}
}
}
pub trait Quantize<T> {
fn quant (&self) -> T;
fn set_quant (&self, quant: T);
}
#[derive(Debug)]
/// Defines global temporal resolutions.
pub struct Timebase {
/// Audio samples per second
pub sr: AtomicF64,
/// MIDI beats per minute
pub bpm: AtomicF64,
/// MIDI ticks per beat
pub ppq: AtomicF64,
}
impl Default for Timebase { fn default () -> Self { Self::new(48000f64, 150f64, 96f64) } }
impl Timebase {
pub fn new (s: impl Into<AtomicF64>, b: impl Into<AtomicF64>, p: impl Into<AtomicF64>) -> Self {
Self { sr: s.into(), bpm: b.into(), ppq: p.into() }
}
/// Iterate over ticks between start and end.
pub fn pulses_between_samples (&self, start: usize, end: usize) -> TicksIterator {
TicksIterator(self.samples_per_pulse(), start, start, end)
}
}
impl SampleRate<f64> for Timebase {
#[inline] fn sr (&self) -> f64 { self.sr.load(Ordering::Relaxed) }
@ -229,18 +233,6 @@ impl PulsesPerQuaver<f64> for Timebase {
#[inline] fn ppq (&self) -> f64 { self.ppq.load(Ordering::Relaxed) }
#[inline] fn set_ppq (&self, ppq: f64) { self.ppq.store(ppq, Ordering::Relaxed); }
}
#[derive(Debug, Default)]
/// Represents a point in time in all scales
pub struct Instant {
timebase: Arc<Timebase>,
/// Current time in microseconds
usec: AtomicUsize,
/// Current time in audio samples
sample: AtomicUsize,
/// Current time in MIDI pulses
pulse: AtomicF64,
}
impl SampleRate<f64> for Instant {
#[inline] fn sr (&self) -> f64 { self.timebase.sr() }
#[inline] fn set_sr (&self, sr: f64) { self.timebase.set_sr(sr); }
@ -311,7 +303,6 @@ impl Instant {
self.set_sample(self.timebase.pulses_to_sample(pulse) as usize);
}
}
/// (pulses, name), assuming 96 PPQ
pub const NOTE_DURATIONS: [(usize, &str);26] = [
(1, "1/384"),
@ -341,7 +332,6 @@ pub const NOTE_DURATIONS: [(usize, &str);26] = [
(3456, "9/1"),
(6144, "16/1"),
];
/// Returns the next shorter length
pub fn prev_note_length (pulses: usize) -> usize {
for i in 1..=16 {
@ -350,38 +340,20 @@ pub fn prev_note_length (pulses: usize) -> usize {
}
pulses
}
/// Returns the next longer length
pub fn next_note_length (pulses: usize) -> usize {
for (length, _) in &NOTE_DURATIONS { if *length > pulses { return *length } }
pulses
}
pub fn pulses_to_name (pulses: usize) -> &'static str {
for (length, name) in &NOTE_DURATIONS { if *length == pulses { return name } }
""
}
/// Defines samples per tick.
pub struct Ticks(pub f64);
impl Ticks {
/// Iterate over ticks between start and end.
pub fn between_samples (&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
}
if self.1 > self.3 { return None }
let fpt = self.0;
let sample = self.1 as f64;
let start = self.2;
@ -398,15 +370,12 @@ impl Iterator for TicksIterator {
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_samples_to_ticks () {
let ticks = Ticks(12.3).between_samples(0, 100).collect::<Vec<_>>();
println!("{ticks:?}");
}
}