Skip to content

Commit

Permalink
Add option for linear volume scaling with alsa.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonTeixidor committed Mar 3, 2018
1 parent 7aa5dd5 commit cb11cf6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ password = PASS
backend = alsa
device = alsa_audio_device # Given by `aplay -L`
mixer = PCM
volume-control = alsa # or softvol
volume-control = alsa # or alsa_linear, or softvol
onevent = command_run_on_playback_events
device_name = name_in_spotify_connect
bitrate = 96|160|320
Expand Down
10 changes: 8 additions & 2 deletions src/alsa_mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alsa;
pub struct AlsaMixer {
pub device: String,
pub mixer: String,
pub linear_scaling: bool,
}

impl AlsaMixer {
Expand All @@ -17,8 +18,12 @@ impl AlsaMixer {
let (min, max) = elem.get_playback_volume_range();

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

elem.set_playback_volume_all(normalised_volume)?;
Ok(())
Expand All @@ -30,6 +35,7 @@ impl Mixer for AlsaMixer {
AlsaMixer {
device: "default".to_string(),
mixer: "Master".to_string(),
linear_scaling: false,
}
}
fn start(&self) {}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn command_line_argument_options() -> Options {
opts.optopt(
"",
"volume-control",
"Possible values are alsa or softvol.",
"Possible values are alsa, alsa_linear, and softvol.",
"CONTROLLER",
);
opts.optflag("v", "verbose", "Add debug information to log.");
Expand Down
5 changes: 3 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hostname;
const CONFIG_FILE: &str = "spotifyd.conf";

pub enum VolumeController {
Alsa,
Alsa { linear: bool },
SoftVol,
}

Expand All @@ -36,7 +36,8 @@ impl FromStr for VolumeController {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_uppercase() {
"ALSA" => Ok(VolumeController::Alsa),
"ALSA" => Ok(VolumeController::Alsa { linear: false }),
"ALSA_LINEAR" => Ok(VolumeController::Alsa { linear: true }),
"SOFTVOL" => Ok(VolumeController::SoftVol),
_ => Err(()),
}
Expand Down
3 changes: 2 additions & 1 deletion src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ pub fn initial_state(handle: Handle, matches: &Matches) -> main_loop::MainLoopSt
let local_audio_device = config.audio_device.clone();
let local_mixer = config.mixer.clone();
let mut mixer = match config.volume_controller {
config::VolumeController::Alsa => {
config::VolumeController::Alsa { linear } => {
info!("Using alsa volume controller.");
Box::new(move || {
Box::new(alsa_mixer::AlsaMixer {
device: local_audio_device
.clone()
.unwrap_or_else(|| "default".to_string()),
mixer: local_mixer.clone().unwrap_or_else(|| "Master".to_string()),
linear_scaling: linear,
}) as Box<mixer::Mixer>
}) as Box<FnMut() -> Box<Mixer>>
}
Expand Down

0 comments on commit cb11cf6

Please sign in to comment.