feat: working version

This commit is contained in:
2024-06-28 23:55:33 +02:00
parent c2f0cd0492
commit 1f48737b2b
7 changed files with 1123 additions and 2 deletions

28
src/utils.rs Normal file
View File

@@ -0,0 +1,28 @@
use anyhow::anyhow;
use pulse::{context::introspect::SinkInputInfo, volume};
const FULL_VOLUME: u32 = 1 << 16;
pub fn volume_to_percentage(volume: volume::ChannelVolumes) -> u8 {
let average = volume.avg().0;
total_volume_to_percentage(average)
}
pub fn total_volume_to_percentage(volume: u32) -> u8 {
((volume as f32 / FULL_VOLUME as f32) * 100.0).round() as u8
}
pub fn percentage_to_total_volume(percentage: u8) -> u32 {
((FULL_VOLUME as f32 / 100.0) * percentage as f32).round() as u32
}
pub fn get_sink_input_name(sink_input: &SinkInputInfo) -> anyhow::Result<String> {
let Some(name_bytes) = sink_input.proplist.get("application.name") else {
return Err(anyhow!("Invalid sink input name"));
};
Ok(String::from_utf8(
name_bytes[..name_bytes.len() - 1].to_vec(),
)?)
}