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 {
fn default() -> Self {
Self {
gain: FloatParam {
value: 0.0,
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)),
value_changed: None,
range: Range::Linear {
gain: FloatParam::new(
"Gain",
0.0,
Range::Linear {
min: -30.0,
max: 30.0,
},
name: "Gain",
unit: " dB",
value_to_string: formatters::f32_rounded(2),
string_to_value: None,
},
some_int: IntParam {
value: 3,
smoothed: Smoother::none(),
name: "Something",
range: Range::Skewed {
)
.with_smoother(Smoother::new(SmoothingStyle::Linear(50.0)))
.with_unit(" dB")
.with_value_to_string(formatters::f32_rounded(2)),
some_int: IntParam::new(
"Something",
3,
Range::Skewed {
min: 0,
max: 3,
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 parking_lot::RwLock;
use std::pin::Pin;
use std::sync::Arc;
struct Gain {
params: Pin<Box<GainParams>>,
@ -39,31 +40,35 @@ impl Default for Gain {
impl Default for GainParams {
fn default() -> Self {
Self {
// There are three ways to specify parameters:
//
// ...either manually specify all fields:
gain: FloatParam {
value: 0.0,
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)),
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 {
min: -30.0,
max: 30.0,
},
name: "Gain",
unit: " dB",
value_to_string: formatters::f32_rounded(2),
value_to_string: Some(formatters::f32_rounded(2)),
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
// name, default value, and range though.
the_field_name_can_change: BoolParam {
value: false,
name: "Important Value",
..Default::default()
},
// ...or use the builder interface:
the_field_name_can_change: BoolParam::new("Important value", false).with_callback(
Arc::new(|_new_value: bool| {
// 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 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()),
}
}

View file

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

View file

@ -3,6 +3,6 @@
use std::sync::Arc;
/// 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>> {
Some(Arc::new(move |x| format!("{:.digits$}", x)))
pub fn f32_rounded(digits: usize) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
Arc::new(move |x| format!("{:.digits$}", x))
}