diff --git a/plugins/examples/gain/src/lib.rs b/plugins/examples/gain/src/lib.rs index 707ee710..9f5b5590 100644 --- a/plugins/examples/gain/src/lib.rs +++ b/plugins/examples/gain/src/lib.rs @@ -14,7 +14,8 @@ struct Gain { struct GainParams { /// The parameter's ID is used to identify the parameter in the wrappred plugin API. As long as /// these IDs remain constant, you can rename and reorder these fields as you wish. The - /// parameters are exposed to the host in the same order they were defined. + /// parameters are exposed to the host in the same order they were defined. In this case, this + /// gain parameter is stored as linear gain while the values are displayed in decibels. #[id = "gain"] pub gain: FloatParam, @@ -56,20 +57,29 @@ impl Default for Gain { impl Default for GainParams { fn default() -> Self { Self { + // This gain is stored as linear gain. NIH-plug comes with useful conversion functions + // to treat these kinds of parameters as if we were dealing with decibels. Storing this + // as decibels is easier to work with, but requires a conversion for every sample. gain: FloatParam::new( "Gain", - 0.0, - FloatRange::Linear { - min: -30.0, - max: 30.0, + nih_dbg!(util::db_to_gain(0.0)), + FloatRange::Skewed { + min: util::db_to_gain(-30.0), + max: util::db_to_gain(30.0), + // This makes the range appear as if it was linear when displaying the values as + // decibels + factor: FloatRange::gain_skew_factor(-30.0, 30.0), }, ) - .with_smoother(SmoothingStyle::Linear(50.0)) - .with_step_size(0.01) + // Because the gain parameter is stored as linear gain instead of storing the value as + // decibels, we need logarithmic smoothing + .with_smoother(SmoothingStyle::Logarithmic(50.0)) .with_unit(" dB") - // This is actually redundant, because a step size of two decimal places already - // causes the parameter to shown rounded - .with_value_to_string(formatters::v2s_f32_rounded(2)), + // There are many predefined formatters we can use here. If the gain was stored as + // decibels instead of as a linear gain value, we could have also used the + // `.with_step_size(0.1)` function to get internal rounding. + .with_value_to_string(formatters::v2s_f32_gain_to_db(2)) + .with_string_to_value(formatters::s2v_f32_gain_to_db()), // Persisted fields can be intialized like any other fields, and they'll keep their // values when restoring the plugin's state. random_data: RwLock::new(Vec::new()), @@ -139,7 +149,7 @@ impl Plugin for Gain { let gain = self.params.gain.smoothed.next(); for sample in channel_samples { - *sample *= util::db_to_gain(gain); + *sample *= gain; } } diff --git a/plugins/examples/gain_gui_egui/src/lib.rs b/plugins/examples/gain_gui_egui/src/lib.rs index c7cf4b17..de66b453 100644 --- a/plugins/examples/gain_gui_egui/src/lib.rs +++ b/plugins/examples/gain_gui_egui/src/lib.rs @@ -48,17 +48,20 @@ impl Default for GainParams { Self { editor_state: EguiState::from_size(300, 180), + // See the main gain example for more details gain: FloatParam::new( "Gain", - 0.0, - FloatRange::Linear { - min: -30.0, - max: 30.0, + util::db_to_gain(0.0), + FloatRange::Skewed { + min: util::db_to_gain(-30.0), + max: util::db_to_gain(30.0), + factor: FloatRange::gain_skew_factor(-30.0, 30.0), }, ) - .with_smoother(SmoothingStyle::Linear(50.0)) - .with_step_size(0.01) - .with_unit(" dB"), + .with_smoother(SmoothingStyle::Logarithmic(50.0)) + .with_unit(" dB") + .with_value_to_string(formatters::v2s_f32_gain_to_db(2)) + .with_string_to_value(formatters::s2v_f32_gain_to_db()), some_int: IntParam::new("Something", 3, IntRange::Linear { min: 0, max: 3 }), } } @@ -169,7 +172,7 @@ impl Plugin for Gain { let gain = self.params.gain.smoothed.next(); for sample in channel_samples { - *sample *= util::db_to_gain(gain); + *sample *= gain; amplitude += *sample; } diff --git a/plugins/examples/gain_gui_iced/src/lib.rs b/plugins/examples/gain_gui_iced/src/lib.rs index f8071848..23042219 100644 --- a/plugins/examples/gain_gui_iced/src/lib.rs +++ b/plugins/examples/gain_gui_iced/src/lib.rs @@ -46,17 +46,20 @@ impl Default for GainParams { Self { editor_state: editor::default_state(), + // See the main gain example for more details gain: FloatParam::new( "Gain", - 0.0, - FloatRange::Linear { - min: -30.0, - max: 30.0, + util::db_to_gain(0.0), + FloatRange::Skewed { + min: util::db_to_gain(-30.0), + max: util::db_to_gain(30.0), + factor: FloatRange::gain_skew_factor(-30.0, 30.0), }, ) - .with_smoother(SmoothingStyle::Linear(50.0)) - .with_step_size(0.01) - .with_unit(" dB"), + .with_smoother(SmoothingStyle::Logarithmic(50.0)) + .with_unit(" dB") + .with_value_to_string(formatters::v2s_f32_gain_to_db(2)) + .with_string_to_value(formatters::s2v_f32_gain_to_db()), } } } @@ -115,7 +118,7 @@ impl Plugin for Gain { let gain = self.params.gain.smoothed.next(); for sample in channel_samples { - *sample *= util::db_to_gain(gain); + *sample *= gain; amplitude += *sample; } diff --git a/plugins/examples/gain_gui_vizia/src/lib.rs b/plugins/examples/gain_gui_vizia/src/lib.rs index d24b2673..f5f4ae0e 100644 --- a/plugins/examples/gain_gui_vizia/src/lib.rs +++ b/plugins/examples/gain_gui_vizia/src/lib.rs @@ -48,15 +48,17 @@ impl Default for GainParams { gain: FloatParam::new( "Gain", - 0.0, - FloatRange::Linear { - min: -30.0, - max: 30.0, + util::db_to_gain(0.0), + FloatRange::Skewed { + min: util::db_to_gain(-30.0), + max: util::db_to_gain(30.0), + factor: FloatRange::gain_skew_factor(-30.0, 30.0), }, ) - .with_smoother(SmoothingStyle::Linear(50.0)) - .with_step_size(0.01) - .with_unit(" dB"), + .with_smoother(SmoothingStyle::Logarithmic(50.0)) + .with_unit(" dB") + .with_value_to_string(formatters::v2s_f32_gain_to_db(2)) + .with_string_to_value(formatters::s2v_f32_gain_to_db()), } } } @@ -115,7 +117,7 @@ impl Plugin for Gain { let gain = self.params.gain.smoothed.next(); for sample in channel_samples { - *sample *= util::db_to_gain(gain); + *sample *= gain; amplitude += *sample; }