Skip to content

Commit 7b376df

Browse files
committed
refactor scheduling as a stateful struct
1 parent 5ed5ad3 commit 7b376df

2 files changed

Lines changed: 142 additions & 127 deletions

File tree

note_off_delay/src/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ use vst::buffer::{AudioBuffer, SendEventBuffer};
1212
use vst::event::Event;
1313
use vst::plugin::{CanDo, Category, HostCallback, Info, Plugin};
1414

15+
use crate::parameters::PARAMETER_COUNT;
1516
use parameters::NoteOffDelayPluginParameters;
1617
use parameters::Parameter;
18+
use std::mem::take;
1719
use util::absolute_time_midi_message_vector::AbsoluteTimeMidiMessageVector;
20+
use util::delayed_message_consumer::{MessageReason, ScheduledEventsHelper};
1821
use util::logging::logging_setup;
19-
use util::delayed_message_consumer::{process_scheduled_events, MessageReason};
2022
use util::messages::format_event;
2123
use util::midi_message_type::MidiMessageType;
2224
use util::parameters::ParameterConversion;
23-
use crate::parameters::PARAMETER_COUNT;
2425

2526
plugin_main!(NoteOffDelayPlugin);
2627

@@ -47,20 +48,19 @@ impl Default for NoteOffDelayPlugin {
4748
impl NoteOffDelayPlugin {
4849
fn send_events(&mut self, samples: usize) {
4950
if let Ok(mut host_callback_lock) = self.parameters.host_mutex.lock() {
50-
let (next_message_queue, events) = process_scheduled_events(
51+
let mut helper = ScheduledEventsHelper::new(
5152
samples,
52-
self.current_time_in_samples,
53-
&self.message_queue,
53+
self.parameters.get_delay().is_active(),
5454
self.parameters.get_max_notes(),
5555
self.parameters
5656
.get_bool_parameter(Parameter::MaxNotesAppliesToDelayedNotesOnly),
57-
self.parameters.get_delay().is_active(),
57+
self.current_time_in_samples,
5858
);
59-
60-
self.message_queue = next_message_queue;
59+
helper.process_scheduled_events(&self.message_queue);
60+
self.message_queue = take(&mut helper.queued_messages);
6161
self.send_buffer
6262
.borrow_mut()
63-
.send_events(events, &mut host_callback_lock.host);
63+
.send_events(helper.events, &mut host_callback_lock.host);
6464
}
6565
}
6666

@@ -108,7 +108,10 @@ impl Plugin for NoteOffDelayPlugin {
108108

109109
fn new(host: HostCallback) -> Self {
110110
logging_setup();
111-
info!("{}", build_info::format!("{{{} v{} built with {} at {}}}", $.crate_info.name, $.crate_info.version, $.compiler, $.timestamp));
111+
info!(
112+
"{}",
113+
build_info::format!("{{{} v{} built with {} at {}}}", $.crate_info.name, $.crate_info.version, $.compiler, $.timestamp)
114+
);
112115
let parameters = NoteOffDelayPluginParameters::new(host);
113116
NoteOffDelayPlugin {
114117
current_time_in_samples: 0,
@@ -176,16 +179,17 @@ impl Plugin for NoteOffDelayPlugin {
176179
MidiMessageType::NoteOffMessage(note_off) => {
177180
let note_off_play_time = midi_event.delta_frames as usize + self.current_time_in_samples;
178181

179-
self.message_queue.insert_message(
180-
midi_event.data,
181-
note_off_play_time,
182-
MessageReason::Live,
183-
);
182+
self.message_queue
183+
.insert_message(midi_event.data, note_off_play_time, MessageReason::Live);
184184

185-
let delay = self.parameters.get_delay() ;
185+
let delay = self.parameters.get_delay();
186186

187187
if delay.is_active() {
188-
match self.message_queue.get_matching_note_on(note_off.channel, note_off.pitch).cloned() {
188+
match self
189+
.message_queue
190+
.get_matching_note_on(note_off.channel, note_off.pitch)
191+
.cloned()
192+
{
189193
None => {}
190194
Some(note_on) => {
191195
let duration = note_off_play_time - note_on.play_time_in_samples;

0 commit comments

Comments
 (0)