2022-04-23 01:00:59 +10:00
|
|
|
use parking_lot::RwLock;
|
2022-04-23 02:29:00 +10:00
|
|
|
use std::sync::Arc;
|
2022-04-23 01:00:59 +10:00
|
|
|
|
2022-04-23 02:29:00 +10:00
|
|
|
use super::context::WrapperProcessContext;
|
|
|
|
use crate::context::Transport;
|
2022-04-23 02:54:39 +10:00
|
|
|
use crate::plugin::{BufferConfig, BusConfig, Editor, Plugin};
|
2022-04-23 02:29:00 +10:00
|
|
|
|
|
|
|
/// Configuration for a standalone plugin that would normally be provided by the DAW.
|
2022-04-23 02:38:32 +10:00
|
|
|
#[derive(Debug, Clone)]
|
2022-04-23 02:29:00 +10:00
|
|
|
pub struct WrapperConfig {
|
|
|
|
/// The number of input channels.
|
|
|
|
pub input_channels: u32,
|
|
|
|
/// The number of output channels.
|
|
|
|
pub output_channels: u32,
|
|
|
|
/// The audio backend's sample rate.
|
|
|
|
pub sample_rate: f32,
|
|
|
|
/// The audio backend's period size.
|
|
|
|
pub period_size: u32,
|
|
|
|
|
|
|
|
/// The current tempo.
|
|
|
|
pub tempo: f32,
|
|
|
|
/// The time signature's numerator.
|
|
|
|
pub timesig_num: u32,
|
|
|
|
/// The time signature's denominator.
|
|
|
|
pub timesig_denom: u32,
|
|
|
|
}
|
2022-04-23 01:00:59 +10:00
|
|
|
|
|
|
|
pub struct Wrapper<P: Plugin> {
|
|
|
|
/// The wrapped plugin instance.
|
|
|
|
plugin: RwLock<P>,
|
2022-04-23 02:54:39 +10:00
|
|
|
/// The plugin's editor, if it has one. This object does not do anything on its own, but we need
|
|
|
|
/// to instantiate this in advance so we don't need to lock the entire [`Plugin`] object when
|
|
|
|
/// creating an editor.
|
|
|
|
editor: Option<Box<dyn Editor>>,
|
2022-04-23 02:29:00 +10:00
|
|
|
|
|
|
|
config: WrapperConfig,
|
|
|
|
|
|
|
|
/// The bus and buffer configurations are static for the standalone target.
|
|
|
|
bus_config: BusConfig,
|
|
|
|
buffer_config: BufferConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Errors that may arise while initializing the wrapped plugins.
|
2022-04-23 02:54:39 +10:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-04-23 02:29:00 +10:00
|
|
|
pub enum WrapperError {
|
|
|
|
/// The plugin does not accept the IO configuration from the config.
|
|
|
|
IncompatibleConfig,
|
|
|
|
/// The plugin returned `false` during initialization.
|
|
|
|
InitializationFailed,
|
2022-04-23 01:00:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Plugin> Wrapper<P> {
|
2022-04-23 02:29:00 +10:00
|
|
|
/// Instantiate a new instance of the standalone wrapper. Returns an error if the plugin does
|
|
|
|
/// not accept the IO configuration from the wrapper config.
|
|
|
|
pub fn new(config: WrapperConfig) -> Result<Arc<Self>, WrapperError> {
|
2022-04-23 02:54:39 +10:00
|
|
|
let plugin = P::default();
|
|
|
|
let editor = plugin.editor();
|
|
|
|
|
2022-04-23 02:29:00 +10:00
|
|
|
let wrapper = Arc::new(Wrapper {
|
2022-04-23 02:54:39 +10:00
|
|
|
plugin: RwLock::new(plugin),
|
|
|
|
editor,
|
|
|
|
|
2022-04-23 02:29:00 +10:00
|
|
|
bus_config: BusConfig {
|
|
|
|
num_input_channels: config.input_channels,
|
|
|
|
num_output_channels: config.output_channels,
|
|
|
|
},
|
|
|
|
buffer_config: BufferConfig {
|
|
|
|
sample_rate: config.sample_rate,
|
|
|
|
max_buffer_size: config.period_size,
|
|
|
|
},
|
|
|
|
config,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Right now the IO configuration is fixed in the standalone target, so if the plugin cannot
|
|
|
|
// work with this then we cannot initialize the plugin at all.
|
|
|
|
{
|
|
|
|
let mut plugin = wrapper.plugin.write();
|
|
|
|
if !plugin.accepts_bus_config(&wrapper.bus_config) {
|
|
|
|
return Err(WrapperError::IncompatibleConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !plugin.initialize(
|
|
|
|
&wrapper.bus_config,
|
|
|
|
&wrapper.buffer_config,
|
|
|
|
&mut wrapper.make_process_context(Transport::new(wrapper.config.sample_rate)),
|
|
|
|
) {
|
|
|
|
return Err(WrapperError::InitializationFailed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(wrapper)
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:54:39 +10:00
|
|
|
/// Open the editor, start processing audio, and block this thread until the editor is closed.
|
|
|
|
/// If the plugin does not have an editor, then this will block until SIGINT is received.
|
|
|
|
///
|
|
|
|
/// Will return an error if the plugin threw an error during audio processing or if the editor
|
|
|
|
/// could not be opened.
|
|
|
|
pub fn run(self: Arc<Self>) -> Result<(), WrapperError> {
|
|
|
|
// TODO: Open the editor and block until it is closed
|
|
|
|
// TODO: Do IO things
|
|
|
|
// TODO: Block until SIGINT is received if the plugin does not have an editor
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:29:00 +10:00
|
|
|
fn make_process_context(&self, transport: Transport) -> WrapperProcessContext<'_, P> {
|
|
|
|
WrapperProcessContext {
|
|
|
|
wrapper: self,
|
|
|
|
transport,
|
2022-04-23 01:00:59 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|