From cc73fa5a37e8715df9496d14bf39c5de9b9a9523 Mon Sep 17 00:00:00 2001 From: Tal <83217276+talwat@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:24:22 +0200 Subject: [PATCH] feat: also clamp volume in mpris --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/player.rs | 11 +++++++---- src/player/mpris.rs | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7767935..b7e5260 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1312,7 +1312,7 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "lowfi" -version = "1.4.0" +version = "1.4.1" dependencies = [ "Inflector", "arc-swap", diff --git a/Cargo.toml b/Cargo.toml index 841146b..2560555 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lowfi" -version = "1.4.0" +version = "1.4.1" edition = "2021" description = "An extremely simple lofi player." license = "MIT" diff --git a/src/player.rs b/src/player.rs index ea2189b..dc0a231 100644 --- a/src/player.rs +++ b/src/player.rs @@ -137,6 +137,11 @@ impl Player { self.current.load().is_some() } + /// Sets the volume of the sink, and also clamps the value to avoid negative/over 100% values. + pub fn set_volume(&self, volume: f32) { + self.sink.set_volume(volume.clamp(0.0, 1.0)) + } + /// Initializes the entire player, including audio devices & sink. /// /// `silent` can control whether alsa's output should be redirected, @@ -256,7 +261,7 @@ impl Player { Downloader::notify(&itx).await?; // Set the initial sink volume to the one specified. - player.sink.set_volume(properties.volume as f32 / 100.0); + player.set_volume(properties.volume as f32 / 100.0); // Whether the last signal was a `NewSong`. // This is helpful, since we only want to autoplay @@ -308,9 +313,7 @@ impl Player { } } Messages::ChangeVolume(change) => { - player - .sink - .set_volume((player.sink.volume() + change).clamp(0.0, 1.0)); + player.set_volume(player.sink.volume() + change); } // This basically just continues, but more importantly, it'll re-evaluate // the select macro at the beginning of the loop. diff --git a/src/player/mpris.rs b/src/player/mpris.rs index 420ef08..15e859e 100644 --- a/src/player/mpris.rs +++ b/src/player/mpris.rs @@ -169,7 +169,7 @@ impl PlayerInterface for Player { } async fn set_volume(&self, volume: Volume) -> Result<()> { - self.player.sink.set_volume(volume as f32); + self.player.set_volume(volume as f32); Ok(()) }