2022-02-06 23:16:28 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nih_plug;
|
|
|
|
|
|
|
|
use atomic_float::AtomicF32;
|
|
|
|
use nih_plug::{
|
2022-02-09 21:51:53 +11:00
|
|
|
formatters, util, Buffer, BufferConfig, BusConfig, Editor, IntParam, Plugin, ProcessContext,
|
2022-02-07 03:12:57 +11:00
|
|
|
ProcessStatus, Vst3Plugin,
|
2022-02-06 23:16:28 +11:00
|
|
|
};
|
|
|
|
use nih_plug::{FloatParam, Param, Params, Range, Smoother, SmoothingStyle};
|
2022-02-09 21:51:53 +11:00
|
|
|
use nih_plug_egui::{create_egui_editor, egui, widgets, EguiState};
|
2022-02-06 23:16:28 +11:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// This is mostly identical to the gain example, minus some fluff, and with a GUI.
|
|
|
|
struct Gain {
|
|
|
|
params: Pin<Arc<GainParams>>,
|
2022-02-09 06:16:39 +11:00
|
|
|
editor_state: Arc<EguiState>,
|
2022-02-06 23:16:28 +11:00
|
|
|
|
2022-02-06 23:36:55 +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`.
|
2022-02-06 23:16:28 +11:00
|
|
|
///
|
|
|
|
/// This is stored as voltage gain.
|
2022-02-06 23:36:55 +11:00
|
|
|
peak_meter: Arc<AtomicF32>,
|
2022-02-06 23:16:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
|
|
|
struct GainParams {
|
|
|
|
#[id = "gain"]
|
|
|
|
pub gain: FloatParam,
|
2022-02-09 21:51:53 +11:00
|
|
|
|
|
|
|
// TODO: Remove this parameter when we're done implementing the widgets
|
|
|
|
#[id = "foobar"]
|
|
|
|
pub some_int: IntParam,
|
2022-02-06 23:16:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Gain {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
params: Arc::pin(GainParams::default()),
|
2022-02-10 05:49:23 +11:00
|
|
|
editor_state: EguiState::from_size(300, 180),
|
2022-02-06 23:16:28 +11:00
|
|
|
|
2022-02-06 23:36:55 +11:00
|
|
|
peak_meter_decay_weight: 1.0,
|
|
|
|
peak_meter: Arc::new(AtomicF32::new(util::MINUS_INFINITY_DB)),
|
2022-02-06 23:16:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GainParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
gain: FloatParam {
|
|
|
|
value: 0.0,
|
|
|
|
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)),
|
|
|
|
value_changed: None,
|
|
|
|
range: Range::Linear {
|
|
|
|
min: -30.0,
|
|
|
|
max: 30.0,
|
|
|
|
},
|
|
|
|
name: "Gain",
|
|
|
|
unit: " dB",
|
|
|
|
value_to_string: formatters::f32_rounded(2),
|
|
|
|
string_to_value: None,
|
|
|
|
},
|
2022-02-09 21:51:53 +11:00
|
|
|
some_int: IntParam {
|
|
|
|
value: 3,
|
|
|
|
smoothed: Smoother::none(),
|
|
|
|
name: "Something",
|
|
|
|
range: Range::Skewed {
|
|
|
|
min: 0,
|
|
|
|
max: 3,
|
|
|
|
factor: Range::skew_factor(1.0),
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-02-06 23:16:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Gain {
|
|
|
|
const NAME: &'static str = "Gain GUI";
|
|
|
|
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;
|
|
|
|
|
|
|
|
fn params(&self) -> Pin<&dyn Params> {
|
|
|
|
self.params.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-02-07 03:12:57 +11:00
|
|
|
fn editor(&self) -> Option<Box<dyn Editor>> {
|
2022-02-06 23:16:28 +11:00
|
|
|
let params = self.params.clone();
|
2022-02-06 23:36:55 +11:00
|
|
|
let peak_meter = self.peak_meter.clone();
|
2022-02-06 23:16:28 +11:00
|
|
|
create_egui_editor(
|
2022-02-09 06:16:39 +11:00
|
|
|
self.editor_state.clone(),
|
2022-02-06 23:16:28 +11:00
|
|
|
(),
|
2022-02-07 03:12:57 +11:00
|
|
|
move |egui_ctx, setter, _state| {
|
2022-02-06 23:16:28 +11:00
|
|
|
egui::CentralPanel::default().show(egui_ctx, |ui| {
|
2022-02-09 21:51:53 +11:00
|
|
|
// 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.
|
2022-02-10 05:49:23 +11:00
|
|
|
ui.label("Some random wierdly distributed integer");
|
2022-02-09 21:51:53 +11:00
|
|
|
ui.add(widgets::ParamSlider::for_param(¶ms.some_int, setter));
|
2022-02-10 05:49:23 +11:00
|
|
|
|
|
|
|
ui.label("Gain");
|
2022-02-09 22:24:01 +11:00
|
|
|
ui.add(widgets::ParamSlider::for_param(¶ms.gain, setter));
|
2022-02-06 23:16:28 +11:00
|
|
|
|
2022-02-10 05:49:23 +11:00
|
|
|
ui.label(
|
|
|
|
"Also gain, but with a lame widget. Can't even render the value correctly!",
|
|
|
|
);
|
2022-02-09 21:51:53 +11:00
|
|
|
// This is a simple naieve version of a parameter slider that's not aware of how
|
|
|
|
// the parmaeters work
|
2022-02-06 23:16:28 +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
|
|
|
|
}
|
|
|
|
None => params.gain.value as f64,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.suffix(" dB"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: Add a proper custom widget instead of reusing a progress bar
|
2022-02-06 23:36:55 +11:00
|
|
|
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 {
|
2022-02-07 00:18:47 +11:00
|
|
|
format!("{:.1} dBFS", peak_meter)
|
2022-02-06 23:16:28 +11:00
|
|
|
} else {
|
|
|
|
String::from("-inf dBFS")
|
|
|
|
};
|
|
|
|
|
2022-02-07 00:59:13 +11:00
|
|
|
let peak_meter_normalized = (peak_meter + 60.0) / 60.0;
|
2022-02-06 23:16:28 +11:00
|
|
|
ui.allocate_space(egui::Vec2::splat(2.0));
|
|
|
|
ui.add(
|
2022-02-06 23:36:55 +11:00
|
|
|
egui::widgets::ProgressBar::new(peak_meter_normalized)
|
|
|
|
.text(peak_meter_text),
|
2022-02-06 23:16:28 +11:00
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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?
|
2022-02-07 00:18:47 +11:00
|
|
|
self.peak_meter_decay_weight = 0.9992f32.powf(44_100.0 / buffer_config.sample_rate);
|
2022-02-06 23:16:28 +11:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer: &mut Buffer,
|
|
|
|
_context: &mut impl ProcessContext,
|
|
|
|
) -> ProcessStatus {
|
|
|
|
for samples in buffer.iter_mut() {
|
|
|
|
let mut amplitude = 0.0;
|
|
|
|
let num_samples = samples.len();
|
2022-02-06 23:36:55 +11:00
|
|
|
|
|
|
|
let gain = self.params.gain.smoothed.next();
|
2022-02-06 23:16:28 +11:00
|
|
|
for sample in samples {
|
|
|
|
*sample *= util::db_to_gain(gain);
|
|
|
|
amplitude += *sample;
|
|
|
|
}
|
|
|
|
|
2022-02-09 06:28:40 +11:00
|
|
|
// 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-02-09 06:33:08 +11:00
|
|
|
if self.editor_state.is_open() {
|
2022-02-09 06:28:40 +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)
|
|
|
|
}
|
2022-02-06 23:16:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessStatus::Normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vst3Plugin for Gain {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"GainGuiYeahBoyyy";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Dynamics";
|
|
|
|
}
|
|
|
|
|
|
|
|
nih_export_vst3!(Gain);
|