2022-01-25 12:19:53 +11:00
|
|
|
// nih-plug: plugins, but rewritten in Rust
|
2022-01-25 12:19:31 +11:00
|
|
|
// Copyright (C) 2022 Robbert van der Helm
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-02-05 22:46:26 +11:00
|
|
|
use raw_window_handle::RawWindowHandle;
|
2022-01-25 12:19:31 +11:00
|
|
|
use std::pin::Pin;
|
|
|
|
|
2022-02-03 01:01:41 +11:00
|
|
|
use crate::buffer::Buffer;
|
2022-02-05 22:46:26 +11:00
|
|
|
use crate::context::{GuiContext, ProcessContext};
|
2022-02-03 01:12:33 +11:00
|
|
|
use crate::param::internals::Params;
|
2022-01-25 12:19:31 +11:00
|
|
|
|
|
|
|
/// Basic functionality that needs to be implemented by a plugin. The wrappers will use this to
|
|
|
|
/// expose the plugin in a particular plugin format.
|
|
|
|
///
|
|
|
|
/// This is super basic, and lots of things I didn't need or want to use yet haven't been
|
|
|
|
/// implemented. Notable missing features include:
|
|
|
|
///
|
|
|
|
/// - Sidechain inputs
|
|
|
|
/// - Multiple output busses
|
2022-01-28 23:40:47 +11:00
|
|
|
/// - Special handling for offline processing
|
2022-01-29 01:18:55 +11:00
|
|
|
/// - Transport and other context information in the process call
|
2022-01-25 12:19:31 +11:00
|
|
|
/// - Sample accurate automation (this would be great, but sadly few hosts even support it so until
|
|
|
|
/// they do we'll ignore that it's a thing)
|
2022-01-26 06:36:43 +11:00
|
|
|
/// - Parameter hierarchies/groups
|
2022-01-27 10:19:50 +11:00
|
|
|
/// - Bypass parameters, right now the VST3 wrapper generates one for you
|
2022-01-25 12:19:31 +11:00
|
|
|
/// - Outputting parameter changes from the plugin
|
2022-02-05 08:31:43 +11:00
|
|
|
/// - MIDI CC handling
|
|
|
|
/// - Outputting MIDI events
|
2022-01-25 12:19:31 +11:00
|
|
|
/// - GUIs
|
2022-02-01 06:18:12 +11:00
|
|
|
pub trait Plugin: Default + Send + Sync {
|
2022-02-05 22:46:26 +11:00
|
|
|
/// The type of the GUI editor instance belonging to this plugin. Use [NoEditor] when you don't
|
2022-02-05 22:52:29 +11:00
|
|
|
/// need an editor. Make sure to implement both the [Self::create_editor()] and
|
|
|
|
/// [Self::editor_size()] functions when you do add an editor.
|
2022-02-05 22:46:26 +11:00
|
|
|
type Editor: Editor;
|
|
|
|
|
2022-01-27 04:14:13 +11:00
|
|
|
const NAME: &'static str;
|
|
|
|
const VENDOR: &'static str;
|
|
|
|
const URL: &'static str;
|
|
|
|
const EMAIL: &'static str;
|
|
|
|
|
2022-01-27 08:20:15 +11:00
|
|
|
/// Semver compatible version string (e.g. `0.0.1`). Hosts likely won't do anything with this,
|
|
|
|
/// but just in case they do this should only contain decimals values and dots.
|
|
|
|
const VERSION: &'static str;
|
2022-01-27 05:49:22 +11:00
|
|
|
|
2022-01-25 12:19:31 +11:00
|
|
|
/// The default number of inputs. Some hosts like, like Bitwig and Ardour, use the defaults
|
|
|
|
/// instead of setting up the busses properly.
|
2022-02-04 11:11:07 +11:00
|
|
|
const DEFAULT_NUM_INPUTS: u32 = 2;
|
2022-01-25 12:19:31 +11:00
|
|
|
/// The default number of inputs. Some hosts like, like Bitwig and Ardour, use the defaults
|
|
|
|
/// instead of setting up the busses properly.
|
2022-02-04 11:11:07 +11:00
|
|
|
const DEFAULT_NUM_OUTPUTS: u32 = 2;
|
2022-01-25 12:19:31 +11:00
|
|
|
|
2022-02-04 11:09:09 +11:00
|
|
|
/// Whether the plugin accepts note events. If this is set to `false`, then the plugin won't
|
|
|
|
/// receive any note events.
|
2022-02-04 11:11:07 +11:00
|
|
|
const ACCEPTS_MIDI: bool = false;
|
2022-02-04 11:09:09 +11:00
|
|
|
|
2022-01-25 12:19:31 +11:00
|
|
|
/// The plugin's parameters. The host will update the parameter values before calling
|
|
|
|
/// `process()`. These parameters are identified by strings that should never change when the
|
|
|
|
/// plugin receives an update.
|
|
|
|
fn params(&self) -> Pin<&dyn Params>;
|
|
|
|
|
2022-02-05 22:46:26 +11:00
|
|
|
/// Create an editor for this plugin and embed it in the parent window. The idea is that you
|
|
|
|
/// take a reference to your [Params] in your editor to be able to read the current values. Then
|
|
|
|
/// whenever you need to change any of those values, you can use the methods on the [GuiContext]
|
|
|
|
/// that's passed to this function. When you change a parameter value there it will be
|
|
|
|
/// broadcasted to the host and also updated in your [Params] struct.
|
|
|
|
//
|
|
|
|
// TODO: Think of how this would work with the event loop. On Linux the wrapper must provide a
|
|
|
|
// timer using VST3's `IRunLoop` interface, but on Window and macOS the window would
|
|
|
|
// normally register its own timer. Right now we just ignore this because it would
|
|
|
|
// otherwise be basically impossible to have this still be GUI-framework agnostic. Any
|
|
|
|
// callback that deos involve actual GUI operations will still be spooled to the IRunLoop
|
|
|
|
// instance.
|
|
|
|
fn create_editor(
|
|
|
|
&self,
|
|
|
|
_parent: RawWindowHandle,
|
|
|
|
_context: &impl GuiContext,
|
|
|
|
) -> Option<Self::Editor> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the current size of the plugin's editor, if it has one.
|
|
|
|
fn editor_size(&self) -> Option<(u32, u32)> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:19:31 +11:00
|
|
|
//
|
|
|
|
// The following functions follow the lifetime of the plugin.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Whether the plugin supports a bus config. This only acts as a check, and the plugin
|
|
|
|
/// shouldn't do anything beyond returning true or false.
|
2022-02-04 11:13:00 +11:00
|
|
|
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
|
|
|
|
config.num_input_channels == 2 && config.num_output_channels == 2
|
|
|
|
}
|
2022-01-25 12:19:31 +11:00
|
|
|
|
2022-02-02 03:09:23 +11:00
|
|
|
/// Initialize the plugin for the given bus and buffer configurations. If the plugin is being
|
|
|
|
/// restored from an old state, then that state will have already been restored at this point.
|
|
|
|
/// If based on those parameters (or for any reason whatsoever) the plugin needs to introduce
|
2022-02-04 02:22:32 +11:00
|
|
|
/// latency, then you can do so here using the process context. Depending on how the host
|
2022-02-04 11:13:00 +11:00
|
|
|
/// restores plugin state, this function may also be called twice in rapid succession. If the
|
|
|
|
/// plugin fails to inialize for whatever reason, then this should return `false`.
|
2022-01-25 12:19:31 +11:00
|
|
|
///
|
2022-02-02 03:09:23 +11:00
|
|
|
/// Before this point, the plugin should not have done any expensive initialization. Please
|
2022-01-25 12:19:31 +11:00
|
|
|
/// don't be that plugin that takes twenty seconds to scan.
|
2022-02-04 11:13:00 +11:00
|
|
|
#[allow(unused_variables)]
|
2022-02-02 03:09:23 +11:00
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
|
|
|
bus_config: &BusConfig,
|
|
|
|
buffer_config: &BufferConfig,
|
2022-02-05 09:03:11 +11:00
|
|
|
context: &mut impl ProcessContext,
|
2022-02-04 11:13:00 +11:00
|
|
|
) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2022-01-25 12:19:31 +11:00
|
|
|
|
2022-02-04 02:22:32 +11:00
|
|
|
/// Process audio. The host's input buffers have already been copied to the output buffers if
|
|
|
|
/// they are not processing audio in place (most hosts do however). All channels are also
|
|
|
|
/// guarenteed to contain the same number of samples. Lastly, denormals have already been taken
|
|
|
|
/// case of by NIH-plug, and you can optionally enable the `assert_process_allocs` feature to
|
|
|
|
/// abort the program when any allocation accurs in the process function while running in debug
|
|
|
|
/// mode.
|
2022-01-25 12:19:31 +11:00
|
|
|
///
|
|
|
|
/// TODO: Provide a way to access auxiliary input channels if the IO configuration is
|
|
|
|
/// assymetric
|
2022-01-29 01:18:55 +11:00
|
|
|
/// TODO: Pass transport and other context information to the plugin
|
2022-02-05 09:03:11 +11:00
|
|
|
fn process(&mut self, buffer: &mut Buffer, context: &mut impl ProcessContext) -> ProcessStatus;
|
2022-01-25 12:19:31 +11:00
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
/// Provides auxiliary metadata needed for a VST3 plugin.
|
|
|
|
pub trait Vst3Plugin: Plugin {
|
|
|
|
/// The unique class ID that identifies this particular plugin. You can use the
|
|
|
|
/// `*b"fooofooofooofooo"` syntax for this.
|
|
|
|
const VST3_CLASS_ID: [u8; 16];
|
|
|
|
/// One or more categories, separated by pipe characters (`|`), up to 127 characters. Anything
|
|
|
|
/// logner than that will be truncated. See the VST3 SDK for examples of common categories:
|
2022-01-29 04:10:28 +11:00
|
|
|
/// <https://github.com/steinbergmedia/vst3_pluginterfaces/blob/2ad397ade5b51007860bedb3b01b8afd2c5f6fba/vst/ivstaudioprocessor.h#L49-L90>
|
2022-01-28 08:13:13 +11:00
|
|
|
const VST3_CATEGORIES: &'static str;
|
|
|
|
}
|
|
|
|
|
2022-02-05 22:46:26 +11:00
|
|
|
/// An editor for a [Plugin]. If you don't have or need an editor, then you can use the [NoEditor]
|
|
|
|
/// struct as a placeholder.
|
|
|
|
pub trait Editor {
|
|
|
|
/// Called just before the window gets closed.
|
|
|
|
fn close(&self);
|
|
|
|
|
|
|
|
/// Return the (currnent) size of the editor in pixels as a `(width, height)` pair.
|
|
|
|
fn size(&self) -> (u32, u32);
|
|
|
|
|
|
|
|
// TODO: Add the things needed for DPI scaling
|
|
|
|
// TODO: Resizing
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NoEditor;
|
|
|
|
|
|
|
|
impl Editor for NoEditor {
|
|
|
|
fn close(&self) {}
|
|
|
|
|
|
|
|
fn size(&self) -> (u32, u32) {
|
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:19:31 +11:00
|
|
|
/// We only support a single main input and output bus at the moment.
|
2022-02-01 05:40:52 +11:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-01-25 12:19:31 +11:00
|
|
|
pub struct BusConfig {
|
|
|
|
/// The number of input channels for the plugin.
|
|
|
|
pub num_input_channels: u32,
|
|
|
|
/// The number of output channels for the plugin.
|
|
|
|
pub num_output_channels: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration for (the host's) audio buffers.
|
2022-02-03 01:39:55 +11:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2022-01-25 12:19:31 +11:00
|
|
|
pub struct BufferConfig {
|
|
|
|
/// The current sample rate.
|
|
|
|
pub sample_rate: f32,
|
|
|
|
/// The maximum buffer size the host will use. The plugin should be able to accept variable
|
|
|
|
/// sized buffers up to this size.
|
|
|
|
pub max_buffer_size: u32,
|
|
|
|
}
|
2022-01-28 07:03:49 +11:00
|
|
|
|
|
|
|
/// Indicates the current situation after the plugin has processed audio.
|
2022-01-28 23:40:47 +11:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-01-28 07:03:49 +11:00
|
|
|
pub enum ProcessStatus {
|
|
|
|
/// Something went wrong while processing audio.
|
|
|
|
Error(&'static str),
|
|
|
|
/// The plugin has finished processing audio. When the input is silent, the most may suspend the
|
|
|
|
/// plugin to save resources as it sees fit.
|
|
|
|
Normal,
|
|
|
|
/// The plugin has a (reverb) tail with a specific length in samples.
|
|
|
|
Tail(u32),
|
|
|
|
/// This plugin will continue to produce sound regardless of whether or not the input is silent,
|
|
|
|
/// and should thus not be deactivated by the host. This is essentially the same as having an
|
|
|
|
/// infite tail.
|
|
|
|
KeepAlive,
|
|
|
|
}
|
2022-02-04 11:48:24 +11:00
|
|
|
|
|
|
|
/// Event for (incoming) notes. Right now this only supports a very small subset of the MIDI
|
2022-02-04 12:37:40 +11:00
|
|
|
/// specification. See the util module for convenient conversion functions.
|
2022-02-04 11:48:24 +11:00
|
|
|
///
|
2022-02-04 11:50:48 +11:00
|
|
|
/// All of the timings are sample offsets withing the current buffer.
|
|
|
|
///
|
2022-02-04 11:48:24 +11:00
|
|
|
/// TODO: Add more events as needed
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum NoteEvent {
|
2022-02-04 11:50:48 +11:00
|
|
|
NoteOn {
|
|
|
|
timing: u32,
|
|
|
|
channel: u8,
|
|
|
|
note: u8,
|
|
|
|
velocity: u8,
|
|
|
|
},
|
|
|
|
NoteOff {
|
|
|
|
timing: u32,
|
|
|
|
channel: u8,
|
|
|
|
note: u8,
|
|
|
|
velocity: u8,
|
|
|
|
},
|
2022-02-04 11:48:24 +11:00
|
|
|
}
|
2022-02-04 12:37:40 +11:00
|
|
|
|
|
|
|
impl NoteEvent {
|
|
|
|
/// Return the sample within the current buffer this event belongs to.
|
|
|
|
pub fn timing(&self) -> u32 {
|
|
|
|
match &self {
|
|
|
|
NoteEvent::NoteOn { timing, .. } => *timing,
|
|
|
|
NoteEvent::NoteOff { timing, .. } => *timing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|