From 0469bdf806240729958eaa53cd1f44b595026459 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 13 Jul 2022 23:16:29 +0200 Subject: [PATCH] Persist the editor states for all plugins --- plugins/crisp/src/lib.rs | 11 ++++++++--- plugins/diopser/src/lib.rs | 13 +++++++++---- plugins/examples/gain_gui_egui/src/lib.rs | 13 +++++++++---- plugins/examples/gain_gui_iced/src/lib.rs | 13 +++++++++---- plugins/examples/gain_gui_vizia/src/lib.rs | 13 +++++++++---- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/plugins/crisp/src/lib.rs b/plugins/crisp/src/lib.rs index 87b35132..42ca4ed7 100644 --- a/plugins/crisp/src/lib.rs +++ b/plugins/crisp/src/lib.rs @@ -41,7 +41,6 @@ const MAX_FILTER_FREQUENCY: f32 = 22_000.0; /// since this effect just turns into literal noise at high frequencies. struct Crisp { params: Arc, - editor_state: Arc, /// Needed for computing the filter coefficients. sample_rate: f32, @@ -60,6 +59,11 @@ struct Crisp { #[derive(Params)] struct CrispParams { + /// The editor state, saved together with the parameter state so the custom scaling can be + /// restored. + #[persist = "editor-state"] + editor_state: Arc, + /// On a range of `[0, 1]`, how much of the modulated sound to mix in. #[id = "amount"] amount: FloatParam, @@ -128,7 +132,6 @@ impl Default for Crisp { fn default() -> Self { Self { params: Arc::new(CrispParams::default()), - editor_state: editor::default_state(), sample_rate: 1.0, @@ -147,6 +150,8 @@ impl Default for CrispParams { let from_f32_hz_then_khz = formatters::s2v_f32_hz_then_khz(); Self { + editor_state: editor::default_state(), + amount: FloatParam::new("Amount", 0.35, FloatRange::Linear { min: 0.0, max: 1.0 }) .with_smoother(SmoothingStyle::Linear(10.0)) .with_unit("%") @@ -307,7 +312,7 @@ impl Plugin for Crisp { } fn editor(&self) -> Option> { - editor::create(self.params.clone(), self.editor_state.clone()) + editor::create(self.params.clone(), self.params.editor_state.clone()) } fn accepts_bus_config(&self, config: &BusConfig) -> bool { diff --git a/plugins/diopser/src/lib.rs b/plugins/diopser/src/lib.rs index a64f9f4d..2fea1fd6 100644 --- a/plugins/diopser/src/lib.rs +++ b/plugins/diopser/src/lib.rs @@ -46,7 +46,6 @@ const MAX_AUTOMATION_STEP_SIZE: u32 = 512; // - A proper GUI struct Diopser { params: Arc, - editor_state: Arc, /// Needed for computing the filter coefficients. sample_rate: f32, @@ -77,6 +76,11 @@ struct Diopser { // resonance and filter stages parameter ranges in the GUI until the user unlocks. #[derive(Params)] struct DiopserParams { + /// The editor state, saved together with the parameter state so the custom scaling can be + /// restored. + #[persist = "editor-state"] + editor_state: Arc, + /// The number of all-pass filters applied in series. #[id = "stages"] filter_stages: IntParam, @@ -121,7 +125,6 @@ impl Default for Diopser { Self { params: Arc::new(DiopserParams::new(should_update_filters.clone())), - editor_state: editor::default_state(), sample_rate: 1.0, @@ -139,6 +142,8 @@ impl Default for Diopser { impl DiopserParams { fn new(should_update_filters: Arc) -> Self { Self { + editor_state: editor::default_state(), + filter_stages: IntParam::new( "Filter Stages", 0, @@ -252,7 +257,7 @@ impl Plugin for Diopser { } fn editor(&self) -> Option> { - editor::create(self.params.clone(), self.editor_state.clone()) + editor::create(self.params.clone(), self.params.editor_state.clone()) } fn accepts_bus_config(&self, config: &BusConfig) -> bool { @@ -307,7 +312,7 @@ impl Plugin for Diopser { } // Compute a spectrum for the GUI if needed - if self.editor_state.is_open() { + if self.params.editor_state.is_open() { self.spectrum_input.compute(buffer); } diff --git a/plugins/examples/gain_gui_egui/src/lib.rs b/plugins/examples/gain_gui_egui/src/lib.rs index 666b8fff..c7cf4b17 100644 --- a/plugins/examples/gain_gui_egui/src/lib.rs +++ b/plugins/examples/gain_gui_egui/src/lib.rs @@ -6,7 +6,6 @@ use std::sync::Arc; /// This is mostly identical to the gain example, minus some fluff, and with a GUI. struct Gain { params: Arc, - editor_state: Arc, /// Needed to normalize the peak meter's response based on the sample rate. peak_meter_decay_weight: f32, @@ -20,6 +19,11 @@ struct Gain { #[derive(Params)] struct GainParams { + /// The editor state, saved together with the parameter state so the custom scaling can be + /// restored. + #[persist = "editor-state"] + editor_state: Arc, + #[id = "gain"] pub gain: FloatParam, @@ -32,7 +36,6 @@ impl Default for Gain { fn default() -> Self { Self { params: Arc::new(GainParams::default()), - editor_state: EguiState::from_size(300, 180), peak_meter_decay_weight: 1.0, peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), @@ -43,6 +46,8 @@ impl Default for Gain { impl Default for GainParams { fn default() -> Self { Self { + editor_state: EguiState::from_size(300, 180), + gain: FloatParam::new( "Gain", 0.0, @@ -80,7 +85,7 @@ impl Plugin for Gain { let params = self.params.clone(); let peak_meter = self.peak_meter.clone(); create_egui_editor( - self.editor_state.clone(), + self.params.editor_state.clone(), (), move |egui_ctx, setter, _state| { egui::CentralPanel::default().show(egui_ctx, |ui| { @@ -170,7 +175,7 @@ impl Plugin for Gain { // To save resources, a plugin can (and probably should!) only perform expensive // calculations that are only displayed on the GUI while the GUI is open - if self.editor_state.is_open() { + if self.params.editor_state.is_open() { amplitude = (amplitude / num_samples as f32).abs(); let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); let new_peak_meter = if amplitude > current_peak_meter { diff --git a/plugins/examples/gain_gui_iced/src/lib.rs b/plugins/examples/gain_gui_iced/src/lib.rs index e7a82227..f8071848 100644 --- a/plugins/examples/gain_gui_iced/src/lib.rs +++ b/plugins/examples/gain_gui_iced/src/lib.rs @@ -8,7 +8,6 @@ mod editor; /// This is mostly identical to the gain example, minus some fluff, and with a GUI. struct Gain { params: Arc, - editor_state: Arc, /// Needed to normalize the peak meter's response based on the sample rate. peak_meter_decay_weight: f32, @@ -22,6 +21,11 @@ struct Gain { #[derive(Params)] struct GainParams { + /// The editor state, saved together with the parameter state so the custom scaling can be + /// restored. + #[persist = "editor-state"] + editor_state: Arc, + #[id = "gain"] pub gain: FloatParam, } @@ -30,7 +34,6 @@ impl Default for Gain { fn default() -> Self { Self { params: Arc::new(GainParams::default()), - editor_state: editor::default_state(), peak_meter_decay_weight: 1.0, peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), @@ -41,6 +44,8 @@ impl Default for Gain { impl Default for GainParams { fn default() -> Self { Self { + editor_state: editor::default_state(), + gain: FloatParam::new( "Gain", 0.0, @@ -77,7 +82,7 @@ impl Plugin for Gain { editor::create( self.params.clone(), self.peak_meter.clone(), - self.editor_state.clone(), + self.params.editor_state.clone(), ) } @@ -116,7 +121,7 @@ impl Plugin for Gain { // To save resources, a plugin can (and probably should!) only perform expensive // calculations that are only displayed on the GUI while the GUI is open - if self.editor_state.is_open() { + if self.params.editor_state.is_open() { amplitude = (amplitude / num_samples as f32).abs(); let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); let new_peak_meter = if amplitude > current_peak_meter { diff --git a/plugins/examples/gain_gui_vizia/src/lib.rs b/plugins/examples/gain_gui_vizia/src/lib.rs index 6c9722e7..d24b2673 100644 --- a/plugins/examples/gain_gui_vizia/src/lib.rs +++ b/plugins/examples/gain_gui_vizia/src/lib.rs @@ -8,7 +8,6 @@ mod editor; /// This is mostly identical to the gain example, minus some fluff, and with a GUI. pub struct Gain { params: Arc, - editor_state: Arc, /// Needed to normalize the peak meter's response based on the sample rate. peak_meter_decay_weight: f32, @@ -22,6 +21,11 @@ pub struct Gain { #[derive(Params)] struct GainParams { + /// The editor state, saved together with the parameter state so the custom scaling can be + /// restored. + #[persist = "editor-state"] + editor_state: Arc, + #[id = "gain"] pub gain: FloatParam, } @@ -30,7 +34,6 @@ impl Default for Gain { fn default() -> Self { Self { params: Arc::new(GainParams::default()), - editor_state: editor::default_state(), peak_meter_decay_weight: 1.0, peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), @@ -41,6 +44,8 @@ impl Default for Gain { impl Default for GainParams { fn default() -> Self { Self { + editor_state: editor::default_state(), + gain: FloatParam::new( "Gain", 0.0, @@ -77,7 +82,7 @@ impl Plugin for Gain { editor::create( self.params.clone(), self.peak_meter.clone(), - self.editor_state.clone(), + self.params.editor_state.clone(), ) } @@ -116,7 +121,7 @@ impl Plugin for Gain { // To save resources, a plugin can (and probably should!) only perform expensive // calculations that are only displayed on the GUI while the GUI is open - if self.editor_state.is_open() { + if self.params.editor_state.is_open() { amplitude = (amplitude / num_samples as f32).abs(); let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); let new_peak_meter = if amplitude > current_peak_meter {