1
0
Fork 0
nih-plug/src/context.rs

181 lines
8.2 KiB
Rust
Raw Normal View History

// 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/>.
//! Different contexts the plugin can use to make callbacks to the host in different...contexts.
use std::sync::Weak;
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
mod linux;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
pub(crate) use self::linux::LinuxEventLoop as OsEventLoop;
#[cfg(target_os = "windows")]
pub(crate) use self::windows::WindowsEventLoop as OsEventLoop;
#[cfg(target_os = "macos")]
compile_error!("The macOS event loop has not yet been implemented");
use crate::param::internals::ParamPtr;
2022-02-05 22:46:26 +11:00
use crate::param::Param;
use crate::plugin::NoteEvent;
2022-02-01 10:01:43 +11:00
pub(crate) const TASK_QUEUE_CAPACITY: usize = 512;
// TODO: ProcessContext for parameter automation and sending events
/// General callbacks the plugin can make during its lifetime. This is passed to the plugin during
2022-02-05 22:56:03 +11:00
/// [crate::plugin::Plugin::initialize()] and as part of [crate::plugin::Plugin::process()].
//
// # Safety
//
// The implementing wrapper needs to be able to handle concurrent requests, and it should perform
// the actual callback within [MainThreadQueue::do_maybe_async].
pub trait ProcessContext {
/// Update the current latency of the plugin. If the plugin is currently processing audio, then
/// this may cause audio playback to be restarted.
fn set_latency_samples(&self, samples: u32);
2022-02-01 10:01:43 +11:00
/// Return the next note event, if there is one. The event contains the timing
///
/// TODO: Rethink this API, both in terms of ergonomics, and if we can do this in a way that
/// doesn't require locks (because of the thread safe-ness, which we don't really need
/// here)
fn next_midi_event(&mut self) -> Option<NoteEvent>;
2022-02-05 22:46:26 +11:00
// TODO: Add this, this works similar to [GuiContext::set_parameter] but it adds the parameter
// change to a queue (or directly to the VST3 plugin's parameter output queues) instead of
// using main thread host automation (and all the locks involved there).
// fn set_parameter<P: Param>(&self, param: &P, value: P::Plain);
}
/// Callbacks the plugin can make when the user interacts with its GUI such as updating parameter
/// values. This is passed to the plugin during [crate::plugin::Plugin::create_editor()]. All of
/// these functions assume they're being called from the main GUI thread.
2022-02-05 22:46:26 +11:00
//
// # Safety
//
// The implementing wrapper can assume that everything is being called from the main thread. Since
// NIH-plug doesn't own the GUI event loop, this invariant cannot be part of the interface.
pub trait GuiContext: Send + Sync + 'static {
/// Inform the host a parameter will be automated. Create a [ParamSetter] and use
/// [ParamSetter::begin_set_parameter] instead for a safe, user friendly API.
///
/// # Safety
///
/// The implementing function still needs to check if `param` actually exists. This function is
/// mostly marked as unsafe for API reasons.
unsafe fn raw_begin_set_parameter(&self, param: ParamPtr);
/// Inform the host a parameter is being automated with an already normalized value. Create a
/// [ParamSetter] and use [ParamSetter::set_parameter] instead for a safe, user friendly API.
///
/// # Safety
///
/// The implementing function still needs to check if `param` actually exists. This function is
/// mostly marked as unsafe for API reasons.
unsafe fn raw_set_parameter_normalized(&self, param: ParamPtr, normalized: f32);
/// Inform the host a parameter has been automated. Create a [ParamSetter] and use
/// [ParamSetter::end_set_parameter] instead for a safe, user friendly API.
///
/// # Safety
///
/// The implementing function still needs to check if `param` actually exists. This function is
/// mostly marked as unsafe for API reasons.
unsafe fn raw_end_set_parameter(&self, param: ParamPtr);
}
/// A convenience helper for setting parameter values. Any changes made here will be broadcasted to
2022-02-06 09:34:38 +11:00
/// the host and reflected in the plugin's [crate::param::internals::Params] object. These functions
/// should only be called from the main thread.
pub struct ParamSetter<'a> {
context: &'a dyn GuiContext,
}
impl<'a> ParamSetter<'a> {
pub fn new(context: &'a dyn GuiContext) -> Self {
Self { context }
}
2022-02-05 22:46:26 +11:00
/// Inform the host that you will start automating a parmater. This needs to be called before
2022-02-05 22:52:29 +11:00
/// calling [Self::set_parameter()] for the specified parameter.
pub fn begin_set_parameter<P: Param>(&self, param: &P) {
2022-02-06 03:32:57 +11:00
unsafe { self.context.raw_begin_set_parameter(param.as_ptr()) };
}
2022-02-05 22:46:26 +11:00
/// Set a parameter to the specified parameter value. You will need to call
2022-02-05 22:52:29 +11:00
/// [Self::begin_set_parameter()] before and [Self::end_set_parameter()] after calling this so
/// the host can properly record automation for the parameter. This can be called multiple times
/// in a row before calling [Self::end_set_parameter()], for instance when moving a slider
/// around.
2022-02-05 22:46:26 +11:00
///
/// This function assumes you're already calling this from a GUI thread. Calling any of these
/// functions from any other thread may result in unexpected behavior.
pub fn set_parameter<P: Param>(&self, param: &P, value: P::Plain) {
2022-02-06 03:32:57 +11:00
let ptr = param.as_ptr();
let normalized = param.preview_normalized(value);
unsafe { self.context.raw_set_parameter_normalized(ptr, normalized) };
}
2022-02-05 22:46:26 +11:00
/// Inform the host that you are done automating a parameter. This needs to be called after one
2022-02-05 22:52:29 +11:00
/// or more [Self::set_parameter()] calls for a parameter so the host knows the automation
/// gesture has finished.
pub fn end_set_parameter<P: Param>(&self, param: &P) {
2022-02-06 03:32:57 +11:00
unsafe { self.context.raw_end_set_parameter(param.as_ptr()) };
}
}
/// A trait describing the functionality of the platform-specific event loop that can execute tasks
2022-02-01 10:01:43 +11:00
/// of type `T` in executor `E`. Posting a task to the internal task queue should be realtime safe.
/// This event loop should be created during the wrapper's initial initialization on the main
/// thread.
///
/// This is never used generically, but having this as a trait will cause any missing functions on
/// an implementation to show up as compiler errors even when using a different platform. And since
/// the tasks and executor will be sent to a thread, they need to have static lifetimes.
///
/// TODO: At some point rethink the design to make it possible to have a singleton message queue for
/// all instances of a plugin.
pub(crate) trait EventLoop<T, E>
where
T: Send + 'static,
E: MainThreadExecutor<T> + 'static,
{
2022-02-01 10:01:43 +11:00
/// Create and start a new event loop. The thread this is called on will be designated as the
/// main thread, so this should be called when constructing the wrapper.
fn new_and_spawn(executor: Weak<E>) -> Self;
2022-02-01 10:01:43 +11:00
/// Either post the function to the task queue so it can be delegated to the main thread, or
/// execute the task directly if this is the main thread. This function needs to be callable at
/// any time without blocking.
///
2022-02-01 10:01:43 +11:00
/// If the task queue is full, then this will return false.
#[must_use]
fn do_maybe_async(&self, task: T) -> bool;
2022-02-01 10:01:43 +11:00
/// Whether the calling thread is the event loop's main thread. This is usually the thread the
/// event loop instance was initialized on.
fn is_main_thread(&self) -> bool;
}
/// Something that can execute tasks of type `T`.
pub(crate) trait MainThreadExecutor<T>: Send + Sync {
/// Execute a task on the current thread. This shoudl only be called from the main thread.
unsafe fn execute(&self, task: T);
}