From 9389eeea0026d89ad4137fefc5fc0fdda7aae34d Mon Sep 17 00:00:00 2001 From: "Diego Augusto S. C" Date: Fri, 3 Nov 2023 06:58:39 -0300 Subject: [PATCH] Add settings in volume --- data/dev.alextren.Spot.gschema.xml | 6 ++++- .../components/playback/playback_controls.blp | 1 - src/app/components/playback/volume_slider.blp | 11 +++++++++ src/player/player.rs | 24 +++++++++++-------- src/settings.rs | 3 +++ 5 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 src/app/components/playback/volume_slider.blp diff --git a/data/dev.alextren.Spot.gschema.xml b/data/dev.alextren.Spot.gschema.xml index bd97fc39..912cab37 100644 --- a/data/dev.alextren.Spot.gschema.xml +++ b/data/dev.alextren.Spot.gschema.xml @@ -52,5 +52,9 @@ 0 Port to communicate with Spotify's server (access point). Setting to 0 (default) allows Spot to use servers running on any port. + + 0.7 + Volume + - \ No newline at end of file + diff --git a/src/app/components/playback/playback_controls.blp b/src/app/components/playback/playback_controls.blp index b05e1e92..bcd59a22 100644 --- a/src/app/components/playback/playback_controls.blp +++ b/src/app/components/playback/playback_controls.blp @@ -56,7 +56,6 @@ template $PlaybackControlsWidget : Box { } ScaleButton volume_slider { - receives-default: true; halign: end; tooltip-text: _("Volume"); adjustment: Adjustment { diff --git a/src/app/components/playback/volume_slider.blp b/src/app/components/playback/volume_slider.blp new file mode 100644 index 00000000..7ed87b6b --- /dev/null +++ b/src/app/components/playback/volume_slider.blp @@ -0,0 +1,11 @@ +template $VolumeButton: ScaleButton { + receives-default: true; + halign: end; + tooltip-text: _("Volume"); + adjustment: Adjustment { + step-increment: 0.01; + page-increment: 0.10; + upper: 1.0; + lower: 0.0; + }; +} diff --git a/src/player/player.rs b/src/player/player.rs index 420c194a..1c383794 100644 --- a/src/player/player.rs +++ b/src/player/player.rs @@ -66,12 +66,13 @@ pub enum AudioBackend { Alsa(String), } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub struct SpotifyPlayerSettings { pub bitrate: Bitrate, pub backend: AudioBackend, pub gapless: bool, pub ap_port: Option, + pub volume: f64, } impl Default for SpotifyPlayerSettings { @@ -81,6 +82,7 @@ impl Default for SpotifyPlayerSettings { gapless: true, backend: AudioBackend::PulseAudio, ap_port: None, + volume: 0.6, } } } @@ -107,9 +109,7 @@ impl SpotifyPlayer { async fn handle(&mut self, action: Command) -> Result<(), SpotifyError> { match action { Command::PlayerSetVolume(volume) => { - if let Some(mixer) = self.mixer.as_mut() { - mixer.set_volume((VolumeCtrl::MAX_VOLUME as f64 * volume) as u16); - } + self.set_volume(volume); Ok(()) } Command::PlayerResume => { @@ -220,6 +220,13 @@ impl SpotifyPlayer { } } + fn set_volume(&mut self, volume: f64) { + if let Some(mixer) = self.mixer.as_mut() { + mixer.set_volume((VolumeCtrl::MAX_VOLUME as f64 * volume) as u16); + self.settings.volume = volume; + } + } + fn create_player(&mut self, session: Session) -> (Player, PlayerEventChannel) { let backend = self.settings.backend.clone(); @@ -233,17 +240,14 @@ impl SpotifyPlayer { let soft_volume = self .mixer .get_or_insert_with(|| { - let mix = Box::new(SoftMixer::open(MixerConfig { + Box::new(SoftMixer::open(MixerConfig { // This value feels reasonable to me. Feel free to change it volume_ctrl: VolumeCtrl::Log(VolumeCtrl::DEFAULT_DB_RANGE / 2.0), ..Default::default() - })); - // TODO: Should read volume from somewhere instead of hard coding. - // Sets volume to 100% - mix.set_volume(VolumeCtrl::MAX_VOLUME); - mix + })) }) .get_soft_volume(); + self.set_volume(self.settings.volume); Player::new(player_config, session, soft_volume, move || match backend { AudioBackend::GStreamer(pipeline) => { let backend = audio_backend::find(Some("gstreamer".to_string())).unwrap(); diff --git a/src/settings.rs b/src/settings.rs index 8e67c033..65aef495 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -70,11 +70,14 @@ impl SpotifyPlayerSettings { x => Some(x as u16), }; + let volume = settings.double("volume").clamp(0.0, 1.0); + Some(Self { bitrate, backend, gapless, ap_port, + volume }) } }