mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-28 14:31:23 +00:00
feat: first attemp to support playing multiple tracks in parallel
This commit is contained in:
+23
-13
@@ -3,12 +3,14 @@ use crate::types::{commands::*, socket::Request};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
let id = request.args.get("id").and_then(|s| s.parse::<u32>().ok());
|
||||
|
||||
match request.name.as_str() {
|
||||
"ping" => Some(Box::new(PingCommand {})),
|
||||
"pause" => Some(Box::new(PauseCommand {})),
|
||||
"resume" => Some(Box::new(ResumeCommand {})),
|
||||
"toggle_pause" => Some(Box::new(TogglePauseCommand {})),
|
||||
"stop" => Some(Box::new(StopCommand {})),
|
||||
"pause" => Some(Box::new(PauseCommand { id })),
|
||||
"resume" => Some(Box::new(ResumeCommand { id })),
|
||||
"toggle_pause" => Some(Box::new(TogglePauseCommand { id })),
|
||||
"stop" => Some(Box::new(StopCommand { id })),
|
||||
"is_paused" => Some(Box::new(IsPausedCommand {})),
|
||||
"get_state" => Some(Box::new(GetStateCommand {})),
|
||||
"get_volume" => Some(Box::new(GetVolumeCommand {})),
|
||||
@@ -19,9 +21,9 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<f32>()
|
||||
.ok();
|
||||
Some(Box::new(SetVolumeCommand { volume }))
|
||||
Some(Box::new(SetVolumeCommand { volume, id }))
|
||||
}
|
||||
"get_position" => Some(Box::new(GetPositionCommand {})),
|
||||
"get_position" => Some(Box::new(GetPositionCommand { id })),
|
||||
"seek" => {
|
||||
let position = request
|
||||
.args
|
||||
@@ -29,9 +31,9 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<f32>()
|
||||
.ok();
|
||||
Some(Box::new(SeekCommand { position }))
|
||||
Some(Box::new(SeekCommand { position, id }))
|
||||
}
|
||||
"get_duration" => Some(Box::new(GetDurationCommand {})),
|
||||
"get_duration" => Some(Box::new(GetDurationCommand { id })),
|
||||
"play" => {
|
||||
let file_path = request
|
||||
.args
|
||||
@@ -39,16 +41,24 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<PathBuf>()
|
||||
.ok();
|
||||
Some(Box::new(PlayCommand { file_path }))
|
||||
let concurrent = request
|
||||
.args
|
||||
.get("concurrent")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<bool>()
|
||||
.ok();
|
||||
Some(Box::new(PlayCommand {
|
||||
file_path,
|
||||
concurrent,
|
||||
}))
|
||||
}
|
||||
"get_current_file_path" => Some(Box::new(GetCurrentFilePathCommand {})),
|
||||
"get_tracks" => Some(Box::new(GetTracksCommand {})),
|
||||
"get_input" => Some(Box::new(GetCurrentInputCommand {})),
|
||||
"get_inputs" => Some(Box::new(GetAllInputsCommand {})),
|
||||
"set_input" => {
|
||||
let name = Some(request.args.get("input_name").unwrap_or(&String::new())).cloned();
|
||||
Some(Box::new(SetCurrentInputCommand { name }))
|
||||
}
|
||||
"get_loop" => Some(Box::new(GetLoopCommand {})),
|
||||
"set_loop" => {
|
||||
let enabled = request
|
||||
.args
|
||||
@@ -56,9 +66,9 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<bool>()
|
||||
.ok();
|
||||
Some(Box::new(SetLoopCommand { enabled }))
|
||||
Some(Box::new(SetLoopCommand { enabled, id }))
|
||||
}
|
||||
"toggle_loop" => Some(Box::new(ToggleLoopCommand {})),
|
||||
"toggle_loop" => Some(Box::new(ToggleLoopCommand { id })),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+24
-42
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
types::{
|
||||
audio_player::PlayerState,
|
||||
audio_player::{PlayerState, TrackInfo},
|
||||
config::GuiConfig,
|
||||
gui::AudioPlayerState,
|
||||
socket::{Request, Response},
|
||||
@@ -50,56 +50,47 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
wait_for_daemon().await.ok();
|
||||
|
||||
let state_req = Request::get_state();
|
||||
let file_path_req = Request::get_current_file_path();
|
||||
let tracks_req = Request::get_tracks();
|
||||
let is_paused_req = Request::get_is_paused();
|
||||
let volume_req = Request::get_volume();
|
||||
let position_req = Request::get_position();
|
||||
let duration_req = Request::get_duration();
|
||||
let current_input_req = Request::get_input();
|
||||
let all_inputs_req = Request::get_inputs();
|
||||
let looped_req = Request::get_loop();
|
||||
|
||||
let (
|
||||
state_res,
|
||||
file_path_res,
|
||||
tracks_res,
|
||||
is_paused_res,
|
||||
volume_res,
|
||||
position_res,
|
||||
duration_res,
|
||||
current_input_res,
|
||||
all_inputs_res,
|
||||
looped_res,
|
||||
) = tokio::join!(
|
||||
make_request(state_req),
|
||||
make_request(file_path_req),
|
||||
make_request(tracks_req),
|
||||
make_request(is_paused_req),
|
||||
make_request(volume_req),
|
||||
make_request(position_req),
|
||||
make_request(duration_req),
|
||||
make_request(current_input_req),
|
||||
make_request(all_inputs_req),
|
||||
make_request(looped_req),
|
||||
);
|
||||
|
||||
let state_res = state_res.unwrap_or_default();
|
||||
let file_path_res = file_path_res.unwrap_or_default();
|
||||
let tracks_res = tracks_res.unwrap_or_default();
|
||||
let is_paused_res = is_paused_res.unwrap_or_default();
|
||||
let volume_res = volume_res.unwrap_or_default();
|
||||
let position_res = position_res.unwrap_or_default();
|
||||
let duration_res = duration_res.unwrap_or_default();
|
||||
let current_input_res = current_input_res.unwrap_or_default();
|
||||
let all_inputs_res = all_inputs_res.unwrap_or_default();
|
||||
let looped_res = looped_res.unwrap_or_default();
|
||||
|
||||
let state = match state_res.status {
|
||||
true => serde_json::from_str::<PlayerState>(&state_res.message).unwrap(),
|
||||
false => PlayerState::default(),
|
||||
};
|
||||
|
||||
let file_path = match file_path_res.status {
|
||||
true => PathBuf::from(file_path_res.message),
|
||||
false => PathBuf::new(),
|
||||
let tracks = match tracks_res.status {
|
||||
true => {
|
||||
serde_json::from_str::<Vec<TrackInfo>>(&tracks_res.message).unwrap_or_default()
|
||||
}
|
||||
false => vec![],
|
||||
};
|
||||
|
||||
let is_paused = match is_paused_res.status {
|
||||
true => is_paused_res.message == "true",
|
||||
false => false,
|
||||
@@ -108,14 +99,6 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
true => volume_res.message.parse::<f32>().unwrap(),
|
||||
false => 0.0,
|
||||
};
|
||||
let position = match position_res.status {
|
||||
true => position_res.message.parse::<f32>().unwrap(),
|
||||
false => 0.0,
|
||||
};
|
||||
let duration = match duration_res.status {
|
||||
true => duration_res.message.parse::<f32>().unwrap(),
|
||||
false => 0.0,
|
||||
};
|
||||
let current_input = match current_input_res.status {
|
||||
true => current_input_res
|
||||
.message
|
||||
@@ -144,10 +127,6 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
.collect::<HashMap<String, String>>(),
|
||||
false => HashMap::new(),
|
||||
};
|
||||
let looped = match looped_res.status {
|
||||
true => looped_res.message.parse::<bool>().unwrap_or_default(),
|
||||
false => false,
|
||||
};
|
||||
|
||||
{
|
||||
let mut guard = audio_player_state_shared.lock().unwrap();
|
||||
@@ -159,7 +138,19 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
}
|
||||
None => state,
|
||||
};
|
||||
guard.current_file_path = file_path;
|
||||
guard.tracks = tracks.clone();
|
||||
if let Some(last_track) = tracks.last() {
|
||||
guard.current_file_path = last_track.path.clone();
|
||||
guard.position = last_track.position;
|
||||
guard.duration = last_track.duration.unwrap_or(1.0);
|
||||
guard.looped = last_track.looped;
|
||||
} else {
|
||||
guard.current_file_path = PathBuf::new();
|
||||
guard.position = 0.0;
|
||||
guard.duration = 1.0;
|
||||
guard.looped = false;
|
||||
}
|
||||
|
||||
guard.is_paused = is_paused;
|
||||
guard.volume = match guard.new_volume {
|
||||
Some(new_volume) => {
|
||||
@@ -168,17 +159,8 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
}
|
||||
None => volume,
|
||||
};
|
||||
guard.position = match guard.new_position {
|
||||
Some(new_position) => {
|
||||
guard.new_position = None;
|
||||
new_position
|
||||
}
|
||||
None => position,
|
||||
};
|
||||
guard.duration = if duration > 0.0 { duration } else { 1.0 };
|
||||
guard.current_input = current_input;
|
||||
guard.all_inputs = all_inputs;
|
||||
guard.looped = looped;
|
||||
}
|
||||
|
||||
sleep(sleep_duration).await;
|
||||
|
||||
Reference in New Issue
Block a user