From cd02ebb8395304512928f1181e877b6a56c999dc Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 4 Sep 2022 19:09:22 +0200 Subject: [PATCH] Use less arbitrary decay weights for the gain GUIs This solves the same problem as #27 but in a cleaner way. The previous commits refactored the smoothing to make it possible to use the calculations in plugin code to do the same thing as #27, but then I realized that that doesn't make much sense since the time to decay into complete silence isn't very meaningful for this kind of computation. --- plugins/examples/gain_gui_egui/src/lib.rs | 10 ++++++++-- plugins/examples/gain_gui_iced/src/lib.rs | 10 ++++++++-- plugins/examples/gain_gui_vizia/src/lib.rs | 10 ++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/plugins/examples/gain_gui_egui/src/lib.rs b/plugins/examples/gain_gui_egui/src/lib.rs index 27062636..7c579d84 100644 --- a/plugins/examples/gain_gui_egui/src/lib.rs +++ b/plugins/examples/gain_gui_egui/src/lib.rs @@ -3,6 +3,9 @@ use nih_plug::prelude::*; use nih_plug_egui::{create_egui_editor, egui, widgets, EguiState}; use std::sync::Arc; +/// The time it takes for the peak meter to decay by 12 dB after switching to complete silence. +const PEAK_METER_DECAY_MS: f64 = 150.0; + /// This is mostly identical to the gain example, minus some fluff, and with a GUI. struct Gain { params: Arc, @@ -154,8 +157,11 @@ impl Plugin for Gain { buffer_config: &BufferConfig, _context: &mut impl InitContext, ) -> bool { - // TODO: How do you tie this exponential decay to an actual time span? - self.peak_meter_decay_weight = 0.9992f32.powf(44_100.0 / buffer_config.sample_rate); + // After `PEAK_METER_DECAY_MS` milliseconds of pure silence, the peak meter's value should + // have dropped by 12 dB + self.peak_meter_decay_weight = 0.25f64 + .powf((buffer_config.sample_rate as f64 * PEAK_METER_DECAY_MS / 1000.0).recip()) + as f32; true } diff --git a/plugins/examples/gain_gui_iced/src/lib.rs b/plugins/examples/gain_gui_iced/src/lib.rs index cf717324..86dfe97a 100644 --- a/plugins/examples/gain_gui_iced/src/lib.rs +++ b/plugins/examples/gain_gui_iced/src/lib.rs @@ -5,6 +5,9 @@ use std::sync::Arc; mod editor; +/// The time it takes for the peak meter to decay by 12 dB after switching to complete silence. +const PEAK_METER_DECAY_MS: f64 = 150.0; + /// This is mostly identical to the gain example, minus some fluff, and with a GUI. struct Gain { params: Arc, @@ -100,8 +103,11 @@ impl Plugin for Gain { buffer_config: &BufferConfig, _context: &mut impl InitContext, ) -> bool { - // TODO: How do you tie this exponential decay to an actual time span? - self.peak_meter_decay_weight = 0.9992f32.powf(44_100.0 / buffer_config.sample_rate); + // After `PEAK_METER_DECAY_MS` milliseconds of pure silence, the peak meter's value should + // have dropped by 12 dB + self.peak_meter_decay_weight = 0.25f64 + .powf((buffer_config.sample_rate as f64 * PEAK_METER_DECAY_MS / 1000.0).recip()) + as f32; true } diff --git a/plugins/examples/gain_gui_vizia/src/lib.rs b/plugins/examples/gain_gui_vizia/src/lib.rs index 28be4a83..8bb2ea13 100644 --- a/plugins/examples/gain_gui_vizia/src/lib.rs +++ b/plugins/examples/gain_gui_vizia/src/lib.rs @@ -5,6 +5,9 @@ use std::sync::Arc; mod editor; +/// The time it takes for the peak meter to decay by 12 dB after switching to complete silence. +const PEAK_METER_DECAY_MS: f64 = 150.0; + /// This is mostly identical to the gain example, minus some fluff, and with a GUI. pub struct Gain { params: Arc, @@ -99,8 +102,11 @@ impl Plugin for Gain { buffer_config: &BufferConfig, _context: &mut impl InitContext, ) -> bool { - // TODO: How do you tie this exponential decay to an actual time span? - self.peak_meter_decay_weight = 0.9992f32.powf(44_100.0 / buffer_config.sample_rate); + // After `PEAK_METER_DECAY_MS` milliseconds of pure silence, the peak meter's value should + // have dropped by 12 dB + self.peak_meter_decay_weight = 0.25f64 + .powf((buffer_config.sample_rate as f64 * PEAK_METER_DECAY_MS / 1000.0).recip()) + as f32; true }