diff --git a/.cargo/config b/.cargo/config.toml similarity index 100% rename from .cargo/config rename to .cargo/config.toml diff --git a/src/editor.rs b/src/editor.rs new file mode 100644 index 0000000..820f2cd --- /dev/null +++ b/src/editor.rs @@ -0,0 +1,65 @@ +use atomic_float::AtomicF32; +use nih_plug::{editor::Editor, util}; +use nih_plug_vizia::{ + ViziaState, ViziaTheming, assets, create_vizia_editor, + vizia::prelude::*, + widgets::{ParamSlider, PeakMeter, ResizeHandle}, +}; +use std::sync::{Arc, atomic::Ordering}; + +use crate::GainParams; + +#[derive(Lens)] +struct Data { + params: Arc, + peak_meter: Arc, +} + +impl Model for Data {} + +pub(crate) fn default_state() -> Arc { + ViziaState::new(|| (200, 150)) +} + +pub(crate) fn create( + params: Arc, + peak_meter: Arc, + editor_state: Arc, +) -> Option> { + create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| { + assets::register_noto_sans_light(cx); + assets::register_noto_sans_thin(cx); + + Data { + params: params.clone(), + peak_meter: peak_meter.clone(), + } + .build(cx); + + VStack::new(cx, |cx| { + Label::new(cx, "Gain GUI") + .font_family(vec![FamilyOwned::Name(String::from(assets::NOTO_SANS))]) + .font_weight(FontWeightKeyword::Thin) + .font_size(30.0) + .height(Pixels(50.0)) + .child_top(Stretch(1.0)) + .child_bottom(Pixels(0.0)); + + Label::new(cx, "Gain"); + ParamSlider::new(cx, Data::params, |params| ¶ms.gain); + + PeakMeter::new( + cx, + Data::peak_meter + .map(|peak_meter| util::gain_to_db(peak_meter.load(Ordering::Relaxed))), + Some(Duration::from_millis(600)), + ) + .top(Pixels(10.0)); + }) + .row_between(Pixels(0.0)) + .child_left(Stretch(1.0)) + .child_right(Stretch(1.0)); + + ResizeHandle::new(cx); + }) +} diff --git a/src/lib.rs b/src/lib.rs index 05fed10..804cebd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,42 +1,37 @@ -use nih_plug::{params::persist, prelude::*}; -use parking_lot::Mutex; -use std::sync::Arc; +use atomic_float::AtomicF32; +use nih_plug::prelude::*; +use nih_plug_vizia::ViziaState; +use std::sync::{Arc, atomic::Ordering}; -struct Gain { +mod editor; + +const PEAK_METER_DECAY_MS: f64 = 150.0; + +pub struct Gain { params: Arc, + + // gotta normalize peak meters response based on sample rate :) + peak_meter_decay_weight: f32, + // current peak meter value, stored in Arc to share between GUI and processing + peak_meter: Arc, } #[derive(Params)] struct GainParams { + #[persist = "editor-state"] + editor_state: Arc, + #[id = "gain"] pub gain: FloatParam, - - #[persist = "industry_secrets"] - pub random: Mutex>, - - #[nested(group = "Subparameters")] - pub sub_params: SubParams, - - #[nested(array, group = "Array Parameters")] - pub array_params: [ArrayParams; 3], -} - -#[derive(Params)] -struct SubParams { - #[id = "thing"] - pub nested_parameter: FloatParam, -} - -#[derive(Params)] -struct ArrayParams { - #[id = "noope"] - pub nope: FloatParam, } impl Default for Gain { fn default() -> Self { Self { params: Arc::new(GainParams::default()), + + peak_meter_decay_weight: 1.0, + peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)), } } } @@ -44,45 +39,21 @@ impl Default for Gain { impl Default for GainParams { fn default() -> Self { Self { - // gain stored as linear gain. + editor_state: editor::default_state(), + gain: FloatParam::new( "Gain", util::db_to_gain(0.0), FloatRange::Skewed { min: -30.0, max: 30.0, - // makes range appear as if linear while displaying decibel values factor: FloatRange::gain_skew_factor(-30.0, 30.0), }, ) - // logarithmic smoothing because value is stored as linear .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()), - - // persisted values can be initialized like any other - random: Mutex::new(Vec::new()), - sub_params: SubParams { - nested_parameter: FloatParam::new( - "Unused Nested Parameter", - 0.5, - FloatRange::Skewed { - min: 2.0, - max: 2.4, - factor: FloatRange::skew_factor(2.0), - }, - ) - .with_value_to_string(formatters::v2s_f32_rounded(2)), - }, - - array_params: [1, 2, 3].map(|i| ArrayParams { - nope: FloatParam::new( - format!("Nope {i}"), - 0.5, - FloatRange::Linear { min: 1.0, max: 2.0 }, - ), - }), } } } @@ -102,11 +73,7 @@ impl Plugin for Gain { AudioIOLayout { main_input_channels: NonZeroU32::new(2), main_output_channels: NonZeroU32::new(2), - - aux_input_ports: &[], - aux_output_ports: &[], - - names: PortNames::const_default(), + ..AudioIOLayout::const_default() }, AudioIOLayout { main_input_channels: NonZeroU32::new(1), @@ -125,6 +92,27 @@ impl Plugin for Gain { self.params.clone() } + fn editor(&mut self, _async_executor: AsyncExecutor) -> Option> { + editor::create( + self.params.clone(), + self.peak_meter.clone(), + self.params.editor_state.clone(), + ) + } + + fn initialize( + &mut self, + _audio_io_layout: &AudioIOLayout, + buffer_config: &BufferConfig, + _context: &mut impl InitContext, + ) -> bool { + self.peak_meter_decay_weight = 0.25f64 + .powf((buffer_config.sample_rate as f64 * PEAK_METER_DECAY_MS / 1000.0).recip()) + as f32; + + true + } + fn process( &mut self, buffer: &mut Buffer, @@ -132,10 +120,27 @@ impl Plugin for Gain { _context: &mut impl ProcessContext, ) -> ProcessStatus { for channel_samples in buffer.iter_samples() { - let gain = self.params.gain.smoothed.next(); + let mut amplitude = 0.0; + let num_samples = channel_samples.len(); + let gain = self.params.gain.smoothed.next(); for sample in channel_samples { *sample *= gain; + amplitude += *sample; + } + + // save resources by only doing GUI calculations when GUI is open + if self.params.editor_state.is_open() { + amplitude = (amplitude / num_samples as f32).abs(); + let current_peak_meter = self.peak_meter.load(Ordering::Relaxed); + let new_peak_meter = if amplitude > current_peak_meter { + amplitude + } else { + current_peak_meter * self.peak_meter_decay_weight + + amplitude * (1.0 - self.peak_meter_decay_weight) + }; + + self.peak_meter.store(new_peak_meter, Ordering::Relaxed); } }