cloudseedcore_rs/
reverb.rs

1use crate::bridge::*;
2use crate::{ParamId, Program};
3
4/// A stereo reverb.
5///
6/// This is a safe wrapper around the CloudSeedCore ReverbController.
7pub struct ReverbController {
8    inner: cxx::UniquePtr<CloudSeedReverb>,
9    max_block_size: u32,
10}
11
12impl ReverbController {
13    /// Creates a reverb instance with the given sample rate
14    /// and maximum block size that will be passed to `process`.
15    pub fn new(sample_rate: f32, max_block_size: u32) -> Self {
16        let inner = cs_new_reverb(sample_rate, max_block_size);
17        Self {
18            inner,
19            max_block_size,
20        }
21    }
22
23    /// Returns the maximum block size that can be passed to `process` for this instance.
24    pub fn max_block_size(&self) -> u32 {
25        self.max_block_size
26    }
27
28    /// Processes a stereo signal.
29    /// All buffers must be at least `num_samples` long.
30    /// Panics if `num_samples` is greater than `max_block_size`.
31    pub fn process(
32        &mut self,
33        in_l: &[f32],
34        in_r: &[f32],
35        out_l: &mut [f32],
36        out_r: &mut [f32],
37        num_samples: u32,
38    ) {
39        assert!(num_samples <= self.max_block_size);
40
41        self.inner
42            .as_mut()
43            .unwrap()
44            .process(in_l, in_r, out_l, out_r, num_samples);
45    }
46
47    /// Clears internal buffers, ending any ongoing reverb tail.
48    pub fn reset(&mut self) {
49        self.inner.as_mut().unwrap().reset();
50    }
51
52    /// Updates the reverb's sample rate in Hz.
53    pub fn set_sample_rate(&mut self, sample_rate: f32) {
54        self.inner.as_mut().unwrap().set_sample_rate(sample_rate);
55    }
56
57    /// Applies a normalized parameter value in range 0..1
58    /// to the parameter with the given id.
59    pub fn set_parameter(&mut self, id: ParamId, value: f32) {
60        let id: u8 = id.into();
61        self.inner.as_mut().unwrap().set_parameter(id as u32, value);
62    }
63
64    /// Returns the normalized parameter value in range 0..1 for the given parameter id.
65    pub fn get_parameter(&self, id: ParamId) -> f32 {
66        let id: u8 = id.into();
67        self.inner.as_ref().unwrap().get_parameter(id as u32)
68    }
69
70    /// Returns a snapshot of all current parameter values.
71    /// The returned [Program] can be used to serialize parameter state.
72    pub fn get_program(&self) -> Program {
73        // fill a temporary array using the cxx bridge, then convert to typed Program
74        let mut vals = [0.0f32; 45];
75        self.inner.as_ref().unwrap().get_all_parameters(&mut vals);
76        Program::from_array(vals)
77    }
78
79    /// Loads a program, ending any ongoing reverb tail.
80    pub fn set_program(&mut self, program: &Program) {
81        let params = program.to_array();
82        self.inner.as_mut().unwrap().load_program(&params);
83    }
84}