mirror of
https://github.com/robbert-vdh/nih-plug.git
synced 2026-07-01 02:36:54 +00:00
Rename the gain GUI examples to match package name
This commit is contained in:
17
plugins/examples/gain_gui_iced/Cargo.toml
Normal file
17
plugins/examples/gain_gui_iced/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "gain_gui_iced"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["Robbert van der Helm <mail@robbertvanderhelm.nl>"]
|
||||
license = "ISC"
|
||||
|
||||
description = "A simple gain plugin with an iced GUI"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
nih_plug = { path = "../../../", features = ["assert_process_allocs"] }
|
||||
nih_plug_iced = { path = "../../../nih_plug_iced" }
|
||||
|
||||
atomic_float = "0.1"
|
||||
119
plugins/examples/gain_gui_iced/src/editor.rs
Normal file
119
plugins/examples/gain_gui_iced/src/editor.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use atomic_float::AtomicF32;
|
||||
use nih_plug::prelude::{util, Editor, GuiContext};
|
||||
use nih_plug_iced::widgets as nih_widgets;
|
||||
use nih_plug_iced::*;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::GainParams;
|
||||
|
||||
// Makes sense to also define this here, makes it a bit easier to keep track of
|
||||
pub(crate) fn default_state() -> Arc<IcedState> {
|
||||
IcedState::from_size(200, 150)
|
||||
}
|
||||
|
||||
pub(crate) fn create(
|
||||
params: Arc<GainParams>,
|
||||
peak_meter: Arc<AtomicF32>,
|
||||
editor_state: Arc<IcedState>,
|
||||
) -> Option<Box<dyn Editor>> {
|
||||
create_iced_editor::<GainEditor>(editor_state, (params, peak_meter))
|
||||
}
|
||||
|
||||
struct GainEditor {
|
||||
params: Arc<GainParams>,
|
||||
context: Arc<dyn GuiContext>,
|
||||
|
||||
peak_meter: Arc<AtomicF32>,
|
||||
|
||||
gain_slider_state: nih_widgets::param_slider::State,
|
||||
peak_meter_state: nih_widgets::peak_meter::State,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Message {
|
||||
/// Update a parameter's value.
|
||||
ParamUpdate(nih_widgets::ParamMessage),
|
||||
}
|
||||
|
||||
impl IcedEditor for GainEditor {
|
||||
type Executor = executor::Default;
|
||||
type Message = Message;
|
||||
type InitializationFlags = (Arc<GainParams>, Arc<AtomicF32>);
|
||||
|
||||
fn new(
|
||||
(params, peak_meter): Self::InitializationFlags,
|
||||
context: Arc<dyn GuiContext>,
|
||||
) -> (Self, Command<Self::Message>) {
|
||||
let editor = GainEditor {
|
||||
params,
|
||||
context,
|
||||
|
||||
peak_meter,
|
||||
|
||||
gain_slider_state: Default::default(),
|
||||
peak_meter_state: Default::default(),
|
||||
};
|
||||
|
||||
(editor, Command::none())
|
||||
}
|
||||
|
||||
fn context(&self) -> &dyn GuiContext {
|
||||
self.context.as_ref()
|
||||
}
|
||||
|
||||
fn update(
|
||||
&mut self,
|
||||
_window: &mut WindowQueue,
|
||||
message: Self::Message,
|
||||
) -> Command<Self::Message> {
|
||||
match message {
|
||||
Message::ParamUpdate(message) => self.handle_param_message(message),
|
||||
}
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
||||
fn view(&mut self) -> Element<'_, Self::Message> {
|
||||
Column::new()
|
||||
.align_items(Alignment::Center)
|
||||
.push(
|
||||
Text::new("Gain GUI")
|
||||
.font(assets::NOTO_SANS_LIGHT)
|
||||
.size(40)
|
||||
.height(50.into())
|
||||
.width(Length::Fill)
|
||||
.horizontal_alignment(alignment::Horizontal::Center)
|
||||
.vertical_alignment(alignment::Vertical::Bottom),
|
||||
)
|
||||
.push(
|
||||
Text::new("Gain")
|
||||
.height(20.into())
|
||||
.width(Length::Fill)
|
||||
.horizontal_alignment(alignment::Horizontal::Center)
|
||||
.vertical_alignment(alignment::Vertical::Center),
|
||||
)
|
||||
.push(
|
||||
nih_widgets::ParamSlider::new(&mut self.gain_slider_state, &self.params.gain)
|
||||
.map(Message::ParamUpdate),
|
||||
)
|
||||
.push(Space::with_height(10.into()))
|
||||
.push(
|
||||
nih_widgets::PeakMeter::new(
|
||||
&mut self.peak_meter_state,
|
||||
util::gain_to_db(self.peak_meter.load(std::sync::atomic::Ordering::Relaxed)),
|
||||
)
|
||||
.hold_time(Duration::from_millis(600)),
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn background_color(&self) -> nih_plug_iced::Color {
|
||||
nih_plug_iced::Color {
|
||||
r: 0.98,
|
||||
g: 0.98,
|
||||
b: 0.98,
|
||||
a: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
151
plugins/examples/gain_gui_iced/src/lib.rs
Normal file
151
plugins/examples/gain_gui_iced/src/lib.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
use atomic_float::AtomicF32;
|
||||
use nih_plug::prelude::*;
|
||||
use nih_plug_iced::IcedState;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod editor;
|
||||
|
||||
/// This is mostly identical to the gain example, minus some fluff, and with a GUI.
|
||||
struct Gain {
|
||||
params: Arc<GainParams>,
|
||||
editor_state: Arc<IcedState>,
|
||||
|
||||
/// 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::new(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 (iced)";
|
||||
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 SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
||||
|
||||
fn params(&self) -> Arc<dyn Params> {
|
||||
self.params.clone()
|
||||
}
|
||||
|
||||
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-iced";
|
||||
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"GainGuiIcedAaAAa";
|
||||
const VST3_CATEGORIES: &'static str = "Fx|Dynamics";
|
||||
}
|
||||
|
||||
nih_export_clap!(Gain);
|
||||
nih_export_vst3!(Gain);
|
||||
Reference in New Issue
Block a user