perf(audio): further reduce CPU usage
This commit is contained in:
parent
fe598a7a60
commit
c8d91a8a9a
2 changed files with 96 additions and 47 deletions
|
|
@ -4,6 +4,8 @@
|
||||||
mod localize;
|
mod localize;
|
||||||
mod mouse_area;
|
mod mouse_area;
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::{localize::localize, pulse::DeviceInfo};
|
use crate::{localize::localize, pulse::DeviceInfo};
|
||||||
use config::AudioAppletConfig;
|
use config::AudioAppletConfig;
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
|
|
@ -55,6 +57,12 @@ pub fn run() -> cosmic::iced::Result {
|
||||||
pub struct Audio {
|
pub struct Audio {
|
||||||
core: cosmic::app::Core,
|
core: cosmic::app::Core,
|
||||||
is_open: IsOpen,
|
is_open: IsOpen,
|
||||||
|
output_volume: f64,
|
||||||
|
output_volume_debounce: bool,
|
||||||
|
output_volume_text: String,
|
||||||
|
input_volume: f64,
|
||||||
|
input_volume_debounce: bool,
|
||||||
|
input_volume_text: String,
|
||||||
current_output: Option<DeviceInfo>,
|
current_output: Option<DeviceInfo>,
|
||||||
current_input: Option<DeviceInfo>,
|
current_input: Option<DeviceInfo>,
|
||||||
outputs: Vec<DeviceInfo>,
|
outputs: Vec<DeviceInfo>,
|
||||||
|
|
@ -70,10 +78,15 @@ pub struct Audio {
|
||||||
impl Audio {
|
impl Audio {
|
||||||
fn update_output(&mut self, output: Option<DeviceInfo>) {
|
fn update_output(&mut self, output: Option<DeviceInfo>) {
|
||||||
self.current_output = output;
|
self.current_output = output;
|
||||||
|
|
||||||
|
if let Some(device) = self.current_output.as_ref() {
|
||||||
|
self.output_volume = volume_to_percent(device.volume.avg());
|
||||||
|
self.output_volume_text = format!("{}%", self.output_volume.round());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn output_icon_name(&self) -> &'static str {
|
fn output_icon_name(&self) -> &'static str {
|
||||||
let volume = self.current_output_volume_percent();
|
let volume = self.output_volume;
|
||||||
let mute = self.current_output_mute();
|
let mute = self.current_output_mute();
|
||||||
if mute || volume == 0. {
|
if mute || volume == 0. {
|
||||||
"audio-volume-muted-symbolic"
|
"audio-volume-muted-symbolic"
|
||||||
|
|
@ -90,10 +103,15 @@ impl Audio {
|
||||||
|
|
||||||
fn update_input(&mut self, input: Option<DeviceInfo>) {
|
fn update_input(&mut self, input: Option<DeviceInfo>) {
|
||||||
self.current_input = input;
|
self.current_input = input;
|
||||||
|
|
||||||
|
if let Some(device) = self.current_output.as_ref() {
|
||||||
|
self.input_volume = volume_to_percent(device.volume.avg());
|
||||||
|
self.input_volume_text = format!("{}%", self.input_volume.round());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn input_icon_name(&self) -> &'static str {
|
fn input_icon_name(&self) -> &'static str {
|
||||||
let volume = self.current_input_volume_percent();
|
let volume = self.input_volume;
|
||||||
let mute = self.current_input_mute();
|
let mute = self.current_input_mute();
|
||||||
if mute || volume == 0. {
|
if mute || volume == 0. {
|
||||||
"microphone-sensitivity-muted-symbolic"
|
"microphone-sensitivity-muted-symbolic"
|
||||||
|
|
@ -117,6 +135,8 @@ enum IsOpen {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
Ignore,
|
Ignore,
|
||||||
|
ApplyOutputVolume,
|
||||||
|
ApplyInputVolume,
|
||||||
SetOutputVolume(f64),
|
SetOutputVolume(f64),
|
||||||
SetInputVolume(f64),
|
SetInputVolume(f64),
|
||||||
SetOutputMute(bool),
|
SetOutputMute(bool),
|
||||||
|
|
@ -249,24 +269,6 @@ impl Audio {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_output_volume_percent(&self) -> f64 {
|
|
||||||
volume_to_percent(
|
|
||||||
self.current_output
|
|
||||||
.as_ref()
|
|
||||||
.map(|o| o.volume.avg())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_input_volume_percent(&self) -> f64 {
|
|
||||||
volume_to_percent(
|
|
||||||
self.current_input
|
|
||||||
.as_ref()
|
|
||||||
.map(|o| o.volume.avg())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_output_mute(&self) -> bool {
|
fn current_output_mute(&self) -> bool {
|
||||||
self.current_output
|
self.current_output
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
|
@ -355,9 +357,51 @@ impl cosmic::Application for Audio {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::SetOutputVolume(vol) => {
|
Message::SetOutputVolume(vol) => {
|
||||||
self.current_output
|
if self.output_volume == vol {
|
||||||
.as_mut()
|
return Command::none();
|
||||||
.map(|o| o.volume.set(o.volume.len(), percent_to_volume(vol)));
|
}
|
||||||
|
|
||||||
|
self.output_volume = vol;
|
||||||
|
self.output_volume_text = format!("{}%", self.output_volume.round());
|
||||||
|
|
||||||
|
if self.output_volume_debounce {
|
||||||
|
return Command::none();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.output_volume_debounce = true;
|
||||||
|
|
||||||
|
return cosmic::command::future(async move {
|
||||||
|
tokio::time::sleep(Duration::from_millis(64)).await;
|
||||||
|
Message::ApplyOutputVolume
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Message::SetInputVolume(vol) => {
|
||||||
|
if self.input_volume == vol {
|
||||||
|
return Command::none();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.input_volume = vol;
|
||||||
|
self.input_volume_text = format!("{}%", self.input_volume.round());
|
||||||
|
|
||||||
|
if self.input_volume_debounce {
|
||||||
|
return Command::none();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.input_volume_debounce = true;
|
||||||
|
|
||||||
|
return cosmic::command::future(async move {
|
||||||
|
tokio::time::sleep(Duration::from_millis(64)).await;
|
||||||
|
Message::ApplyInputVolume
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Message::ApplyOutputVolume => {
|
||||||
|
self.output_volume_debounce = false;
|
||||||
|
|
||||||
|
self.current_output.as_mut().map(|o| {
|
||||||
|
o.volume
|
||||||
|
.set(o.volume.len(), percent_to_volume(self.output_volume))
|
||||||
|
});
|
||||||
|
|
||||||
if let PulseState::Connected(connection) = &mut self.pulse_state {
|
if let PulseState::Connected(connection) = &mut self.pulse_state {
|
||||||
if let Some(device) = &self.current_output {
|
if let Some(device) = &self.current_output {
|
||||||
if let Some(name) = &device.name {
|
if let Some(name) = &device.name {
|
||||||
|
|
@ -369,10 +413,14 @@ impl cosmic::Application for Audio {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::SetInputVolume(vol) => {
|
Message::ApplyInputVolume => {
|
||||||
self.current_input
|
self.input_volume_debounce = false;
|
||||||
.as_mut()
|
|
||||||
.map(|i| i.volume.set(i.volume.len(), percent_to_volume(vol)));
|
self.current_input.as_mut().map(|i| {
|
||||||
|
i.volume
|
||||||
|
.set(i.volume.len(), percent_to_volume(self.input_volume))
|
||||||
|
});
|
||||||
|
|
||||||
if let PulseState::Connected(connection) = &mut self.pulse_state {
|
if let PulseState::Connected(connection) = &mut self.pulse_state {
|
||||||
if let Some(device) = &self.current_input {
|
if let Some(device) = &self.current_input {
|
||||||
if let Some(name) = &device.name {
|
if let Some(name) = &device.name {
|
||||||
|
|
@ -688,8 +736,6 @@ impl cosmic::Application for Audio {
|
||||||
|
|
||||||
fn view_window(&self, _id: window::Id) -> Element<Message> {
|
fn view_window(&self, _id: window::Id) -> Element<Message> {
|
||||||
let audio_disabled = matches!(self.pulse_state, PulseState::Disconnected(_));
|
let audio_disabled = matches!(self.pulse_state, PulseState::Disconnected(_));
|
||||||
let out_f64 = self.current_output_volume_percent();
|
|
||||||
let in_f64 = self.current_input_volume_percent();
|
|
||||||
let out_mute = self.current_output_mute();
|
let out_mute = self.current_output_mute();
|
||||||
let in_mute = self.current_input_mute();
|
let in_mute = self.current_input_mute();
|
||||||
|
|
||||||
|
|
@ -713,9 +759,9 @@ impl cosmic::Application for Audio {
|
||||||
.icon_size(24)
|
.icon_size(24)
|
||||||
.line_height(24)
|
.line_height(24)
|
||||||
.on_press(Message::SetOutputMute(!out_mute)),
|
.on_press(Message::SetOutputMute(!out_mute)),
|
||||||
slider(0.0..=100.0, out_f64, Message::SetOutputVolume)
|
slider(0.0..=100.0, self.output_volume, Message::SetOutputVolume)
|
||||||
.width(Length::FillPortion(5)),
|
.width(Length::FillPortion(5)),
|
||||||
text(format!("{}%", out_f64.round()))
|
text(&self.output_volume_text)
|
||||||
.size(16)
|
.size(16)
|
||||||
.width(Length::FillPortion(1))
|
.width(Length::FillPortion(1))
|
||||||
.horizontal_alignment(Horizontal::Right)
|
.horizontal_alignment(Horizontal::Right)
|
||||||
|
|
@ -734,9 +780,9 @@ impl cosmic::Application for Audio {
|
||||||
.icon_size(24)
|
.icon_size(24)
|
||||||
.line_height(24)
|
.line_height(24)
|
||||||
.on_press(Message::SetInputMute(!in_mute)),
|
.on_press(Message::SetInputMute(!in_mute)),
|
||||||
slider(0.0..=100.0, in_f64, Message::SetInputVolume)
|
slider(0.0..=100.0, self.input_volume, Message::SetInputVolume)
|
||||||
.width(Length::FillPortion(5)),
|
.width(Length::FillPortion(5)),
|
||||||
text(format!("{}%", in_f64.round()))
|
text(&self.input_volume_text)
|
||||||
.size(16)
|
.size(16)
|
||||||
.width(Length::FillPortion(1))
|
.width(Length::FillPortion(1))
|
||||||
.horizontal_alignment(Horizontal::Right)
|
.horizontal_alignment(Horizontal::Right)
|
||||||
|
|
|
||||||
|
|
@ -216,28 +216,31 @@ impl PulseHandle {
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
let mut server: Option<PulseServer> = None;
|
let mut server: Option<PulseServer> = None;
|
||||||
|
|
||||||
loop {
|
let mut msgs = Vec::new();
|
||||||
// This is where the we match messages from the GUI to pass to the pulse server
|
|
||||||
let mut msgs = Vec::new();
|
|
||||||
|
|
||||||
|
loop {
|
||||||
if let Some(msg) = to_pulse_recv.recv().await {
|
if let Some(msg) = to_pulse_recv.recv().await {
|
||||||
msgs.push(msg);
|
msgs.push(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Consume any additional messages in the channel.
|
||||||
while let Ok(msg) = to_pulse_recv.try_recv() {
|
while let Ok(msg) = to_pulse_recv.try_recv() {
|
||||||
|
// Deduplicate volume change messages.
|
||||||
|
if matches!(
|
||||||
|
msg,
|
||||||
|
Message::SetSinkVolumeByName(..) | Message::SetSourceVolumeByName(..)
|
||||||
|
) {
|
||||||
|
let last_msg = msgs.last_mut().unwrap(); //
|
||||||
|
if mem::discriminant(last_msg) == mem::discriminant(&msg) {
|
||||||
|
*last_msg = msg;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
msgs.push(msg);
|
msgs.push(msg);
|
||||||
}
|
}
|
||||||
// deduplicate Messages that do not rely on response
|
|
||||||
// Reverse to retain the last element instead of the first
|
|
||||||
msgs.reverse();
|
|
||||||
msgs.dedup_by(|a, b| match a {
|
|
||||||
Message::SetSinkVolumeByName(..) | Message::SetSourceVolumeByName(..) => {
|
|
||||||
mem::discriminant(a) == mem::discriminant(b)
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
});
|
|
||||||
msgs.reverse();
|
|
||||||
|
|
||||||
for msg in msgs {
|
for msg in msgs.drain(..) {
|
||||||
match msg {
|
match msg {
|
||||||
Message::GetDefaultSink => {
|
Message::GetDefaultSink => {
|
||||||
let server = match server.as_mut() {
|
let server = match server.as_mut() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue