Skip to main content

aura_core/
events.rs

1//! GUI → audio-thread param event queue.
2
3use std::sync::Mutex;
4
5/// Param event from the editor toward the host (gestures mark automation).
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub enum ParamEvent {
8    GestureBegin(u32),
9    Value { id: u32, plain: f64 },
10    GestureEnd(u32),
11}
12
13/// GUI → audio-thread param event queue.
14///
15/// Editor (`push`, GUI thread) → format wrapper (`drain` in `process` / `flush`,
16/// audio thread) → host automation events.
17///
18/// **Capacity:** unbounded `Mutex<Vec<_>>`. Not a ring. A poisoned lock
19/// (`push` no-op, `drain` returns empty) **drops events** rather than
20/// panicking the audio thread. That full-drop is intentional
21/// (`ponytail:` no lock-free ring until profiling says so).
22#[derive(Default)]
23pub struct ParamEventQueue {
24    inner: Mutex<Vec<ParamEvent>>,
25}
26
27impl ParamEventQueue {
28    pub fn push(&self, event: ParamEvent) {
29        if let Ok(mut v) = self.inner.lock() {
30            v.push(event);
31        }
32    }
33
34    /// Take all pending events (oldest first).
35    pub fn drain(&self) -> Vec<ParamEvent> {
36        match self.inner.lock() {
37            Ok(mut v) => std::mem::take(&mut *v),
38            Err(_) => Vec::new(),
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn drain_fifo_oldest_first() {
49        let q = ParamEventQueue::default();
50        q.push(ParamEvent::GestureBegin(1));
51        q.push(ParamEvent::Value { id: 1, plain: 0.5 });
52        q.push(ParamEvent::GestureEnd(1));
53        assert_eq!(
54            q.drain(),
55            [
56                ParamEvent::GestureBegin(1),
57                ParamEvent::Value { id: 1, plain: 0.5 },
58                ParamEvent::GestureEnd(1),
59            ]
60        );
61        assert_eq!(q.drain().len(), 0);
62    }
63}