2022-03-04 09:23:51 +11:00
|
|
|
use nih_plug::prelude::*;
|
2022-11-05 04:08:01 +11:00
|
|
|
use parking_lot::Mutex;
|
2022-04-07 23:31:46 +10:00
|
|
|
use std::sync::Arc;
|
2022-01-26 09:02:15 +11:00
|
|
|
|
|
|
|
struct Gain {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<GainParams>,
|
2022-01-26 09:02:15 +11:00
|
|
|
}
|
2022-01-26 08:18:55 +11:00
|
|
|
|
2022-07-17 02:51:01 +10:00
|
|
|
/// The [`Params`] derive macro gathers all of the information needed for the wrapper to know about
|
2022-03-21 23:26:28 +11:00
|
|
|
/// the plugin's parameters, persistent serializable fields, and nested parameter groups. You can
|
2022-07-17 02:51:01 +10:00
|
|
|
/// also easily implement [`Params`] by hand if you want to, for instance, have multiple instances
|
2022-04-25 02:34:40 +10:00
|
|
|
/// of a parameters struct for multiple identical oscillators/filters/envelopes.
|
2022-01-26 08:18:55 +11:00
|
|
|
#[derive(Params)]
|
2022-01-26 09:02:15 +11:00
|
|
|
struct GainParams {
|
2022-09-29 20:28:56 +10:00
|
|
|
/// The parameter's ID is used to identify the parameter in the wrapped plugin API. As long as
|
2022-03-21 23:26:28 +11:00
|
|
|
/// these IDs remain constant, you can rename and reorder these fields as you wish. The
|
2022-07-25 05:21:13 +10:00
|
|
|
/// parameters are exposed to the host in the same order they were defined. In this case, this
|
|
|
|
/// gain parameter is stored as linear gain while the values are displayed in decibels.
|
2022-01-30 06:40:14 +11:00
|
|
|
#[id = "gain"]
|
2022-01-26 09:02:15 +11:00
|
|
|
pub gain: FloatParam,
|
2022-01-31 02:16:15 +11:00
|
|
|
|
2022-07-17 02:51:01 +10:00
|
|
|
/// This field isn't used in this example, but anything written to the vector would be restored
|
2022-06-28 21:42:46 +10:00
|
|
|
/// together with a preset/state file saved for this plugin. This can be useful for storing
|
2022-01-31 02:16:15 +11:00
|
|
|
/// things like sample data.
|
|
|
|
#[persist = "industry_secrets"]
|
2022-11-05 04:08:01 +11:00
|
|
|
pub random_data: Mutex<Vec<f32>>,
|
2022-03-04 06:37:01 +11:00
|
|
|
|
2022-03-17 03:04:38 +11:00
|
|
|
/// You can also nest parameter structs. These will appear as a separate nested group if your
|
|
|
|
/// DAW displays parameters in a tree structure.
|
2022-10-13 10:20:56 +11:00
|
|
|
#[nested(group = "Subparameters")]
|
2022-03-04 06:37:01 +11:00
|
|
|
pub sub_params: SubParams,
|
2022-10-13 10:20:56 +11:00
|
|
|
|
|
|
|
/// Nested parameters also support some advanced functionality for reusing the same parameter
|
|
|
|
/// struct multiple times.
|
|
|
|
#[nested(array, group = "Array Parameters")]
|
|
|
|
pub array_params: [ArrayParams; 3],
|
2022-03-04 06:37:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
|
|
|
struct SubParams {
|
|
|
|
#[id = "thing"]
|
|
|
|
pub nested_parameter: FloatParam,
|
2022-03-17 03:04:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
2022-10-13 10:20:56 +11:00
|
|
|
struct ArrayParams {
|
|
|
|
/// This parameter's ID will get a `_1`, `_2`, and a `_3` suffix because of how it's used in
|
|
|
|
/// `array_params` above.
|
2022-03-17 03:04:38 +11:00
|
|
|
#[id = "noope"]
|
|
|
|
pub nope: FloatParam,
|
2022-01-26 09:02:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Gain {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc::new(GainParams::default()),
|
2022-01-26 09:02:15 +11:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 08:18:55 +11:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:02:15 +11:00
|
|
|
impl Default for GainParams {
|
2022-01-26 08:18:55 +11:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-07-25 05:21:13 +10:00
|
|
|
// This gain is stored as linear gain. NIH-plug comes with useful conversion functions
|
|
|
|
// to treat these kinds of parameters as if we were dealing with decibels. Storing this
|
|
|
|
// as decibels is easier to work with, but requires a conversion for every sample.
|
2022-03-21 23:26:28 +11:00
|
|
|
gain: FloatParam::new(
|
|
|
|
"Gain",
|
2022-08-18 21:45:13 +10:00
|
|
|
util::db_to_gain(0.0),
|
2022-07-25 05:21:13 +10:00
|
|
|
FloatRange::Skewed {
|
|
|
|
min: util::db_to_gain(-30.0),
|
|
|
|
max: util::db_to_gain(30.0),
|
|
|
|
// This makes the range appear as if it was linear when displaying the values as
|
|
|
|
// decibels
|
|
|
|
factor: FloatRange::gain_skew_factor(-30.0, 30.0),
|
2022-01-26 08:18:55 +11:00
|
|
|
},
|
2022-03-21 23:26:28 +11:00
|
|
|
)
|
2022-07-25 05:21:13 +10:00
|
|
|
// Because the gain parameter is stored as linear gain instead of storing the value as
|
|
|
|
// decibels, we need logarithmic smoothing
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(50.0))
|
2022-03-21 23:26:28 +11:00
|
|
|
.with_unit(" dB")
|
2022-07-25 05:21:13 +10:00
|
|
|
// There are many predefined formatters we can use here. If the gain was stored as
|
|
|
|
// decibels instead of as a linear gain value, we could have also used the
|
|
|
|
// `.with_step_size(0.1)` function to get internal rounding.
|
|
|
|
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
|
|
|
|
.with_string_to_value(formatters::s2v_f32_gain_to_db()),
|
2022-09-29 20:33:08 +10:00
|
|
|
// Persisted fields can be initialized like any other fields, and they'll keep their
|
2022-07-17 02:51:01 +10:00
|
|
|
// values when restoring the plugin's state.
|
2022-11-05 04:08:01 +11:00
|
|
|
random_data: Mutex::new(Vec::new()),
|
2022-03-04 06:37:01 +11:00
|
|
|
sub_params: SubParams {
|
|
|
|
nested_parameter: FloatParam::new(
|
|
|
|
"Unused Nested Parameter",
|
|
|
|
0.5,
|
|
|
|
FloatRange::Skewed {
|
|
|
|
min: 2.0,
|
|
|
|
max: 2.4,
|
|
|
|
factor: FloatRange::skew_factor(2.0),
|
|
|
|
},
|
|
|
|
)
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_rounded(2)),
|
2022-03-04 06:37:01 +11:00
|
|
|
},
|
2022-10-13 10:20:56 +11:00
|
|
|
array_params: [1, 2, 3].map(|index| ArrayParams {
|
|
|
|
nope: FloatParam::new(
|
|
|
|
format!("Nope {index}"),
|
|
|
|
0.5,
|
|
|
|
FloatRange::Linear { min: 1.0, max: 2.0 },
|
|
|
|
),
|
|
|
|
}),
|
2022-01-26 09:02:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Gain {
|
2022-01-27 04:14:13 +11:00
|
|
|
const NAME: &'static str = "Gain";
|
|
|
|
const VENDOR: &'static str = "Moist Plugins GmbH";
|
2022-11-12 04:08:10 +11:00
|
|
|
// You can use `env!("CARGO_PKG_HOMEPAGE")` to reference the homepage field from the
|
|
|
|
// `Cargo.toml` file here
|
2022-01-27 04:14:13 +11:00
|
|
|
const URL: &'static str = "https://youtu.be/dQw4w9WgXcQ";
|
|
|
|
const EMAIL: &'static str = "info@example.com";
|
|
|
|
|
2022-11-10 01:50:21 +11:00
|
|
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2022-01-27 05:49:22 +11:00
|
|
|
|
2022-08-19 22:34:21 +10:00
|
|
|
const DEFAULT_INPUT_CHANNELS: u32 = 2;
|
|
|
|
const DEFAULT_OUTPUT_CHANNELS: u32 = 2;
|
2022-01-26 09:02:15 +11:00
|
|
|
|
2022-05-24 01:07:48 +10:00
|
|
|
const DEFAULT_AUX_INPUTS: Option<AuxiliaryIOConfig> = None;
|
|
|
|
const DEFAULT_AUX_OUTPUTS: Option<AuxiliaryIOConfig> = None;
|
|
|
|
|
2022-04-08 04:27:37 +10:00
|
|
|
const MIDI_INPUT: MidiConfig = MidiConfig::None;
|
2022-03-11 06:18:42 +11:00
|
|
|
// Setting this to `true` will tell the wrapper to split the buffer up into smaller blocks
|
|
|
|
// whenever there are inter-buffer parameter changes. This way no changes to the plugin are
|
|
|
|
// required to support sample accurate automation and the wrapper handles all of the boring
|
|
|
|
// stuff like making sure transport and other timing information stays consistent between the
|
|
|
|
// splits.
|
2022-03-11 04:57:17 +11:00
|
|
|
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
2022-02-04 11:09:09 +11:00
|
|
|
|
2022-10-22 10:59:29 +11:00
|
|
|
// More advanced plugins can use this to run expensive background tasks. See the field's
|
|
|
|
// documentation for more information. `()` means that the plugin does not have any background
|
|
|
|
// tasks.
|
|
|
|
type BackgroundTask = ();
|
2022-10-22 08:52:46 +11:00
|
|
|
|
2022-04-07 23:31:46 +10:00
|
|
|
fn params(&self) -> Arc<dyn Params> {
|
2022-05-25 07:55:48 +10:00
|
|
|
self.params.clone()
|
2022-01-26 09:02:15 +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
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:42:58 +11:00
|
|
|
// This plugin doesn't need any special initialization, but if you need to do anything expensive
|
2022-07-17 02:51:01 +10:00
|
|
|
// then this would be the place. State is kept around when the host reconfigures the
|
|
|
|
// plugin. If we do need special initialization, we could implement the `initialize()` and/or
|
2022-03-08 10:42:58 +11:00
|
|
|
// `reset()` methods
|
2022-01-26 09:02:15 +11:00
|
|
|
|
2022-02-05 09:03:11 +11:00
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer: &mut Buffer,
|
2022-05-27 10:30:57 +10:00
|
|
|
_aux: &mut AuxiliaryBuffers,
|
2022-10-22 10:15:16 +11:00
|
|
|
_context: &mut impl ProcessContext<Self>,
|
2022-02-05 09:03:11 +11:00
|
|
|
) -> ProcessStatus {
|
2022-03-10 06:11:37 +11:00
|
|
|
for channel_samples in buffer.iter_samples() {
|
2022-02-03 07:08:23 +11:00
|
|
|
// Smoothing is optionally built into the parameters themselves
|
|
|
|
let gain = self.params.gain.smoothed.next();
|
|
|
|
|
2022-03-04 07:38:50 +11:00
|
|
|
for sample in channel_samples {
|
2022-07-25 05:21:13 +10:00
|
|
|
*sample *= gain;
|
2022-01-26 09:02:15 +11:00
|
|
|
}
|
2022-01-26 08:18:55 +11:00
|
|
|
}
|
2022-01-28 07:03:49 +11:00
|
|
|
|
|
|
|
ProcessStatus::Normal
|
2022-01-26 08:18:55 +11:00
|
|
|
}
|
2022-05-24 21:03:30 +10:00
|
|
|
|
|
|
|
// This can be used for cleaning up special resources like socket connections whenever the
|
|
|
|
// plugin is deactivated. Most plugins won't need to do anything here.
|
|
|
|
fn deactivate(&mut self) {}
|
2022-01-26 08:18:55 +11:00
|
|
|
}
|
2022-01-26 22:37:45 +11:00
|
|
|
|
2022-03-01 03:04:39 +11:00
|
|
|
impl ClapPlugin for Gain {
|
|
|
|
const CLAP_ID: &'static str = "com.moist-plugins-gmbh.gain";
|
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,
|
2022-07-04 20:44:30 +10:00
|
|
|
ClapFeature::Mono,
|
2022-06-02 09:16:30 +10:00
|
|
|
ClapFeature::Utility,
|
|
|
|
];
|
2022-03-01 03:04:39 +11:00
|
|
|
}
|
2022-03-01 00:45:07 +11:00
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl Vst3Plugin for Gain {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"GainMoistestPlug";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Dynamics";
|
|
|
|
}
|
|
|
|
|
2022-03-01 00:45:31 +11:00
|
|
|
nih_export_clap!(Gain);
|
2022-01-28 08:13:13 +11:00
|
|
|
nih_export_vst3!(Gain);
|