Skip to main content

sim_lib_plugin_vst3/
adapter.rs

1use sim_kernel::Result;
2use sim_lib_audio_dsp::Gain;
3use sim_lib_audio_graph_core::{PrepareConfig, ProcessBlock, Processor};
4use sim_lib_plugin_core::{PluginDescriptor, PluginInstance, PluginState, ProcessorPlugin};
5
6use crate::{Vst3ParamMap, vst3_gain_descriptor};
7
8/// A graph [`Processor`] wrapped as a VST3-shaped plugin instance.
9///
10/// Pairs a `sim-lib-plugin-core` `ProcessorPlugin` with a [`Vst3ParamMap`] that
11/// translates host-facing VST3 parameter ids into the SIM parameter ids the
12/// inner processor understands. The wrapper implements `PluginInstance`, so it
13/// can be driven through the shared plugin-host contract.
14#[derive(Clone, Debug)]
15pub struct Vst3ExportedProcessor<P> {
16    inner: ProcessorPlugin<P>,
17    param_map: Vst3ParamMap,
18}
19
20impl<P> Vst3ExportedProcessor<P> {
21    /// Wraps `processor` under `descriptor`, using `param_map` for VST3-to-SIM
22    /// parameter id translation.
23    pub fn new(descriptor: PluginDescriptor, processor: P, param_map: Vst3ParamMap) -> Self {
24        Self {
25            inner: ProcessorPlugin::new(descriptor, processor),
26            param_map,
27        }
28    }
29
30    /// Returns the VST3-to-SIM parameter id map for this exported processor.
31    pub fn param_map(&self) -> &Vst3ParamMap {
32        &self.param_map
33    }
34}
35
36sim_lib_plugin_core::forward_plugin_instance!(Vst3ExportedProcessor);
37
38/// Exports any graph [`Processor`] as a VST3-shaped plugin instance.
39///
40/// Builds an identity [`Vst3ParamMap`] from `descriptor`'s parameter ids (each
41/// VST3 id maps to the matching SIM id) and wraps `processor` in a
42/// [`Vst3ExportedProcessor`].
43pub fn export_processor_as_vst3<P: Processor>(
44    descriptor: PluginDescriptor,
45    processor: P,
46) -> Vst3ExportedProcessor<P> {
47    let param_map = Vst3ParamMap::identity(descriptor.parameters.iter().map(|param| param.id));
48    Vst3ExportedProcessor::new(descriptor, processor, param_map)
49}
50
51/// Exports the built-in gain processor as a VST3-shaped plugin instance.
52///
53/// Uses the [`vst3_gain_descriptor`](crate::vst3_gain_descriptor) fixture and a
54/// `Gain` processor initialized to `gain`. Returns an error if the descriptor
55/// fails to build.
56pub fn export_gain_as_vst3(gain: f32) -> Result<Vst3ExportedProcessor<Gain>> {
57    Ok(export_processor_as_vst3(
58        vst3_gain_descriptor()?,
59        Gain::new(gain),
60    ))
61}