2022-03-18 09:24:54 +11:00
|
|
|
use atomic_float::AtomicF32;
|
|
|
|
use nih_plug::prelude::*;
|
|
|
|
use nih_plug_egui::{create_egui_editor, egui, widgets, EguiState};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-09-05 03:09:22 +10:00
|
|
|
/// The time it takes for the peak meter to decay by 12 dB after switching to complete silence.
|
|
|
|
const PEAK_METER_DECAY_MS: f64 = 150.0;
|
|
|
|
|
2022-03-18 09:24:54 +11:00
|
|
|
/// This is mostly identical to the gain example, minus some fluff, and with a GUI.
|
|
|
|
struct Gain {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<GainParams>,
|
2022-03-18 09:24:54 +11:00
|
|
|
|
|
|
|
/// 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 {
|
2022-07-14 07:16:29 +10:00
|
|
|
/// The editor state, saved together with the parameter state so the custom scaling can be
|
|
|
|
/// restored.
|
|
|
|
#[persist = "editor-state"]
|
|
|
|
editor_state: Arc<EguiState>,
|
|
|
|
|
2022-03-18 09:24:54 +11:00
|
|
|
#[id = "gain"]
|
|
|
|
pub gain: FloatParam,
|
|
|
|
|
|
|
|
// TODO: Remove this parameter when we're done implementing the widgets
|
|
|
|
#[id = "foobar"]
|
|
|
|
pub some_int: IntParam,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Gain {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc::new(GainParams::default()),
|
2022-03-18 09:24:54 +11:00
|
|
|
|
|
|
|
peak_meter_decay_weight: 1.0,
|
|
|
|
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GainParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-07-14 07:16:29 +10:00
|
|
|
editor_state: EguiState::from_size(300, 180),
|
|
|
|
|
2022-07-25 05:21:13 +10:00
|
|
|
// See the main gain example for more details
|
2022-03-18 09:24:54 +11:00
|
|
|
gain: FloatParam::new(
|
|
|
|
"Gain",
|
2022-07-25 05:21:13 +10:00
|
|
|
util::db_to_gain(0.0),
|
|
|
|
FloatRange::Skewed {
|
|
|
|
min: util::db_to_gain(-30.0),
|
|
|
|
max: util::db_to_gain(30.0),
|
|
|
|
factor: FloatRange::gain_skew_factor(-30.0, 30.0),
|
2022-03-18 09:24:54 +11:00
|
|
|
},
|
|
|
|
)
|
2022-07-25 05:21:13 +10:00
|
|
|
.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()),
|
2022-03-18 09:24:54 +11:00
|
|
|
some_int: IntParam::new("Something", 3, IntRange::Linear { min: 0, max: 3 }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Gain {
|
|
|
|
const NAME: &'static str = "Gain GUI (egui)";
|
|
|
|
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";
|
|
|
|
|
2022-08-19 22:34:21 +10:00
|
|
|
const DEFAULT_INPUT_CHANNELS: u32 = 2;
|
|
|
|
const DEFAULT_OUTPUT_CHANNELS: u32 = 2;
|
2022-03-18 09:24:54 +11:00
|
|
|
|
|
|
|
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
|
|
|
|
2022-04-07 23:31:46 +10:00
|
|
|
fn params(&self) -> Arc<dyn Params> {
|
|
|
|
self.params.clone()
|
2022-03-18 09:24:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn editor(&self) -> Option<Box<dyn Editor>> {
|
|
|
|
let params = self.params.clone();
|
|
|
|
let peak_meter = self.peak_meter.clone();
|
|
|
|
create_egui_editor(
|
2022-07-14 07:16:29 +10:00
|
|
|
self.params.editor_state.clone(),
|
2022-03-18 09:24:54 +11:00
|
|
|
(),
|
|
|
|
move |egui_ctx, setter, _state| {
|
|
|
|
egui::CentralPanel::default().show(egui_ctx, |ui| {
|
|
|
|
// NOTE: See `plugins/diopser/src/editor.rs` for an example using the generic UI widget
|
|
|
|
|
|
|
|
// This is a fancy widget that can get all the information it needs to properly
|
|
|
|
// display and modify the parameter from the parametr itself
|
|
|
|
// It's not yet fully implemented, as the text is missing.
|
|
|
|
ui.label("Some random integer");
|
|
|
|
ui.add(widgets::ParamSlider::for_param(¶ms.some_int, setter));
|
|
|
|
|
|
|
|
ui.label("Gain");
|
|
|
|
ui.add(widgets::ParamSlider::for_param(¶ms.gain, setter));
|
|
|
|
|
|
|
|
ui.label(
|
|
|
|
"Also gain, but with a lame widget. Can't even render the value correctly!",
|
|
|
|
);
|
|
|
|
// This is a simple naieve version of a parameter slider that's not aware of how
|
2022-04-25 02:34:40 +10:00
|
|
|
// the parameters work
|
2022-03-18 09:24:54 +11:00
|
|
|
ui.add(
|
|
|
|
egui::widgets::Slider::from_get_set(-30.0..=30.0, |new_value| {
|
|
|
|
match new_value {
|
|
|
|
Some(new_value) => {
|
|
|
|
setter.begin_set_parameter(¶ms.gain);
|
|
|
|
setter.set_parameter(¶ms.gain, new_value as f32);
|
|
|
|
setter.end_set_parameter(¶ms.gain);
|
|
|
|
new_value
|
|
|
|
}
|
2022-09-07 05:55:14 +10:00
|
|
|
None => params.gain.value() as f64,
|
2022-03-18 09:24:54 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.suffix(" dB"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: Add a proper custom widget instead of reusing a progress bar
|
|
|
|
let peak_meter =
|
|
|
|
util::gain_to_db(peak_meter.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
let peak_meter_text = if peak_meter > util::MINUS_INFINITY_DB {
|
|
|
|
format!("{:.1} dBFS", peak_meter)
|
|
|
|
} else {
|
|
|
|
String::from("-inf dBFS")
|
|
|
|
};
|
|
|
|
|
|
|
|
let peak_meter_normalized = (peak_meter + 60.0) / 60.0;
|
|
|
|
ui.allocate_space(egui::Vec2::splat(2.0));
|
|
|
|
ui.add(
|
|
|
|
egui::widgets::ProgressBar::new(peak_meter_normalized)
|
|
|
|
.text(peak_meter_text),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-05-27 09:17:15 +10:00
|
|
|
_context: &mut impl InitContext,
|
2022-03-18 09:24:54 +11:00
|
|
|
) -> bool {
|
2022-09-05 03:09:22 +10:00
|
|
|
// After `PEAK_METER_DECAY_MS` milliseconds of pure silence, the peak meter's value should
|
|
|
|
// have dropped by 12 dB
|
|
|
|
self.peak_meter_decay_weight = 0.25f64
|
|
|
|
.powf((buffer_config.sample_rate as f64 * PEAK_METER_DECAY_MS / 1000.0).recip())
|
|
|
|
as f32;
|
2022-03-18 09:24:54 +11:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer: &mut Buffer,
|
2022-05-27 10:30:57 +10:00
|
|
|
_aux: &mut AuxiliaryBuffers,
|
2022-03-18 09:24:54 +11:00
|
|
|
_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 {
|
2022-07-25 05:21:13 +10:00
|
|
|
*sample *= gain;
|
2022-03-18 09:24:54 +11:00
|
|
|
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
|
2022-07-14 07:16:29 +10:00
|
|
|
if self.params.editor_state.is_open() {
|
2022-03-18 09:24:54 +11:00
|
|
|
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-egui.gain-gui";
|
2022-07-04 20:44:30 +10:00
|
|
|
const CLAP_DESCRIPTION: Option<&'static str> = Some("A smoothed gain parameter example plugin");
|
|
|
|
const CLAP_MANUAL_URL: Option<&'static str> = Some(Self::URL);
|
|
|
|
const CLAP_SUPPORT_URL: Option<&'static str> = None;
|
2022-06-02 09:16:30 +10:00
|
|
|
const CLAP_FEATURES: &'static [ClapFeature] = &[
|
|
|
|
ClapFeature::AudioEffect,
|
|
|
|
ClapFeature::Stereo,
|
|
|
|
ClapFeature::Mono,
|
|
|
|
ClapFeature::Utility,
|
|
|
|
];
|
2022-03-18 09:24:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vst3Plugin for Gain {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"GainGuiYeahBoyyy";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Dynamics";
|
|
|
|
}
|
|
|
|
|
|
|
|
nih_export_clap!(Gain);
|
|
|
|
nih_export_vst3!(Gain);
|