Skip to content

Commit

Permalink
simplify sequencer and phrase "consume" callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
emuell committed Oct 7, 2024
1 parent 5707c2d commit f853afb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{borrow::Cow, cell::RefCell, cmp::Ordering, fmt::Debug, rc::Rc};

use crate::{
BeatTimeBase, BeatTimeStep, Event, InputParameter, InputParameterSet, InstrumentId, Rhythm,
BeatTimeBase, BeatTimeStep, InputParameter, InputParameterSet, InstrumentId, Rhythm,
RhythmIter, RhythmIterItem, SampleTime, SampleTimeDisplay,
};

Expand Down Expand Up @@ -123,12 +123,12 @@ impl Phrase {
/// visitor function for all emitted events.
pub fn consume_events_until_time<F>(&mut self, sample_time: SampleTime, consumer: &mut F)
where
F: FnMut(RhythmIndex, SampleTime, Option<Event>, SampleTime),
F: FnMut(RhythmIndex, RhythmIterItem),
{
// emit next events until we've reached the desired sample_time
while let Some((rhythm_index, event)) = self.next_event_until_time(sample_time) {
debug_assert!(event.time < sample_time);
consumer(rhythm_index, event.time, event.event, event.duration);
while let Some((rhythm_index, rhythm_item)) = self.next_event_until_time(sample_time) {
debug_assert!(rhythm_item.time < sample_time);
consumer(rhythm_index, rhythm_item);
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,28 +299,28 @@ impl SamplePlayer {
fn run_until_time(
&mut self,
sequence: &mut Sequence,
start_offset: SampleTime,
sample_time: SampleTime,
time_offset: SampleTime,
time: SampleTime,
) {
let time_base = *sequence.time_base();
sequence.consume_events_until_time(
sample_time,
&mut |rhythm_index, sample_time, event: Option<Event>, event_duration| {
time,
&mut |rhythm_index, rhythm_item| {
// print
if self.show_events {
const SHOW_INSTRUMENTS_AND_PARAMETERS: bool = true;
println!(
"{}: {}",
time_base.display(sample_time),
match &event {
time_base.display(rhythm_item.time),
match &rhythm_item.event {
Some(event) => event.to_string(SHOW_INSTRUMENTS_AND_PARAMETERS),
None => "---".to_string(),
}
);
}
// play
let playing_notes_in_rhythm = &mut self.playing_notes[rhythm_index];
if let Some(Event::NoteEvents(notes)) = event {
if let Some(Event::NoteEvents(notes)) = rhythm_item.event {
for (voice_index, note_event) in notes.iter().enumerate() {
if let Some(note_event) = note_event {
// stop playing samples on this voice channel
Expand All @@ -332,7 +332,7 @@ impl SamplePlayer {
{
if let Err(_err) = self.player.stop_source_at_sample_time(
*playback_id,
start_offset + sample_time,
time_offset + rhythm_item.time,
) {
// this is expected when the sample played to end
}
Expand Down Expand Up @@ -361,13 +361,13 @@ impl SamplePlayer {
voice_index: Some(voice_index),
});
let sample_delay = (note_event.delay
* event_duration as f32)
* rhythm_item.duration as f32)
as SampleTime;
let playback_id = self
.player
.play_file_source_with_context(
sample,
Some(start_offset + sample_time + sample_delay),
Some(time_offset + rhythm_item.time + sample_delay),
Some(context),
)
.expect("Failed to play file source");
Expand Down
19 changes: 8 additions & 11 deletions src/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Arrange multiple `Phrase`S into a single `Rhythm`.
use crate::{event::Event, phrase::RhythmIndex, BeatTimeBase, Phrase, Rhythm, SampleTime};
use crate::{phrase::RhythmIndex, BeatTimeBase, Phrase, Rhythm, RhythmIterItem, SampleTime};

#[cfg(doc)]
use crate::EventIter;
Expand Down Expand Up @@ -59,16 +59,13 @@ impl Sequence {

/// Run rhythms until a given sample time is reached, calling the given `visitor`
/// function for all emitted events to consume them.
pub fn consume_events_until_time<F>(&mut self, sample_time: SampleTime, consumer: &mut F)
pub fn consume_events_until_time<F>(&mut self, time: SampleTime, consumer: &mut F)
where
F: FnMut(RhythmIndex, SampleTime, Option<Event>, SampleTime),
F: FnMut(RhythmIndex, RhythmIterItem),
{
debug_assert!(
sample_time >= self.sample_position,
"can not rewind playback here"
);
while sample_time - self.sample_position > 0 {
let (next_phrase_start, samples_to_run) = self.samples_until_next_phrase(sample_time);
debug_assert!(time >= self.sample_position, "can not rewind playback here");
while time - self.sample_position > 0 {
let (next_phrase_start, samples_to_run) = self.samples_until_next_phrase(time);
if next_phrase_start <= samples_to_run {
// run current phrase until it ends
let sample_position = self.sample_position;
Expand Down Expand Up @@ -152,11 +149,11 @@ impl Sequence {
&mut self.phrases[self.phrase_index]
}

fn samples_until_next_phrase(&self, run_until_time: u64) -> (u64, u64) {
fn samples_until_next_phrase(&self, time: u64) -> (u64, u64) {
let phrase_length_in_samples =
self.current_phrase().length().to_samples(&self.time_base) as SampleTime;
let next_phrase_start = phrase_length_in_samples - self.sample_position_in_phrase;
let samples_to_run = run_until_time - self.sample_position;
let samples_to_run = time - self.sample_position;
(next_phrase_start, samples_to_run)
}
}

0 comments on commit f853afb

Please sign in to comment.