Skip to content

Commit

Permalink
Add settings in volume
Browse files Browse the repository at this point in the history
  • Loading branch information
Diegovsky committed Nov 3, 2023
1 parent 7215bb9 commit 9389eee
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
6 changes: 5 additions & 1 deletion data/dev.alextren.Spot.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@
<default>0</default>
<summary>Port to communicate with Spotify's server (access point). Setting to 0 (default) allows Spot to use servers running on any port.</summary>
</key>
<key name="volume" type='d'>
<default>0.7</default>
<summary>Volume</summary>
</key>
</schema>
</schemalist>
</schemalist>
1 change: 0 additions & 1 deletion src/app/components/playback/playback_controls.blp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ template $PlaybackControlsWidget : Box {
}

ScaleButton volume_slider {
receives-default: true;
halign: end;
tooltip-text: _("Volume");
adjustment: Adjustment {
Expand Down
11 changes: 11 additions & 0 deletions src/app/components/playback/volume_slider.blp
Original file line number Diff line number Diff line change
@@ -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;
};
}
24 changes: 14 additions & 10 deletions src/player/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>,
pub volume: f64,
}

impl Default for SpotifyPlayerSettings {
Expand All @@ -81,6 +82,7 @@ impl Default for SpotifyPlayerSettings {
gapless: true,
backend: AudioBackend::PulseAudio,
ap_port: None,
volume: 0.6,
}
}
}
Expand All @@ -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 => {
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
}
Expand Down

0 comments on commit 9389eee

Please sign in to comment.