1
0
Fork 0

Use linear gain params in gain examples

Using the new skewed coefficient calculation function for gain ranges
from a couple commits ago.

Closes #18.
This commit is contained in:
Robbert van der Helm 2022-07-24 21:21:13 +02:00
parent 6a1395e99a
commit fdbff129f8
4 changed files with 53 additions and 35 deletions

View file

@ -14,7 +14,8 @@ struct Gain {
struct GainParams { struct GainParams {
/// The parameter's ID is used to identify the parameter in the wrappred plugin API. As long as /// 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 /// 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"] #[id = "gain"]
pub gain: FloatParam, pub gain: FloatParam,
@ -56,20 +57,29 @@ impl Default for Gain {
impl Default for GainParams { impl Default for GainParams {
fn default() -> Self { fn default() -> Self {
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: FloatParam::new(
"Gain", "Gain",
0.0, nih_dbg!(util::db_to_gain(0.0)),
FloatRange::Linear { FloatRange::Skewed {
min: -30.0, min: util::db_to_gain(-30.0),
max: 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)) // Because the gain parameter is stored as linear gain instead of storing the value as
.with_step_size(0.01) // decibels, we need logarithmic smoothing
.with_smoother(SmoothingStyle::Logarithmic(50.0))
.with_unit(" dB") .with_unit(" dB")
// This is actually redundant, because a step size of two decimal places already // There are many predefined formatters we can use here. If the gain was stored as
// causes the parameter to shown rounded // decibels instead of as a linear gain value, we could have also used the
.with_value_to_string(formatters::v2s_f32_rounded(2)), // `.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 // Persisted fields can be intialized like any other fields, and they'll keep their
// values when restoring the plugin's state. // values when restoring the plugin's state.
random_data: RwLock::new(Vec::new()), random_data: RwLock::new(Vec::new()),
@ -139,7 +149,7 @@ impl Plugin for Gain {
let gain = self.params.gain.smoothed.next(); let gain = self.params.gain.smoothed.next();
for sample in channel_samples { for sample in channel_samples {
*sample *= util::db_to_gain(gain); *sample *= gain;
} }
} }

View file

@ -48,17 +48,20 @@ impl Default for GainParams {
Self { Self {
editor_state: EguiState::from_size(300, 180), editor_state: EguiState::from_size(300, 180),
// See the main gain example for more details
gain: FloatParam::new( gain: FloatParam::new(
"Gain", "Gain",
0.0, util::db_to_gain(0.0),
FloatRange::Linear { FloatRange::Skewed {
min: -30.0, min: util::db_to_gain(-30.0),
max: 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_smoother(SmoothingStyle::Logarithmic(50.0))
.with_step_size(0.01) .with_unit(" dB")
.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 }), 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(); let gain = self.params.gain.smoothed.next();
for sample in channel_samples { for sample in channel_samples {
*sample *= util::db_to_gain(gain); *sample *= gain;
amplitude += *sample; amplitude += *sample;
} }

View file

@ -46,17 +46,20 @@ impl Default for GainParams {
Self { Self {
editor_state: editor::default_state(), editor_state: editor::default_state(),
// See the main gain example for more details
gain: FloatParam::new( gain: FloatParam::new(
"Gain", "Gain",
0.0, util::db_to_gain(0.0),
FloatRange::Linear { FloatRange::Skewed {
min: -30.0, min: util::db_to_gain(-30.0),
max: 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_smoother(SmoothingStyle::Logarithmic(50.0))
.with_step_size(0.01) .with_unit(" dB")
.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(); let gain = self.params.gain.smoothed.next();
for sample in channel_samples { for sample in channel_samples {
*sample *= util::db_to_gain(gain); *sample *= gain;
amplitude += *sample; amplitude += *sample;
} }

View file

@ -48,15 +48,17 @@ impl Default for GainParams {
gain: FloatParam::new( gain: FloatParam::new(
"Gain", "Gain",
0.0, util::db_to_gain(0.0),
FloatRange::Linear { FloatRange::Skewed {
min: -30.0, min: util::db_to_gain(-30.0),
max: 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_smoother(SmoothingStyle::Logarithmic(50.0))
.with_step_size(0.01) .with_unit(" dB")
.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(); let gain = self.params.gain.smoothed.next();
for sample in channel_samples { for sample in channel_samples {
*sample *= util::db_to_gain(gain); *sample *= gain;
amplitude += *sample; amplitude += *sample;
} }