Rework Params trait API with Arc instead of Pin

This is a breaking change requiring a small change to plugin
implementations.

The reason why `Pin<&dyn Params>` was used was more as a hint to
indicate that the object must last for the plugin's lifetime, but `Pin`
doesn't enforce that. It also makes the APIs a lot more awkward.
Requiring the use of `Arc` fixes the following problems:

- When storing the params object in the wrapper, the `ParamPtr`s are
  guaranteed to be stable.
- This makes it possible to access the `Params` object without acquiring
  a lock on the plugin, this is very important for implementing
  plugin-side preset management.
- It enforces immutability on the `Params` object.
- And of course the API is much nicer without a bunch of unsafe code to
  work around Pin's limitations.
This commit is contained in:
Robbert van der Helm
2022-04-07 15:31:46 +02:00
parent 7cc05fce9a
commit 083885a40c
24 changed files with 105 additions and 115 deletions

View File

@@ -18,7 +18,6 @@ use nih_plug::prelude::*;
use realfft::num_complex::Complex32;
use realfft::{ComplexToReal, RealFftPlanner, RealToComplex};
use std::f32;
use std::pin::Pin;
use std::sync::Arc;
const MIN_WINDOW_ORDER: usize = 6;
@@ -41,7 +40,7 @@ const MAX_OVERLAP_ORDER: usize = 5;
const MAX_OVERLAP_TIMES: usize = 1 << MAX_OVERLAP_ORDER; // 32
struct PubertySimulator {
params: Pin<Box<PubertySimulatorParams>>,
params: Arc<PubertySimulatorParams>,
/// An adapter that performs most of the overlap-add algorithm for us.
stft: util::StftHelper,
@@ -82,7 +81,7 @@ struct PubertySimulatorParams {
impl Default for PubertySimulator {
fn default() -> Self {
Self {
params: Box::pin(PubertySimulatorParams::default()),
params: Arc::new(PubertySimulatorParams::default()),
stft: util::StftHelper::new(2, MAX_WINDOW_SIZE),
window_function: Vec::with_capacity(MAX_WINDOW_SIZE),
@@ -150,8 +149,8 @@ impl Plugin for PubertySimulator {
const DEFAULT_NUM_INPUTS: u32 = 2;
const DEFAULT_NUM_OUTPUTS: u32 = 2;
fn params(&self) -> Pin<&dyn Params> {
self.params.as_ref()
fn params(&self) -> Arc<dyn Params> {
self.params.clone()
}
fn accepts_bus_config(&self, config: &BusConfig) -> bool {