1
0
Fork 0

Use the new builder interface

This commit is contained in:
Robbert van der Helm 2022-02-12 17:19:52 +01:00
parent 34fa536acb
commit bc11c97fcf
4 changed files with 50 additions and 55 deletions

View file

@ -51,30 +51,26 @@ impl Default for Gain {
impl Default for GainParams { impl Default for GainParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
gain: FloatParam { gain: FloatParam::new(
value: 0.0, "Gain",
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)), 0.0,
value_changed: None, Range::Linear {
range: Range::Linear {
min: -30.0, min: -30.0,
max: 30.0, max: 30.0,
}, },
name: "Gain", )
unit: " dB", .with_smoother(Smoother::new(SmoothingStyle::Linear(50.0)))
value_to_string: formatters::f32_rounded(2), .with_unit(" dB")
string_to_value: None, .with_value_to_string(formatters::f32_rounded(2)),
}, some_int: IntParam::new(
some_int: IntParam { "Something",
value: 3, 3,
smoothed: Smoother::none(), Range::Skewed {
name: "Something",
range: Range::Skewed {
min: 0, min: 0,
max: 3, max: 3,
factor: Range::skew_factor(1.0), factor: Range::skew_factor(1.0),
}, },
..Default::default() ),
},
} }
} }
} }

View file

@ -8,6 +8,7 @@ use nih_plug::{
use nih_plug::{BoolParam, FloatParam, Param, Params, Range, Smoother, SmoothingStyle}; use nih_plug::{BoolParam, FloatParam, Param, Params, Range, Smoother, SmoothingStyle};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc;
struct Gain { struct Gain {
params: Pin<Box<GainParams>>, params: Pin<Box<GainParams>>,
@ -39,31 +40,35 @@ impl Default for Gain {
impl Default for GainParams { impl Default for GainParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
// There are three ways to specify parameters:
//
// ...either manually specify all fields:
gain: FloatParam { gain: FloatParam {
value: 0.0, value: 0.0,
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)), smoothed: Smoother::new(SmoothingStyle::Linear(50.0)),
value_changed: None, value_changed: None,
// If, for instance, updating this parameter would require other parts of the
// plugin's internal state to be updated other values to also be updated, then you
// can use a callback like this, where `requires_updates` is an `Arc<AtomicBool>`
// that's also stored on the parameters struct:
// value_changed: Some(Arc::new(move |_new| { requires_update.store(true, Ordering::Release); })),
range: Range::Linear { range: Range::Linear {
min: -30.0, min: -30.0,
max: 30.0, max: 30.0,
}, },
name: "Gain", name: "Gain",
unit: " dB", unit: " dB",
value_to_string: formatters::f32_rounded(2), value_to_string: Some(formatters::f32_rounded(2)),
string_to_value: None, string_to_value: None,
// ...or specify the fields you want to initialize directly and leave the other
// fields at their defaults:
// // ..Default::default(),
}, },
// For brevity's sake you can also use the default values. Don't forget to set the field // ...or use the builder interface:
// name, default value, and range though. the_field_name_can_change: BoolParam::new("Important value", false).with_callback(
the_field_name_can_change: BoolParam { Arc::new(|_new_value: bool| {
value: false, // If, for instance, updating this parameter would require other parts of the
name: "Important Value", // plugin's internal state to be updated other values to also be updated, then
..Default::default() // you can use this callback to for instance modify an atomic in the plugin.
}, }),
),
// Persisted fields can be intialized like any other fields, and they'll keep their when
// restoring the plugin's state.
random_data: RwLock::new(Vec::new()), random_data: RwLock::new(Vec::new()),
} }
} }

View file

@ -58,36 +58,30 @@ impl Default for Sine {
impl Default for SineParams { impl Default for SineParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
gain: FloatParam { gain: FloatParam::new(
value: -10.0, "Gain",
smoothed: Smoother::new(SmoothingStyle::Linear(3.0)), -10.0,
range: Range::Linear { Range::Linear {
min: -30.0, min: -30.0,
max: 0.0, max: 0.0,
}, },
name: "Gain", )
unit: " dB", .with_smoother(Smoother::new(SmoothingStyle::Linear(3.0)))
value_to_string: formatters::f32_rounded(2), .with_unit(" dB")
..Default::default() .with_value_to_string(formatters::f32_rounded(2)),
}, frequency: FloatParam::new(
frequency: FloatParam { "Frequency",
value: 420.0, 420.0,
smoothed: Smoother::new(SmoothingStyle::Linear(10.0)), Range::Skewed {
range: Range::Skewed {
min: 1.0, min: 1.0,
max: 20_000.0, max: 20_000.0,
factor: Range::skew_factor(-2.0), factor: Range::skew_factor(-2.0),
}, },
name: "Frequency", )
unit: " Hz", .with_smoother(Smoother::new(SmoothingStyle::Linear(10.0)))
value_to_string: formatters::f32_rounded(0), .with_unit(" Hz")
..Default::default() .with_value_to_string(formatters::f32_rounded(0)),
}, use_midi: BoolParam::new("Use MIDI", false),
use_midi: BoolParam {
value: false,
name: "Use MIDI",
..Default::default()
},
} }
} }
} }

View file

@ -3,6 +3,6 @@
use std::sync::Arc; use std::sync::Arc;
/// Round an `f32` value to always have a specific number of decimal digits. /// Round an `f32` value to always have a specific number of decimal digits.
pub fn f32_rounded(digits: usize) -> Option<Arc<dyn Fn(f32) -> String + Send + Sync>> { pub fn f32_rounded(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
Some(Arc::new(move |x| format!("{:.digits$}", x))) Arc::new(move |x| format!("{:.digits$}", x))
} }