2022-03-04 07:18:57 +11:00
|
|
|
use atomic_refcell::AtomicRefCell;
|
2022-02-07 03:50:15 +11:00
|
|
|
use crossbeam::atomic::AtomicCell;
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::collections::{HashMap, VecDeque};
|
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use vst3_sys::base::{kInvalidArgument, kResultOk, tresult};
|
|
|
|
use vst3_sys::vst::IComponentHandler;
|
|
|
|
|
2022-03-04 02:33:41 +11:00
|
|
|
use super::context::{WrapperGuiContext, WrapperProcessContext};
|
2022-02-17 06:43:53 +11:00
|
|
|
use super::util::{ObjectPtr, VstPtr, BYPASS_PARAM_HASH, BYPASS_PARAM_ID};
|
|
|
|
use super::view::WrapperView;
|
2022-02-07 03:50:15 +11:00
|
|
|
use crate::buffer::Buffer;
|
2022-02-28 22:55:59 +11:00
|
|
|
use crate::event_loop::{EventLoop, MainThreadExecutor, OsEventLoop};
|
2022-02-07 03:50:15 +11:00
|
|
|
use crate::param::internals::ParamPtr;
|
2022-03-01 05:21:14 +11:00
|
|
|
use crate::plugin::{BufferConfig, BusConfig, Editor, NoteEvent, ProcessStatus, Vst3Plugin};
|
2022-02-07 03:50:15 +11:00
|
|
|
use crate::wrapper::util::hash_param_id;
|
|
|
|
|
|
|
|
/// 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-03-01 05:21:14 +11:00
|
|
|
pub(crate) struct WrapperInner<P: Vst3Plugin> {
|
2022-02-07 03:50:15 +11:00
|
|
|
/// The wrapped plugin instance.
|
|
|
|
pub plugin: RwLock<P>,
|
|
|
|
/// The plugin's editor, if it has one. This object does not do anything on its own, but we need
|
2022-03-04 09:05:01 +11:00
|
|
|
/// to instantiate this in advance so we don't need to lock the entire [`Plugin`] object when
|
2022-02-07 03:50:15 +11:00
|
|
|
/// creating an editor.
|
|
|
|
pub editor: Option<Arc<dyn Editor>>,
|
|
|
|
|
2022-03-04 09:05:01 +11:00
|
|
|
/// The host's [`IComponentHandler`] instance, if passed through
|
|
|
|
/// [`IEditController::set_component_handler`].
|
2022-03-04 07:18:57 +11:00
|
|
|
pub component_handler: AtomicRefCell<Option<VstPtr<dyn IComponentHandler>>>,
|
2022-02-07 03:50:15 +11:00
|
|
|
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Our own [`IPlugView`] instance. This is set while the editor is actually visible (which is
|
|
|
|
/// different form the lifetime of [`WrapperView`][super::WrapperView] itself).
|
2022-02-17 06:43:53 +11:00
|
|
|
pub plug_view: RwLock<Option<ObjectPtr<WrapperView<P>>>>,
|
|
|
|
|
2022-02-07 03:50:15 +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?
|
2022-03-04 07:18:57 +11:00
|
|
|
pub event_loop: AtomicRefCell<MaybeUninit<OsEventLoop<Task, Self>>>,
|
2022-02-07 03:50:15 +11:00
|
|
|
|
|
|
|
/// Whether the plugin is currently processing audio. In other words, the last state
|
|
|
|
/// `IAudioProcessor::setActive()` has been called with.
|
|
|
|
pub is_processing: AtomicBool,
|
|
|
|
/// The current bus configuration, modified through `IAudioProcessor::setBusArrangements()`.
|
|
|
|
pub current_bus_config: AtomicCell<BusConfig>,
|
|
|
|
/// The current buffer configuration, containing the sample rate and the maximum block size.
|
|
|
|
/// Will be set in `IAudioProcessor::setupProcessing()`.
|
|
|
|
pub current_buffer_config: AtomicCell<Option<BufferConfig>>,
|
|
|
|
/// Whether the plugin is currently bypassed. This is not yet integrated with the `Plugin`
|
|
|
|
/// trait.
|
|
|
|
pub bypass_state: AtomicBool,
|
|
|
|
/// The last process status returned by the plugin. This is used for tail handling.
|
|
|
|
pub last_process_status: AtomicCell<ProcessStatus>,
|
2022-03-04 09:05:01 +11:00
|
|
|
/// The current latency in samples, as set by the plugin through the [`ProcessContext`].
|
2022-02-07 03:50:15 +11:00
|
|
|
pub current_latency: AtomicU32,
|
|
|
|
/// 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
|
|
|
|
/// 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-03-04 07:18:57 +11:00
|
|
|
pub output_buffer: AtomicRefCell<Buffer<'static>>,
|
2022-02-07 03:50:15 +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
|
2022-03-04 07:18:57 +11:00
|
|
|
pub input_events: AtomicRefCell<VecDeque<NoteEvent>>,
|
2022-02-07 03:50:15 +11:00
|
|
|
|
|
|
|
/// The keys from `param_map` in a stable order.
|
|
|
|
pub param_hashes: Vec<u32>,
|
|
|
|
/// 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.
|
|
|
|
pub param_by_hash: HashMap<u32, ParamPtr>,
|
|
|
|
/// The default normalized parameter value for every parameter in `param_ids`. We need to store
|
2022-02-09 09:47:41 +11:00
|
|
|
/// this in case the host requeries the parmaeter later. This is also indexed by the hash so we
|
|
|
|
/// can retrieve them later for the UI if needed.
|
|
|
|
pub param_defaults_normalized: HashMap<u32, f32>,
|
2022-02-07 03:50:15 +11:00
|
|
|
/// Mappings from string parameter indentifiers to parameter hashes. Useful for debug logging
|
|
|
|
/// and when storing and restorign plugin state.
|
|
|
|
pub param_id_to_hash: HashMap<&'static str, u32>,
|
2022-03-04 09:05:01 +11:00
|
|
|
/// The inverse mapping from [`param_by_hash`][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).
|
2022-02-07 03:50:15 +11:00
|
|
|
pub param_ptr_to_hash: HashMap<ParamPtr, u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)]
|
|
|
|
pub enum Task {
|
|
|
|
/// Trigger a restart with the given restart flags. This is a bit set of the flags from
|
2022-03-04 09:05:01 +11:00
|
|
|
/// [`vst3_sys::vst::RestartFlags`].
|
2022-02-07 03:50:15 +11:00
|
|
|
TriggerRestart(i32),
|
|
|
|
}
|
|
|
|
|
2022-03-01 05:21:14 +11:00
|
|
|
impl<P: Vst3Plugin> WrapperInner<P> {
|
2022-02-07 03:50:15 +11:00
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new() -> Arc<Self> {
|
|
|
|
let plugin = RwLock::new(P::default());
|
|
|
|
let editor = plugin.read().editor().map(Arc::from);
|
|
|
|
|
|
|
|
let mut wrapper = Self {
|
|
|
|
plugin,
|
|
|
|
editor,
|
|
|
|
|
2022-03-04 07:18:57 +11:00
|
|
|
component_handler: AtomicRefCell::new(None),
|
2022-02-07 03:50:15 +11:00
|
|
|
|
2022-02-17 06:43:53 +11:00
|
|
|
plug_view: RwLock::new(None),
|
|
|
|
|
2022-03-04 07:18:57 +11:00
|
|
|
event_loop: AtomicRefCell::new(MaybeUninit::uninit()),
|
2022-02-07 03:50:15 +11:00
|
|
|
|
|
|
|
is_processing: AtomicBool::new(false),
|
|
|
|
// 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.
|
|
|
|
current_bus_config: AtomicCell::new(BusConfig {
|
|
|
|
num_input_channels: P::DEFAULT_NUM_INPUTS,
|
|
|
|
num_output_channels: P::DEFAULT_NUM_OUTPUTS,
|
|
|
|
}),
|
|
|
|
current_buffer_config: AtomicCell::new(None),
|
|
|
|
bypass_state: AtomicBool::new(false),
|
|
|
|
last_process_status: AtomicCell::new(ProcessStatus::Normal),
|
|
|
|
current_latency: AtomicU32::new(0),
|
2022-03-04 07:18:57 +11:00
|
|
|
output_buffer: AtomicRefCell::new(Buffer::default()),
|
|
|
|
input_events: AtomicRefCell::new(VecDeque::with_capacity(512)),
|
2022-02-07 03:50:15 +11:00
|
|
|
|
|
|
|
param_hashes: Vec::new(),
|
|
|
|
param_by_hash: HashMap::new(),
|
2022-02-09 09:47:41 +11:00
|
|
|
param_defaults_normalized: HashMap::new(),
|
2022-02-07 03:50:15 +11:00
|
|
|
param_id_to_hash: HashMap::new(),
|
|
|
|
param_ptr_to_hash: HashMap::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-13 22:47:14 +11:00
|
|
|
let param_map = wrapper.plugin.read().params().param_map();
|
|
|
|
let param_ids = wrapper.plugin.read().params().param_ids();
|
2022-02-07 03:50:15 +11:00
|
|
|
nih_debug_assert!(
|
|
|
|
!param_map.contains_key(BYPASS_PARAM_ID),
|
2022-03-01 11:27:01 +11:00
|
|
|
"The wrapper already adds its own bypass parameter"
|
2022-02-07 03:50:15 +11:00
|
|
|
);
|
|
|
|
|
|
|
|
// Only calculate these hashes once, and in the stable order defined by the plugin
|
|
|
|
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();
|
|
|
|
wrapper.param_hashes = param_id_hashes_ptrs
|
|
|
|
.iter()
|
|
|
|
.map(|&(_, hash, _)| hash)
|
|
|
|
.collect();
|
|
|
|
wrapper.param_by_hash = param_id_hashes_ptrs
|
|
|
|
.iter()
|
|
|
|
.map(|&(_, hash, ptr)| (hash, *ptr))
|
|
|
|
.collect();
|
|
|
|
wrapper.param_defaults_normalized = param_id_hashes_ptrs
|
|
|
|
.iter()
|
2022-02-09 09:47:41 +11:00
|
|
|
.map(|&(_, hash, ptr)| (hash, unsafe { ptr.normalized_value() }))
|
2022-02-07 03:50:15 +11:00
|
|
|
.collect();
|
|
|
|
wrapper.param_id_to_hash = param_id_hashes_ptrs
|
|
|
|
.iter()
|
|
|
|
.map(|&(id, hash, _)| (*id, hash))
|
|
|
|
.collect();
|
|
|
|
wrapper.param_ptr_to_hash = param_id_hashes_ptrs
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, hash, ptr)| (*ptr, hash))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
let wrapper: Arc<WrapperInner<P>> = wrapper.into();
|
2022-03-04 07:18:57 +11:00
|
|
|
*wrapper.event_loop.borrow_mut() =
|
2022-02-07 03:50:15 +11:00
|
|
|
MaybeUninit::new(OsEventLoop::new_and_spawn(Arc::downgrade(&wrapper)));
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
}
|
|
|
|
|
2022-03-04 02:33:41 +11:00
|
|
|
pub fn make_gui_context(self: Arc<Self>) -> Arc<WrapperGuiContext<P>> {
|
|
|
|
Arc::new(WrapperGuiContext { inner: self })
|
|
|
|
}
|
|
|
|
|
2022-02-07 03:50:15 +11:00
|
|
|
pub fn make_process_context(&self) -> WrapperProcessContext<'_, P> {
|
|
|
|
WrapperProcessContext {
|
|
|
|
inner: self,
|
2022-03-04 07:18:57 +11:00
|
|
|
input_events_guard: self.input_events.borrow_mut(),
|
2022-02-07 03:50:15 +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.
|
2022-03-02 05:31:16 +11:00
|
|
|
pub fn set_normalized_value_by_hash(
|
2022-02-07 03:50:15 +11:00
|
|
|
&self,
|
|
|
|
hash: u32,
|
|
|
|
normalized_value: f32,
|
|
|
|
sample_rate: Option<f32>,
|
|
|
|
) -> tresult {
|
|
|
|
if hash == *BYPASS_PARAM_HASH {
|
|
|
|
self.bypass_state
|
|
|
|
.store(normalized_value >= 0.5, Ordering::SeqCst);
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else if let Some(param_ptr) = self.param_by_hash.get(&hash) {
|
|
|
|
// Also update the parameter's smoothing if applicable
|
|
|
|
match (param_ptr, sample_rate) {
|
2022-03-02 05:31:16 +11:00
|
|
|
(_, Some(sample_rate)) => unsafe {
|
2022-02-07 03:50:15 +11:00
|
|
|
param_ptr.set_normalized_value(normalized_value);
|
|
|
|
param_ptr.update_smoother(sample_rate, false);
|
2022-03-02 05:31:16 +11:00
|
|
|
},
|
|
|
|
_ => unsafe { param_ptr.set_normalized_value(normalized_value) },
|
2022-02-07 03:50:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kInvalidArgument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 05:21:14 +11:00
|
|
|
impl<P: Vst3Plugin> MainThreadExecutor<Task> for WrapperInner<P> {
|
2022-02-07 03:50:15 +11:00
|
|
|
unsafe fn execute(&self, task: Task) {
|
|
|
|
// This function is always called from the main thread
|
|
|
|
// 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-03-04 07:18:57 +11:00
|
|
|
Task::TriggerRestart(flags) => match &*self.component_handler.borrow() {
|
2022-02-07 03:50:15 +11:00
|
|
|
Some(handler) => {
|
|
|
|
handler.restart_component(flags);
|
|
|
|
}
|
|
|
|
None => nih_debug_assert_failure!("Component handler not yet set"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|