wip: refactor pt.32: 89 errors, traits

This commit is contained in:
🪞👃🪞 2024-11-15 00:17:36 +01:00
parent 8b5931c321
commit ce78b95d8a
28 changed files with 946 additions and 822 deletions

View file

@ -0,0 +1,56 @@
use crate::*;
#[derive(Clone, Debug)]
pub enum ClockCommand {
SetBpm(f64),
SetQuant(f64),
SetSync(f64),
}
impl<T: ClockApi> Command<T> for ClockCommand {
fn execute (self, state: &mut T) -> Perhaps<Self> {
use ClockCommand::*;
Ok(Some(match self {
SetBpm(bpm) => SetBpm(state.timebase().bpm.set(bpm)),
SetQuant(quant) => SetQuant(state.quant().set(quant)),
SetSync(sync) => SetSync(state.sync().set(sync)),
}))
}
}
pub trait ClockApi {
fn quant (&self) -> &Quantize;
fn sync (&self) -> &LaunchSync;
fn current (&self) -> &Instant;
fn timebase (&self) -> &Timebase {
&self.current().timebase
}
fn sr (&self) -> &SampleRate {
&self.timebase().sr
}
fn bpm (&self) -> &BeatsPerMinute {
&self.timebase().bpm
}
fn ppq (&self) -> &PulsesPerQuaver {
&self.timebase().ppq
}
}
pub trait HasClock {
fn clock (&self) -> &impl ClockApi;
}
impl<T: HasClock> ClockApi for T {
fn quant (&self) -> &Quantize {
self.clock().quant()
}
fn sync (&self) -> &LaunchSync {
self.clock().sync()
}
fn current (&self) -> &Instant {
self.clock().current()
}
}