From bc11c97fcfe1a19408c1950980187838c36a7067 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 12 Feb 2022 17:19:52 +0100 Subject: [PATCH] Use the new builder interface --- plugins/examples/gain-gui/src/lib.rs | 30 +++++++++------------ plugins/examples/gain/src/lib.rs | 31 ++++++++++++--------- plugins/examples/sine/src/lib.rs | 40 ++++++++++++---------------- src/formatters.rs | 4 +-- 4 files changed, 50 insertions(+), 55 deletions(-) diff --git a/plugins/examples/gain-gui/src/lib.rs b/plugins/examples/gain-gui/src/lib.rs index de234e12..f1236b74 100644 --- a/plugins/examples/gain-gui/src/lib.rs +++ b/plugins/examples/gain-gui/src/lib.rs @@ -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() - }, + ), } } } diff --git a/plugins/examples/gain/src/lib.rs b/plugins/examples/gain/src/lib.rs index 47f066a9..1c1f3632 100644 --- a/plugins/examples/gain/src/lib.rs +++ b/plugins/examples/gain/src/lib.rs @@ -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>, @@ -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` - // 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()), } } diff --git a/plugins/examples/sine/src/lib.rs b/plugins/examples/sine/src/lib.rs index 5fb8351b..c7e2f5f2 100644 --- a/plugins/examples/sine/src/lib.rs +++ b/plugins/examples/sine/src/lib.rs @@ -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), } } } diff --git a/src/formatters.rs b/src/formatters.rs index 7719442f..74db3fe1 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -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 String + Send + Sync>> { - Some(Arc::new(move |x| format!("{:.digits$}", x))) +pub fn f32_rounded(digits: usize) -> Arc String + Send + Sync> { + Arc::new(move |x| format!("{:.digits$}", x)) }