Skip to content

Commit

Permalink
rename "omit" and "seek" to "advance"
Browse files Browse the repository at this point in the history
  • Loading branch information
emuell committed Jul 6, 2024
1 parent ff2fdd1 commit b891254
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 95 deletions.
10 changes: 5 additions & 5 deletions benches/benchmarks/rhythm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn run(c: &mut Criterion) {
pub fn seek(c: &mut Criterion) {
let mut group = c.benchmark_group("Rust Phrase");
let phrase = create_phrase();
let samples_per_sec = phrase.time_base().samples_per_sec;
let samples_per_sec = phrase.time_base().samples_per_sec as SampleTime;
let seek_step = 10;
let seek_time = 60 * 60;
group.bench_function("Seek", |b| {
Expand All @@ -202,10 +202,10 @@ pub fn seek(c: &mut Criterion) {
phrase
},
|mut phrase| {
let mut sample_time = samples_per_sec as SampleTime;
while sample_time < (seek_time * samples_per_sec) as SampleTime {
phrase.skip_until_time(sample_time);
sample_time += seek_step * samples_per_sec as SampleTime;
let mut sample_time = samples_per_sec;
while sample_time < seek_time * samples_per_sec {
phrase.advance_until_time(sample_time);
sample_time += seek_step * samples_per_sec;
}
},
criterion::BatchSize::SmallInput,
Expand Down
10 changes: 5 additions & 5 deletions benches/benchmarks/scripted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn run(c: &mut Criterion) {
pub fn seek(c: &mut Criterion) {
let mut group = c.benchmark_group("Scripted Phrase");
let phrase = create_phrase();
let samples_per_sec = phrase.time_base().samples_per_sec;
let samples_per_sec = phrase.time_base().samples_per_sec as SampleTime;
let seek_step = 10;
let seek_time = 60 * 60;
group.bench_function("Seek", |b| {
Expand All @@ -191,10 +191,10 @@ pub fn seek(c: &mut Criterion) {
phrase
},
|mut phrase| {
let mut sample_time = samples_per_sec as SampleTime;
while sample_time < (seek_time * samples_per_sec) as SampleTime {
phrase.skip_until_time(sample_time);
sample_time += seek_step * samples_per_sec as SampleTime;
let mut sample_time = samples_per_sec;
while sample_time < seek_time * samples_per_sec {
phrase.advance_until_time(sample_time);
sample_time += seek_step * samples_per_sec;
}
},
criterion::BatchSize::SmallInput,
Expand Down
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,11 @@ pub trait EventIter: Debug {
/// Returns an optional stack of event iter items, which should be emitted for the given pulse.
fn run(&mut self, pulse: PulseIterItem, emit_event: bool) -> Option<Vec<EventIterItem>>;

/// Move iterator with the given pulse value forward without returning an event.
/// Move iterator with the given pulse value forward without generating an event.
///
/// This can be used to optimize iterator skipping in some EventIter implementations, but by
/// default calls `run` and simply discards the generated event return value.
fn omit(&mut self, pulse: PulseIterItem, emit_event: bool) {
fn advance(&mut self, pulse: PulseIterItem, emit_event: bool) {
let _ = self.run(pulse, emit_event);
}

Expand Down
17 changes: 6 additions & 11 deletions src/event/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl CycleEventIter {
}

/// Generate a note event from a single cycle event, applying mappings if necessary
fn note_events(&mut self, event: CycleEvent) -> Result<Vec<Option<NoteEvent>>, String> {
fn map_note_event(&mut self, event: CycleEvent) -> Result<Vec<Option<NoteEvent>>, String> {
let mut note_events = {
if let Some(note_events) = self.mappings.get(event.string()) {
// apply custom note mappings
Expand All @@ -215,7 +215,7 @@ impl CycleEventIter {

/// Generate next batch of events from the next cycle run.
/// Converts cycle events to note events and flattens channels into note columns.
fn generate_events(&mut self) -> Vec<EventIterItem> {
fn generate(&mut self) -> Vec<EventIterItem> {
// run the cycle event generator
let events = {
match self.cycle.generate() {
Expand All @@ -232,7 +232,7 @@ impl CycleEventIter {
for event in channel_events.into_iter() {
let start = event.span().start();
let length = event.span().length();
match self.note_events(event) {
match self.map_note_event(event) {
Ok(note_events) => {
if !note_events.is_empty() {
timed_note_events.add(channel_index, start, length, note_events);
Expand All @@ -248,11 +248,6 @@ impl CycleEventIter {
// convert timed note events into EventIterItems
timed_note_events.into_event_iter_items()
}

/// Move the cycle event generator without generating any events
fn omit_events(&mut self) {
self.cycle.omit();
}
}

impl EventIter for CycleEventIter {
Expand All @@ -266,15 +261,15 @@ impl EventIter for CycleEventIter {

fn run(&mut self, _pulse: PulseIterItem, emit_event: bool) -> Option<Vec<EventIterItem>> {
if emit_event {
Some(self.generate_events())
Some(self.generate())
} else {
None
}
}

fn omit(&mut self, _pulse: PulseIterItem, emit_event: bool) {
fn advance(&mut self, _pulse: PulseIterItem, emit_event: bool) {
if emit_event {
self.omit_events()
self.cycle.advance();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/event/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl EventIter for FixedEventIter {
Some(vec![EventIterItem::new(event)])
}

fn omit(&mut self, _pulse: PulseIterItem, emit_event: bool) {
fn advance(&mut self, _pulse: PulseIterItem, emit_event: bool) {
if !emit_event || self.events.is_empty() {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/event/scripted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ScriptedEventIter {
})
}

fn generate_event(&mut self, pulse: PulseIterItem) -> LuaResult<Option<Vec<EventIterItem>>> {
fn generate(&mut self, pulse: PulseIterItem) -> LuaResult<Option<Vec<EventIterItem>>> {
// reset timeout
self.timeout_hook.reset();
// update function context
Expand All @@ -65,7 +65,7 @@ impl ScriptedEventIter {
Ok(Some(vec![EventIterItem::new(event)]))
}

fn omit_event(&mut self, pulse: PulseIterItem) -> LuaResult<()> {
fn advance(&mut self, pulse: PulseIterItem) -> LuaResult<()> {
if self.callback.is_stateful().unwrap_or(true) {
// reset timeout
self.timeout_hook.reset();
Expand Down Expand Up @@ -116,7 +116,7 @@ impl EventIter for ScriptedEventIter {
fn run(&mut self, pulse: PulseIterItem, emit_event: bool) -> Option<Vec<EventIterItem>> {
// generate a new event and move or only update pulse counters
if emit_event {
let event = match self.generate_event(pulse) {
let event = match self.generate(pulse) {
Ok(event) => event,
Err(err) => {
self.callback.handle_error(&err);
Expand All @@ -134,10 +134,10 @@ impl EventIter for ScriptedEventIter {
}
}

fn omit(&mut self, pulse: PulseIterItem, emit_event: bool) {
fn advance(&mut self, pulse: PulseIterItem, emit_event: bool) {
// generate a new event and move or only update pulse counters
if emit_event {
if let Err(err) = self.omit_event(pulse) {
if let Err(err) = self.advance(pulse) {
self.callback.handle_error(&err);
}
self.step += 1;
Expand Down
13 changes: 4 additions & 9 deletions src/event/scripted_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ScriptedCycleEventIter {

/// Generate next batch of events from the next cycle run.
/// Converts cycle events to note events and flattens channels into note columns.
fn generate_events(&mut self) -> Vec<EventIterItem> {
fn generate(&mut self) -> Vec<EventIterItem> {
// run the cycle event generator
let events = {
match self.cycle.generate() {
Expand Down Expand Up @@ -170,11 +170,6 @@ impl ScriptedCycleEventIter {
// convert timed note events into EventIterItems
timed_note_events.into_event_iter_items()
}

/// Move the cycle event generator without generating any events
fn omit_events(&mut self) {
self.cycle.omit();
}
}

impl EventIter for ScriptedCycleEventIter {
Expand Down Expand Up @@ -202,15 +197,15 @@ impl EventIter for ScriptedCycleEventIter {

fn run(&mut self, _pulse: PulseIterItem, emit_event: bool) -> Option<Vec<EventIterItem>> {
if emit_event {
Some(self.generate_events())
Some(self.generate())
} else {
None
}
}

fn omit(&mut self, _pulse: PulseIterItem, emit_event: bool) {
fn advance(&mut self, _pulse: PulseIterItem, emit_event: bool) {
if emit_event {
self.omit_events()
self.cycle.advance();
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ impl Phrase {
}
}

/// Seek rhythms until a given sample time is reached, ignoring all events until that time.
pub fn skip_events_until_time(&mut self, sample_time: SampleTime) {
/// Move rhythms until a given sample time is reached, ignoring all events until that time.
pub fn advance_until_time(&mut self, sample_time: SampleTime) {
// skip next events in all rhythms
for (rhythm_slot, next_event) in self
.rhythm_slots
Expand All @@ -126,13 +126,13 @@ impl Phrase {
// skip cached, next due events
if let Some((_, event)) = next_event {
if event.time >= sample_time {
// cached event is not yet due: no need to seek the slot
// cached event is not yet due: no need to advance the slot
continue;
}
*next_event = None;
}
if let RhythmSlot::Rhythm(rhythm) = rhythm_slot {
rhythm.borrow_mut().skip_until_time(sample_time);
rhythm.borrow_mut().advance_until_time(sample_time);
}
}
}
Expand Down Expand Up @@ -246,8 +246,8 @@ impl RhythmIter for Phrase {
.map(|(_, event)| event)
}

fn skip_until_time(&mut self, sample_time: SampleTime) {
self.skip_events_until_time(sample_time)
fn advance_until_time(&mut self, sample_time: SampleTime) {
self.advance_until_time(sample_time)
}
}

Expand Down Expand Up @@ -469,8 +469,8 @@ mod test {
}

// fast skip using skip_events_until_time
fn skip_phrase_by_omitting(phrase: &mut Phrase, time: SampleTime) {
phrase.skip_events_until_time(time)
fn skip_phrase_by_advancing(phrase: &mut Phrase, time: SampleTime) {
phrase.advance_until_time(time)
}

#[test]
Expand All @@ -485,7 +485,7 @@ mod test {
phrase2.set_sample_offset(sample_offset);
let mut events2 = Vec::new();

// run_time, seek_time
// run_time, advance_time
let run_steps = [
(1024, 1),
(2000, 555432),
Expand All @@ -507,7 +507,7 @@ mod test {

sample_time += seek_time;
skip_phrase_by_running(&mut phrase1, sample_time);
skip_phrase_by_omitting(&mut phrase2, sample_time);
skip_phrase_by_advancing(&mut phrase2, sample_time);
}

assert_eq!(events1, events2);
Expand Down
6 changes: 3 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ impl SamplePlayer {
// match playing notes state to the passed rhythm
self.playing_notes
.resize(sequence.phrase_rhythm_slot_count(), HashMap::new());
// seek new phase to our previously played time
sequence.skip_events_until_time(self.emitted_sample_time);
// move new phase to our previously played time
sequence.advance_until_time(self.emitted_sample_time);
log::debug!(target: "Player",
"Seek sequence to time {:.2}",
"Advance sequence to time {:.2}",
time_base.samples_to_seconds(self.emitted_sample_time)
);
}
Expand Down
17 changes: 7 additions & 10 deletions src/rhythm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,18 @@ pub trait RhythmIter: Debug {
/// Set a new custom sample offset value.
fn set_sample_offset(&mut self, sample_offset: SampleTime);

/// Step iter: runs pattern to generate a new pulse, then generates an event from the event iter.
/// Returns `None` when the pattern finished playing.
fn run(&mut self) -> Option<RhythmIterItem> {
self.run_until_time(SampleTime::MAX)
}
/// Sample time iter: Generates the next due event but running the pattern to generate a new
/// Sample time iter: Generate a single next due event but running the pattern to generate a new
/// pulse, if the pulse's sample time is smaller than the given sample time. Then generates an
/// event from the event iter and returns it. Else returns `None` when the pattern finished playing.
/// event from the event iter and returns it.
///
/// Returns `None` when no event is due of when the pattern finished playing, else Some event.
fn run_until_time(&mut self, sample_time: SampleTime) -> Option<RhythmIterItem>;

/// Skip, dry run *all events* until the given target time is reached.
/// Skip *all events* until the given target time is reached.
///
/// This calls `run_until_time` by default, until the target time is reached and
/// discards all generated events, but may be overridden to optimize run time.
fn skip_until_time(&mut self, sample_time: SampleTime) {
fn advance_until_time(&mut self, sample_time: SampleTime) {
while self.run_until_time(sample_time).is_some() {
// continue
}
Expand All @@ -88,7 +85,7 @@ impl Iterator for dyn RhythmIter {
type Item = RhythmIterItem;

fn next(&mut self) -> Option<Self::Item> {
self.run()
self.run_until_time(SampleTime::MAX)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/rhythm/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<Step: GenericRhythmTimeStep, Offset: GenericRhythmTimeStep> Iterator
type Item = RhythmIterItem;

fn next(&mut self) -> Option<Self::Item> {
self.run()
self.run_until_time(SampleTime::MAX)
}
}

Expand All @@ -353,7 +353,7 @@ impl<Step: GenericRhythmTimeStep, Offset: GenericRhythmTimeStep> RhythmIter
self.run_rhythm(sample_time, fetch_new_items)
}

fn skip_until_time(&mut self, sample_time: SampleTime) {
fn advance_until_time(&mut self, sample_time: SampleTime) {
// memorize current time
self.event_iter_sample_time = sample_time;
// clear pending event iter items with regular runs
Expand All @@ -371,7 +371,7 @@ impl<Step: GenericRhythmTimeStep, Offset: GenericRhythmTimeStep> RhythmIter
if self.pattern_playback_finished {
return;
}
// batch omit events in whole steps, if possible
// batch advance event iter in full pulse steps
loop {
// quickly check if the next event is due before the given target time
let next_sample_time = self.sample_offset as f64 + self.event_iter_next_sample_time;
Expand All @@ -384,8 +384,8 @@ impl<Step: GenericRhythmTimeStep, Offset: GenericRhythmTimeStep> RhythmIter
// test if the event iter crosses the target time
let step_duration = self.current_steps_sample_duration();
if ((next_sample_time + step_duration) as SampleTime) < sample_time {
// omit all events from the gated pulse
self.event_iter.omit(pulse, emit_event);
// skip all events from the gated pulse
self.event_iter.advance(pulse, emit_event);
self.event_iter_next_sample_time += step_duration;
} else {
// generate new events from the gated pulse
Expand Down
Loading

0 comments on commit b891254

Please sign in to comment.