Skip to content

Commit

Permalink
fix alsa mixer calculations (#1229)
Browse files Browse the repository at this point in the history
fixes #1222

I hope, this fixes the issues mentioned with `alsa` and `alsa_linear`.
Please note that one of them will behave weirdly on your system
depending on, whether your alsa control is linear or not. (At least that
is what I assume based on my experience...) However, they should both be
able to reach 0% and not be reset when hitting play.
  • Loading branch information
eladyn authored Oct 21, 2024
1 parent 690ab7e commit 70f2a2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Credential caching has been re-enabled. ([#1214])

### Fixed
- alsa mixer: volume calculation has been fixed ([#1229])

[#1214]: https://github.com/Spotifyd/spotifyd/pull/1214
[#1229]: https://github.com/Spotifyd/spotifyd/pull/1229
[#1228]: https://github.com/Spotifyd/spotifyd/pull/1228

## [0.3.5]
Expand Down
8 changes: 4 additions & 4 deletions src/alsa_mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl AlsaMixer {

let volume_steps = (max - min) as f64;
let normalised_volume = if self.linear_scaling {
((f64::from(volume) / f64::from(u16::MAX)) * volume_steps) as i64 + min
(((volume as f64) / (u16::MAX as f64)) * volume_steps) as i64 + min
} else {
(f64::from(volume).log(f64::from(u16::MAX)) * volume_steps).floor() as i64 + min
((volume as f64 + 1.0).log((u16::MAX as f64) + 1.0) * volume_steps).floor() as i64 + min
};

elem.set_playback_volume_all(normalised_volume)?;
Expand Down Expand Up @@ -55,8 +55,8 @@ impl Mixer for AlsaMixer {
elem.get_playback_volume(alsa::mixer::SelemChannelId::mono())
.ok()
.map(|volume| {
let volume_steps = max - min + 1;
((volume - min) * (0xFFFF / volume_steps)) as u16
(((volume - min) as f64 / (max - min) as f64) * (u16::MAX as f64)).floor()
as u16
})
});
match vol {
Expand Down

0 comments on commit 70f2a2d

Please sign in to comment.