1
0
Fork 0

Add the start of a Gain GUI port to vizia

This commit is contained in:
Robbert van der Helm 2022-03-18 01:28:18 +01:00
parent fd6836b891
commit 2c62b13ba5
5 changed files with 232 additions and 0 deletions

9
Cargo.lock generated
View file

@ -1315,6 +1315,15 @@ dependencies = [
"nih_plug_iced",
]
[[package]]
name = "gain_gui_vizia"
version = "0.1.0"
dependencies = [
"atomic_float",
"nih_plug",
"nih_plug_vizia",
]
[[package]]
name = "generational-arena"
version = "0.2.8"

View file

@ -23,6 +23,7 @@ members = [
"plugins/examples/gain",
"plugins/examples/gain-gui-egui",
"plugins/examples/gain-gui-iced",
"plugins/examples/gain-gui-vizia",
"plugins/examples/sine",
"plugins/examples/stft",

View file

@ -0,0 +1,17 @@
[package]
name = "gain_gui_vizia"
version = "0.1.0"
edition = "2021"
authors = ["Robbert van der Helm <mail@robbertvanderhelm.nl>"]
license = "ISC"
description = "A simple gain plugin with an vizia GUI"
[lib]
crate-type = ["cdylib"]
[dependencies]
nih_plug = { path = "../../../", features = ["assert_process_allocs"] }
nih_plug_vizia = { path = "../../../nih_plug_vizia" }
atomic_float = "0.1"

View file

@ -0,0 +1,52 @@
use atomic_float::AtomicF32;
use nih_plug::prelude::Editor;
use nih_plug_vizia::vizia::*;
use nih_plug_vizia::{assets, create_vizia_editor, ViziaState};
use std::pin::Pin;
use std::sync::Arc;
use crate::GainParams;
/// VIZIA uses points instead of pixels for text
const POINT_SCALE: f32 = 0.75;
const STYLE: &str = r#"
"#;
// Makes sense to also define this here, makes it a bit easier to keep track of
pub(crate) fn default_state() -> Arc<ViziaState> {
ViziaState::from_size(200, 150)
}
pub(crate) fn create(
params: Pin<Arc<GainParams>>,
peak_meter: Arc<AtomicF32>,
editor_state: Arc<ViziaState>,
) -> Option<Box<dyn Editor>> {
create_vizia_editor(editor_state, |cx, setter| {
// TOOD: `:root { background-color: #fafafa; }` in a stylesheet doesn't work
cx.style
.background_color
.insert(Entity::root(), Color::rgb(250, 250, 250));
// VIZIA uses points instead of pixels
cx.style
.font_size
.insert(Entity::root(), 20.0 * POINT_SCALE);
cx.add_theme(STYLE);
assets::register_fonts(cx);
cx.set_default_font(assets::NOTO_SANS_REGULAR);
VStack::new(cx, |cx| {
Label::new(cx, "Gain GUI")
.font(assets::NOTO_SANS_LIGHT)
.font_size(40.0 * POINT_SCALE)
.height(Pixels(50.0))
.child_top(Stretch(1.0))
.child_bottom(Pixels(0.0));
Label::new(cx, "Gain");
})
.child_left(Stretch(1.0))
.child_right(Stretch(1.0));
})
}

View file

@ -0,0 +1,153 @@
use atomic_float::AtomicF32;
use nih_plug::prelude::*;
use nih_plug_vizia::ViziaState;
use std::pin::Pin;
use std::sync::Arc;
mod editor;
/// This is mostly identical to the gain example, minus some fluff, and with a GUI.
struct Gain {
params: Pin<Arc<GainParams>>,
editor_state: Arc<ViziaState>,
/// Needed to normalize the peak meter's response based on the sample rate.
peak_meter_decay_weight: f32,
/// The current data for the peak meter. This is stored as an [`Arc`] so we can share it between
/// the GUI and the audio processing parts. If you have more state to share, then it's a good
/// idea to put all of that in a struct behind a single `Arc`.
///
/// This is stored as voltage gain.
peak_meter: Arc<AtomicF32>,
}
#[derive(Params)]
struct GainParams {
#[id = "gain"]
pub gain: FloatParam,
}
impl Default for Gain {
fn default() -> Self {
Self {
params: Arc::pin(GainParams::default()),
editor_state: editor::default_state(),
peak_meter_decay_weight: 1.0,
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
}
}
}
impl Default for GainParams {
fn default() -> Self {
Self {
gain: FloatParam::new(
"Gain",
0.0,
FloatRange::Linear {
min: -30.0,
max: 30.0,
},
)
.with_smoother(SmoothingStyle::Linear(50.0))
.with_step_size(0.01)
.with_unit(" dB"),
}
}
}
impl Plugin for Gain {
const NAME: &'static str = "Gain GUI (VIZIA)";
const VENDOR: &'static str = "Moist Plugins GmbH";
const URL: &'static str = "https://youtu.be/dQw4w9WgXcQ";
const EMAIL: &'static str = "info@example.com";
const VERSION: &'static str = "0.0.1";
const DEFAULT_NUM_INPUTS: u32 = 2;
const DEFAULT_NUM_OUTPUTS: u32 = 2;
const ACCEPTS_MIDI: bool = false;
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
fn params(&self) -> Pin<&dyn Params> {
self.params.as_ref()
}
fn editor(&self) -> Option<Box<dyn Editor>> {
editor::create(
self.params.clone(),
self.peak_meter.clone(),
self.editor_state.clone(),
)
}
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
// This works with any symmetrical IO layout
config.num_input_channels == config.num_output_channels && config.num_input_channels > 0
}
fn initialize(
&mut self,
_bus_config: &BusConfig,
buffer_config: &BufferConfig,
_context: &mut impl ProcessContext,
) -> 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);
true
}
fn process(
&mut self,
buffer: &mut Buffer,
_context: &mut impl ProcessContext,
) -> ProcessStatus {
for channel_samples in buffer.iter_samples() {
let mut amplitude = 0.0;
let num_samples = channel_samples.len();
let gain = self.params.gain.smoothed.next();
for sample in channel_samples {
*sample *= util::db_to_gain(gain);
amplitude += *sample;
}
// 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() {
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 {
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, std::sync::atomic::Ordering::Relaxed)
}
}
ProcessStatus::Normal
}
}
impl ClapPlugin for Gain {
const CLAP_ID: &'static str = "com.moist-plugins-gmbh.gain-gui-vizia";
const CLAP_DESCRIPTION: &'static str = "A smoothed gain parameter example plugin";
const CLAP_FEATURES: &'static [&'static str] = &["audio_effect", "mono", "stereo", "tool"];
const CLAP_MANUAL_URL: &'static str = Self::URL;
const CLAP_SUPPORT_URL: &'static str = Self::URL;
}
impl Vst3Plugin for Gain {
const VST3_CLASS_ID: [u8; 16] = *b"GainGuiVIIIZIAAA";
const VST3_CATEGORIES: &'static str = "Fx|Dynamics";
}
nih_export_clap!(Gain);
nih_export_vst3!(Gain);