Skip to main content

truce_core/
process.rs

1use crate::events::{EventBody, EventList, TransportInfo};
2
3pub struct ProcessContext<'a> {
4    pub transport: &'a TransportInfo,
5    pub sample_rate: f64,
6    pub block_size: usize,
7    pub output_events: &'a mut EventList,
8    params_fn: Option<&'a dyn Fn(u32) -> f64>,
9    meters_fn: Option<&'a dyn Fn(u32, f32)>,
10}
11
12impl<'a> ProcessContext<'a> {
13    pub fn new(
14        transport: &'a TransportInfo,
15        sample_rate: f64,
16        block_size: usize,
17        output_events: &'a mut EventList,
18    ) -> Self {
19        Self {
20            transport,
21            sample_rate,
22            block_size,
23            output_events,
24            params_fn: None,
25            meters_fn: None,
26        }
27    }
28
29    /// Set the parameter lookup callback.
30    pub fn with_params(mut self, f: &'a dyn Fn(u32) -> f64) -> Self {
31        self.params_fn = Some(f);
32        self
33    }
34
35    /// Set the meter reporting callback.
36    pub fn with_meters(mut self, f: &'a dyn Fn(u32, f32)) -> Self {
37        self.meters_fn = Some(f);
38        self
39    }
40
41    /// Read a parameter's plain value by ID.
42    pub fn param(&self, id: u32) -> f64 {
43        match self.params_fn {
44            Some(f) => f(id),
45            None => 0.0,
46        }
47    }
48
49    /// Report a meter value (0.0 to 1.0).
50    pub fn set_meter(&self, id: impl Into<u32>, value: f32) {
51        let id = id.into();
52        if let Some(f) = self.meters_fn {
53            f(id, value);
54        }
55    }
56
57    /// Sync a Params struct with parameter changes from the given events.
58    pub fn sync_params<P: truce_params::Params>(&self, events: &EventList, params: &mut P) {
59        for event in events.iter() {
60            if let EventBody::ParamChange { id, value } = &event.body {
61                params.set_normalized(*id, *value);
62            }
63        }
64        params.snap_smoothers();
65    }
66}
67
68#[derive(Clone, Copy, Debug, PartialEq)]
69pub enum ProcessStatus {
70    /// Plugin produced meaningful output.
71    Normal,
72    /// Plugin is producing tail. Value = remaining tail samples.
73    Tail(u32),
74    /// Keep alive even if input is silent.
75    KeepAlive,
76}