1
0
Fork 0

Add a way to fetch a parameter's default value

This commit is contained in:
Robbert van der Helm 2022-02-08 23:47:41 +01:00
parent 5e65f5b77a
commit 1d6a9aac74
3 changed files with 36 additions and 5 deletions

View file

@ -98,6 +98,16 @@ pub trait GuiContext: Send + Sync + 'static {
/// 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);
/// Retrieve the default value for a parameter, in case you forgot. This does not perform a
/// callback Create a [ParamSetter] and use [ParamSetter::default_param_value] 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_default_normalized_param_value(&self, param: ParamPtr) -> f32;
}
/// A convenience helper for setting parameter values. Any changes made here will be broadcasted to
@ -138,6 +148,16 @@ impl<'a> ParamSetter<'a> {
pub fn end_set_parameter<P: Param>(&self, param: &P) {
unsafe { self.context.raw_end_set_parameter(param.as_ptr()) };
}
/// Retrieve the default value for a parameter, in case you forgot. This is useful when
/// implementing GUIs, and it does not perform a callback.
fn raw_default_normalized_param_value<P: Param>(&self, param: &P) -> P::Plain {
let normalized = unsafe {
self.context
.raw_default_normalized_param_value(param.as_ptr())
};
param.preview_plain(normalized)
}
}
/// A trait describing the functionality of the platform-specific event loop that can execute tasks

View file

@ -88,8 +88,9 @@ pub(crate) struct WrapperInner<P: Plugin> {
/// addresses will remain stable, as they are obtained from a pinned object.
pub param_by_hash: HashMap<u32, ParamPtr>,
/// The default normalized parameter value for every parameter in `param_ids`. We need to store
/// this in case the host requeries the parmaeter later.
pub param_defaults_normalized: Vec<f32>,
/// this in case the host requeries the parmaeter later. This is also indexed by the hash so we
/// can retrieve them later for the UI if needed.
pub param_defaults_normalized: HashMap<u32, f32>,
/// Mappings from string parameter indentifiers to parameter hashes. Useful for debug logging
/// and when storing and restorign plugin state.
pub param_id_to_hash: HashMap<&'static str, u32>,
@ -143,7 +144,7 @@ impl<P: Plugin> WrapperInner<P> {
param_hashes: Vec::new(),
param_by_hash: HashMap::new(),
param_defaults_normalized: Vec::new(),
param_defaults_normalized: HashMap::new(),
param_id_to_hash: HashMap::new(),
param_ptr_to_hash: HashMap::new(),
};
@ -178,7 +179,7 @@ impl<P: Plugin> WrapperInner<P> {
.collect();
wrapper.param_defaults_normalized = param_id_hashes_ptrs
.iter()
.map(|&(_, _, ptr)| unsafe { ptr.normalized_value() })
.map(|&(_, hash, ptr)| (hash, unsafe { ptr.normalized_value() }))
.collect();
wrapper.param_id_to_hash = param_id_hashes_ptrs
.iter()
@ -292,6 +293,16 @@ impl<P: Plugin> GuiContext for WrapperInner<P> {
None => nih_debug_assert_failure!("Component handler not yet set"),
}
}
unsafe fn raw_default_normalized_param_value(&self, param: ParamPtr) -> f32 {
match self.param_ptr_to_hash.get(&param) {
Some(hash) => self.param_defaults_normalized[hash],
None => {
nih_debug_assert_failure!("Unknown parameter: {:?}", param);
0.5
}
}
}
}
impl<P: Plugin> MainThreadExecutor<Task> for WrapperInner<P> {

View file

@ -431,7 +431,7 @@ impl<P: Plugin> IEditController for Wrapper<P> {
| vst3_sys::vst::ParameterFlags::kIsBypass as i32;
} else {
let param_hash = &self.inner.param_hashes[param_index as usize];
let default_value = &self.inner.param_defaults_normalized[param_index as usize];
let default_value = &self.inner.param_defaults_normalized[param_hash];
let param_ptr = &self.inner.param_by_hash[param_hash];
info.id = *param_hash;