Skip to content

Commit

Permalink
Use slider input for volume control
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Aug 14, 2023
1 parent df24ee2 commit 7ba1a11
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ impl SliderInput {
position: Self::MAX_POSITION - position,
}
}

/// Interpret the position as a factor for adjusting the volume of a signal.
///
/// Source: <https://stackoverflow.com/questions/49014440/what-is-the-correct-audio-volume-slider-formula>
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn position_as_volume_factor(self, silence_db: f64) -> f32 {
let volume_factor =
10.0f64.powf((1.0 - f64::from(self.position)) * silence_db / 20.0) as f32;
// Still in range after transformation
debug_assert!(Self::POSITION_RANGE.contains(&volume_factor));
volume_factor
}
}

impl From<ControlValue> for SliderInput {
Expand Down

0 comments on commit 7ba1a11

Please sign in to comment.