Skip to main content

aura_core/
process.rs

1//! Per-block process context (minimal).
2
3use aura_midi::{MidiBuffer, UmpBuffer};
4
5use crate::config::ProcessMode;
6use crate::note_events::NoteBuffer;
7use crate::tuning::Tuning;
8
9/// What the plugin wants after `process`.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
11pub enum ProcessStatus {
12    /// Keep calling process (normal).
13    #[default]
14    Continue,
15    /// No more output expected until something changes (tail finished).
16    TailFinished,
17    /// Error — host may suspend.
18    Error,
19}
20
21/// Per-block context handed to `process()`.
22///
23/// Format wrappers fill [`Self::midi`] (7-bit) and [`Self::ump`] (native
24/// packets). CLAP writes `CLAP_EVENT_MIDI2` into `ump` unchanged and still
25/// mirrors a MIDI 1 image into `midi` when one exists. VST3/LV2 lift 7-bit
26/// MIDI into type-0x2 UMP so a plugin can read one buffer.
27///
28/// CLAP also fills [`Self::notes`] with native note-on/off/choke, note
29/// expressions, and per-note `PARAM_MOD` (`note_id >= 0`).
30///
31/// Plugin → host: [`Self::midi_out`] (7-bit), [`Self::ump_out`] (native UMP;
32/// CLAP emits `CLAP_EVENT_MIDI2`), [`Self::notes_out`] (`NOTE_*` / `NOTE_END`).
33/// VST3/LV2 down-convert `ump_out` and On/Off/Choke.
34#[non_exhaustive]
35pub struct ProcessContext {
36    pub sample_rate: f64,
37    pub block_size: usize,
38    pub process_mode: ProcessMode,
39    /// Host timeline for this block; `None` when the host provides none.
40    pub transport: Option<crate::transport::Transport>,
41    /// Host → plugin MIDI / note events for this block (sorted by sample offset).
42    pub midi: MidiBuffer,
43    /// Plugin → host MIDI / note events for this block (sorted by sample offset).
44    pub midi_out: MidiBuffer,
45    /// CLAP-shaped notes / expressions / poly mods (empty on VST3/LV2 today).
46    pub notes: NoteBuffer,
47    /// Plugin → host CLAP notes (`NOTE_ON`/`OFF`/`CHOKE`/`END` / expressions).
48    pub notes_out: NoteBuffer,
49    /// Host → plugin Universal MIDI Packets (MIDI 2 native + MIDI 1 as type 0x2).
50    pub ump: UmpBuffer,
51    /// Plugin → host UMP. CLAP emits `CLAP_EVENT_MIDI2`; VST3/LV2 down-convert.
52    pub ump_out: UmpBuffer,
53    /// Host-driven tuning context (CLAP `clap.tuning`).
54    pub tuning: Tuning,
55}
56
57impl ProcessContext {
58    #[must_use]
59    pub fn new(sample_rate: f64, block_size: usize) -> Self {
60        Self {
61            sample_rate,
62            block_size,
63            process_mode: ProcessMode::Realtime,
64            transport: None,
65            midi: MidiBuffer::new(),
66            midi_out: MidiBuffer::new(),
67            notes: NoteBuffer::new(),
68            notes_out: NoteBuffer::new(),
69            ump: UmpBuffer::new(),
70            ump_out: UmpBuffer::new(),
71            tuning: Tuning::disabled(),
72        }
73    }
74
75    #[must_use]
76    pub fn with_process_mode(mut self, mode: ProcessMode) -> Self {
77        self.process_mode = mode;
78        self
79    }
80
81    #[must_use]
82    pub fn with_transport(mut self, transport: crate::transport::Transport) -> Self {
83        self.transport = Some(transport);
84        self
85    }
86
87    #[must_use]
88    pub fn with_midi(mut self, midi: MidiBuffer) -> Self {
89        self.midi = midi;
90        self
91    }
92
93    #[must_use]
94    pub fn with_midi_out(mut self, midi_out: MidiBuffer) -> Self {
95        self.midi_out = midi_out;
96        self
97    }
98
99    #[must_use]
100    pub fn with_notes(mut self, notes: NoteBuffer) -> Self {
101        self.notes = notes;
102        self
103    }
104
105    #[must_use]
106    pub fn with_notes_out(mut self, notes_out: NoteBuffer) -> Self {
107        self.notes_out = notes_out;
108        self
109    }
110
111    #[must_use]
112    pub fn with_ump(mut self, ump: UmpBuffer) -> Self {
113        self.ump = ump;
114        self
115    }
116
117    #[must_use]
118    pub fn with_ump_out(mut self, ump_out: UmpBuffer) -> Self {
119        self.ump_out = ump_out;
120        self
121    }
122
123    #[must_use]
124    pub fn with_tuning(mut self, tuning: Tuning) -> Self {
125        self.tuning = tuning;
126        self
127    }
128
129    /// Clear all event buffers so the context can be reused across blocks.
130    pub fn clear_events(&mut self) {
131        self.midi.clear();
132        self.midi_out.clear();
133        self.notes.clear();
134        self.notes_out.clear();
135        self.ump.clear();
136        self.ump_out.clear();
137    }
138}