gui experiment
This commit is contained in:
parent
963c797594
commit
fb3ba5d291
3 changed files with 127 additions and 57 deletions
65
src/editor.rs
Normal file
65
src/editor.rs
Normal file
|
@ -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<GainParams>,
|
||||||
|
peak_meter: Arc<AtomicF32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Model for Data {}
|
||||||
|
|
||||||
|
pub(crate) fn default_state() -> Arc<ViziaState> {
|
||||||
|
ViziaState::new(|| (200, 150))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn create(
|
||||||
|
params: Arc<GainParams>,
|
||||||
|
peak_meter: Arc<AtomicF32>,
|
||||||
|
editor_state: Arc<ViziaState>,
|
||||||
|
) -> Option<Box<dyn Editor>> {
|
||||||
|
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);
|
||||||
|
})
|
||||||
|
}
|
119
src/lib.rs
119
src/lib.rs
|
@ -1,42 +1,37 @@
|
||||||
use nih_plug::{params::persist, prelude::*};
|
use atomic_float::AtomicF32;
|
||||||
use parking_lot::Mutex;
|
use nih_plug::prelude::*;
|
||||||
use std::sync::Arc;
|
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<GainParams>,
|
params: Arc<GainParams>,
|
||||||
|
|
||||||
|
// 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<AtomicF32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Params)]
|
#[derive(Params)]
|
||||||
struct GainParams {
|
struct GainParams {
|
||||||
|
#[persist = "editor-state"]
|
||||||
|
editor_state: Arc<ViziaState>,
|
||||||
|
|
||||||
#[id = "gain"]
|
#[id = "gain"]
|
||||||
pub gain: FloatParam,
|
pub gain: FloatParam,
|
||||||
|
|
||||||
#[persist = "industry_secrets"]
|
|
||||||
pub random: Mutex<Vec<f32>>,
|
|
||||||
|
|
||||||
#[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 {
|
impl Default for Gain {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
params: Arc::new(GainParams::default()),
|
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 {
|
impl Default for GainParams {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
// gain stored as linear gain.
|
editor_state: editor::default_state(),
|
||||||
|
|
||||||
gain: FloatParam::new(
|
gain: FloatParam::new(
|
||||||
"Gain",
|
"Gain",
|
||||||
util::db_to_gain(0.0),
|
util::db_to_gain(0.0),
|
||||||
FloatRange::Skewed {
|
FloatRange::Skewed {
|
||||||
min: -30.0,
|
min: -30.0,
|
||||||
max: 30.0,
|
max: 30.0,
|
||||||
// makes range appear as if linear while displaying decibel values
|
|
||||||
factor: FloatRange::gain_skew_factor(-30.0, 30.0),
|
factor: FloatRange::gain_skew_factor(-30.0, 30.0),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
// logarithmic smoothing because value is stored as linear
|
|
||||||
.with_smoother(SmoothingStyle::Logarithmic(50.0))
|
.with_smoother(SmoothingStyle::Logarithmic(50.0))
|
||||||
.with_unit(" dB")
|
.with_unit(" dB")
|
||||||
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
|
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
|
||||||
.with_string_to_value(formatters::s2v_f32_gain_to_db()),
|
.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 {
|
AudioIOLayout {
|
||||||
main_input_channels: NonZeroU32::new(2),
|
main_input_channels: NonZeroU32::new(2),
|
||||||
main_output_channels: NonZeroU32::new(2),
|
main_output_channels: NonZeroU32::new(2),
|
||||||
|
..AudioIOLayout::const_default()
|
||||||
aux_input_ports: &[],
|
|
||||||
aux_output_ports: &[],
|
|
||||||
|
|
||||||
names: PortNames::const_default(),
|
|
||||||
},
|
},
|
||||||
AudioIOLayout {
|
AudioIOLayout {
|
||||||
main_input_channels: NonZeroU32::new(1),
|
main_input_channels: NonZeroU32::new(1),
|
||||||
|
@ -125,6 +92,27 @@ impl Plugin for Gain {
|
||||||
self.params.clone()
|
self.params.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn editor(&mut self, _async_executor: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
|
||||||
|
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<Self>,
|
||||||
|
) -> 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(
|
fn process(
|
||||||
&mut self,
|
&mut self,
|
||||||
buffer: &mut Buffer,
|
buffer: &mut Buffer,
|
||||||
|
@ -132,10 +120,27 @@ impl Plugin for Gain {
|
||||||
_context: &mut impl ProcessContext<Self>,
|
_context: &mut impl ProcessContext<Self>,
|
||||||
) -> ProcessStatus {
|
) -> ProcessStatus {
|
||||||
for channel_samples in buffer.iter_samples() {
|
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 {
|
for sample in channel_samples {
|
||||||
*sample *= gain;
|
*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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue