1
0
Fork 0

Persist the editor states for all plugins

This commit is contained in:
Robbert van der Helm 2022-07-13 23:16:29 +02:00
parent b6720383a3
commit 0469bdf806
5 changed files with 44 additions and 19 deletions

View file

@ -41,7 +41,6 @@ const MAX_FILTER_FREQUENCY: f32 = 22_000.0;
/// since this effect just turns into literal noise at high frequencies. /// since this effect just turns into literal noise at high frequencies.
struct Crisp { struct Crisp {
params: Arc<CrispParams>, params: Arc<CrispParams>,
editor_state: Arc<ViziaState>,
/// Needed for computing the filter coefficients. /// Needed for computing the filter coefficients.
sample_rate: f32, sample_rate: f32,
@ -60,6 +59,11 @@ struct Crisp {
#[derive(Params)] #[derive(Params)]
struct CrispParams { struct CrispParams {
/// The editor state, saved together with the parameter state so the custom scaling can be
/// restored.
#[persist = "editor-state"]
editor_state: Arc<ViziaState>,
/// On a range of `[0, 1]`, how much of the modulated sound to mix in. /// On a range of `[0, 1]`, how much of the modulated sound to mix in.
#[id = "amount"] #[id = "amount"]
amount: FloatParam, amount: FloatParam,
@ -128,7 +132,6 @@ impl Default for Crisp {
fn default() -> Self { fn default() -> Self {
Self { Self {
params: Arc::new(CrispParams::default()), params: Arc::new(CrispParams::default()),
editor_state: editor::default_state(),
sample_rate: 1.0, sample_rate: 1.0,
@ -147,6 +150,8 @@ impl Default for CrispParams {
let from_f32_hz_then_khz = formatters::s2v_f32_hz_then_khz(); let from_f32_hz_then_khz = formatters::s2v_f32_hz_then_khz();
Self { Self {
editor_state: editor::default_state(),
amount: FloatParam::new("Amount", 0.35, FloatRange::Linear { min: 0.0, max: 1.0 }) amount: FloatParam::new("Amount", 0.35, FloatRange::Linear { min: 0.0, max: 1.0 })
.with_smoother(SmoothingStyle::Linear(10.0)) .with_smoother(SmoothingStyle::Linear(10.0))
.with_unit("%") .with_unit("%")
@ -307,7 +312,7 @@ impl Plugin for Crisp {
} }
fn editor(&self) -> Option<Box<dyn Editor>> { fn editor(&self) -> Option<Box<dyn Editor>> {
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 { fn accepts_bus_config(&self, config: &BusConfig) -> bool {

View file

@ -46,7 +46,6 @@ const MAX_AUTOMATION_STEP_SIZE: u32 = 512;
// - A proper GUI // - A proper GUI
struct Diopser { struct Diopser {
params: Arc<DiopserParams>, params: Arc<DiopserParams>,
editor_state: Arc<ViziaState>,
/// Needed for computing the filter coefficients. /// Needed for computing the filter coefficients.
sample_rate: f32, sample_rate: f32,
@ -77,6 +76,11 @@ struct Diopser {
// resonance and filter stages parameter ranges in the GUI until the user unlocks. // resonance and filter stages parameter ranges in the GUI until the user unlocks.
#[derive(Params)] #[derive(Params)]
struct DiopserParams { struct DiopserParams {
/// The editor state, saved together with the parameter state so the custom scaling can be
/// restored.
#[persist = "editor-state"]
editor_state: Arc<ViziaState>,
/// The number of all-pass filters applied in series. /// The number of all-pass filters applied in series.
#[id = "stages"] #[id = "stages"]
filter_stages: IntParam, filter_stages: IntParam,
@ -121,7 +125,6 @@ impl Default for Diopser {
Self { Self {
params: Arc::new(DiopserParams::new(should_update_filters.clone())), params: Arc::new(DiopserParams::new(should_update_filters.clone())),
editor_state: editor::default_state(),
sample_rate: 1.0, sample_rate: 1.0,
@ -139,6 +142,8 @@ impl Default for Diopser {
impl DiopserParams { impl DiopserParams {
fn new(should_update_filters: Arc<AtomicBool>) -> Self { fn new(should_update_filters: Arc<AtomicBool>) -> Self {
Self { Self {
editor_state: editor::default_state(),
filter_stages: IntParam::new( filter_stages: IntParam::new(
"Filter Stages", "Filter Stages",
0, 0,
@ -252,7 +257,7 @@ impl Plugin for Diopser {
} }
fn editor(&self) -> Option<Box<dyn Editor>> { fn editor(&self) -> Option<Box<dyn Editor>> {
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 { fn accepts_bus_config(&self, config: &BusConfig) -> bool {
@ -307,7 +312,7 @@ impl Plugin for Diopser {
} }
// Compute a spectrum for the GUI if needed // 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); self.spectrum_input.compute(buffer);
} }

View file

@ -6,7 +6,6 @@ use std::sync::Arc;
/// This is mostly identical to the gain example, minus some fluff, and with a GUI. /// This is mostly identical to the gain example, minus some fluff, and with a GUI.
struct Gain { struct Gain {
params: Arc<GainParams>, params: Arc<GainParams>,
editor_state: Arc<EguiState>,
/// Needed to normalize the peak meter's response based on the sample rate. /// Needed to normalize the peak meter's response based on the sample rate.
peak_meter_decay_weight: f32, peak_meter_decay_weight: f32,
@ -20,6 +19,11 @@ struct Gain {
#[derive(Params)] #[derive(Params)]
struct GainParams { struct GainParams {
/// The editor state, saved together with the parameter state so the custom scaling can be
/// restored.
#[persist = "editor-state"]
editor_state: Arc<EguiState>,
#[id = "gain"] #[id = "gain"]
pub gain: FloatParam, pub gain: FloatParam,
@ -32,7 +36,6 @@ impl Default for Gain {
fn default() -> Self { fn default() -> Self {
Self { Self {
params: Arc::new(GainParams::default()), params: Arc::new(GainParams::default()),
editor_state: EguiState::from_size(300, 180),
peak_meter_decay_weight: 1.0, peak_meter_decay_weight: 1.0,
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
@ -43,6 +46,8 @@ impl Default for Gain {
impl Default for GainParams { impl Default for GainParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
editor_state: EguiState::from_size(300, 180),
gain: FloatParam::new( gain: FloatParam::new(
"Gain", "Gain",
0.0, 0.0,
@ -80,7 +85,7 @@ impl Plugin for Gain {
let params = self.params.clone(); let params = self.params.clone();
let peak_meter = self.peak_meter.clone(); let peak_meter = self.peak_meter.clone();
create_egui_editor( create_egui_editor(
self.editor_state.clone(), self.params.editor_state.clone(),
(), (),
move |egui_ctx, setter, _state| { move |egui_ctx, setter, _state| {
egui::CentralPanel::default().show(egui_ctx, |ui| { 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 // 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 // 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(); amplitude = (amplitude / num_samples as f32).abs();
let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed);
let new_peak_meter = if amplitude > current_peak_meter { let new_peak_meter = if amplitude > current_peak_meter {

View file

@ -8,7 +8,6 @@ mod editor;
/// This is mostly identical to the gain example, minus some fluff, and with a GUI. /// This is mostly identical to the gain example, minus some fluff, and with a GUI.
struct Gain { struct Gain {
params: Arc<GainParams>, params: Arc<GainParams>,
editor_state: Arc<IcedState>,
/// Needed to normalize the peak meter's response based on the sample rate. /// Needed to normalize the peak meter's response based on the sample rate.
peak_meter_decay_weight: f32, peak_meter_decay_weight: f32,
@ -22,6 +21,11 @@ struct Gain {
#[derive(Params)] #[derive(Params)]
struct GainParams { struct GainParams {
/// The editor state, saved together with the parameter state so the custom scaling can be
/// restored.
#[persist = "editor-state"]
editor_state: Arc<IcedState>,
#[id = "gain"] #[id = "gain"]
pub gain: FloatParam, pub gain: FloatParam,
} }
@ -30,7 +34,6 @@ impl Default for Gain {
fn default() -> Self { fn default() -> Self {
Self { Self {
params: Arc::new(GainParams::default()), params: Arc::new(GainParams::default()),
editor_state: editor::default_state(),
peak_meter_decay_weight: 1.0, peak_meter_decay_weight: 1.0,
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
@ -41,6 +44,8 @@ impl Default for Gain {
impl Default for GainParams { impl Default for GainParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
editor_state: editor::default_state(),
gain: FloatParam::new( gain: FloatParam::new(
"Gain", "Gain",
0.0, 0.0,
@ -77,7 +82,7 @@ impl Plugin for Gain {
editor::create( editor::create(
self.params.clone(), self.params.clone(),
self.peak_meter.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 // 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 // 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(); amplitude = (amplitude / num_samples as f32).abs();
let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed);
let new_peak_meter = if amplitude > current_peak_meter { let new_peak_meter = if amplitude > current_peak_meter {

View file

@ -8,7 +8,6 @@ mod editor;
/// This is mostly identical to the gain example, minus some fluff, and with a GUI. /// This is mostly identical to the gain example, minus some fluff, and with a GUI.
pub struct Gain { pub struct Gain {
params: Arc<GainParams>, params: Arc<GainParams>,
editor_state: Arc<ViziaState>,
/// Needed to normalize the peak meter's response based on the sample rate. /// Needed to normalize the peak meter's response based on the sample rate.
peak_meter_decay_weight: f32, peak_meter_decay_weight: f32,
@ -22,6 +21,11 @@ pub struct Gain {
#[derive(Params)] #[derive(Params)]
struct GainParams { struct GainParams {
/// The editor state, saved together with the parameter state so the custom scaling can be
/// restored.
#[persist = "editor-state"]
editor_state: Arc<ViziaState>,
#[id = "gain"] #[id = "gain"]
pub gain: FloatParam, pub gain: FloatParam,
} }
@ -30,7 +34,6 @@ impl Default for Gain {
fn default() -> Self { fn default() -> Self {
Self { Self {
params: Arc::new(GainParams::default()), params: Arc::new(GainParams::default()),
editor_state: editor::default_state(),
peak_meter_decay_weight: 1.0, peak_meter_decay_weight: 1.0,
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
@ -41,6 +44,8 @@ impl Default for Gain {
impl Default for GainParams { impl Default for GainParams {
fn default() -> Self { fn default() -> Self {
Self { Self {
editor_state: editor::default_state(),
gain: FloatParam::new( gain: FloatParam::new(
"Gain", "Gain",
0.0, 0.0,
@ -77,7 +82,7 @@ impl Plugin for Gain {
editor::create( editor::create(
self.params.clone(), self.params.clone(),
self.peak_meter.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 // 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 // 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(); amplitude = (amplitude / num_samples as f32).abs();
let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed);
let new_peak_meter = if amplitude > current_peak_meter { let new_peak_meter = if amplitude > current_peak_meter {