diff --git a/src/nodes/note_buffer.rs b/src/nodes/note_buffer.rs index 7012482..9af1aec 100644 --- a/src/nodes/note_buffer.rs +++ b/src/nodes/note_buffer.rs @@ -4,6 +4,19 @@ use crate::dsp::MAX_BLOCK_SIZE; +#[derive(Debug, Clone, Copy)] +pub struct TimedEvent { + /// The frame number in the current block by the audio driver or plugin API/DAW + timing: usize, + kind: MidiEvent, +} + +#[derive(Debug, Clone, Copy)] +pub enum MidiEvent { + NoteOn { channel: u8, note: u8, vel: f32 }, + NoteOff { channel: u8, note: u8, vel: f32 }, +} + #[derive(Debug, Clone, Copy)] pub struct NoteChannelState { pub vel: f32, @@ -84,6 +97,44 @@ impl NoteBuffer { } } +struct EventWindowing { + pub event: Option, +} + +impl EventWindowing { + pub fn new() -> Self { + Self { + event: None, + } + } + + #[inline] + pub fn feed_me(&self) -> bool { + self.event.is_none() + } + + #[inline] + pub fn feed(&mut self, event: TimedEvent) { + self.event = Some(event); + } + + #[inline] + pub fn next_event_in_range(&mut self, to_time: usize) -> Option { + let to_time = to_time as u32; + + if let Some(event) = self.event.take() { + if event.timing() < to_time { + return Some(event); + } else { + self.event = Some(event); + } + } + + None + } +} + + #[cfg(test)] mod tests { use super::*;