From 7ba1a119ba567a67823edc56a7447dce801f3d76 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Mon, 14 Aug 2023 11:24:29 +0200 Subject: [PATCH] Use slider input for volume control --- src/input/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/input/mod.rs b/src/input/mod.rs index e75bdb3..adbdf2d 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -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: + #[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 for SliderInput {