From 3d8c7920442bfb4a346725fc3bc2e677cd61d252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20M=C3=BCller?= Date: Mon, 7 Oct 2024 15:29:23 +0200 Subject: [PATCH] add new "consume_event" function to phrase --- src/phrase.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/phrase.rs b/src/phrase.rs index affe7a5..43f4f20 100644 --- a/src/phrase.rs +++ b/src/phrase.rs @@ -119,13 +119,25 @@ impl Phrase { &self.rhythm_slots } + /// Run rhythms to produce a single next event, calling the given `consumer` + /// visitor function when an event got produced. + pub fn consume_event(&mut self, consumer: &mut F) + where + F: FnMut(RhythmIndex, RhythmIterItem), + { + // emit and consume next event, if any + if let Some((rhythm_index, rhythm_item)) = self.next_event_until_time(SampleTime::MAX) { + consumer(rhythm_index, rhythm_item); + } + } + /// Run rhythms until a given sample time is reached, calling the given `consumer` /// visitor function for all emitted events. pub fn consume_events_until_time(&mut self, sample_time: SampleTime, consumer: &mut F) where F: FnMut(RhythmIndex, RhythmIterItem), { - // emit next events until we've reached the desired sample_time + // emit and consume next events until we've reached the desired sample_time 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);