Skip to content

Commit

Permalink
Add more positional mappings for sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Aug 19, 2023
1 parent 19ee9e0 commit dcda0cd
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,25 @@ impl SliderInput {
}
}

#[must_use]
pub fn map_position_linear(self, min_value: f32, max_value: f32) -> f32 {
let Self { position } = self;
min_value + position * (max_value - min_value)
}

/// Interpret the position as a ratio for adjusting the volume of a signal.
///
/// The position is interpreted as a volume level between the silence level
/// (< 0 dB) and 0 dB.
///
/// Multiply the signal with the returned value to adjust the volume.
#[must_use]
#[inline]
#[allow(clippy::cast_possible_truncation)]
pub fn position_as_gain_ratio(self, silence_db: f64) -> f32 {
pub fn map_position_to_gain_ratio(self, silence_db: f32) -> f32 {
debug_assert!(silence_db < 0.0);
let gain_ratio = db_to_ratio((1.0 - f64::from(self.position)) * silence_db) as f32;
let Self { position } = self;
let gain_ratio = db_to_ratio((1.0 - position) * silence_db);
// Still in range after transformation
debug_assert!(Self::POSITION_RANGE.contains(&gain_ratio));
gain_ratio
Expand Down Expand Up @@ -238,32 +246,45 @@ impl CenterSliderInput {
}
}

#[must_use]
#[inline]
pub fn map_position_linear(self, min_value: f32, center_value: f32, max_value: f32) -> f32 {
debug_assert!(
(min_value <= center_value && center_value <= max_value)
|| (min_value >= center_value && center_value >= max_value)
);
let Self { position } = self;
match position
.partial_cmp(&Self::CENTER_POSITION)
.unwrap_or(Ordering::Equal)
{
Ordering::Equal => center_value,
Ordering::Less => -position * (center_value - min_value) + min_value,
Ordering::Greater => position * (max_value - center_value) + center_value,
}
}

/// Interpret the position as a ratio for tuning the volume of a signal.
///
/// The position is interpreted as a volume level between the the `min_db`
/// (< 0 dB) and `max_db` (> 0 dB), e.g. -26 dB and +6 dB (Pioneer DJM).
///
/// Multiply the signal with the returned value to tune the volume.
#[must_use]
#[inline]
#[allow(clippy::cast_possible_truncation)]
pub fn position_as_gain_ratio(self, min_db: f64, max_db: f64) -> f32 {
pub fn map_position_to_gain_ratio(self, min_db: f32, max_db: f32) -> f32 {
debug_assert!(min_db < 0.0);
debug_assert!(max_db > 0.0);
debug_assert!(min_db < max_db);
match self
.position
let Self { position } = self;
match position
.partial_cmp(&Self::CENTER_POSITION)
.unwrap_or(Ordering::Equal)
{
Ordering::Equal => 1.0,
Ordering::Less => {
let gain_ratio = db_to_ratio(f64::from(-self.position) * min_db) as f32;
gain_ratio
}
Ordering::Greater => {
let gain_ratio = db_to_ratio(f64::from(self.position) * max_db) as f32;
gain_ratio
}
Ordering::Less => db_to_ratio(-position * min_db),
Ordering::Greater => db_to_ratio(position * max_db),
}
}
}
Expand Down Expand Up @@ -554,8 +575,8 @@ impl CrossfaderCurve {
}

#[inline]
fn db_to_ratio(gain: f64) -> f64 {
10.0f64.powf(gain / 20.0)
fn db_to_ratio(gain: f32) -> f32 {
10.0f32.powf(gain / 20.0)
}

#[cfg(test)]
Expand Down

0 comments on commit dcda0cd

Please sign in to comment.