Decouple state functions from CLAP/VST3 wrapper
This should also be usable with a plain `params` object, since we don't need hashes in the standalone wrapper.
This commit is contained in:
parent
9acd312768
commit
566095802c
4 changed files with 65 additions and 67 deletions
|
@ -1358,8 +1358,7 @@ impl<P: ClapPlugin> Wrapper<P> {
|
||||||
unsafe {
|
unsafe {
|
||||||
state::serialize_object(
|
state::serialize_object(
|
||||||
self.params.clone(),
|
self.params.clone(),
|
||||||
&self.param_by_hash,
|
state::make_params_iter(&self.param_by_hash, &self.param_id_to_hash),
|
||||||
&self.param_id_to_hash,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1404,8 +1403,7 @@ impl<P: ClapPlugin> Wrapper<P> {
|
||||||
state::deserialize_object(
|
state::deserialize_object(
|
||||||
&state,
|
&state,
|
||||||
self.params.clone(),
|
self.params.clone(),
|
||||||
&self.param_by_hash,
|
state::make_params_getter(&self.param_by_hash, &self.param_id_to_hash),
|
||||||
&self.param_id_to_hash,
|
|
||||||
self.current_buffer_config.load().as_ref(),
|
self.current_buffer_config.load().as_ref(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1788,8 +1786,7 @@ impl<P: ClapPlugin> Wrapper<P> {
|
||||||
state::deserialize_object(
|
state::deserialize_object(
|
||||||
&state,
|
&state,
|
||||||
wrapper.params.clone(),
|
wrapper.params.clone(),
|
||||||
&wrapper.param_by_hash,
|
state::make_params_getter(&wrapper.param_by_hash, &wrapper.param_id_to_hash),
|
||||||
&wrapper.param_id_to_hash,
|
|
||||||
wrapper.current_buffer_config.load().as_ref(),
|
wrapper.current_buffer_config.load().as_ref(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2540,8 +2537,7 @@ impl<P: ClapPlugin> Wrapper<P> {
|
||||||
|
|
||||||
let serialized = state::serialize_json(
|
let serialized = state::serialize_json(
|
||||||
wrapper.params.clone(),
|
wrapper.params.clone(),
|
||||||
&wrapper.param_by_hash,
|
state::make_params_iter(&wrapper.param_by_hash, &wrapper.param_id_to_hash),
|
||||||
&wrapper.param_id_to_hash,
|
|
||||||
);
|
);
|
||||||
match serialized {
|
match serialized {
|
||||||
Ok(serialized) => {
|
Ok(serialized) => {
|
||||||
|
@ -2600,8 +2596,7 @@ impl<P: ClapPlugin> Wrapper<P> {
|
||||||
let success = state::deserialize_json(
|
let success = state::deserialize_json(
|
||||||
&read_buffer,
|
&read_buffer,
|
||||||
wrapper.params.clone(),
|
wrapper.params.clone(),
|
||||||
&wrapper.param_by_hash,
|
state::make_params_getter(&wrapper.param_by_hash, &wrapper.param_id_to_hash),
|
||||||
&wrapper.param_id_to_hash,
|
|
||||||
wrapper.current_buffer_config.load().as_ref(),
|
wrapper.current_buffer_config.load().as_ref(),
|
||||||
);
|
);
|
||||||
if !success {
|
if !success {
|
||||||
|
|
|
@ -38,37 +38,51 @@ pub struct PluginState {
|
||||||
pub fields: HashMap<String, String>,
|
pub fields: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a parameters iterator from the hashtables stored in the plugin wrappers. This avoids
|
||||||
|
/// having to call `.param_map()` again, which may include expensive user written code.
|
||||||
|
pub(crate) fn make_params_iter<'a>(
|
||||||
|
param_by_hash: &'a HashMap<u32, ParamPtr>,
|
||||||
|
param_id_to_hash: &'a HashMap<String, u32>,
|
||||||
|
) -> impl IntoIterator<Item = (&'a String, ParamPtr)> {
|
||||||
|
param_id_to_hash.iter().filter_map(|(param_id_str, hash)| {
|
||||||
|
let param_ptr = param_by_hash.get(hash)?;
|
||||||
|
Some((param_id_str, *param_ptr))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a getter function that gets a parameter from the hashtables stored in the plugin by
|
||||||
|
/// string ID.
|
||||||
|
pub(crate) fn make_params_getter<'a>(
|
||||||
|
param_by_hash: &'a HashMap<u32, ParamPtr>,
|
||||||
|
param_id_to_hash: &'a HashMap<String, u32>,
|
||||||
|
) -> impl for<'b> Fn(&'b str) -> Option<ParamPtr> + 'a {
|
||||||
|
|param_id_str| {
|
||||||
|
param_id_to_hash
|
||||||
|
.get(param_id_str)
|
||||||
|
.and_then(|hash| param_by_hash.get(hash))
|
||||||
|
.copied()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Serialize a plugin's state to a state object. This is separate from [`serialize_json()`] to
|
/// Serialize a plugin's state to a state object. This is separate from [`serialize_json()`] to
|
||||||
/// allow passing the raw object directly to the plugin.
|
/// allow passing the raw object directly to the plugin. The parameters are not pulled directly from
|
||||||
pub(crate) unsafe fn serialize_object(
|
/// `plugin_params` by default to avoid unnecessary allocations in the `.param_map()` method, as the
|
||||||
|
/// plugin wrappers will already have a list of parameters handy. See [`make_params_iter()`].
|
||||||
|
pub(crate) unsafe fn serialize_object<'a>(
|
||||||
plugin_params: Arc<dyn Params>,
|
plugin_params: Arc<dyn Params>,
|
||||||
param_by_hash: &HashMap<u32, ParamPtr>,
|
params_iter: impl IntoIterator<Item = (&'a String, ParamPtr)>,
|
||||||
param_id_to_hash: &HashMap<String, u32>,
|
|
||||||
) -> PluginState {
|
) -> PluginState {
|
||||||
// We'll serialize parameter values as a simple `string_param_id: display_value` map.
|
// We'll serialize parameter values as a simple `string_param_id: display_value` map.
|
||||||
let params: HashMap<_, _> = param_id_to_hash
|
let params: HashMap<_, _> = params_iter
|
||||||
.iter()
|
.into_iter()
|
||||||
.filter_map(|(param_id_str, hash)| {
|
.map(|(param_id_str, param_ptr)| match param_ptr {
|
||||||
let param_ptr = param_by_hash.get(hash)?;
|
ParamPtr::FloatParam(p) => (param_id_str.clone(), ParamValue::F32((*p).plain_value())),
|
||||||
Some((param_id_str, param_ptr))
|
ParamPtr::IntParam(p) => (param_id_str.clone(), ParamValue::I32((*p).plain_value())),
|
||||||
})
|
ParamPtr::BoolParam(p) => (param_id_str.clone(), ParamValue::Bool((*p).plain_value())),
|
||||||
.map(|(param_id_str, ¶m_ptr)| match param_ptr {
|
|
||||||
ParamPtr::FloatParam(p) => (
|
|
||||||
param_id_str.to_string(),
|
|
||||||
ParamValue::F32((*p).plain_value()),
|
|
||||||
),
|
|
||||||
ParamPtr::IntParam(p) => (
|
|
||||||
param_id_str.to_string(),
|
|
||||||
ParamValue::I32((*p).plain_value()),
|
|
||||||
),
|
|
||||||
ParamPtr::BoolParam(p) => (
|
|
||||||
param_id_str.to_string(),
|
|
||||||
ParamValue::Bool((*p).plain_value()),
|
|
||||||
),
|
|
||||||
ParamPtr::EnumParam(p) => (
|
ParamPtr::EnumParam(p) => (
|
||||||
// Enums are serialized based on the active variant's index (which may not be
|
// Enums are serialized based on the active variant's index (which may not be
|
||||||
// the same as the discriminator)
|
// the same as the discriminator)
|
||||||
param_id_str.to_string(),
|
param_id_str.clone(),
|
||||||
ParamValue::I32((*p).plain_value()),
|
ParamValue::I32((*p).plain_value()),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
@ -83,12 +97,11 @@ pub(crate) unsafe fn serialize_object(
|
||||||
|
|
||||||
/// Serialize a plugin's state to a vector containing JSON data. This can (and should) be shared
|
/// Serialize a plugin's state to a vector containing JSON data. This can (and should) be shared
|
||||||
/// across plugin formats.
|
/// across plugin formats.
|
||||||
pub(crate) unsafe fn serialize_json(
|
pub(crate) unsafe fn serialize_json<'a>(
|
||||||
plugin_params: Arc<dyn Params>,
|
plugin_params: Arc<dyn Params>,
|
||||||
param_by_hash: &HashMap<u32, ParamPtr>,
|
params_iter: impl IntoIterator<Item = (&'a String, ParamPtr)>,
|
||||||
param_id_to_hash: &HashMap<String, u32>,
|
|
||||||
) -> serde_json::Result<Vec<u8>> {
|
) -> serde_json::Result<Vec<u8>> {
|
||||||
let plugin_state = serialize_object(plugin_params, param_by_hash, param_id_to_hash);
|
let plugin_state = serialize_object(plugin_params, params_iter);
|
||||||
serde_json::to_vec(&plugin_state)
|
serde_json::to_vec(&plugin_state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,21 +109,20 @@ pub(crate) unsafe fn serialize_json(
|
||||||
/// do its own internal preset management. Returns `false` and logs an error if the state could not
|
/// do its own internal preset management. Returns `false` and logs an error if the state could not
|
||||||
/// be deserialized.
|
/// be deserialized.
|
||||||
///
|
///
|
||||||
|
/// This uses a parameter getter function to avoid having to rebuild the parameter map, which may
|
||||||
|
/// include expensive user written code. See [`make_params_getter()`].
|
||||||
|
///
|
||||||
/// Make sure to reinitialize plugin after deserializing the state so it can react to the new
|
/// Make sure to reinitialize plugin after deserializing the state so it can react to the new
|
||||||
/// parameter values. The smoothers have already been reset by this function.
|
/// parameter values. The smoothers have already been reset by this function.
|
||||||
pub(crate) unsafe fn deserialize_object(
|
pub(crate) unsafe fn deserialize_object(
|
||||||
state: &PluginState,
|
state: &PluginState,
|
||||||
plugin_params: Arc<dyn Params>,
|
plugin_params: Arc<dyn Params>,
|
||||||
param_by_hash: &HashMap<u32, ParamPtr>,
|
params_getter: impl for<'a> Fn(&'a str) -> Option<ParamPtr>,
|
||||||
param_id_to_hash: &HashMap<String, u32>,
|
|
||||||
current_buffer_config: Option<&BufferConfig>,
|
current_buffer_config: Option<&BufferConfig>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let sample_rate = current_buffer_config.map(|c| c.sample_rate);
|
let sample_rate = current_buffer_config.map(|c| c.sample_rate);
|
||||||
for (param_id_str, param_value) in &state.params {
|
for (param_id_str, param_value) in &state.params {
|
||||||
let param_ptr = match param_id_to_hash
|
let param_ptr = match params_getter(param_id_str.as_str()) {
|
||||||
.get(param_id_str.as_str())
|
|
||||||
.and_then(|hash| param_by_hash.get(hash))
|
|
||||||
{
|
|
||||||
Some(ptr) => ptr,
|
Some(ptr) => ptr,
|
||||||
None => {
|
None => {
|
||||||
nih_debug_assert_failure!("Unknown parameter: {}", param_id_str);
|
nih_debug_assert_failure!("Unknown parameter: {}", param_id_str);
|
||||||
|
@ -119,13 +131,13 @@ pub(crate) unsafe fn deserialize_object(
|
||||||
};
|
};
|
||||||
|
|
||||||
match (param_ptr, param_value) {
|
match (param_ptr, param_value) {
|
||||||
(ParamPtr::FloatParam(p), ParamValue::F32(v)) => (**p).set_plain_value(*v),
|
(ParamPtr::FloatParam(p), ParamValue::F32(v)) => (*p).set_plain_value(*v),
|
||||||
(ParamPtr::IntParam(p), ParamValue::I32(v)) => (**p).set_plain_value(*v),
|
(ParamPtr::IntParam(p), ParamValue::I32(v)) => (*p).set_plain_value(*v),
|
||||||
(ParamPtr::BoolParam(p), ParamValue::Bool(v)) => (**p).set_plain_value(*v),
|
(ParamPtr::BoolParam(p), ParamValue::Bool(v)) => (*p).set_plain_value(*v),
|
||||||
// Enums are serialized based on the active variant's index (which may not be the same
|
// Enums are serialized based on the active variant's index (which may not be the same
|
||||||
// as the discriminator)
|
// as the discriminator)
|
||||||
(ParamPtr::EnumParam(p), ParamValue::I32(variant_idx)) => {
|
(ParamPtr::EnumParam(p), ParamValue::I32(variant_idx)) => {
|
||||||
(**p).set_plain_value(*variant_idx)
|
(*p).set_plain_value(*variant_idx)
|
||||||
}
|
}
|
||||||
(param_ptr, param_value) => {
|
(param_ptr, param_value) => {
|
||||||
nih_debug_assert_failure!(
|
nih_debug_assert_failure!(
|
||||||
|
@ -158,8 +170,7 @@ pub(crate) unsafe fn deserialize_object(
|
||||||
pub(crate) unsafe fn deserialize_json(
|
pub(crate) unsafe fn deserialize_json(
|
||||||
state: &[u8],
|
state: &[u8],
|
||||||
plugin_params: Arc<dyn Params>,
|
plugin_params: Arc<dyn Params>,
|
||||||
param_by_hash: &HashMap<u32, ParamPtr>,
|
params_getter: impl for<'a> Fn(&'a str) -> Option<ParamPtr>,
|
||||||
param_id_to_hash: &HashMap<String, u32>,
|
|
||||||
current_buffer_config: Option<&BufferConfig>,
|
current_buffer_config: Option<&BufferConfig>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let state: PluginState = match serde_json::from_slice(state) {
|
let state: PluginState = match serde_json::from_slice(state) {
|
||||||
|
@ -170,11 +181,5 @@ pub(crate) unsafe fn deserialize_json(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
deserialize_object(
|
deserialize_object(&state, plugin_params, params_getter, current_buffer_config)
|
||||||
&state,
|
|
||||||
plugin_params,
|
|
||||||
param_by_hash,
|
|
||||||
param_id_to_hash,
|
|
||||||
current_buffer_config,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -391,8 +391,7 @@ impl<P: Vst3Plugin> WrapperInner<P> {
|
||||||
unsafe {
|
unsafe {
|
||||||
state::serialize_object(
|
state::serialize_object(
|
||||||
self.params.clone(),
|
self.params.clone(),
|
||||||
&self.param_by_hash,
|
state::make_params_iter(&self.param_by_hash, &self.param_id_to_hash),
|
||||||
&self.param_id_to_hash,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -437,8 +436,7 @@ impl<P: Vst3Plugin> WrapperInner<P> {
|
||||||
state::deserialize_object(
|
state::deserialize_object(
|
||||||
&state,
|
&state,
|
||||||
self.params.clone(),
|
self.params.clone(),
|
||||||
&self.param_by_hash,
|
state::make_params_getter(&self.param_by_hash, &self.param_id_to_hash),
|
||||||
&self.param_id_to_hash,
|
|
||||||
self.current_buffer_config.load().as_ref(),
|
self.current_buffer_config.load().as_ref(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -278,8 +278,7 @@ impl<P: Vst3Plugin> IComponent for Wrapper<P> {
|
||||||
let success = state::deserialize_json(
|
let success = state::deserialize_json(
|
||||||
&read_buffer,
|
&read_buffer,
|
||||||
self.inner.params.clone(),
|
self.inner.params.clone(),
|
||||||
&self.inner.param_by_hash,
|
state::make_params_getter(&self.inner.param_by_hash, &self.inner.param_id_to_hash),
|
||||||
&self.inner.param_id_to_hash,
|
|
||||||
self.inner.current_buffer_config.load().as_ref(),
|
self.inner.current_buffer_config.load().as_ref(),
|
||||||
);
|
);
|
||||||
if !success {
|
if !success {
|
||||||
|
@ -312,8 +311,7 @@ impl<P: Vst3Plugin> IComponent for Wrapper<P> {
|
||||||
|
|
||||||
let serialized = state::serialize_json(
|
let serialized = state::serialize_json(
|
||||||
self.inner.params.clone(),
|
self.inner.params.clone(),
|
||||||
&self.inner.param_by_hash,
|
state::make_params_iter(&self.inner.param_by_hash, &self.inner.param_id_to_hash),
|
||||||
&self.inner.param_id_to_hash,
|
|
||||||
);
|
);
|
||||||
match serialized {
|
match serialized {
|
||||||
Ok(serialized) => {
|
Ok(serialized) => {
|
||||||
|
@ -1243,8 +1241,10 @@ impl<P: Vst3Plugin> IAudioProcessor for Wrapper<P> {
|
||||||
state::deserialize_object(
|
state::deserialize_object(
|
||||||
&state,
|
&state,
|
||||||
self.inner.params.clone(),
|
self.inner.params.clone(),
|
||||||
&self.inner.param_by_hash,
|
state::make_params_getter(
|
||||||
&self.inner.param_id_to_hash,
|
&self.inner.param_by_hash,
|
||||||
|
&self.inner.param_id_to_hash,
|
||||||
|
),
|
||||||
self.inner.current_buffer_config.load().as_ref(),
|
self.inner.current_buffer_config.load().as_ref(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue