Skip to main content

rill_core/traits/
parameter_write.rs

1//! ParameterWrite — polymorphic control interface for DSP engines.
2//!
3//! Decouples parameter dispatch from the concrete engine type,
4//! enabling generic controllers (sequencers, MIDI, OSC) to write
5//! parameters to oscillators, chip emulators, or any `Algorithm`
6//! implementor through a uniform `write_parameter(name, value)` call.
7
8use crate::traits::{ParamValue, ProcessResult};
9
10/// Polymorphic parameter write interface for DSP engines.
11///
12/// Implementors accept named parameter writes and apply them
13/// immediately to internal state.  The set of supported names
14/// is engine-specific.
15///
16/// # Relationship to `Algorithm<T>`
17///
18/// `ParameterWrite` handles the *control* path (parameter changes
19/// from UI, MIDI, OSC, sequencers).  `Algorithm<T>` handles the
20/// *signal* path (per-block audio generation via `process()`).
21/// Engines typically implement both.
22///
23/// # Example
24///
25/// ```ignore
26/// fn send_cc(target: &mut dyn ParameterWrite, cc: u8, value: u8) {
27///     let _ = target.write_parameter(
28///         "amplitude",
29///         ParamValue::Float(value as f32 / 127.0),
30///     );
31/// }
32/// ```
33pub trait ParameterWrite {
34    /// Write a named parameter value.
35    ///
36    /// Returns `Ok(())` if the parameter was applied, or
37    /// `Err(ProcessError)` if the name is unknown or the value
38    /// type is invalid.
39    fn write_parameter(&mut self, name: &str, value: ParamValue) -> ProcessResult<()>;
40
41    /// Read a named parameter value.
42    ///
43    /// Returns `None` by default — engines that support reading
44    /// override to return current values.
45    fn read_parameter(&self, _name: &str) -> Option<ParamValue> {
46        None
47    }
48}