mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-07-26 05:46:59 +00:00
feat(lib, daemon, cli, gui): add volume multiplier (#175)
* initial implementation * rename DaemonConfig.volume_multiplier to default_volume_multiplier * add volume_multiplier to the FullState * add pwsp-gui integration
This commit is contained in:
@@ -40,6 +40,7 @@ pub struct FullState {
|
||||
pub state: PlayerState,
|
||||
pub tracks: Vec<TrackInfo>,
|
||||
pub volume: f32,
|
||||
pub volume_multiplier: f32,
|
||||
pub current_input: String,
|
||||
pub all_inputs: HashMap<String, String>,
|
||||
}
|
||||
@@ -63,16 +64,19 @@ pub struct AudioPlayer {
|
||||
pub input_device_name: Option<String>,
|
||||
|
||||
pub volume: f32, // Master volume
|
||||
pub volume_multiplier: f32,
|
||||
}
|
||||
|
||||
impl AudioPlayer {
|
||||
pub async fn new() -> Result<Self> {
|
||||
let (default_input_name, default_volume) = with_daemon_config(|c| {
|
||||
(
|
||||
c.default_input_name.clone(),
|
||||
c.default_volume.unwrap_or(1.0),
|
||||
)
|
||||
});
|
||||
let (default_input_name, default_volume, default_volume_multiplier) =
|
||||
with_daemon_config(|c| {
|
||||
(
|
||||
c.default_input_name.clone(),
|
||||
c.default_volume.unwrap_or(1.0),
|
||||
c.default_volume_multiplier.unwrap_or(1.0),
|
||||
)
|
||||
});
|
||||
|
||||
let mut audio_player = AudioPlayer {
|
||||
stream_handle: None,
|
||||
@@ -84,6 +88,7 @@ impl AudioPlayer {
|
||||
input_device_name: default_input_name,
|
||||
|
||||
volume: default_volume,
|
||||
volume_multiplier: default_volume_multiplier,
|
||||
};
|
||||
|
||||
if audio_player.input_device_name.is_some() {
|
||||
@@ -266,12 +271,16 @@ impl AudioPlayer {
|
||||
if let Some(id) = id {
|
||||
if let Some(sound) = self.tracks.get_mut(&id) {
|
||||
sound.volume = volume;
|
||||
sound.sink.set_volume(self.volume * volume);
|
||||
sound
|
||||
.sink
|
||||
.set_volume(self.volume * sound.volume * self.volume_multiplier);
|
||||
}
|
||||
} else {
|
||||
self.volume = volume;
|
||||
for sound in self.tracks.values_mut() {
|
||||
sound.sink.set_volume(self.volume * sound.volume);
|
||||
sound
|
||||
.sink
|
||||
.set_volume(self.volume * sound.volume * self.volume_multiplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -351,7 +360,7 @@ impl AudioPlayer {
|
||||
.ok_or_else(|| anyhow::anyhow!("stream_handle is unexpectedly missing"))?
|
||||
.mixer();
|
||||
let sink = Player::connect_new(mixer);
|
||||
sink.set_volume(self.volume); // Default volume is 1.0 * master
|
||||
sink.set_volume(self.volume * self.volume_multiplier); // Default volume is 1.0 * master
|
||||
sink.append(source);
|
||||
sink.play();
|
||||
|
||||
|
||||
@@ -46,11 +46,17 @@ pub struct GetVolumeCommand {
|
||||
pub id: Option<u32>,
|
||||
}
|
||||
|
||||
pub struct GetVolumeMultiplierCommand {}
|
||||
|
||||
pub struct SetVolumeCommand {
|
||||
pub volume: Option<f32>,
|
||||
pub id: Option<u32>,
|
||||
}
|
||||
|
||||
pub struct SetVolumeMultiplierCommand {
|
||||
pub volume_multiplier: Option<f32>,
|
||||
}
|
||||
|
||||
pub struct GetPositionCommand {
|
||||
pub id: Option<u32>,
|
||||
}
|
||||
@@ -270,6 +276,18 @@ impl Executable for GetVolumeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetVolumeMultiplierCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let audio_player = match get_audio_player().await {
|
||||
Ok(player) => player.lock().await,
|
||||
Err(err) => return Response::new(false, format!("Audio player error: {}", err)),
|
||||
};
|
||||
|
||||
Response::new(true, audio_player.volume_multiplier.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for SetVolumeCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
@@ -286,6 +304,26 @@ impl Executable for SetVolumeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for SetVolumeMultiplierCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
if let Some(volume_multiplier) = self.volume_multiplier {
|
||||
let mut audio_player = match get_audio_player().await {
|
||||
Ok(player) => player.lock().await,
|
||||
Err(err) => return Response::new(false, format!("Audio player error: {}", err)),
|
||||
};
|
||||
audio_player.volume_multiplier = volume_multiplier;
|
||||
audio_player.set_volume(volume_multiplier, None); // Reset current volume for all tracks to apply multiplier
|
||||
Response::new(
|
||||
true,
|
||||
format!("Audio volume multiplier was set to {}", volume_multiplier),
|
||||
)
|
||||
} else {
|
||||
Response::new(false, "Invalid volume multiplier value")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetPositionCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
@@ -516,6 +554,7 @@ impl Executable for GetFullStateCommand {
|
||||
state: audio_player.get_state(),
|
||||
tracks: audio_player.get_tracks(),
|
||||
volume: audio_player.volume,
|
||||
volume_multiplier: audio_player.volume_multiplier,
|
||||
current_input: current_input_nick,
|
||||
all_inputs,
|
||||
};
|
||||
|
||||
@@ -17,6 +17,7 @@ use std::{
|
||||
pub struct DaemonConfig {
|
||||
pub default_input_name: Option<String>,
|
||||
pub default_volume: Option<f32>,
|
||||
pub default_volume_multiplier: Option<f32>,
|
||||
}
|
||||
|
||||
impl DaemonConfig {
|
||||
@@ -73,6 +74,7 @@ pub struct GuiConfig {
|
||||
pub left_panel_width: f32,
|
||||
|
||||
pub save_volume: bool,
|
||||
pub save_volume_multiplier: bool,
|
||||
pub save_input: bool,
|
||||
pub save_scale_factor: bool,
|
||||
pub pause_on_exit: bool,
|
||||
@@ -117,6 +119,7 @@ impl Default for GuiConfig {
|
||||
left_panel_width: 280.0,
|
||||
|
||||
save_volume: false,
|
||||
save_volume_multiplier: false,
|
||||
save_input: false,
|
||||
save_scale_factor: false,
|
||||
pause_on_exit: false,
|
||||
|
||||
@@ -31,13 +31,16 @@ pub struct AppState {
|
||||
|
||||
pub show_settings: bool,
|
||||
pub volume_dragged: bool,
|
||||
pub volume_multiplier_dragged: bool,
|
||||
pub force_focus_search: bool,
|
||||
|
||||
pub volume_slider_value: f32,
|
||||
pub volume_multiplier_slider_value: f32,
|
||||
|
||||
pub search_field_id: Option<Id>,
|
||||
|
||||
pub ignore_volume_update_until: Option<Instant>,
|
||||
pub ignore_volume_multiplier_update_until: Option<Instant>,
|
||||
|
||||
pub current_dir: Option<PathBuf>,
|
||||
pub dirs: Vec<PathBuf>,
|
||||
@@ -65,6 +68,7 @@ pub struct AudioPlayerState {
|
||||
pub tracks: Vec<TrackInfo>,
|
||||
|
||||
pub volume: f32, // Master volume
|
||||
pub volume_multiplier: f32,
|
||||
|
||||
pub current_input: String,
|
||||
pub all_inputs: HashMap<String, String>,
|
||||
|
||||
@@ -95,6 +95,10 @@ impl Request {
|
||||
Request::new("get_volume", args)
|
||||
}
|
||||
|
||||
pub fn get_volume_multiplier() -> Self {
|
||||
Request::new("get_volume_multiplier", vec![])
|
||||
}
|
||||
|
||||
pub fn get_position(id: Option<u32>) -> Self {
|
||||
let mut args = vec![];
|
||||
let id_str;
|
||||
@@ -139,6 +143,13 @@ impl Request {
|
||||
Request::new("set_volume".to_string(), args)
|
||||
}
|
||||
|
||||
pub fn set_volume_multiplier(volume: f32) -> Self {
|
||||
Request::new(
|
||||
"set_volume_multiplier",
|
||||
vec![("volume_multiplier", &volume.to_string())],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn seek(position: f32, id: Option<u32>) -> Self {
|
||||
let mut args = vec![("position".to_string(), position.to_string())];
|
||||
if let Some(id) = id {
|
||||
|
||||
@@ -15,6 +15,7 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
"is_paused" => Some(Box::new(IsPausedCommand {})),
|
||||
"get_state" => Some(Box::new(GetStateCommand {})),
|
||||
"get_volume" => Some(Box::new(GetVolumeCommand { id })),
|
||||
"get_volume_multiplier" => Some(Box::new(GetVolumeMultiplierCommand {})),
|
||||
"set_volume" => {
|
||||
let volume = request
|
||||
.args
|
||||
@@ -24,6 +25,15 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
.ok();
|
||||
Some(Box::new(SetVolumeCommand { volume, id }))
|
||||
}
|
||||
"set_volume_multiplier" => {
|
||||
let volume_multiplier = request
|
||||
.args
|
||||
.get("volume_multiplier")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<f32>()
|
||||
.ok();
|
||||
Some(Box::new(SetVolumeMultiplierCommand { volume_multiplier }))
|
||||
}
|
||||
"get_position" => Some(Box::new(GetPositionCommand { id })),
|
||||
"seek" => {
|
||||
let position = request
|
||||
|
||||
@@ -116,6 +116,7 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
};
|
||||
guard.tracks = full_state.tracks;
|
||||
guard.volume = full_state.volume;
|
||||
guard.volume_multiplier = full_state.volume_multiplier;
|
||||
guard.current_input = full_state
|
||||
.current_input
|
||||
.split(" - ")
|
||||
|
||||
Reference in New Issue
Block a user