2022-02-06 23:16:28 +11:00
|
|
|
use atomic_float::AtomicF32;
|
2022-03-04 09:23:51 +11:00
|
|
|
use nih_plug::prelude::*;
|
2022-03-14 07:59:13 +11:00
|
|
|
use nih_plug_iced::IcedState;
|
2022-02-06 23:16:28 +11:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-03-14 07:59:13 +11:00
|
|
|
mod editor;
|
|
|
|
|
2022-02-06 23:16:28 +11:00
|
|
|
/// This is mostly identical to the gain example, minus some fluff, and with a GUI.
|
|
|
|
struct Gain {
|
|
|
|
params: Pin<Arc<GainParams>>,
|
2022-03-14 07:59:13 +11:00
|
|
|
editor_state: Arc<IcedState>,
|
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,
|
2022-03-04 09:05:01 +11:00
|
|
|
/// The current data for the peak meter. This is stored as an [`Arc`] so we can share it between
|
2022-02-06 23:36:55 +11:00
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Gain {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
params: Arc::pin(GainParams::default()),
|
2022-03-14 07:59:13 +11:00
|
|
|
editor_state: editor::default_state(),
|
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 {
|
2022-02-13 03:19:52 +11:00
|
|
|
gain: FloatParam::new(
|
|
|
|
"Gain",
|
|
|
|
0.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::Linear {
|
2022-02-06 23:16:28 +11:00
|
|
|
min: -30.0,
|
|
|
|
max: 30.0,
|
|
|
|
},
|
2022-02-13 03:19:52 +11:00
|
|
|
)
|
2022-02-13 05:56:37 +11:00
|
|
|
.with_smoother(SmoothingStyle::Linear(50.0))
|
2022-02-14 02:43:11 +11:00
|
|
|
.with_step_size(0.01)
|
|
|
|
.with_unit(" dB"),
|
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;
|
2022-03-11 04:57:17 +11:00
|
|
|
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
2022-02-06 23:16:28 +11:00
|
|
|
|
|
|
|
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-03-16 03:06:47 +11:00
|
|
|
editor::create(
|
|
|
|
self.params.clone(),
|
|
|
|
self.peak_meter.clone(),
|
|
|
|
self.editor_state.clone(),
|
|
|
|
)
|
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 {
|
2022-03-10 06:11:37 +11:00
|
|
|
for channel_samples in buffer.iter_samples() {
|
2022-02-06 23:16:28 +11:00
|
|
|
let mut amplitude = 0.0;
|
2022-03-04 07:38:50 +11:00
|
|
|
let num_samples = channel_samples.len();
|
2022-02-06 23:36:55 +11:00
|
|
|
|
|
|
|
let gain = self.params.gain.smoothed.next();
|
2022-03-04 07:38:50 +11:00
|
|
|
for sample in channel_samples {
|
2022-02-06 23:16:28 +11:00
|
|
|
*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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:04:39 +11:00
|
|
|
impl ClapPlugin for Gain {
|
2022-03-04 07:31:26 +11:00
|
|
|
const CLAP_ID: &'static str = "com.moist-plugins-gmbh.gain-gui";
|
2022-03-01 03:18:11 +11:00
|
|
|
const CLAP_DESCRIPTION: &'static str = "A smoothed gain parameter example plugin";
|
2022-03-01 03:29:53 +11:00
|
|
|
const CLAP_FEATURES: &'static [&'static str] = &["audio_effect", "mono", "stereo", "tool"];
|
2022-03-01 03:18:11 +11:00
|
|
|
const CLAP_MANUAL_URL: &'static str = Self::URL;
|
|
|
|
const CLAP_SUPPORT_URL: &'static str = Self::URL;
|
2022-03-01 03:04:39 +11:00
|
|
|
}
|
2022-03-01 00:45:07 +11:00
|
|
|
|
2022-02-06 23:16:28 +11:00
|
|
|
impl Vst3Plugin for Gain {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"GainGuiYeahBoyyy";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Dynamics";
|
|
|
|
}
|
|
|
|
|
2022-03-01 00:45:31 +11:00
|
|
|
nih_export_clap!(Gain);
|
2022-02-06 23:16:28 +11:00
|
|
|
nih_export_vst3!(Gain);
|