1
0
Fork 0

Move the Params implementation to CompressorParams

So now CompressorBankParams can just derive Params.
This commit is contained in:
Robbert van der Helm 2022-07-23 18:23:03 +02:00
parent dc97fb1019
commit 4327828e6b

View file

@ -89,18 +89,23 @@ pub struct ThresholdParams {
threshold_db: FloatParam,
}
/// Contains the compressor parameters for both the upwards and downwards compressor banks. This has
/// a manual `Params` trait implementation to avoid copy-pasting the parameters between the two
/// compressors and making mistakes that way
/// Contains the compressor parameters for both the upwards and downwards compressor banks.
#[derive(Params)]
pub struct CompressorBankParams {
downwards: CompressorParams,
upwards: CompressorParams,
#[nested = "downwards"]
pub downwards: CompressorParams,
#[nested = "upwards"]
pub upwards: CompressorParams,
}
/// This struct contains the parameters for either the upward or downward compressors. The `Params`
/// trait is implemented manually to avoid copy-pasting. Both versions will have a parameter ID and
/// a parameter name prefix to distinguish them.
/// trait is implemented manually to avoid copy-pasting parameters for both types of compressor.
/// Both versions will have a parameter ID and a parameter name prefix to distinguish them.
pub struct CompressorParams {
/// The prefix to use in the `.param_map()` function so the upwards and downwards compressors
/// get unique parameter IDs.
param_id_prefix: &'static str,
/// The compression threshold relative to the target curve.
threshold_offset_db: FloatParam,
/// The compression ratio. At 1.0 the compressor is disengaged.
@ -115,16 +120,28 @@ pub struct CompressorParams {
high_freq_ratio_rolloff: FloatParam,
}
unsafe impl Params for CompressorBankParams {
unsafe impl Params for CompressorParams {
fn param_map(&self) -> Vec<(String, ParamPtr, String)> {
// The `Params` trait here is implemented manually as an alternative to copy-pasting all of
// the parameters and potentially making mistakes
let mut param_map = self
.downwards
.param_map_with_prefix("downwards_", "downwards");
param_map.append(&mut self.upwards.param_map_with_prefix("upwards_", "upwards"));
param_map
let prefix = self.param_id_prefix;
vec![
(
format!("{prefix}threshold_offset"),
self.threshold_offset_db.as_ptr(),
// The parent `CompressorBankParams` struct will add the group here
String::new(),
),
(format!("{prefix}ratio"), self.ratio.as_ptr(), String::new()),
(
format!("{prefix}knee"),
self.knee_width_db.as_ptr(),
String::new(),
),
(
format!("{prefix}high_freq_rolloff"),
self.high_freq_ratio_rolloff.as_ptr(),
String::new(),
),
]
}
}
@ -201,11 +218,13 @@ impl CompressorBankParams {
pub fn new(compressor: &CompressorBank) -> Self {
CompressorBankParams {
downwards: CompressorParams::new(
"downwards_",
"Downwards",
compressor.should_update_downwards_thresholds.clone(),
compressor.should_update_downwards_ratios.clone(),
),
upwards: CompressorParams::new(
"upwards_",
"Upwards",
compressor.should_update_upwards_thresholds.clone(),
compressor.should_update_upwards_ratios.clone(),
@ -219,6 +238,7 @@ impl CompressorParams {
/// any of the threshold or ratio parameters causes the passed atomics to be updated. These
/// should be taken from a [`CompressorBank`] so the parameters are linked to it.
pub fn new(
param_id_prefix: &'static str,
name_prefix: &str,
should_update_thresholds: Arc<AtomicBool>,
should_update_ratios: Arc<AtomicBool>,
@ -229,6 +249,8 @@ impl CompressorParams {
Arc::new(move |_| should_update_ratios.store(true, Ordering::SeqCst));
CompressorParams {
param_id_prefix,
// TODO: Set nicer default values for these things
// As explained above, these offsets are relative to the target curve
threshold_offset_db: FloatParam::new(
@ -277,42 +299,6 @@ impl CompressorParams {
.with_step_size(0.1),
}
}
/// Create a parameter map for this object with a given prefix and group name.
///
/// # Safety
///
/// While this function in and of itself it not unsafe (it just creates pointers), these
/// pointers can only be safely dereferences and passed to functions that will derefernce them
/// if this object is not moved.
pub fn param_map_with_prefix(
&self,
prefix: &str,
group: &str,
) -> Vec<(String, ParamPtr, String)> {
vec![
(
format!("{prefix}threshold_offset"),
self.threshold_offset_db.as_ptr(),
String::from(group),
),
(
format!("{prefix}ratio"),
self.ratio.as_ptr(),
String::from(group),
),
(
format!("{prefix}knee"),
self.knee_width_db.as_ptr(),
String::from(group),
),
(
format!("{prefix}high_freq_rolloff"),
self.high_freq_ratio_rolloff.as_ptr(),
String::from(group),
),
]
}
}
impl CompressorBank {