Refactoring, fixing volume calculation

This commit is contained in:
Erwin Boskma 2021-11-24 15:33:23 +01:00
parent 72b72a504f
commit 20290547f1
Signed by: erwin
GPG key ID: 270B20D17394F7E5

View file

@ -2,7 +2,6 @@ use std::error::Error;
use argh::FromArgs; use argh::FromArgs;
use notify_rust::{Hint, Notification}; use notify_rust::{Hint, Notification};
use pulse::volume::Volume;
use pulsectl::controllers::{DeviceControl, SinkController}; use pulsectl::controllers::{DeviceControl, SinkController};
/// Control the volume of the default sink /// Control the volume of the default sink
@ -47,7 +46,12 @@ struct GetArgs {}
#[argh(subcommand, name = "set")] #[argh(subcommand, name = "set")]
struct SetArgs { struct SetArgs {
#[argh(positional)] #[argh(positional)]
volume: u8, volume: i32,
}
enum Volume {
Muted,
Percent(i32),
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -61,70 +65,67 @@ fn main() -> Result<(), Box<dyn Error>> {
match args.command { match args.command {
Subcommand::Up(_) => { Subcommand::Up(_) => {
handler.increase_device_volume_by_percent(default_device.index, 0.05); handler.increase_device_volume_by_percent(default_device.index, 0.05);
notify_volume(default_device.volume.avg().0 as i32);
} }
Subcommand::Down(_) => { Subcommand::Down(_) => {
handler.decrease_device_volume_by_percent(default_device.index, 0.05); handler.decrease_device_volume_by_percent(default_device.index, 0.05);
notify_volume(default_device.volume.avg().0 as i32);
} }
Subcommand::Mute(_) => { Subcommand::Mute(_) => {
handler.set_device_mute_by_index(default_device.index, !muted); handler.set_device_mute_by_index(default_device.index, !muted);
notify_muted(default_device.volume.avg().0 as i32, default_device.mute);
} }
Subcommand::Get(_) => println!("Volume: {}", default_device.volume.avg()), Subcommand::Get(_) => {
println!("Volume: {}", default_device.volume.avg());
return Ok(());
}
Subcommand::Set(SetArgs { volume }) => { Subcommand::Set(SetArgs { volume }) => {
let mut current_volume = default_device.volume; let mut current_volume = default_device.volume;
let new_volume = Volume(volume_from_percent(volume) as u32); let new_volume = pulse::volume::Volume(volume_from_percent(volume) as u32);
let volumes = current_volume.set(2, new_volume); let volumes = current_volume.set(2, new_volume);
handler.set_device_volume_by_index(default_device.index, volumes); handler.set_device_volume_by_index(default_device.index, volumes);
notify_volume(default_device.volume.avg().0 as i32);
} }
} }
let v = if handler.get_default_device()?.mute {
Volume::Muted
} else {
Volume::Percent(volume_to_percent(
handler.get_default_device()?.volume.avg().0 as i32,
))
};
notify_volume(v);
Ok(()) Ok(())
} }
fn icon_name(volume: i32) -> String { fn icon_name(volume: Volume) -> String {
let name = match volume { let name = match volume {
x if x <= 0 => "muted", Volume::Muted => "muted",
x if x <= 30 => "low", Volume::Percent(x) if x <= 33 => "low",
x if x <= 70 => "medium", Volume::Percent(x) if x <= 67 => "medium",
_ => "high", _ => "high",
}; };
format!("audio-volume-{}-symbolic", name) format!("audio-volume-{}-symbolic", name)
} }
fn notify_volume(new_volume: i32) { fn notify_volume(new_volume: Volume) {
let value = dbg!(match new_volume {
Volume::Muted => 0,
Volume::Percent(new_volume) => volume_to_percent(new_volume),
});
Notification::new() Notification::new()
.summary("Volume") .summary("Volume")
.icon(icon_name(new_volume).as_str()) .icon(icon_name(new_volume).as_str())
.appname("pamedia") .appname("pamedia")
.hint(Hint::Custom( .hint(Hint::SoundName("audio-volume-change".into()))
"x-canonical-private-synchronous".into(), .hint(Hint::Custom("synchronous".into(), "audio".into()))
"audio".into(), .hint(Hint::CustomInt("value".into(), value))
))
.hint(Hint::CustomInt("value".into(), new_volume))
.show() .show()
.expect("error showing notification"); .expect("error showing notification");
} }
fn notify_muted(volume: i32, muted: bool) { fn volume_from_percent(volume: i32) -> f64 {
let icon = icon_name(if muted { 0 } else { volume });
Notification::new()
.summary("Volume")
.icon(icon.as_str())
.appname("pamedia")
.hint(Hint::Custom(
"x-canonical-private-synchronous".into(),
"audio".into(),
))
.hint(Hint::CustomInt("value".into(), volume))
.show()
.expect("error showing notification");
}
fn volume_from_percent(volume: u8) -> f64 {
(volume as f64) * (f64::from(pulse::volume::Volume::NORMAL.0) / 100.0) (volume as f64) * (f64::from(pulse::volume::Volume::NORMAL.0) / 100.0)
} }
fn volume_to_percent(volume: i32) -> i32 {
((volume as f64) / (f64::from(pulse::volume::Volume::NORMAL.0)) * 100.0).round() as i32
}