2022-01-26 22:37:45 +11:00
|
|
|
// nih-plug: plugins, but rewritten in Rust
|
|
|
|
// 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-01-28 23:39:44 +11:00
|
|
|
// The VST3 macro generates an `allocate()` function for initializing the struct, so Clippy will
|
|
|
|
// complain as soon as a struct has more than 8 fields
|
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
2022-02-01 05:40:52 +11:00
|
|
|
use crossbeam::atomic::AtomicCell;
|
2022-01-27 10:19:50 +11:00
|
|
|
use lazy_static::lazy_static;
|
2022-02-05 09:03:11 +11:00
|
|
|
use parking_lot::{RwLock, RwLockWriteGuard};
|
2022-02-06 03:15:07 +11:00
|
|
|
use raw_window_handle::RawWindowHandle;
|
2022-01-29 03:23:16 +11:00
|
|
|
use std::cmp;
|
2022-02-04 11:48:24 +11:00
|
|
|
use std::collections::{HashMap, VecDeque};
|
2022-02-06 04:42:06 +11:00
|
|
|
use std::ffi::{c_void, CStr};
|
2022-01-27 04:14:54 +11:00
|
|
|
use std::marker::PhantomData;
|
2022-02-01 05:47:16 +11:00
|
|
|
use std::mem::{self, MaybeUninit};
|
2022-01-29 03:23:16 +11:00
|
|
|
use std::ptr;
|
2022-02-01 05:47:16 +11:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
2022-02-01 05:40:52 +11:00
|
|
|
use std::sync::Arc;
|
2022-01-27 07:12:13 +11:00
|
|
|
use vst3_sys::base::{kInvalidArgument, kNoInterface, kResultFalse, kResultOk, tresult, TBool};
|
2022-01-30 00:20:14 +11:00
|
|
|
use vst3_sys::base::{IBStream, IPluginBase, IPluginFactory, IPluginFactory2, IPluginFactory3};
|
2022-02-06 04:06:24 +11:00
|
|
|
use vst3_sys::gui::IPlugView;
|
2022-01-30 11:16:27 +11:00
|
|
|
use vst3_sys::utils::SharedVstPtr;
|
2022-01-29 10:55:08 +11:00
|
|
|
use vst3_sys::vst::{
|
2022-02-04 11:48:24 +11:00
|
|
|
IAudioProcessor, IComponent, IComponentHandler, IEditController, IEventList, IParamValueQueue,
|
2022-02-02 02:52:55 +11:00
|
|
|
IParameterChanges, TChar,
|
2022-01-29 10:55:08 +11:00
|
|
|
};
|
2022-01-30 06:54:52 +11:00
|
|
|
use vst3_sys::VST3;
|
2022-01-28 05:30:42 +11:00
|
|
|
use widestring::U16CStr;
|
2022-01-27 04:14:54 +11:00
|
|
|
|
2022-02-03 01:01:41 +11:00
|
|
|
use crate::buffer::Buffer;
|
2022-02-06 03:15:07 +11:00
|
|
|
use crate::context::{EventLoop, GuiContext, MainThreadExecutor, OsEventLoop, ProcessContext};
|
2022-02-02 07:01:28 +11:00
|
|
|
use crate::param::internals::ParamPtr;
|
2022-02-03 07:33:20 +11:00
|
|
|
use crate::param::range::Range;
|
2022-02-02 07:01:28 +11:00
|
|
|
use crate::param::Param;
|
2022-02-06 03:15:07 +11:00
|
|
|
use crate::plugin::{
|
|
|
|
BufferConfig, BusConfig, Editor, NoteEvent, Plugin, ProcessStatus, Vst3Plugin,
|
|
|
|
};
|
2022-01-30 00:20:14 +11:00
|
|
|
use crate::wrapper::state::{ParamValue, State};
|
2022-02-04 01:58:00 +11:00
|
|
|
use crate::wrapper::util::{hash_param_id, process_wrapper, strlcpy, u16strlcpy};
|
2022-02-06 10:06:01 +11:00
|
|
|
use crate::EditorWindowHandle;
|
2022-01-27 10:19:50 +11:00
|
|
|
|
|
|
|
// Alias needed for the VST3 attribute macro
|
|
|
|
use vst3_sys as vst3_com;
|
2022-01-26 22:37:45 +11:00
|
|
|
|
2022-01-27 05:19:20 +11:00
|
|
|
/// Re-export for the wrapper.
|
|
|
|
pub use vst3_sys::sys::GUID;
|
|
|
|
|
2022-01-27 08:23:44 +11:00
|
|
|
/// The VST3 SDK version this is roughtly based on.
|
2022-01-27 10:19:50 +11:00
|
|
|
const VST3_SDK_VERSION: &str = "VST 3.6.14";
|
2022-02-06 04:41:41 +11:00
|
|
|
|
|
|
|
// Window handle type constants missing from vst3-sys
|
2022-02-06 10:07:10 +11:00
|
|
|
#[allow(unused)]
|
2022-02-06 04:41:41 +11:00
|
|
|
const VST3_PLATFORM_HWND: &str = "HWND";
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_HIVIEW: &str = "HIView";
|
2022-02-06 10:07:10 +11:00
|
|
|
#[allow(unused)]
|
2022-02-06 04:41:41 +11:00
|
|
|
const VST3_PLATFORM_NSVIEW: &str = "NSView";
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_UIVIEW: &str = "UIView";
|
2022-02-06 10:07:10 +11:00
|
|
|
#[allow(unused)]
|
2022-02-06 04:41:41 +11:00
|
|
|
const VST3_PLATFORM_X11_WINDOW: &str = "X11EmbedWindowID";
|
|
|
|
|
2022-01-27 10:19:50 +11:00
|
|
|
/// Right now the wrapper adds its own bypass parameter.
|
|
|
|
///
|
|
|
|
/// TODO: Actually use this parameter.
|
|
|
|
const BYPASS_PARAM_ID: &str = "bypass";
|
|
|
|
lazy_static! {
|
|
|
|
static ref BYPASS_PARAM_HASH: u32 = hash_param_id(BYPASS_PARAM_ID);
|
|
|
|
}
|
2022-01-27 08:23:44 +11:00
|
|
|
|
2022-01-28 23:45:17 +11:00
|
|
|
/// Early exit out of a VST3 function when one of the passed pointers is null
|
|
|
|
macro_rules! check_null_ptr {
|
|
|
|
($ptr:expr $(, $ptrs:expr)* $(, )?) => {
|
2022-01-29 03:23:16 +11:00
|
|
|
check_null_ptr_msg!("Null pointer passed to function", $ptr $(, $ptrs)*)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-05 22:56:03 +11:00
|
|
|
/// The same as [check_null_ptr!], but with a custom message.
|
2022-01-29 03:23:16 +11:00
|
|
|
macro_rules! check_null_ptr_msg {
|
|
|
|
($msg:expr, $ptr:expr $(, $ptrs:expr)* $(, )?) => {
|
2022-01-28 23:45:17 +11:00
|
|
|
if $ptr.is_null() $(|| $ptrs.is_null())* {
|
2022-01-29 03:23:16 +11:00
|
|
|
nih_debug_assert_failure!($msg);
|
2022-01-28 23:45:17 +11:00
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
/// The actual wrapper bits. We need this as an `Arc<T>` so we can safely use our event loop API.
|
|
|
|
/// Since we can't combine that with VST3's interior reference counting this just has to be moved to
|
|
|
|
/// its own struct.
|
2022-02-05 08:53:42 +11:00
|
|
|
struct WrapperInner<P: Plugin> {
|
2022-01-27 10:19:50 +11:00
|
|
|
/// The wrapped plugin instance.
|
2022-02-06 03:15:07 +11:00
|
|
|
plugin: Box<RwLock<P>>,
|
2022-01-30 00:20:14 +11:00
|
|
|
|
2022-02-02 02:52:55 +11:00
|
|
|
/// The host's `IComponentHandler` instance, if passed through
|
|
|
|
/// `IEditController::set_component_handler`.
|
|
|
|
component_handler: RwLock<Option<VstPtr<dyn IComponentHandler>>>,
|
|
|
|
|
2022-02-01 05:47:16 +11:00
|
|
|
/// A realtime-safe task queue so the plugin can schedule tasks that need to be run later on the
|
|
|
|
/// GUI thread.
|
|
|
|
///
|
|
|
|
/// This RwLock is only needed because it has to be initialized late. There is no reason to
|
|
|
|
/// mutably borrow the event loop, so reads will never be contested.
|
|
|
|
///
|
|
|
|
/// TODO: Is there a better type for Send+Sync late initializaiton?
|
|
|
|
event_loop: RwLock<MaybeUninit<OsEventLoop<Task, Self>>>,
|
2022-02-02 02:40:51 +11:00
|
|
|
|
|
|
|
/// Whether the plugin is currently processing audio. In other words, the last state
|
|
|
|
/// `IAudioProcessor::setActive()` has been called with.
|
|
|
|
is_processing: AtomicBool,
|
2022-01-30 00:20:14 +11:00
|
|
|
/// The current bus configuration, modified through `IAudioProcessor::setBusArrangements()`.
|
2022-02-01 05:40:52 +11:00
|
|
|
current_bus_config: AtomicCell<BusConfig>,
|
2022-02-03 01:39:55 +11:00
|
|
|
/// The current buffer configuration, containing the sample rate and the maximum block size.
|
|
|
|
/// Will be set in `IAudioProcessor::setupProcessing()`.
|
|
|
|
current_buffer_config: AtomicCell<Option<BufferConfig>>,
|
2022-01-27 10:19:50 +11:00
|
|
|
/// Whether the plugin is currently bypassed. This is not yet integrated with the `Plugin`
|
|
|
|
/// trait.
|
2022-02-01 05:40:52 +11:00
|
|
|
bypass_state: AtomicBool,
|
2022-01-28 23:40:47 +11:00
|
|
|
/// The last process status returned by the plugin. This is used for tail handling.
|
2022-02-01 05:40:52 +11:00
|
|
|
last_process_status: AtomicCell<ProcessStatus>,
|
2022-02-01 05:47:16 +11:00
|
|
|
/// The current latency in samples, as set by the plugin through the [ProcessContext].
|
|
|
|
current_latency: AtomicU32,
|
2022-01-29 03:23:16 +11:00
|
|
|
/// Contains slices for the plugin's outputs. You can't directly create a nested slice form
|
|
|
|
/// apointer to pointers, so this needs to be preallocated in the setup call and kept around
|
2022-02-03 00:39:21 +11:00
|
|
|
/// between process calls. This buffer owns the vector, because otherwise it would need to store
|
|
|
|
/// a mutable reference to the data contained in this mutex.
|
2022-02-05 08:53:42 +11:00
|
|
|
output_buffer: RwLock<Buffer<'static>>,
|
2022-02-04 11:48:24 +11:00
|
|
|
/// The incoming events for the plugin, if `P::ACCEPTS_MIDI` is set.
|
|
|
|
///
|
|
|
|
/// TODO: Maybe load these lazily at some point instead of needing to spool them all to this
|
|
|
|
/// queue first
|
|
|
|
input_events: RwLock<VecDeque<NoteEvent>>,
|
2022-01-29 03:23:16 +11:00
|
|
|
|
2022-02-01 08:20:09 +11:00
|
|
|
/// The keys from `param_map` in a stable order.
|
|
|
|
param_hashes: Vec<u32>,
|
2022-01-28 08:31:53 +11:00
|
|
|
/// A mapping from parameter ID hashes (obtained from the string parameter IDs) to pointers to
|
|
|
|
/// parameters belonging to the plugin. As long as `plugin` does not get recreated, these
|
|
|
|
/// addresses will remain stable, as they are obtained from a pinned object.
|
|
|
|
param_by_hash: HashMap<u32, ParamPtr>,
|
2022-01-27 10:19:50 +11:00
|
|
|
/// The default normalized parameter value for every parameter in `param_ids`. We need to store
|
|
|
|
/// this in case the host requeries the parmaeter later.
|
|
|
|
param_defaults_normalized: Vec<f32>,
|
2022-02-01 08:23:29 +11:00
|
|
|
/// Mappings from string parameter indentifiers to parameter hashes. Useful for debug logging
|
|
|
|
/// and when storing and restorign plugin state.
|
2022-02-01 08:24:07 +11:00
|
|
|
param_id_to_hash: HashMap<&'static str, u32>,
|
2022-02-06 03:47:55 +11:00
|
|
|
/// The inverse mapping from [Self::param_by_hash]. This is needed to be able to have an
|
|
|
|
/// ergonomic parameter setting API that uses references to the parameters instead of having to
|
|
|
|
/// add a setter function to the parameter (or even worse, have it be completely untyped).
|
|
|
|
param_ptr_to_hash: HashMap<ParamPtr, u32>,
|
2022-01-27 07:12:13 +11:00
|
|
|
}
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
#[VST3(implements(IComponent, IEditController, IAudioProcessor))]
|
2022-02-05 08:53:42 +11:00
|
|
|
pub(crate) struct Wrapper<P: Plugin> {
|
|
|
|
inner: Arc<WrapperInner<P>>,
|
|
|
|
}
|
2022-02-05 08:55:10 +11:00
|
|
|
|
2022-02-06 04:06:24 +11:00
|
|
|
/// The plugin's [IPlugView] instance created in [IEditController::create_view] if `P` has an
|
|
|
|
/// editor. This is managed separately so the lifetime bounds match up.
|
|
|
|
#[VST3(implements(IPlugView))]
|
|
|
|
struct WrapperView<P: Plugin> {
|
|
|
|
inner: Arc<WrapperInner<P>>,
|
|
|
|
editor: RwLock<Option<Box<dyn Editor>>>,
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:55:10 +11:00
|
|
|
/// A [ProcessContext] implementation for the wrapper. This is a separate object so it can hold on
|
|
|
|
/// to lock guards for event queues. Otherwise reading these events would require constant
|
|
|
|
/// unnecessary atomic operations to lock the uncontested RwLocks.
|
|
|
|
struct WrapperProcessContext<'a, P: Plugin> {
|
|
|
|
inner: &'a WrapperInner<P>,
|
2022-02-05 09:03:11 +11:00
|
|
|
input_events_guard: RwLockWriteGuard<'a, VecDeque<NoteEvent>>,
|
2022-02-05 08:55:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Plugin> ProcessContext for WrapperProcessContext<'_, P> {
|
|
|
|
fn set_latency_samples(&self, samples: u32) {
|
|
|
|
// Only trigger a restart if it's actually needed
|
|
|
|
let old_latency = self.inner.current_latency.swap(samples, Ordering::SeqCst);
|
|
|
|
if old_latency != samples {
|
|
|
|
let task_posted = unsafe { self.inner.event_loop.read().assume_init_ref() }
|
|
|
|
.do_maybe_async(Task::TriggerRestart(
|
|
|
|
vst3_sys::vst::RestartFlags::kLatencyChanged as i32,
|
|
|
|
));
|
|
|
|
nih_debug_assert!(task_posted, "The task queue is full, dropping task...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 09:03:11 +11:00
|
|
|
fn next_midi_event(&mut self) -> Option<NoteEvent> {
|
|
|
|
self.input_events_guard.pop_front()
|
2022-02-05 08:55:10 +11:00
|
|
|
}
|
2022-02-01 06:18:12 +11:00
|
|
|
}
|
2022-02-01 05:40:52 +11:00
|
|
|
|
2022-02-06 03:15:07 +11:00
|
|
|
// We can't use a nice standalone context object for this as we don't have any control over the
|
|
|
|
// lifetime since we currently try to stay GUI framework agnostic. Because of that, the only
|
|
|
|
// alternative is to pass an `Arc<Self as GuiContext>` to the plugin and hope it doesn't do anything
|
|
|
|
// weird with it.
|
|
|
|
impl<P: Plugin> GuiContext for WrapperInner<P> {
|
2022-02-06 03:47:55 +11:00
|
|
|
// All of these functions are supposed to be called from the main thread, so we'll put some
|
|
|
|
// trust in the caller and assume that this is indeed the case
|
2022-02-06 03:15:07 +11:00
|
|
|
unsafe fn raw_begin_set_parameter(&self, param: ParamPtr) {
|
2022-02-06 03:47:55 +11:00
|
|
|
match &*self.component_handler.read() {
|
|
|
|
Some(handler) => match self.param_ptr_to_hash.get(¶m) {
|
|
|
|
Some(hash) => {
|
|
|
|
handler.begin_edit(*hash);
|
|
|
|
}
|
|
|
|
None => nih_debug_assert_failure!("Unknown parameter: {:?}", param),
|
|
|
|
},
|
|
|
|
None => nih_debug_assert_failure!("Component handler not yet set"),
|
|
|
|
}
|
2022-02-06 03:15:07 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn raw_set_parameter_normalized(&self, param: ParamPtr, normalized: f32) {
|
2022-02-06 03:47:55 +11:00
|
|
|
match &*self.component_handler.read() {
|
|
|
|
Some(handler) => match self.param_ptr_to_hash.get(¶m) {
|
|
|
|
Some(hash) => {
|
2022-02-07 00:15:43 +11:00
|
|
|
// Only update the parameters manually if the host is not processing audio. If
|
|
|
|
// the plugin is currently processing audio, the host will pass this change back
|
|
|
|
// to the plugin in the audio callback. This also prevents the values from
|
|
|
|
// changing in the middle of the process callback, which would be unsound.
|
|
|
|
if !self.is_processing.load(Ordering::SeqCst) {
|
|
|
|
self.set_normalized_value_by_hash(
|
|
|
|
*hash,
|
|
|
|
normalized,
|
|
|
|
self.current_buffer_config.load().map(|c| c.sample_rate),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-06 03:47:55 +11:00
|
|
|
handler.perform_edit(*hash, normalized as f64);
|
|
|
|
}
|
|
|
|
None => nih_debug_assert_failure!("Unknown parameter: {:?}", param),
|
|
|
|
},
|
|
|
|
None => nih_debug_assert_failure!("Component handler not yet set"),
|
|
|
|
}
|
2022-02-06 03:15:07 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn raw_end_set_parameter(&self, param: ParamPtr) {
|
2022-02-06 03:47:55 +11:00
|
|
|
match &*self.component_handler.read() {
|
|
|
|
Some(handler) => match self.param_ptr_to_hash.get(¶m) {
|
|
|
|
Some(hash) => {
|
|
|
|
handler.end_edit(*hash);
|
|
|
|
}
|
|
|
|
None => nih_debug_assert_failure!("Unknown parameter: {:?}", param),
|
|
|
|
},
|
|
|
|
None => nih_debug_assert_failure!("Component handler not yet set"),
|
|
|
|
}
|
2022-02-06 03:15:07 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 05:47:16 +11:00
|
|
|
/// Tasks that can be sent from the plugin to be executed on the main thread in a non-blocking
|
|
|
|
/// realtime safe way (either a random thread or `IRunLoop` on Linux, the OS' message loop on
|
|
|
|
/// Windows and macOS).
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Task {
|
|
|
|
/// Trigger a restart with the given restart flags. This is a bit set of the flags from
|
|
|
|
/// [vst3_sys::vst::RestartFlags].
|
2022-02-02 02:59:11 +11:00
|
|
|
TriggerRestart(i32),
|
2022-02-01 05:47:16 +11:00
|
|
|
}
|
|
|
|
|
2022-02-02 02:52:55 +11:00
|
|
|
/// Send+Sync wrapper for these interface pointers.
|
|
|
|
#[repr(transparent)]
|
|
|
|
struct VstPtr<T: vst3_sys::ComInterface + ?Sized> {
|
|
|
|
ptr: vst3_sys::VstPtr<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: vst3_sys::ComInterface + ?Sized> std::ops::Deref for VstPtr<T> {
|
|
|
|
type Target = vst3_sys::VstPtr<T>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.ptr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: vst3_sys::ComInterface + ?Sized> From<vst3_sys::VstPtr<T>> for VstPtr<T> {
|
|
|
|
fn from(ptr: vst3_sys::VstPtr<T>) -> Self {
|
|
|
|
Self { ptr }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SAFETY: Sharing these pointers across thread is s safe as they have internal atomic reference
|
|
|
|
/// counting, so as long as a `VstPtr<T>` handle exists the object will stay alive.
|
|
|
|
unsafe impl<T: vst3_sys::ComInterface + ?Sized> Send for VstPtr<T> {}
|
|
|
|
unsafe impl<T: vst3_sys::ComInterface + ?Sized> Sync for VstPtr<T> {}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> WrapperInner<P> {
|
2022-02-01 06:18:12 +11:00
|
|
|
// XXX: The unsafe blocks in this function are unnecessary. but rust-analyzer gets a bit
|
|
|
|
// confused by all of these vtables
|
|
|
|
#[allow(unused_unsafe)]
|
2022-02-01 05:40:52 +11:00
|
|
|
pub fn new() -> Arc<Self> {
|
2022-02-01 06:18:12 +11:00
|
|
|
let mut wrapper = Self {
|
2022-02-06 03:15:07 +11:00
|
|
|
plugin: Box::new(RwLock::default()),
|
2022-02-02 02:40:51 +11:00
|
|
|
|
2022-02-02 02:52:55 +11:00
|
|
|
component_handler: RwLock::new(None),
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
event_loop: RwLock::new(MaybeUninit::uninit()),
|
2022-02-02 02:40:51 +11:00
|
|
|
|
|
|
|
is_processing: AtomicBool::new(false),
|
2022-01-27 07:12:13 +11:00
|
|
|
// Some hosts, like the current version of Bitwig and Ardour at the time of writing,
|
|
|
|
// will try using the plugin's default not yet initialized bus arrangement. Because of
|
|
|
|
// that, we'll always initialize this configuration even before the host requests a
|
|
|
|
// channel layout.
|
2022-02-02 02:40:51 +11:00
|
|
|
current_bus_config: AtomicCell::new(BusConfig {
|
2022-01-27 07:12:13 +11:00
|
|
|
num_input_channels: P::DEFAULT_NUM_INPUTS,
|
|
|
|
num_output_channels: P::DEFAULT_NUM_OUTPUTS,
|
2022-01-28 05:55:24 +11:00
|
|
|
}),
|
2022-02-03 01:39:55 +11:00
|
|
|
current_buffer_config: AtomicCell::new(None),
|
2022-02-02 02:40:51 +11:00
|
|
|
bypass_state: AtomicBool::new(false),
|
|
|
|
last_process_status: AtomicCell::new(ProcessStatus::Normal),
|
|
|
|
current_latency: AtomicU32::new(0),
|
2022-02-03 06:37:06 +11:00
|
|
|
output_buffer: RwLock::new(Buffer::default()),
|
2022-02-04 11:48:24 +11:00
|
|
|
input_events: RwLock::new(VecDeque::with_capacity(512)),
|
2022-02-02 02:40:51 +11:00
|
|
|
|
|
|
|
param_hashes: Vec::new(),
|
|
|
|
param_by_hash: HashMap::new(),
|
|
|
|
param_defaults_normalized: Vec::new(),
|
|
|
|
param_id_to_hash: HashMap::new(),
|
2022-02-06 03:47:55 +11:00
|
|
|
param_ptr_to_hash: HashMap::new(),
|
2022-02-01 06:18:12 +11:00
|
|
|
};
|
2022-01-27 10:19:50 +11:00
|
|
|
|
|
|
|
// This is a mapping from the parameter IDs specified by the plugin to pointers to thsoe
|
|
|
|
// parameters. Since the object returned by `params()` is pinned, these pointers are safe to
|
|
|
|
// dereference as long as `wrapper.plugin` is alive
|
2022-02-01 05:40:52 +11:00
|
|
|
// XXX: This unsafe block is unnecessary. rust-analyzer gets a bit confused and this this
|
|
|
|
// `read()` function is from `IBStream` which it definitely is not.
|
|
|
|
let param_map = unsafe { wrapper.plugin.read() }.params().param_map();
|
2022-02-01 08:20:09 +11:00
|
|
|
let param_ids = unsafe { wrapper.plugin.read() }.params().param_ids();
|
2022-01-28 08:31:53 +11:00
|
|
|
nih_debug_assert!(
|
|
|
|
!param_map.contains_key(BYPASS_PARAM_ID),
|
|
|
|
"The wrapper alread yadds its own bypass parameter"
|
|
|
|
);
|
|
|
|
|
2022-02-01 08:20:09 +11:00
|
|
|
// Only calculate these hashes once, and in the stable order defined by the plugin
|
2022-02-01 08:42:36 +11:00
|
|
|
let param_id_hashes_ptrs: Vec<_> = param_ids
|
|
|
|
.iter()
|
|
|
|
.filter_map(|id| {
|
|
|
|
let param_ptr = param_map.get(id)?;
|
|
|
|
Some((id, hash_param_id(id), param_ptr))
|
|
|
|
})
|
|
|
|
.collect();
|
2022-02-01 08:20:09 +11:00
|
|
|
wrapper.param_hashes = param_id_hashes_ptrs
|
2022-02-01 08:42:36 +11:00
|
|
|
.iter()
|
|
|
|
.map(|&(_, hash, _)| hash)
|
2022-02-01 08:20:09 +11:00
|
|
|
.collect();
|
|
|
|
wrapper.param_by_hash = param_id_hashes_ptrs
|
2022-02-01 08:42:36 +11:00
|
|
|
.iter()
|
|
|
|
.map(|&(_, hash, ptr)| (hash, *ptr))
|
2022-01-27 10:19:50 +11:00
|
|
|
.collect();
|
2022-02-01 08:20:09 +11:00
|
|
|
wrapper.param_defaults_normalized = param_id_hashes_ptrs
|
2022-02-01 08:42:36 +11:00
|
|
|
.iter()
|
|
|
|
.map(|&(_, _, ptr)| unsafe { ptr.normalized_value() })
|
2022-01-28 08:31:53 +11:00
|
|
|
.collect();
|
2022-02-01 08:24:07 +11:00
|
|
|
wrapper.param_id_to_hash = param_id_hashes_ptrs
|
2022-02-06 03:47:55 +11:00
|
|
|
.iter()
|
|
|
|
.map(|&(id, hash, _)| (*id, hash))
|
|
|
|
.collect();
|
|
|
|
wrapper.param_ptr_to_hash = param_id_hashes_ptrs
|
2022-02-01 08:42:36 +11:00
|
|
|
.into_iter()
|
2022-02-06 03:47:55 +11:00
|
|
|
.map(|(_, hash, ptr)| (*ptr, hash))
|
2022-01-30 02:42:20 +11:00
|
|
|
.collect();
|
2022-01-27 10:19:50 +11:00
|
|
|
|
2022-02-01 05:47:16 +11:00
|
|
|
// FIXME: Right now this is safe, but if we are going to have a singleton main thread queue
|
|
|
|
// serving multiple plugin instances, Arc can't be used because its reference count
|
|
|
|
// is separate from the internal COM-style reference count.
|
2022-02-05 08:53:42 +11:00
|
|
|
let wrapper: Arc<WrapperInner<P>> = wrapper.into();
|
2022-02-01 06:18:12 +11:00
|
|
|
// XXX: This unsafe block is unnecessary. rust-analyzer gets a bit confused and this this
|
|
|
|
// `write()` function is from `IBStream` which it definitely is not.
|
|
|
|
*unsafe { wrapper.event_loop.write() } =
|
2022-02-02 01:31:16 +11:00
|
|
|
MaybeUninit::new(OsEventLoop::new_and_spawn(Arc::downgrade(&wrapper)));
|
2022-02-01 05:47:16 +11:00
|
|
|
|
2022-01-27 10:19:50 +11:00
|
|
|
wrapper
|
2022-01-27 07:12:13 +11:00
|
|
|
}
|
2022-01-29 10:55:08 +11:00
|
|
|
|
2022-02-05 08:55:10 +11:00
|
|
|
pub fn make_process_context(&self) -> WrapperProcessContext<'_, P> {
|
2022-02-05 09:03:11 +11:00
|
|
|
WrapperProcessContext {
|
|
|
|
inner: self,
|
|
|
|
input_events_guard: self.input_events.write(),
|
|
|
|
}
|
2022-02-05 08:55:10 +11:00
|
|
|
}
|
|
|
|
|
2022-02-03 07:08:23 +11:00
|
|
|
/// Convenience function for setting a value for a parameter as triggered by a VST3 parameter
|
|
|
|
/// update. The same rate is for updating parameter smoothing.
|
|
|
|
unsafe fn set_normalized_value_by_hash(
|
|
|
|
&self,
|
|
|
|
hash: u32,
|
2022-02-06 08:26:21 +11:00
|
|
|
normalized_value: f32,
|
2022-02-03 07:08:23 +11:00
|
|
|
sample_rate: Option<f32>,
|
|
|
|
) -> tresult {
|
2022-01-29 10:55:08 +11:00
|
|
|
if hash == *BYPASS_PARAM_HASH {
|
2022-02-01 05:40:52 +11:00
|
|
|
self.bypass_state
|
|
|
|
.store(normalized_value >= 0.5, Ordering::SeqCst);
|
2022-01-29 10:55:08 +11:00
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else if let Some(param_ptr) = self.param_by_hash.get(&hash) {
|
2022-02-03 07:08:23 +11:00
|
|
|
// Also update the parameter's smoothing if applicable
|
|
|
|
match (param_ptr, sample_rate) {
|
2022-02-03 07:33:20 +11:00
|
|
|
(_, Some(sample_rate)) => {
|
2022-02-06 08:26:21 +11:00
|
|
|
param_ptr.set_normalized_value(normalized_value);
|
2022-02-03 07:41:20 +11:00
|
|
|
param_ptr.update_smoother(sample_rate, false);
|
2022-02-03 07:08:23 +11:00
|
|
|
}
|
2022-02-06 08:26:21 +11:00
|
|
|
_ => param_ptr.set_normalized_value(normalized_value),
|
2022-02-03 07:08:23 +11:00
|
|
|
}
|
2022-01-29 10:55:08 +11:00
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kInvalidArgument
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 07:12:13 +11:00
|
|
|
}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> Wrapper<P> {
|
2022-02-01 06:18:12 +11:00
|
|
|
pub fn new() -> Box<Self> {
|
|
|
|
Self::allocate(WrapperInner::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 04:06:24 +11:00
|
|
|
impl<P: Plugin> WrapperView<P> {
|
|
|
|
pub fn new(inner: Arc<WrapperInner<P>>) -> Box<Self> {
|
|
|
|
Self::allocate(inner, RwLock::new(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> MainThreadExecutor<Task> for WrapperInner<P> {
|
2022-02-02 03:01:05 +11:00
|
|
|
unsafe fn execute(&self, task: Task) {
|
|
|
|
// This function is always called from the main thread
|
2022-02-01 05:47:16 +11:00
|
|
|
// TODO: When we add GUI resizing and context menus, this should propagate those events to
|
|
|
|
// `IRunLoop` on Linux to keep REAPER happy. That does mean a double spool, but we can
|
|
|
|
// come up with a nicer solution to handle that later (can always add a separate
|
|
|
|
// function for checking if a to be scheduled task can be handled right ther and
|
|
|
|
// then).
|
|
|
|
match task {
|
2022-02-02 02:59:11 +11:00
|
|
|
Task::TriggerRestart(flags) => match &*self.component_handler.read() {
|
2022-02-02 03:01:05 +11:00
|
|
|
Some(handler) => {
|
2022-02-02 02:59:11 +11:00
|
|
|
handler.restart_component(flags);
|
2022-02-02 03:01:05 +11:00
|
|
|
}
|
2022-02-02 02:59:11 +11:00
|
|
|
None => nih_debug_assert_failure!("Component handler not yet set"),
|
|
|
|
},
|
2022-02-01 05:47:16 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> IPluginBase for Wrapper<P> {
|
2022-01-27 07:12:13 +11:00
|
|
|
unsafe fn initialize(&self, _context: *mut c_void) -> tresult {
|
|
|
|
// We currently don't need or allow any initialization logic
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn terminate(&self) -> tresult {
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> IComponent for Wrapper<P> {
|
2022-01-27 07:12:13 +11:00
|
|
|
unsafe fn get_controller_class_id(&self, _tuid: *mut vst3_sys::IID) -> tresult {
|
|
|
|
// We won't separate the edit controller to keep the implemetnation a bit smaller
|
|
|
|
kNoInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_io_mode(&self, _mode: vst3_sys::vst::IoMode) -> tresult {
|
|
|
|
// This would need to integrate with the GUI, which we currently don't have
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_bus_count(
|
|
|
|
&self,
|
|
|
|
type_: vst3_sys::vst::MediaType,
|
2022-02-04 11:26:36 +11:00
|
|
|
dir: vst3_sys::vst::BusDirection,
|
2022-01-27 07:12:13 +11:00
|
|
|
) -> i32 {
|
|
|
|
// All plugins currently only have a single input and a single output bus
|
|
|
|
match type_ {
|
2022-01-27 08:23:44 +11:00
|
|
|
x if x == vst3_sys::vst::MediaTypes::kAudio as i32 => 1,
|
2022-02-04 11:26:36 +11:00
|
|
|
x if x == vst3_sys::vst::MediaTypes::kEvent as i32
|
|
|
|
&& dir == vst3_sys::vst::BusDirections::kInput as i32
|
|
|
|
&& P::ACCEPTS_MIDI =>
|
|
|
|
{
|
|
|
|
1
|
|
|
|
}
|
2022-01-27 07:12:13 +11:00
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_bus_info(
|
|
|
|
&self,
|
|
|
|
type_: vst3_sys::vst::MediaType,
|
|
|
|
dir: vst3_sys::vst::BusDirection,
|
|
|
|
index: i32,
|
|
|
|
info: *mut vst3_sys::vst::BusInfo,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(info);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-02-04 11:26:36 +11:00
|
|
|
match (type_, dir, index) {
|
|
|
|
(t, _, _) if t == vst3_sys::vst::MediaTypes::kAudio as i32 => {
|
2022-01-27 07:12:13 +11:00
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-01-27 08:23:44 +11:00
|
|
|
info.media_type = vst3_sys::vst::MediaTypes::kAudio as i32;
|
|
|
|
info.bus_type = vst3_sys::vst::BusTypes::kMain as i32;
|
|
|
|
info.flags = vst3_sys::vst::BusFlags::kDefaultActive as u32;
|
2022-01-27 07:12:13 +11:00
|
|
|
match (dir, index) {
|
2022-01-27 08:23:44 +11:00
|
|
|
(d, 0) if d == vst3_sys::vst::BusDirections::kInput as i32 => {
|
|
|
|
info.direction = vst3_sys::vst::BusDirections::kInput as i32;
|
2022-01-28 05:55:24 +11:00
|
|
|
info.channel_count =
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner.current_bus_config.load().num_input_channels as i32;
|
2022-01-27 07:12:13 +11:00
|
|
|
u16strlcpy(&mut info.name, "Input");
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
2022-01-27 08:23:44 +11:00
|
|
|
(d, 0) if d == vst3_sys::vst::BusDirections::kOutput as i32 => {
|
|
|
|
info.direction = vst3_sys::vst::BusDirections::kOutput as i32;
|
2022-01-28 05:55:24 +11:00
|
|
|
info.channel_count =
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner.current_bus_config.load().num_output_channels as i32;
|
2022-01-27 07:12:13 +11:00
|
|
|
u16strlcpy(&mut info.name, "Output");
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
_ => kInvalidArgument,
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 11:26:36 +11:00
|
|
|
(t, d, 0)
|
|
|
|
if t == vst3_sys::vst::MediaTypes::kEvent as i32
|
|
|
|
&& d == vst3_sys::vst::BusDirections::kInput as i32
|
|
|
|
&& P::ACCEPTS_MIDI =>
|
|
|
|
{
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
|
|
|
info.media_type = vst3_sys::vst::MediaTypes::kEvent as i32;
|
|
|
|
info.direction = vst3_sys::vst::BusDirections::kInput as i32;
|
|
|
|
info.channel_count = 16;
|
|
|
|
u16strlcpy(&mut info.name, "MIDI");
|
|
|
|
info.bus_type = vst3_sys::vst::BusTypes::kMain as i32;
|
|
|
|
info.flags = vst3_sys::vst::BusFlags::kDefaultActive as u32;
|
|
|
|
kResultOk
|
|
|
|
}
|
2022-01-27 07:12:13 +11:00
|
|
|
_ => kInvalidArgument,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_routing_info(
|
|
|
|
&self,
|
|
|
|
in_info: *mut vst3_sys::vst::RoutingInfo,
|
|
|
|
out_info: *mut vst3_sys::vst::RoutingInfo,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(in_info, out_info);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-01-27 07:12:13 +11:00
|
|
|
*out_info = mem::zeroed();
|
|
|
|
|
|
|
|
let in_info = &*in_info;
|
|
|
|
let out_info = &mut *out_info;
|
|
|
|
match (in_info.media_type, in_info.bus_index) {
|
|
|
|
(t, 0) if t == vst3_sys::vst::MediaTypes::kAudio as i32 => {
|
|
|
|
out_info.media_type = vst3_sys::vst::MediaTypes::kAudio as i32;
|
|
|
|
out_info.bus_index = in_info.bus_index;
|
|
|
|
out_info.channel = in_info.channel;
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
_ => kInvalidArgument,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn activate_bus(
|
|
|
|
&self,
|
|
|
|
type_: vst3_sys::vst::MediaType,
|
2022-02-04 11:26:36 +11:00
|
|
|
dir: vst3_sys::vst::BusDirection,
|
2022-01-27 07:12:13 +11:00
|
|
|
index: i32,
|
|
|
|
_state: vst3_sys::base::TBool,
|
|
|
|
) -> tresult {
|
|
|
|
// We don't need any special handling here
|
2022-02-04 11:26:36 +11:00
|
|
|
match (type_, dir, index) {
|
|
|
|
(t, _, 0) if t == vst3_sys::vst::MediaTypes::kAudio as i32 => kResultOk,
|
|
|
|
(t, d, 0)
|
|
|
|
if t == vst3_sys::vst::MediaTypes::kEvent as i32
|
|
|
|
&& d == vst3_sys::vst::BusDirections::kInput as i32
|
|
|
|
&& P::ACCEPTS_MIDI =>
|
|
|
|
{
|
|
|
|
kResultOk
|
|
|
|
}
|
2022-01-27 07:12:13 +11:00
|
|
|
_ => kInvalidArgument,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_active(&self, _state: TBool) -> tresult {
|
|
|
|
// We don't need any special handling here
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:16:27 +11:00
|
|
|
unsafe fn set_state(&self, state: SharedVstPtr<dyn IBStream>) -> tresult {
|
2022-01-30 02:42:20 +11:00
|
|
|
check_null_ptr!(state);
|
|
|
|
|
2022-01-30 06:54:52 +11:00
|
|
|
let state = state.upgrade().unwrap();
|
2022-01-30 02:42:20 +11:00
|
|
|
|
|
|
|
// We need to know how large the state is before we can read it. The current position can be
|
|
|
|
// zero, but it can also be something else. Bitwig prepends the preset header in the stream,
|
|
|
|
// while some other hosts don't expose that to the plugin.
|
|
|
|
let mut current_pos = 0;
|
|
|
|
let mut eof_pos = 0;
|
|
|
|
if state.tell(&mut current_pos) != kResultOk
|
|
|
|
|| state.seek(0, vst3_sys::base::kIBSeekEnd, &mut eof_pos) != kResultOk
|
|
|
|
|| state.seek(current_pos, vst3_sys::base::kIBSeekSet, ptr::null_mut()) != kResultOk
|
|
|
|
{
|
|
|
|
nih_debug_assert_failure!("Could not get the stream length");
|
|
|
|
return kResultFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stream_byte_size = (eof_pos - current_pos) as i32;
|
|
|
|
let mut num_bytes_read = 0;
|
|
|
|
let mut read_buffer: Vec<u8> = Vec::with_capacity(stream_byte_size as usize);
|
|
|
|
state.read(
|
|
|
|
read_buffer.as_mut_ptr() as *mut c_void,
|
|
|
|
read_buffer.capacity() as i32,
|
|
|
|
&mut num_bytes_read,
|
|
|
|
);
|
|
|
|
read_buffer.set_len(num_bytes_read as usize);
|
|
|
|
|
|
|
|
// If the size is zero, some hsots will always return `kResultFalse` even if the read was
|
|
|
|
// 'successful', so we can't check the return value but we can check the number of bytes
|
|
|
|
// read.
|
|
|
|
if read_buffer.len() != stream_byte_size as usize {
|
|
|
|
nih_debug_assert_failure!("Unexpected stream length");
|
|
|
|
return kResultFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
let state: State = match serde_json::from_slice(&read_buffer) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(err) => {
|
|
|
|
nih_debug_assert_failure!("Error while deserializing state: {}", err);
|
|
|
|
return kResultFalse;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-03 07:33:20 +11:00
|
|
|
let sample_rate = self
|
|
|
|
.inner
|
|
|
|
.current_buffer_config
|
|
|
|
.load()
|
|
|
|
.map(|c| c.sample_rate);
|
2022-01-30 02:42:20 +11:00
|
|
|
for (param_id_str, param_value) in state.params {
|
2022-01-30 12:04:35 +11:00
|
|
|
// Handle the bypass parameter separately
|
|
|
|
if param_id_str == BYPASS_PARAM_ID {
|
|
|
|
match param_value {
|
2022-02-01 06:18:12 +11:00
|
|
|
ParamValue::Bool(b) => self.inner.bypass_state.store(b, Ordering::SeqCst),
|
2022-01-30 12:04:35 +11:00
|
|
|
_ => nih_debug_assert_failure!(
|
2022-02-01 08:32:20 +11:00
|
|
|
"Invalid serialized value {:?} for parameter \"{}\"",
|
2022-01-30 12:04:35 +11:00
|
|
|
param_value,
|
|
|
|
param_id_str,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-30 02:42:20 +11:00
|
|
|
let param_ptr = match self
|
2022-02-01 06:18:12 +11:00
|
|
|
.inner
|
2022-02-01 08:24:07 +11:00
|
|
|
.param_id_to_hash
|
2022-01-30 02:42:20 +11:00
|
|
|
.get(param_id_str.as_str())
|
2022-02-01 06:18:12 +11:00
|
|
|
.and_then(|hash| self.inner.param_by_hash.get(hash))
|
2022-01-30 02:42:20 +11:00
|
|
|
{
|
|
|
|
Some(ptr) => ptr,
|
|
|
|
None => {
|
|
|
|
nih_debug_assert_failure!("Unknown parameter: {}", param_id_str);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match (param_ptr, param_value) {
|
|
|
|
(ParamPtr::FloatParam(p), ParamValue::F32(v)) => (**p).set_plain_value(v),
|
|
|
|
(ParamPtr::IntParam(p), ParamValue::I32(v)) => (**p).set_plain_value(v),
|
2022-02-01 08:32:20 +11:00
|
|
|
(ParamPtr::BoolParam(p), ParamValue::Bool(v)) => (**p).set_plain_value(v),
|
2022-01-30 02:42:20 +11:00
|
|
|
(param_ptr, param_value) => {
|
|
|
|
nih_debug_assert_failure!(
|
2022-02-01 08:32:20 +11:00
|
|
|
"Invalid serialized value {:?} for parameter \"{}\" ({:?})",
|
2022-01-30 02:42:20 +11:00
|
|
|
param_value,
|
|
|
|
param_id_str,
|
|
|
|
param_ptr,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 07:33:20 +11:00
|
|
|
|
|
|
|
// Make sure everything starts out in sync
|
|
|
|
if let Some(sample_rate) = sample_rate {
|
2022-02-03 07:41:20 +11:00
|
|
|
param_ptr.update_smoother(sample_rate, true);
|
2022-02-03 07:33:20 +11:00
|
|
|
}
|
2022-01-30 02:42:20 +11:00
|
|
|
}
|
|
|
|
|
2022-01-31 03:09:18 +11:00
|
|
|
// The plugin can also persist arbitrary fields alongside its parameters. This is useful for
|
|
|
|
// storing things like sample data.
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner
|
|
|
|
.plugin
|
2022-02-01 05:40:52 +11:00
|
|
|
.read()
|
2022-01-31 03:09:18 +11:00
|
|
|
.params()
|
|
|
|
.deserialize_fields(&state.fields);
|
|
|
|
|
2022-02-03 01:39:55 +11:00
|
|
|
// Reinitialize the plugin after loading state so it can respond to the new parmaeters
|
|
|
|
let bus_config = self.inner.current_bus_config.load();
|
|
|
|
if let Some(buffer_config) = self.inner.current_buffer_config.load() {
|
2022-02-05 08:55:10 +11:00
|
|
|
self.inner.plugin.write().initialize(
|
|
|
|
&bus_config,
|
|
|
|
&buffer_config,
|
2022-02-05 09:03:11 +11:00
|
|
|
&mut self.inner.make_process_context(),
|
2022-02-05 08:55:10 +11:00
|
|
|
);
|
2022-02-03 01:39:55 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 02:42:20 +11:00
|
|
|
kResultOk
|
2022-01-27 07:12:13 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:16:27 +11:00
|
|
|
unsafe fn get_state(&self, state: SharedVstPtr<dyn IBStream>) -> tresult {
|
2022-01-30 00:20:14 +11:00
|
|
|
check_null_ptr!(state);
|
|
|
|
|
2022-01-30 06:54:52 +11:00
|
|
|
let state = state.upgrade().unwrap();
|
2022-01-30 00:20:14 +11:00
|
|
|
|
|
|
|
// We'll serialize parmaeter values as a simple `string_param_id: display_value` map.
|
2022-01-30 12:04:35 +11:00
|
|
|
let mut params: HashMap<_, _> = self
|
2022-02-01 06:18:12 +11:00
|
|
|
.inner
|
2022-02-01 08:24:07 +11:00
|
|
|
.param_id_to_hash
|
2022-01-30 00:20:14 +11:00
|
|
|
.iter()
|
2022-02-01 08:23:29 +11:00
|
|
|
.filter_map(|(param_id_str, hash)| {
|
2022-02-01 06:18:12 +11:00
|
|
|
let param_ptr = self.inner.param_by_hash.get(hash)?;
|
2022-01-30 00:20:14 +11:00
|
|
|
Some((param_id_str, param_ptr))
|
|
|
|
})
|
|
|
|
.map(|(¶m_id_str, ¶m_ptr)| match param_ptr {
|
2022-02-03 06:39:04 +11:00
|
|
|
ParamPtr::FloatParam(p) => (
|
|
|
|
param_id_str.to_string(),
|
|
|
|
ParamValue::F32((*p).plain_value()),
|
|
|
|
),
|
|
|
|
ParamPtr::IntParam(p) => (
|
|
|
|
param_id_str.to_string(),
|
|
|
|
ParamValue::I32((*p).plain_value()),
|
|
|
|
),
|
|
|
|
ParamPtr::BoolParam(p) => (
|
|
|
|
param_id_str.to_string(),
|
|
|
|
ParamValue::Bool((*p).plain_value()),
|
|
|
|
),
|
2022-01-30 00:20:14 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2022-01-30 12:04:35 +11:00
|
|
|
|
|
|
|
// Don't forget about the bypass parameter
|
|
|
|
params.insert(
|
|
|
|
BYPASS_PARAM_ID.to_string(),
|
2022-02-01 06:18:12 +11:00
|
|
|
ParamValue::Bool(self.inner.bypass_state.load(Ordering::SeqCst)),
|
2022-01-30 12:04:35 +11:00
|
|
|
);
|
|
|
|
|
2022-01-31 03:09:18 +11:00
|
|
|
// The plugin can also persist arbitrary fields alongside its parameters. This is useful for
|
|
|
|
// storing things like sample data.
|
2022-02-01 06:18:12 +11:00
|
|
|
let fields = self.inner.plugin.read().params().serialize_fields();
|
2022-01-30 00:20:14 +11:00
|
|
|
|
2022-01-31 03:09:18 +11:00
|
|
|
let plugin_state = State { params, fields };
|
2022-01-30 00:20:14 +11:00
|
|
|
match serde_json::to_vec(&plugin_state) {
|
|
|
|
Ok(serialized) => {
|
|
|
|
let mut num_bytes_written = 0;
|
|
|
|
let result = state.write(
|
|
|
|
serialized.as_ptr() as *const c_void,
|
|
|
|
serialized.len() as i32,
|
|
|
|
&mut num_bytes_written,
|
|
|
|
);
|
|
|
|
|
|
|
|
nih_debug_assert_eq!(result, kResultOk);
|
|
|
|
nih_debug_assert_eq!(num_bytes_written as usize, serialized.len());
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
nih_debug_assert_failure!("Could not save state: {}", err);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 07:12:13 +11:00
|
|
|
}
|
2022-01-26 22:37:45 +11:00
|
|
|
}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> IEditController for Wrapper<P> {
|
2022-01-30 11:16:27 +11:00
|
|
|
unsafe fn set_component_state(&self, _state: SharedVstPtr<dyn IBStream>) -> tresult {
|
2022-01-27 10:19:50 +11:00
|
|
|
// We have a single file component, so we don't need to do anything here
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
2022-02-03 01:32:21 +11:00
|
|
|
unsafe fn set_state(&self, _state: SharedVstPtr<dyn IBStream>) -> tresult {
|
|
|
|
// We don't store any separate state here. The plugin's state will have been restored
|
|
|
|
// through the component. Calling that same function here will likely lead to duplicate
|
|
|
|
// state restores
|
|
|
|
kResultOk
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
2022-02-03 01:32:21 +11:00
|
|
|
unsafe fn get_state(&self, _state: SharedVstPtr<dyn IBStream>) -> tresult {
|
2022-01-27 10:19:50 +11:00
|
|
|
// Same for this function
|
2022-02-03 01:32:21 +11:00
|
|
|
kResultOk
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_parameter_count(&self) -> i32 {
|
|
|
|
// NOTE: We add a bypass parameter ourselves on index `self.param_ids.len()`, so these
|
|
|
|
// indices are all off by one
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner.param_hashes.len() as i32 + 1
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_parameter_info(
|
|
|
|
&self,
|
|
|
|
param_index: i32,
|
|
|
|
info: *mut vst3_sys::vst::ParameterInfo,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(info);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-01-27 10:19:50 +11:00
|
|
|
// Parameter index `self.param_ids.len()` is our own bypass parameter
|
2022-02-01 06:18:12 +11:00
|
|
|
if param_index < 0 || param_index > self.inner.param_hashes.len() as i32 {
|
2022-01-27 10:19:50 +11:00
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = std::mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-02-01 06:18:12 +11:00
|
|
|
if param_index == self.inner.param_hashes.len() as i32 {
|
2022-01-29 00:52:04 +11:00
|
|
|
info.id = *BYPASS_PARAM_HASH;
|
2022-01-27 10:19:50 +11:00
|
|
|
u16strlcpy(&mut info.title, "Bypass");
|
|
|
|
u16strlcpy(&mut info.short_title, "Bypass");
|
|
|
|
u16strlcpy(&mut info.units, "");
|
2022-01-31 23:33:30 +11:00
|
|
|
info.step_count = 1;
|
2022-01-27 10:19:50 +11:00
|
|
|
info.default_normalized_value = 0.0;
|
|
|
|
info.unit_id = vst3_sys::vst::kRootUnitId;
|
|
|
|
info.flags = vst3_sys::vst::ParameterFlags::kCanAutomate as i32
|
|
|
|
| vst3_sys::vst::ParameterFlags::kIsBypass as i32;
|
|
|
|
} else {
|
2022-02-01 06:18:12 +11:00
|
|
|
let param_hash = &self.inner.param_hashes[param_index as usize];
|
|
|
|
let default_value = &self.inner.param_defaults_normalized[param_index as usize];
|
|
|
|
let param_ptr = &self.inner.param_by_hash[param_hash];
|
2022-01-27 10:19:50 +11:00
|
|
|
|
2022-01-28 08:31:53 +11:00
|
|
|
info.id = *param_hash;
|
2022-01-27 10:19:50 +11:00
|
|
|
u16strlcpy(&mut info.title, param_ptr.name());
|
|
|
|
u16strlcpy(&mut info.short_title, param_ptr.name());
|
|
|
|
u16strlcpy(&mut info.units, param_ptr.unit());
|
2022-01-31 23:33:30 +11:00
|
|
|
info.step_count = match param_ptr {
|
|
|
|
ParamPtr::FloatParam(_) => 0,
|
|
|
|
ParamPtr::IntParam(p) => match (**p).range {
|
2022-02-03 01:12:33 +11:00
|
|
|
Range::Linear { min, max } => max - min,
|
|
|
|
Range::Skewed { min, max, .. } => max - min,
|
|
|
|
Range::SymmetricalSkewed { min, max, .. } => max - min,
|
2022-01-31 23:33:30 +11:00
|
|
|
},
|
|
|
|
ParamPtr::BoolParam(_) => 1,
|
|
|
|
};
|
2022-01-27 10:19:50 +11:00
|
|
|
info.default_normalized_value = *default_value as f64;
|
|
|
|
info.unit_id = vst3_sys::vst::kRootUnitId;
|
|
|
|
info.flags = vst3_sys::vst::ParameterFlags::kCanAutomate as i32;
|
|
|
|
}
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_param_string_by_value(
|
|
|
|
&self,
|
|
|
|
id: u32,
|
|
|
|
value_normalized: f64,
|
|
|
|
string: *mut TChar,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(string);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-01-27 10:19:50 +11:00
|
|
|
// Somehow there's no length there, so we'll assume our own maximum
|
|
|
|
let dest = &mut *(string as *mut [TChar; 128]);
|
|
|
|
|
|
|
|
if id == *BYPASS_PARAM_HASH {
|
|
|
|
if value_normalized > 0.5 {
|
|
|
|
u16strlcpy(dest, "Bypassed")
|
|
|
|
} else {
|
|
|
|
u16strlcpy(dest, "Enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
kResultOk
|
2022-02-01 06:18:12 +11:00
|
|
|
} else if let Some(param_ptr) = self.inner.param_by_hash.get(&id) {
|
2022-01-27 10:19:50 +11:00
|
|
|
u16strlcpy(
|
|
|
|
dest,
|
2022-01-29 00:06:51 +11:00
|
|
|
¶m_ptr.normalized_value_to_string(value_normalized as f32, false),
|
2022-01-27 10:19:50 +11:00
|
|
|
);
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kInvalidArgument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_param_value_by_string(
|
|
|
|
&self,
|
|
|
|
id: u32,
|
|
|
|
string: *const TChar,
|
|
|
|
value_normalized: *mut f64,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(string, value_normalized);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-01-28 05:30:42 +11:00
|
|
|
let string = match U16CStr::from_ptr_str(string as *const u16).to_string() {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => return kInvalidArgument,
|
|
|
|
};
|
|
|
|
|
|
|
|
if id == *BYPASS_PARAM_HASH {
|
|
|
|
let value = match string.as_str() {
|
|
|
|
"Bypassed" => 1.0,
|
|
|
|
"Enabled" => 0.0,
|
|
|
|
_ => return kResultFalse,
|
|
|
|
};
|
|
|
|
*value_normalized = value;
|
|
|
|
|
|
|
|
kResultOk
|
2022-02-01 06:18:12 +11:00
|
|
|
} else if let Some(param_ptr) = self.inner.param_by_hash.get(&id) {
|
2022-01-28 05:30:42 +11:00
|
|
|
let value = match param_ptr.string_to_normalized_value(&string) {
|
|
|
|
Some(v) => v as f64,
|
|
|
|
None => return kResultFalse,
|
|
|
|
};
|
|
|
|
*value_normalized = value;
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kInvalidArgument
|
|
|
|
}
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn normalized_param_to_plain(&self, id: u32, value_normalized: f64) -> f64 {
|
2022-01-28 05:55:24 +11:00
|
|
|
if id == *BYPASS_PARAM_HASH {
|
|
|
|
value_normalized
|
2022-02-01 06:18:12 +11:00
|
|
|
} else if let Some(param_ptr) = self.inner.param_by_hash.get(&id) {
|
2022-01-30 00:54:48 +11:00
|
|
|
param_ptr.preview_plain(value_normalized as f32) as f64
|
2022-01-28 05:55:24 +11:00
|
|
|
} else {
|
|
|
|
0.5
|
|
|
|
}
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn plain_param_to_normalized(&self, id: u32, plain_value: f64) -> f64 {
|
2022-01-28 05:55:24 +11:00
|
|
|
if id == *BYPASS_PARAM_HASH {
|
|
|
|
plain_value.clamp(0.0, 1.0)
|
2022-02-01 06:18:12 +11:00
|
|
|
} else if let Some(param_ptr) = self.inner.param_by_hash.get(&id) {
|
2022-01-28 05:55:24 +11:00
|
|
|
param_ptr.preview_normalized(plain_value as f32) as f64
|
|
|
|
} else {
|
|
|
|
0.5
|
|
|
|
}
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_param_normalized(&self, id: u32) -> f64 {
|
2022-01-28 05:55:24 +11:00
|
|
|
if id == *BYPASS_PARAM_HASH {
|
2022-02-01 06:18:12 +11:00
|
|
|
if self.inner.bypass_state.load(Ordering::SeqCst) {
|
2022-01-28 05:55:24 +11:00
|
|
|
1.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
2022-02-01 06:18:12 +11:00
|
|
|
} else if let Some(param_ptr) = self.inner.param_by_hash.get(&id) {
|
2022-01-28 05:55:24 +11:00
|
|
|
param_ptr.normalized_value() as f64
|
|
|
|
} else {
|
|
|
|
0.5
|
|
|
|
}
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_param_normalized(&self, id: u32, value: f64) -> tresult {
|
2022-01-29 10:55:08 +11:00
|
|
|
// If the plugin is currently processing audio, then this parameter change will also be sent
|
|
|
|
// to the process function
|
2022-02-01 06:18:12 +11:00
|
|
|
if self.inner.is_processing.load(Ordering::SeqCst) {
|
2022-01-29 10:55:08 +11:00
|
|
|
return kResultOk;
|
2022-01-28 05:55:24 +11:00
|
|
|
}
|
2022-01-29 10:55:08 +11:00
|
|
|
|
2022-02-03 07:08:23 +11:00
|
|
|
let sample_rate = self
|
|
|
|
.inner
|
|
|
|
.current_buffer_config
|
|
|
|
.load()
|
|
|
|
.map(|c| c.sample_rate);
|
|
|
|
self.inner
|
2022-02-06 08:26:21 +11:00
|
|
|
.set_normalized_value_by_hash(id, value as f32, sample_rate)
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 06:54:52 +11:00
|
|
|
unsafe fn set_component_handler(
|
|
|
|
&self,
|
2022-02-02 02:52:55 +11:00
|
|
|
handler: SharedVstPtr<dyn vst3_sys::vst::IComponentHandler>,
|
2022-01-30 06:54:52 +11:00
|
|
|
) -> tresult {
|
2022-02-02 02:52:55 +11:00
|
|
|
*self.inner.component_handler.write() = handler.upgrade().map(VstPtr::from);
|
|
|
|
|
2022-01-28 05:55:24 +11:00
|
|
|
kResultOk
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
|
2022-01-28 05:55:24 +11:00
|
|
|
unsafe fn create_view(&self, _name: vst3_sys::base::FIDString) -> *mut c_void {
|
2022-02-06 04:07:03 +11:00
|
|
|
// Without specialization this is the least redundant way to check if the plugin has an
|
|
|
|
// editor. The default implementation returns a None here.
|
|
|
|
match self.inner.plugin.read().editor_size() {
|
|
|
|
Some(_) => {
|
|
|
|
Box::into_raw(WrapperView::<P>::new(self.inner.clone())) as *mut vst3_sys::c_void
|
|
|
|
}
|
|
|
|
None => ptr::null_mut(),
|
|
|
|
}
|
2022-01-27 10:19:50 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:53:42 +11:00
|
|
|
impl<P: Plugin> IAudioProcessor for Wrapper<P> {
|
2022-01-28 23:40:47 +11:00
|
|
|
unsafe fn set_bus_arrangements(
|
|
|
|
&self,
|
|
|
|
inputs: *mut vst3_sys::vst::SpeakerArrangement,
|
|
|
|
num_ins: i32,
|
|
|
|
outputs: *mut vst3_sys::vst::SpeakerArrangement,
|
|
|
|
num_outs: i32,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(inputs, outputs);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
|
|
|
// We currently only do single audio bus IO configurations
|
|
|
|
if num_ins != 1 || num_outs != 1 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
let input_channel_map = &*inputs;
|
|
|
|
let output_channel_map = &*outputs;
|
|
|
|
let proposed_config = BusConfig {
|
|
|
|
num_input_channels: input_channel_map.count_ones(),
|
|
|
|
num_output_channels: output_channel_map.count_ones(),
|
|
|
|
};
|
2022-02-01 06:18:12 +11:00
|
|
|
if self
|
|
|
|
.inner
|
|
|
|
.plugin
|
|
|
|
.read()
|
|
|
|
.accepts_bus_config(&proposed_config)
|
|
|
|
{
|
|
|
|
self.inner.current_bus_config.store(proposed_config);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_bus_arrangement(
|
|
|
|
&self,
|
|
|
|
dir: vst3_sys::vst::BusDirection,
|
|
|
|
index: i32,
|
|
|
|
arr: *mut vst3_sys::vst::SpeakerArrangement,
|
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(arr);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
2022-01-30 11:43:28 +11:00
|
|
|
let channel_count_to_map = |count| match count {
|
|
|
|
0 => vst3_sys::vst::kEmpty,
|
|
|
|
1 => vst3_sys::vst::kMono,
|
|
|
|
2 => vst3_sys::vst::kStereo,
|
|
|
|
5 => vst3_sys::vst::k50,
|
|
|
|
6 => vst3_sys::vst::k51,
|
|
|
|
7 => vst3_sys::vst::k70Cine,
|
|
|
|
8 => vst3_sys::vst::k71Cine,
|
|
|
|
n => {
|
|
|
|
nih_debug_assert_failure!(
|
|
|
|
"No defined layout for {} channels, making something up on the spot...",
|
|
|
|
n
|
|
|
|
);
|
|
|
|
(1 << n) - 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
let config = self.inner.current_bus_config.load();
|
2022-01-30 11:43:28 +11:00
|
|
|
let num_channels = match (dir, index) {
|
|
|
|
(d, 0) if d == vst3_sys::vst::BusDirections::kInput as i32 => config.num_input_channels,
|
|
|
|
(d, 0) if d == vst3_sys::vst::BusDirections::kOutput as i32 => {
|
|
|
|
config.num_output_channels
|
|
|
|
}
|
|
|
|
_ => return kInvalidArgument,
|
|
|
|
};
|
|
|
|
let channel_map = channel_count_to_map(num_channels);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
2022-02-04 03:03:33 +11:00
|
|
|
nih_debug_assert_eq!(num_channels, channel_map.count_ones());
|
2022-01-30 11:43:28 +11:00
|
|
|
*arr = channel_map;
|
2022-01-28 23:40:47 +11:00
|
|
|
|
2022-01-30 11:43:28 +11:00
|
|
|
kResultOk
|
2022-01-28 23:40:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn can_process_sample_size(&self, symbolic_sample_size: i32) -> tresult {
|
|
|
|
if symbolic_sample_size == vst3_sys::vst::SymbolicSampleSizes::kSample32 as i32 {
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_latency_samples(&self) -> u32 {
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner.current_latency.load(Ordering::SeqCst)
|
2022-01-28 23:40:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn setup_processing(&self, setup: *const vst3_sys::vst::ProcessSetup) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(setup);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
|
|
|
// There's no special handling for offline processing at the moment
|
|
|
|
let setup = &*setup;
|
|
|
|
nih_debug_assert_eq!(
|
|
|
|
setup.symbolic_sample_size,
|
|
|
|
vst3_sys::vst::SymbolicSampleSizes::kSample32 as i32
|
|
|
|
);
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
let bus_config = self.inner.current_bus_config.load();
|
2022-01-28 23:40:47 +11:00
|
|
|
let buffer_config = BufferConfig {
|
|
|
|
sample_rate: setup.sample_rate as f32,
|
|
|
|
max_buffer_size: setup.max_samples_per_block as u32,
|
|
|
|
};
|
|
|
|
|
2022-02-03 07:33:20 +11:00
|
|
|
// Befure initializing the plugin, make sure all smoothers are set the the default values
|
|
|
|
for param in self.inner.param_by_hash.values() {
|
2022-02-03 07:41:20 +11:00
|
|
|
param.update_smoother(buffer_config.sample_rate, true);
|
2022-02-03 07:33:20 +11:00
|
|
|
}
|
|
|
|
|
2022-02-05 08:55:10 +11:00
|
|
|
if self.inner.plugin.write().initialize(
|
|
|
|
&bus_config,
|
|
|
|
&buffer_config,
|
2022-02-05 09:03:11 +11:00
|
|
|
&mut self.inner.make_process_context(),
|
2022-02-05 08:55:10 +11:00
|
|
|
) {
|
2022-01-29 03:23:16 +11:00
|
|
|
// Preallocate enough room in the output slices vector so we can convert a `*mut *mut
|
|
|
|
// f32` to a `&mut [&mut f32]` in the process call
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner
|
2022-02-03 00:39:21 +11:00
|
|
|
.output_buffer
|
2022-02-01 05:40:52 +11:00
|
|
|
.write()
|
2022-02-03 00:39:21 +11:00
|
|
|
.as_raw_vec()
|
2022-01-29 03:23:16 +11:00
|
|
|
.resize_with(bus_config.num_output_channels as usize, || &mut []);
|
|
|
|
|
2022-02-03 01:39:55 +11:00
|
|
|
// Also store this for later, so we can reinitialize the plugin after restoring state
|
|
|
|
self.inner.current_buffer_config.store(Some(buffer_config));
|
|
|
|
|
2022-01-28 23:40:47 +11:00
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:55:08 +11:00
|
|
|
unsafe fn set_processing(&self, state: TBool) -> tresult {
|
2022-01-28 23:40:47 +11:00
|
|
|
// Always reset the processing status when the plugin gets activated or deactivated
|
2022-02-01 06:18:12 +11:00
|
|
|
self.inner.last_process_status.store(ProcessStatus::Normal);
|
|
|
|
self.inner.is_processing.store(state != 0, Ordering::SeqCst);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
|
|
|
// We don't have any special handling for suspending and resuming plugins, yet
|
2022-01-29 10:55:08 +11:00
|
|
|
kResultOk
|
2022-01-28 23:40:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn process(&self, data: *mut vst3_sys::vst::ProcessData) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(data);
|
2022-01-28 23:40:47 +11:00
|
|
|
|
2022-02-06 10:51:18 +11:00
|
|
|
// We need to handle incoming automation first
|
|
|
|
let data = &*data;
|
|
|
|
let sample_rate = self
|
|
|
|
.inner
|
|
|
|
.current_buffer_config
|
|
|
|
.load()
|
|
|
|
.map(|c| c.sample_rate);
|
|
|
|
if let Some(param_changes) = data.input_param_changes.upgrade() {
|
|
|
|
let num_param_queues = param_changes.get_parameter_count();
|
|
|
|
for change_queue_idx in 0..num_param_queues {
|
|
|
|
if let Some(param_change_queue) =
|
|
|
|
param_changes.get_parameter_data(change_queue_idx).upgrade()
|
|
|
|
{
|
|
|
|
let param_hash = param_change_queue.get_parameter_id();
|
|
|
|
let num_changes = param_change_queue.get_point_count();
|
|
|
|
|
|
|
|
// TODO: Handle sample accurate parameter changes, possibly in a similar way to
|
|
|
|
// the smoothing
|
|
|
|
let mut sample_offset = 0i32;
|
|
|
|
let mut value = 0.0f64;
|
|
|
|
if num_changes > 0
|
|
|
|
&& param_change_queue.get_point(
|
|
|
|
num_changes - 1,
|
|
|
|
&mut sample_offset,
|
|
|
|
&mut value,
|
|
|
|
) == kResultOk
|
2022-01-30 11:50:22 +11:00
|
|
|
{
|
2022-02-06 10:51:18 +11:00
|
|
|
self.inner.set_normalized_value_by_hash(
|
|
|
|
param_hash,
|
|
|
|
value as f32,
|
|
|
|
sample_rate,
|
|
|
|
);
|
2022-01-30 11:50:22 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 10:51:18 +11:00
|
|
|
}
|
2022-01-30 11:50:22 +11:00
|
|
|
|
2022-02-06 10:51:18 +11:00
|
|
|
// And also incoming note events if the plugin accepts MDII
|
|
|
|
if P::ACCEPTS_MIDI {
|
|
|
|
let mut input_events = self.inner.input_events.write();
|
|
|
|
if let Some(events) = data.input_events.upgrade() {
|
|
|
|
let num_events = events.get_event_count();
|
|
|
|
|
|
|
|
input_events.clear();
|
|
|
|
let mut event: MaybeUninit<_> = MaybeUninit::uninit();
|
|
|
|
for i in 0..num_events {
|
|
|
|
nih_debug_assert_eq!(events.get_event(i, event.as_mut_ptr()), kResultOk);
|
|
|
|
let event = event.assume_init();
|
|
|
|
let timing = event.sample_offset as u32;
|
|
|
|
if event.type_ == vst3_sys::vst::EventTypes::kNoteOnEvent as u16 {
|
|
|
|
let event = event.event.note_on;
|
|
|
|
input_events.push_back(NoteEvent::NoteOn {
|
|
|
|
timing,
|
|
|
|
channel: event.channel as u8,
|
|
|
|
note: event.pitch as u8,
|
|
|
|
velocity: (event.velocity * 127.0).round() as u8,
|
|
|
|
});
|
|
|
|
} else if event.type_ == vst3_sys::vst::EventTypes::kNoteOffEvent as u16 {
|
|
|
|
let event = event.event.note_off;
|
|
|
|
input_events.push_back(NoteEvent::NoteOff {
|
|
|
|
timing,
|
|
|
|
channel: event.channel as u8,
|
|
|
|
note: event.pitch as u8,
|
|
|
|
velocity: (event.velocity * 127.0).round() as u8,
|
|
|
|
});
|
2022-02-04 11:48:24 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 10:51:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's possible the host only wanted to send new parameter values
|
|
|
|
if data.num_outputs == 0 {
|
|
|
|
nih_log!("VST3 parameter flush");
|
|
|
|
return kResultOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The setups we suppport are:
|
|
|
|
// - 1 input bus
|
|
|
|
// - 1 output bus
|
|
|
|
// - 1 input bus and 1 output bus
|
|
|
|
nih_debug_assert!(
|
|
|
|
data.num_inputs >= 0
|
|
|
|
&& data.num_inputs <= 1
|
|
|
|
&& data.num_outputs >= 0
|
|
|
|
&& data.num_outputs <= 1,
|
|
|
|
"The host provides more than one input or output bus"
|
|
|
|
);
|
|
|
|
nih_debug_assert_eq!(
|
|
|
|
data.symbolic_sample_size,
|
|
|
|
vst3_sys::vst::SymbolicSampleSizes::kSample32 as i32
|
|
|
|
);
|
|
|
|
nih_debug_assert!(data.num_samples >= 0);
|
|
|
|
|
|
|
|
let num_output_channels = (*data.outputs).num_channels as usize;
|
|
|
|
check_null_ptr_msg!(
|
|
|
|
"Process output pointer is null",
|
|
|
|
data.outputs,
|
|
|
|
(*data.outputs).buffers,
|
|
|
|
);
|
2022-02-04 11:48:24 +11:00
|
|
|
|
2022-02-06 10:51:18 +11:00
|
|
|
// This vector has been reallocated to contain enough slices as there are output
|
|
|
|
// channels
|
|
|
|
let mut output_buffer = self.inner.output_buffer.write();
|
|
|
|
{
|
|
|
|
let output_slices = output_buffer.as_raw_vec();
|
|
|
|
nih_debug_assert_eq!(num_output_channels, output_slices.len());
|
|
|
|
for (output_channel_idx, output_channel_slice) in output_slices.iter_mut().enumerate() {
|
|
|
|
// SAFETY: These pointers may not be valid outside of this function even though
|
|
|
|
// their lifetime is equal to this structs. This is still safe because they are
|
|
|
|
// only dereferenced here later as part of this process function.
|
|
|
|
*output_channel_slice = std::slice::from_raw_parts_mut(
|
|
|
|
*((*data.outputs).buffers as *mut *mut f32).add(output_channel_idx),
|
|
|
|
data.num_samples as usize,
|
|
|
|
);
|
2022-02-03 00:39:21 +11:00
|
|
|
}
|
2022-02-06 10:51:18 +11:00
|
|
|
}
|
2022-01-29 03:23:16 +11:00
|
|
|
|
2022-02-06 10:51:18 +11:00
|
|
|
// Most hosts process data in place, in which case we don't need to do any copying
|
|
|
|
// ourselves. If the pointers do not alias, then we'll do the copy here and then the plugin
|
|
|
|
// can just do normal in place processing.
|
|
|
|
if !data.inputs.is_null() {
|
|
|
|
let num_input_channels = (*data.inputs).num_channels as usize;
|
2022-01-29 03:23:16 +11:00
|
|
|
nih_debug_assert!(
|
2022-02-06 10:51:18 +11:00
|
|
|
num_input_channels <= num_output_channels,
|
|
|
|
"Stereo to mono and similar configurations are not supported"
|
2022-02-04 01:58:00 +11:00
|
|
|
);
|
2022-02-06 10:51:18 +11:00
|
|
|
for input_channel_idx in 0..cmp::min(num_input_channels, num_output_channels) {
|
|
|
|
let output_channel_ptr =
|
|
|
|
*((*data.outputs).buffers as *mut *mut f32).add(input_channel_idx);
|
|
|
|
let input_channel_ptr =
|
|
|
|
*((*data.inputs).buffers as *const *const f32).add(input_channel_idx);
|
|
|
|
if input_channel_ptr != output_channel_ptr {
|
|
|
|
ptr::copy_nonoverlapping(
|
|
|
|
input_channel_ptr,
|
|
|
|
output_channel_ptr,
|
2022-01-29 03:23:16 +11:00
|
|
|
data.num_samples as usize,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 10:51:18 +11:00
|
|
|
}
|
2022-01-29 03:23:16 +11:00
|
|
|
|
2022-02-06 10:51:18 +11:00
|
|
|
let mut plugin = self.inner.plugin.write();
|
|
|
|
let mut context = self.inner.make_process_context();
|
|
|
|
// Panic on allocations if the `assert_process_allocs` feature has been enabled, and make
|
|
|
|
// sure that FTZ is set up correctly
|
|
|
|
process_wrapper(
|
|
|
|
move || match plugin.process(&mut output_buffer, &mut context) {
|
2022-02-04 01:58:00 +11:00
|
|
|
ProcessStatus::Error(err) => {
|
|
|
|
nih_debug_assert_failure!("Process error: {}", err);
|
|
|
|
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
_ => kResultOk,
|
2022-02-06 10:51:18 +11:00
|
|
|
},
|
|
|
|
)
|
2022-01-28 23:40:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_tail_samples(&self) -> u32 {
|
|
|
|
// https://github.com/steinbergmedia/vst3_pluginterfaces/blob/2ad397ade5b51007860bedb3b01b8afd2c5f6fba/vst/ivstaudioprocessor.h#L145-L159
|
2022-02-01 06:18:12 +11:00
|
|
|
match self.inner.last_process_status.load() {
|
2022-01-28 23:40:47 +11:00
|
|
|
ProcessStatus::Tail(samples) => samples,
|
|
|
|
ProcessStatus::KeepAlive => u32::MAX, // kInfiniteTail
|
|
|
|
_ => 0, // kNoTail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 04:06:24 +11:00
|
|
|
impl<P: Plugin> IPlugView for WrapperView<P> {
|
2022-02-06 04:42:06 +11:00
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
|
|
|
unsafe fn is_platform_type_supported(&self, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
match type_.to_str() {
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_X11_WINDOW => kResultOk,
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Invalid window handle type: {:?}", type_);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
#[cfg(all(target_os = "macos"))]
|
|
|
|
unsafe fn is_platform_type_supported(&self, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
match type_.to_str() {
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_NSVIEW => kResultOk,
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Invalid window handle type: {:?}", type_);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(target_os = "windows"))]
|
|
|
|
unsafe fn is_platform_type_supported(&self, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
match type_.to_str() {
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_HWND => kResultOk,
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Invalid window handle type: {:?}", type_);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn attached(&self, parent: *mut c_void, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let mut editor = self.editor.write();
|
|
|
|
if editor.is_none() {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
let handle = match type_.to_str() {
|
2022-02-06 09:31:35 +11:00
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
2022-02-06 04:42:06 +11:00
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_X11_WINDOW => {
|
2022-02-06 09:31:35 +11:00
|
|
|
let mut handle = raw_window_handle::unix::XcbHandle::empty();
|
2022-02-06 04:42:06 +11:00
|
|
|
handle.window = parent as usize as u32;
|
|
|
|
RawWindowHandle::Xcb(handle)
|
|
|
|
}
|
2022-02-06 09:31:35 +11:00
|
|
|
#[cfg(all(target_os = "macos"))]
|
2022-02-06 04:42:06 +11:00
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_NSVIEW => {
|
2022-02-06 09:31:35 +11:00
|
|
|
let mut handle = raw_window_handle::macos::MacOSHandle::empty();
|
2022-02-06 04:42:06 +11:00
|
|
|
handle.ns_view = parent;
|
2022-02-06 09:31:35 +11:00
|
|
|
RawWindowHandle::MacOS(handle)
|
2022-02-06 04:42:06 +11:00
|
|
|
}
|
2022-02-06 09:31:35 +11:00
|
|
|
#[cfg(all(target_os = "windows"))]
|
2022-02-06 04:42:06 +11:00
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_HWND => {
|
2022-02-06 09:31:35 +11:00
|
|
|
let mut handle = raw_window_handle::windows::WindowsHandle::empty();
|
2022-02-06 04:42:06 +11:00
|
|
|
handle.hwnd = parent;
|
2022-02-06 09:31:35 +11:00
|
|
|
RawWindowHandle::Windows(handle)
|
2022-02-06 04:42:06 +11:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Unknown window handle type: {:?}", type_);
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
*editor = self
|
|
|
|
.inner
|
|
|
|
.plugin
|
2022-02-07 01:22:30 +11:00
|
|
|
.read()
|
2022-02-06 10:06:01 +11:00
|
|
|
.create_editor(EditorWindowHandle { handle }, self.inner.clone());
|
2022-02-06 04:42:06 +11:00
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn removed(&self) -> tresult {
|
2022-02-06 04:42:06 +11:00
|
|
|
let mut editor = self.editor.write();
|
|
|
|
if editor.is_some() {
|
|
|
|
*editor = None;
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
unsafe fn on_wheel(&self, _distance: f32) -> tresult {
|
|
|
|
// We'll let the plugin use the OS' input mechamisms because not all DAWs (or very few
|
|
|
|
// actually) implement these functions
|
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_key_down(
|
|
|
|
&self,
|
2022-02-06 04:42:06 +11:00
|
|
|
_key: vst3_sys::base::char16,
|
|
|
|
_key_code: i16,
|
|
|
|
_modifiers: i16,
|
2022-02-06 04:06:24 +11:00
|
|
|
) -> tresult {
|
2022-02-06 04:42:06 +11:00
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_key_up(
|
|
|
|
&self,
|
2022-02-06 04:42:06 +11:00
|
|
|
_key: vst3_sys::base::char16,
|
|
|
|
_key_code: i16,
|
|
|
|
_modifiers: i16,
|
2022-02-06 04:06:24 +11:00
|
|
|
) -> tresult {
|
2022-02-06 04:42:06 +11:00
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
unsafe fn get_size(&self, size: *mut vst3_sys::gui::ViewRect) -> tresult {
|
|
|
|
check_null_ptr!(size);
|
|
|
|
|
|
|
|
// If the editor is already open, then take the size from the editor in case the plugin
|
|
|
|
// updates one but not the other
|
|
|
|
let (width, height) = match self.editor.read().as_ref() {
|
|
|
|
Some(editor) => editor.size(),
|
|
|
|
None => self
|
|
|
|
.inner
|
|
|
|
.plugin
|
|
|
|
.read()
|
|
|
|
.editor_size()
|
|
|
|
.expect("Wait, this returned a Some just now!?"),
|
|
|
|
};
|
|
|
|
|
|
|
|
*size = mem::zeroed();
|
|
|
|
|
|
|
|
let size = &mut *size;
|
|
|
|
size.left = 0;
|
|
|
|
size.right = width as i32;
|
|
|
|
size.top = 0;
|
|
|
|
size.bottom = height as i32;
|
|
|
|
|
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
unsafe fn on_size(&self, _new_size: *mut vst3_sys::gui::ViewRect) -> tresult {
|
|
|
|
// TODO: Implement resizing
|
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
unsafe fn on_focus(&self, _state: TBool) -> tresult {
|
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
unsafe fn set_frame(&self, _frame: *mut c_void) -> tresult {
|
|
|
|
// TODO: Implement resizing. We don't implement that right now, so we also don't need the
|
|
|
|
// plug frame.
|
|
|
|
kResultOk
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn can_resize(&self) -> tresult {
|
2022-02-06 04:42:06 +11:00
|
|
|
// TODO: Implement resizing
|
|
|
|
kResultFalse
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
|
2022-02-06 04:42:06 +11:00
|
|
|
unsafe fn check_size_constraint(&self, rect: *mut vst3_sys::gui::ViewRect) -> tresult {
|
|
|
|
check_null_ptr!(rect);
|
|
|
|
|
|
|
|
// TODO: Add this with the resizing
|
|
|
|
if (*rect).right - (*rect).left > 0 && (*rect).bottom - (*rect).top > 0 {
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
2022-02-06 04:06:24 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:20:14 +11:00
|
|
|
#[doc(hidden)]
|
2022-01-27 08:23:44 +11:00
|
|
|
#[VST3(implements(IPluginFactory, IPluginFactory2, IPluginFactory3))]
|
2022-01-28 08:13:13 +11:00
|
|
|
pub struct Factory<P: Vst3Plugin> {
|
2022-01-27 05:19:20 +11:00
|
|
|
/// The exposed plugin's GUID. Instead of generating this, we'll just let the programmer decide
|
|
|
|
/// on their own.
|
|
|
|
cid: GUID,
|
2022-01-27 04:14:54 +11:00
|
|
|
/// The type will be used for constructing plugin instances later.
|
|
|
|
_phantom: PhantomData<P>,
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> Factory<P> {
|
|
|
|
pub fn new() -> Box<Self> {
|
|
|
|
Self::allocate(
|
|
|
|
GUID {
|
|
|
|
data: P::VST3_CLASS_ID,
|
|
|
|
},
|
|
|
|
PhantomData::default(),
|
|
|
|
)
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> IPluginFactory for Factory<P> {
|
2022-01-27 04:14:54 +11:00
|
|
|
unsafe fn get_factory_info(&self, info: *mut vst3_sys::base::PFactoryInfo) -> tresult {
|
2022-01-27 05:57:56 +11:00
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
2022-01-29 00:20:16 +11:00
|
|
|
strlcpy(&mut info.vendor, P::VENDOR);
|
2022-01-27 05:57:56 +11:00
|
|
|
strlcpy(&mut info.url, P::URL);
|
|
|
|
strlcpy(&mut info.email, P::EMAIL);
|
|
|
|
info.flags = vst3_sys::base::FactoryFlags::kUnicode as i32;
|
|
|
|
|
|
|
|
kResultOk
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn count_classes(&self) -> i32 {
|
2022-01-27 05:57:56 +11:00
|
|
|
// We don't do shell plugins, and good of an idea having separated components and edit
|
|
|
|
// controllers in theory is, few software can use it, and doing that would make our simple
|
|
|
|
// microframework a lot less simple
|
|
|
|
1
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_class_info(&self, index: i32, info: *mut vst3_sys::base::PClassInfo) -> tresult {
|
2022-01-27 05:57:56 +11:00
|
|
|
if index != 0 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
|
|
|
info.cid = self.cid;
|
|
|
|
info.cardinality = vst3_sys::base::ClassCardinality::kManyInstances as i32;
|
|
|
|
strlcpy(&mut info.category, "Audio Module Class");
|
|
|
|
strlcpy(&mut info.name, P::NAME);
|
|
|
|
|
|
|
|
kResultOk
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn create_instance(
|
|
|
|
&self,
|
2022-01-27 07:12:02 +11:00
|
|
|
cid: *const vst3_sys::IID,
|
|
|
|
_iid: *const vst3_sys::IID,
|
|
|
|
obj: *mut *mut vst3_sys::c_void,
|
2022-01-27 04:14:54 +11:00
|
|
|
) -> tresult {
|
2022-01-28 23:45:17 +11:00
|
|
|
check_null_ptr!(cid, obj);
|
2022-01-28 08:51:29 +11:00
|
|
|
|
2022-01-27 07:12:02 +11:00
|
|
|
if *cid != self.cid {
|
|
|
|
return kNoInterface;
|
|
|
|
}
|
|
|
|
|
2022-02-01 06:18:12 +11:00
|
|
|
*obj = Box::into_raw(Wrapper::<P>::new()) as *mut vst3_sys::c_void;
|
2022-01-27 07:12:02 +11:00
|
|
|
|
|
|
|
kResultOk
|
2022-01-27 04:14:54 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> IPluginFactory2 for Factory<P> {
|
2022-01-27 08:23:44 +11:00
|
|
|
unsafe fn get_class_info2(
|
|
|
|
&self,
|
|
|
|
index: i32,
|
|
|
|
info: *mut vst3_sys::base::PClassInfo2,
|
|
|
|
) -> tresult {
|
|
|
|
if index != 0 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
|
|
|
info.cid = self.cid;
|
|
|
|
info.cardinality = vst3_sys::base::ClassCardinality::kManyInstances as i32;
|
|
|
|
strlcpy(&mut info.category, "Audio Module Class");
|
|
|
|
strlcpy(&mut info.name, P::NAME);
|
|
|
|
info.class_flags = 1 << 1; // kSimpleModeSupported
|
|
|
|
strlcpy(&mut info.subcategories, P::VST3_CATEGORIES);
|
|
|
|
strlcpy(&mut info.vendor, P::VENDOR);
|
|
|
|
strlcpy(&mut info.version, P::VERSION);
|
|
|
|
strlcpy(&mut info.sdk_version, VST3_SDK_VERSION);
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
impl<P: Vst3Plugin> IPluginFactory3 for Factory<P> {
|
2022-01-27 08:23:44 +11:00
|
|
|
unsafe fn get_class_info_unicode(
|
|
|
|
&self,
|
|
|
|
index: i32,
|
|
|
|
info: *mut vst3_sys::base::PClassInfoW,
|
|
|
|
) -> tresult {
|
|
|
|
if index != 0 {
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = mem::zeroed();
|
|
|
|
|
|
|
|
let info = &mut *info;
|
|
|
|
info.cid = self.cid;
|
|
|
|
info.cardinality = vst3_sys::base::ClassCardinality::kManyInstances as i32;
|
|
|
|
strlcpy(&mut info.category, "Audio Module Class");
|
|
|
|
u16strlcpy(&mut info.name, P::NAME);
|
|
|
|
info.class_flags = 1 << 1; // kSimpleModeSupported
|
|
|
|
strlcpy(&mut info.subcategories, P::VST3_CATEGORIES);
|
|
|
|
u16strlcpy(&mut info.vendor, P::VENDOR);
|
|
|
|
u16strlcpy(&mut info.version, P::VERSION);
|
|
|
|
u16strlcpy(&mut info.sdk_version, VST3_SDK_VERSION);
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_host_context(&self, _context: *mut c_void) -> tresult {
|
|
|
|
// We don't need to do anything with this
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:13:13 +11:00
|
|
|
/// Export a VST3 plugin from this library using the provided plugin type.
|
2022-01-26 22:37:45 +11:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nih_export_vst3 {
|
2022-01-28 08:13:13 +11:00
|
|
|
($plugin_ty:ty) => {
|
2022-01-27 04:14:54 +11:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn GetPluginFactory() -> *mut ::std::ffi::c_void {
|
2022-01-28 08:13:13 +11:00
|
|
|
let factory = ::nih_plug::wrapper::vst3::Factory::<$plugin_ty>::new();
|
2022-01-27 04:14:54 +11:00
|
|
|
|
|
|
|
Box::into_raw(factory) as *mut ::std::ffi::c_void
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need any special initialization logic, so all of these module entry point
|
|
|
|
// functions just return true all the time
|
|
|
|
|
2022-01-26 22:37:45 +11:00
|
|
|
// These two entry points are used on Linux, and they would theoretically also be used on
|
|
|
|
// the BSDs:
|
|
|
|
// https://github.com/steinbergmedia/vst3_public_sdk/blob/c3948deb407bdbff89de8fb6ab8500ea4df9d6d9/source/main/linuxmain.cpp#L47-L52
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
2022-01-27 04:14:54 +11:00
|
|
|
pub extern "C" fn ModuleEntry(_lib_handle: *mut ::std::ffi::c_void) -> bool {
|
2022-01-26 22:37:45 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
|
|
|
pub extern "C" fn ModuleExit() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
// These two entry points are used on macOS:
|
|
|
|
// https://github.com/steinbergmedia/vst3_public_sdk/blob/bc459feee68803346737901471441fd4829ec3f9/source/main/macmain.cpp#L60-L61
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "macos")]
|
2022-01-27 04:14:54 +11:00
|
|
|
pub extern "C" fn bundleEntry(_lib_handle: *mut ::std::ffi::c_void) -> bool {
|
2022-01-26 22:37:45 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub extern "C" fn bundleExit() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
// And these two entry points are used on Windows:
|
|
|
|
// https://github.com/steinbergmedia/vst3_public_sdk/blob/bc459feee68803346737901471441fd4829ec3f9/source/main/dllmain.cpp#L59-L60
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub extern "system" fn InitModule() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub extern "system" fn DeinitModule() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|