Skip to content

Commit

Permalink
Merge 774fde6 into 0295922
Browse files Browse the repository at this point in the history
  • Loading branch information
emuell authored Jul 2, 2024
2 parents 0295922 + 774fde6 commit 03cbd37
Show file tree
Hide file tree
Showing 12 changed files with 642 additions and 177 deletions.
2 changes: 1 addition & 1 deletion benches/benchmarks/rhythm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn seek(c: &mut Criterion) {
phrase.reset();
let mut sample_time = samples_per_sec as SampleTime;
while sample_time < (seek_time * samples_per_sec) as SampleTime {
phrase.seek_until_time(sample_time);
phrase.skip_until_time(sample_time);
sample_time += seek_step * samples_per_sec as SampleTime;
}
})
Expand Down
2 changes: 1 addition & 1 deletion benches/benchmarks/scripted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn seek(c: &mut Criterion) {
phrase.reset();
let mut sample_time = samples_per_sec as SampleTime;
while sample_time < (seek_time * samples_per_sec) as SampleTime {
phrase.seek_until_time(sample_time);
phrase.skip_until_time(sample_time);
sample_time += seek_step * samples_per_sec as SampleTime;
}
})
Expand Down
10 changes: 9 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,15 @@ 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>>;

/// Create a new cloned instance of this event iter. This actualy is a clone(), wrapped into
/// Move iterator with the given pulse value forward without returning 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) {
let _ = self.run(pulse, emit_event);
}

/// Create a new cloned instance of this event iter. This actually is a clone(), wrapped into
/// a `Box<dyn EventIter>`, but called 'duplicate' to avoid conflicts with possible
/// Clone impls.
fn duplicate(&self) -> Box<dyn EventIter>;
Expand Down
14 changes: 14 additions & 0 deletions src/event/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ impl CycleEventIter {
// convert timed note events into EventIterItems
timed_note_events.into_event_iter_items()
}

/// Generate next batch of events from the next cycle run but ignore the results.
fn omit_events(&mut self) {
// run the cycle event generator
if let Err(err) = self.cycle.omit() {
panic!("Cycle runtime error: {err}");
}
}
}

impl EventIter for CycleEventIter {
Expand All @@ -267,6 +275,12 @@ impl EventIter for CycleEventIter {
}
}

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

fn duplicate(&self) -> Box<dyn EventIter> {
Box::new(self.clone())
}
Expand Down
12 changes: 8 additions & 4 deletions src/event/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ impl EventIter for FixedEventIter {
return None;
}
let event = self.events[self.event_index].clone();
self.event_index += 1;
if self.event_index >= self.events.len() {
self.event_index = 0;
}
self.event_index = (self.event_index + 1) % self.events.len();
Some(vec![EventIterItem::new(event)])
}

fn omit(&mut self, _pulse: PulseIterItem, emit_event: bool) {
if !emit_event || self.events.is_empty() {
return;
}
self.event_index = (self.event_index + 1) % self.events.len();
}

fn duplicate(&self) -> Box<dyn EventIter> {
Box::new(self.clone())
}
Expand Down
16 changes: 6 additions & 10 deletions src/event/mutated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,13 @@ impl EventIter for MutatedEventIter {
}

fn run(&mut self, _pulse: PulseIterItem, emit_event: bool) -> Option<Vec<EventIterItem>> {
if emit_event {
let event = self.events[self.event_index].clone();
self.events[self.event_index] = Self::mutate(event.clone(), &mut self.map);
self.event_index += 1;
if self.event_index >= self.events.len() {
self.event_index = 0;
}
Some(vec![EventIterItem::new(event)])
} else {
None
if !emit_event || self.events.is_empty() {
return None;
}
let event = self.events[self.event_index].clone();
self.events[self.event_index] = Self::mutate(event.clone(), &mut self.map);
self.event_index = (self.event_index + 1) % self.events.len();
Some(vec![EventIterItem::new(event)])
}

fn duplicate(&self) -> Box<dyn EventIter> {
Expand Down
32 changes: 30 additions & 2 deletions src/event/scripted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ScriptedEventIter {
})
}

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

fn omit_event(&mut self, pulse: PulseIterItem) -> LuaResult<()> {
// reset timeout
self.timeout_hook.reset();
// update function context
self.callback.set_context_pulse_value(pulse)?;
self.callback
.set_context_pulse_step(self.pulse_step, self.pulse_time_step)?;
self.callback.set_context_step(self.step)?;
// invoke callback and ignore the result
self.callback.call()?;
Ok(())
}
}

impl Clone for ScriptedEventIter {
Expand Down Expand Up @@ -99,7 +112,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.next_event(pulse) {
let event = match self.generate_event(pulse) {
Ok(event) => event,
Err(err) => {
self.callback.handle_error(&err);
Expand All @@ -117,6 +130,21 @@ impl EventIter for ScriptedEventIter {
}
}

fn omit(&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) {
self.callback.handle_error(&err);
}
self.step += 1;
self.pulse_step += 1;
self.pulse_time_step += pulse.step_time;
} else {
self.pulse_step += 1;
self.pulse_time_step += pulse.step_time;
}
}

fn duplicate(&self) -> Box<dyn EventIter> {
Box::new(self.clone())
}
Expand Down
14 changes: 14 additions & 0 deletions src/event/scripted_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ impl ScriptedCycleEventIter {
// convert timed note events into EventIterItems
timed_note_events.into_event_iter_items()
}

/// Generate next batch of events from the next cycle run but ignore the results.
fn omit_events(&mut self) {
// run the cycle event generator
if let Err(err) = self.cycle.omit() {
add_lua_callback_error("cycle", &LuaError::RuntimeError(err));
}
}
}

impl EventIter for ScriptedCycleEventIter {
Expand Down Expand Up @@ -203,6 +211,12 @@ impl EventIter for ScriptedCycleEventIter {
}
}

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

fn duplicate(&self) -> Box<dyn EventIter> {
Box::new(self.clone())
}
Expand Down
Loading

0 comments on commit 03cbd37

Please sign in to comment.