1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2022 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.

#[derive(Debug, Clone, Copy)]
pub struct HxTimedEvent {
    /// The frame number in the current block by the audio driver or plugin API/DAW
    timing: usize,
    kind: HxMidiEvent,
}

impl HxTimedEvent {
    pub fn new_timed(timing: usize, kind: HxMidiEvent) -> Self {
        Self { timing, kind }
    }

    pub fn is_cc(&self) -> bool {
        if let HxMidiEvent::CC { .. } = self.kind {
            true
        } else {
            false
        }
    }

    pub fn kind(&self) -> HxMidiEvent {
        self.kind
    }

    pub fn cc(timing: usize, channel: u8, cc: u8, value: f32) -> Self {
        Self { timing, kind: HxMidiEvent::CC { channel, cc, value } }
    }

    pub fn note_on(timing: usize, channel: u8, note: u8, vel: f32) -> Self {
        Self { timing, kind: HxMidiEvent::NoteOn { channel, note, vel } }
    }

    pub fn note_off(timing: usize, channel: u8, note: u8) -> Self {
        Self { timing, kind: HxMidiEvent::NoteOff { channel, note } }
    }
}

pub struct MidiEventPointer<'a> {
    buf: &'a [HxTimedEvent],
    idx: usize,
}

impl<'a> MidiEventPointer<'a> {
    pub fn new(buf: &'a [HxTimedEvent]) -> Self {
        Self { buf, idx: 0 }
    }

    pub fn next_at(&mut self, time: usize) -> Option<HxMidiEvent> {
        if self.idx < self.buf.len() && self.buf[self.idx].timing <= time {
            self.idx += 1;
            Some(self.buf[self.idx - 1].kind)
        } else {
            None
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub enum HxMidiEvent {
    NoteOn { channel: u8, note: u8, vel: f32 },
    NoteOff { channel: u8, note: u8 },
    CC { channel: u8, cc: u8, value: f32 },
}

pub struct EventWindowing {
    pub event: Option<HxTimedEvent>,
}

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: HxTimedEvent) {
        self.event = Some(event);
    }

    #[inline]
    pub fn next_event_in_range(
        &mut self,
        to_time: usize,
        block_size: usize,
    ) -> Option<HxTimedEvent> {
        if let Some(event) = self.event.take() {
            if event.timing < (to_time + block_size) {
                return Some(HxTimedEvent { timing: event.timing - to_time, kind: event.kind });
            } else {
                self.event = Some(event);
            }
        }

        None
    }
}