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, threshold_db: FloatParam,
} }
/// Contains the compressor parameters for both the upwards and downwards compressor banks. This has /// Contains the compressor parameters for both the upwards and downwards compressor banks.
/// a manual `Params` trait implementation to avoid copy-pasting the parameters between the two #[derive(Params)]
/// compressors and making mistakes that way
pub struct CompressorBankParams { pub struct CompressorBankParams {
downwards: CompressorParams, #[nested = "downwards"]
upwards: CompressorParams, pub downwards: CompressorParams,
#[nested = "upwards"]
pub upwards: CompressorParams,
} }
/// This struct contains the parameters for either the upward or downward compressors. The `Params` /// 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 /// trait is implemented manually to avoid copy-pasting parameters for both types of compressor.
/// a parameter name prefix to distinguish them. /// Both versions will have a parameter ID and a parameter name prefix to distinguish them.
pub struct CompressorParams { 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. /// The compression threshold relative to the target curve.
threshold_offset_db: FloatParam, threshold_offset_db: FloatParam,
/// The compression ratio. At 1.0 the compressor is disengaged. /// The compression ratio. At 1.0 the compressor is disengaged.
@ -115,16 +120,28 @@ pub struct CompressorParams {
high_freq_ratio_rolloff: FloatParam, high_freq_ratio_rolloff: FloatParam,
} }
unsafe impl Params for CompressorBankParams { unsafe impl Params for CompressorParams {
fn param_map(&self) -> Vec<(String, ParamPtr, String)> { fn param_map(&self) -> Vec<(String, ParamPtr, String)> {
// The `Params` trait here is implemented manually as an alternative to copy-pasting all of let prefix = self.param_id_prefix;
// the parameters and potentially making mistakes vec![
let mut param_map = self (
.downwards format!("{prefix}threshold_offset"),
.param_map_with_prefix("downwards_", "downwards"); self.threshold_offset_db.as_ptr(),
param_map.append(&mut self.upwards.param_map_with_prefix("upwards_", "upwards")); // The parent `CompressorBankParams` struct will add the group here
String::new(),
param_map ),
(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 { pub fn new(compressor: &CompressorBank) -> Self {
CompressorBankParams { CompressorBankParams {
downwards: CompressorParams::new( downwards: CompressorParams::new(
"downwards_",
"Downwards", "Downwards",
compressor.should_update_downwards_thresholds.clone(), compressor.should_update_downwards_thresholds.clone(),
compressor.should_update_downwards_ratios.clone(), compressor.should_update_downwards_ratios.clone(),
), ),
upwards: CompressorParams::new( upwards: CompressorParams::new(
"upwards_",
"Upwards", "Upwards",
compressor.should_update_upwards_thresholds.clone(), compressor.should_update_upwards_thresholds.clone(),
compressor.should_update_upwards_ratios.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 /// 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. /// should be taken from a [`CompressorBank`] so the parameters are linked to it.
pub fn new( pub fn new(
param_id_prefix: &'static str,
name_prefix: &str, name_prefix: &str,
should_update_thresholds: Arc<AtomicBool>, should_update_thresholds: Arc<AtomicBool>,
should_update_ratios: Arc<AtomicBool>, should_update_ratios: Arc<AtomicBool>,
@ -229,6 +249,8 @@ impl CompressorParams {
Arc::new(move |_| should_update_ratios.store(true, Ordering::SeqCst)); Arc::new(move |_| should_update_ratios.store(true, Ordering::SeqCst));
CompressorParams { CompressorParams {
param_id_prefix,
// TODO: Set nicer default values for these things // TODO: Set nicer default values for these things
// As explained above, these offsets are relative to the target curve // As explained above, these offsets are relative to the target curve
threshold_offset_db: FloatParam::new( threshold_offset_db: FloatParam::new(
@ -277,42 +299,6 @@ impl CompressorParams {
.with_step_size(0.1), .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 { impl CompressorBank {