use sim_kernel::{Error, Result};
use sim_lib_audio_dsp::Gain;
use sim_lib_plugin_core::{PluginFormat, PluginInstance, PluginLoadSpec};
use crate::{ClapExportedProcessor, export_gain_as_clap};
pub trait ClapHostProvider {
type Plugin: PluginInstance;
fn instantiate(&self, spec: &PluginLoadSpec) -> Result<Self::Plugin>;
}
#[derive(Clone, Debug, PartialEq)]
pub struct FixtureClapHostProvider {
location: String,
gain: f32,
}
impl FixtureClapHostProvider {
pub fn gain() -> Self {
Self {
location: "fixture://gain".to_owned(),
gain: 0.5,
}
}
pub fn with_gain(mut self, gain: f32) -> Self {
self.gain = gain;
self
}
pub fn with_location(mut self, location: impl Into<String>) -> Self {
self.location = location.into();
self
}
pub fn location(&self) -> &str {
&self.location
}
}
impl ClapHostProvider for FixtureClapHostProvider {
type Plugin = ClapExportedProcessor<Gain>;
fn instantiate(&self, spec: &PluginLoadSpec) -> Result<Self::Plugin> {
spec.require_format(PluginFormat::Clap)?;
if spec.location() != self.location {
return Err(Error::Eval(format!(
"CLAP fixture location '{}' is not available",
spec.location()
)));
}
export_gain_as_clap(self.gain)
}
}