document time units

This commit is contained in:
🪞👃🪞 2024-11-02 02:50:54 +02:00
parent 924b68fccf
commit ef80c48939
2 changed files with 26 additions and 11 deletions

View file

@ -34,9 +34,8 @@ macro_rules! impl_op {
}
}
/// Define and implement a unit of time
macro_rules! time_unit {
macro_rules! impl_time_unit {
($T:ident) => {
#[derive(Debug, Default)] pub struct $T(AtomicF64);
impl TimeUnit for $T {
fn get (&self) -> f64 { self.0.load(Ordering::Relaxed) }
fn set (&self, value: f64) { self.0.store(value, Ordering::Relaxed) }
@ -56,7 +55,9 @@ macro_rules! time_unit {
}
}
time_unit!(SampleRate);
/// Audio sample rate in Hz (samples per second)
#[derive(Debug, Default)] pub struct SampleRate(AtomicF64);
impl_time_unit!(SampleRate);
impl SampleRate {
/// Return the duration of a sample in microseconds (floating)
#[inline] pub fn usec_per_sample (&self) -> f64 {
@ -76,11 +77,17 @@ impl SampleRate {
}
}
time_unit!(BeatsPerMinute);
/// Tempo in beats per minute
#[derive(Debug, Default)] pub struct BeatsPerMinute(AtomicF64);
impl_time_unit!(BeatsPerMinute);
time_unit!(PulsesPerQuaver);
/// MIDI resolution in PPQ (pulses per quarter note)
#[derive(Debug, Default)] pub struct PulsesPerQuaver(AtomicF64);
impl_time_unit!(PulsesPerQuaver);
time_unit!(Microsecond);
/// Timestamp in microseconds
#[derive(Debug, Default)] pub struct Microsecond(AtomicF64);
impl_time_unit!(Microsecond);
impl Microsecond {
#[inline] pub fn format_msu (&self) -> String {
let usecs = self.get() as usize;
@ -90,13 +97,21 @@ impl Microsecond {
}
}
time_unit!(SampleCount);
/// Timestamp in audio samples
#[derive(Debug, Default)] pub struct SampleCount(AtomicF64);
impl_time_unit!(SampleCount);
time_unit!(Pulse);
/// Timestamp in MIDI pulses
#[derive(Debug, Default)] pub struct Pulse(AtomicF64);
impl_time_unit!(Pulse);
time_unit!(LaunchSync);
/// Quantization setting for launching clips
#[derive(Debug, Default)] pub struct LaunchSync(AtomicF64);
impl_time_unit!(LaunchSync);
time_unit!(Quantize);
/// Quantization setting for notes
#[derive(Debug, Default)] pub struct Quantize(AtomicF64);
impl_time_unit!(Quantize);
/// Temporal resolutions: sample rate, tempo, MIDI pulses per quaver (beat)
#[derive(Debug, Clone)]

View file

@ -367,7 +367,7 @@ impl Phrase {
clone.uuid = uuid::Uuid::new_v4();
clone
}
pub fn toggle_loop (&mut self) { self.loop_on = !self.loop_on; }
pub fn toggle_loop (&mut self) { self.loop_on = !self.loop_on; }
pub fn record_event (&mut self, pulse: usize, message: MidiMessage) {
if pulse >= self.length { panic!("extend phrase first") }
self.notes[pulse].push(message);