wip: refactor pt.34 (35e) more traits, fewer structs

This commit is contained in:
🪞👃🪞 2024-11-15 01:44:51 +01:00
parent cbbecc5aba
commit beca1a6ade
19 changed files with 361 additions and 379 deletions

View file

@ -24,25 +24,26 @@ pub trait HasMidiBuffer {
}
}
pub trait HasPhrase {
pub trait HasPhrase: PlayheadApi + HasClock + HasMidiBuffer {
fn phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
fn next_phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
fn next_phrase_mut (&mut self) -> &mut Option<(Instant, Arc<RwLock<Phrase>>)>;
fn switchover (&mut self, scope: &ProcessScope) {
if self.is_rolling() {
let sample0 = scope.last_frame_time() as usize;
//let samples = scope.n_frames() as usize;
if let Some((start_at, phrase)) = &self.next_phrase {
if let Some((start_at, phrase)) = &self.next_phrase() {
let start = start_at.sample.get() as usize;
let sample = self.clock.started.read().unwrap().unwrap().0;
let sample = self.clock().started.read().unwrap().unwrap().0;
// If it's time to switch to the next phrase:
if start <= sample0.saturating_sub(sample) {
// Samples elapsed since phrase was supposed to start
let skipped = sample0 - start;
// Switch over to enqueued phrase
let started = Instant::from_sample(&self.clock.timebase(), start as f64);
let started = Instant::from_sample(&self.clock().timebase(), start as f64);
self.phrase = Some((started, phrase.clone()));
// Unset enqueuement (TODO: where to implement looping?)
self.next_phrase = None
*self.next_phrase_mut() = None
}
// TODO fill in remaining ticks of chunk from next phrase.
// ?? just call self.play(scope) again, since enqueuement is off ???
@ -53,16 +54,16 @@ pub trait HasPhrase {
}
}
fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
let start = self.clock.next_launch_pulse();
let start = self.clock().next_launch_pulse();
self.next_phrase = Some((
Instant::from_pulse(&self.clock.timebase(), start as f64),
Instant::from_pulse(&self.clock().timebase(), start as f64),
phrase.map(|p|p.clone())
));
self.reset = true;
}
fn pulses_since_start (&self) -> Option<f64> {
if let Some((started, Some(_))) = self.phrase.as_ref() {
Some(self.clock.current.pulse.get() - started.pulse.get())
Some(self.clock().current.pulse.get() - started.pulse.get())
} else {
None
}