mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-28 14:31:23 +00:00
Compare commits
132 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c1f3f34169 | |||
| 6c06da7b0d | |||
| 023caa4616 | |||
| fc593f8a8e | |||
| f281ad5a78 | |||
| fe8fec4ea7 | |||
| 7583e72687 | |||
| e4b4a3d198 | |||
| a7af1caace | |||
| c5086189d6 | |||
| ac667f1273 | |||
| fdb79e609f | |||
| 531a49dbdf | |||
| 2a13c25230 | |||
| 408f5317c4 | |||
| 3672d6f43e | |||
| cc3bec0746 | |||
| 180a69bd40 | |||
| 6de51bc11c | |||
| bc9f086245 | |||
| f84cc03a6c | |||
| 96dda1d8a9 | |||
| 87b791ee3a | |||
| ae5da6d9bd | |||
| 546b6fd13f | |||
| e8d1947f18 | |||
| 9e55cb5f8b | |||
| 7f57175519 | |||
| b21b32f746 | |||
| 3827d54ba8 | |||
| 9801b0cdf5 | |||
| 154f33b15b | |||
| fa217c3b25 | |||
| 1fb8daf389 | |||
| f2213d1def | |||
| 6997a6fa71 | |||
| e1ae768f26 | |||
| 57c4461952 | |||
| 5c46ff4a1e | |||
| 9112102c16 | |||
| 9d1eb8bee9 | |||
| 39fe8a59c7 | |||
| 5870f7d440 | |||
| 4a060e4669 | |||
| 3981833a3b | |||
| 1ea84dbb28 | |||
| 195aa56ceb | |||
| b205800a66 | |||
| 5e538db201 | |||
| 0bcd23195f | |||
| ca0196f927 | |||
| 874c8063aa | |||
| d877f79d5c | |||
| 3cfd2cb9bd | |||
| bf1f8410c1 | |||
| ad7c63617b | |||
| 00b25dde9d | |||
| e3118a4b89 | |||
| 23f5ee0d22 | |||
| 5c4df232d5 | |||
| 7f0e6f7996 | |||
| ab60e6b0ab | |||
| 3b3c5c32f9 | |||
| 74b76dd61d | |||
| 3b3db608e8 | |||
| b1a24cb50e | |||
| 1855a8b72c | |||
| 42ab513ad9 | |||
| ba04546b17 | |||
| 6feec98b4c | |||
| 43e1c60baa | |||
| 2f5fc04244 | |||
| aa45a49c3a | |||
| dd8ff8b1bf | |||
| 25667ce0f0 | |||
| ccf48e6426 | |||
| da79be0d22 | |||
| 01afb01198 | |||
| 37723ddc30 | |||
| b672d9bd10 | |||
| fa8a7b7590 | |||
| d3c8706c33 | |||
| 071e805457 | |||
| 4f30f5047e | |||
| e173a27c6e | |||
| 5891cc071e | |||
| b02acb44fb | |||
| c4a01a9f72 | |||
| be5a619b89 | |||
| 00a3aa4ca4 | |||
| dee908a347 | |||
| 4f8681a8c6 | |||
| 675f29f51f | |||
| 2efe16376c | |||
| 607e30da7c | |||
| d6f96f3035 | |||
| 81cea863e4 | |||
| ac721d0c4d | |||
| 06f12cd740 | |||
| dda87f3222 | |||
| 8b1c4773c3 | |||
| 9b71c105fc | |||
| 82d603cbab | |||
| e6d0e6e128 | |||
| 4c62d9f1e7 | |||
| c40251b2a1 | |||
| b7fa90d37e | |||
| e3b787204d | |||
| 0a5d61b510 | |||
| ef3cb0f48c | |||
| 19b5e52ba6 | |||
| 6b75654511 | |||
| 22c5ca6384 | |||
| eacfaaf8f6 | |||
| f3e574e8c9 | |||
| cc02c6c3c0 | |||
| 7ec53f259e | |||
| dddd4c5440 | |||
| 1a9836a7d9 | |||
| 26eec0c0ef | |||
| 3ba7a79010 | |||
| 6ab54d7ec9 | |||
| af4f175e0c | |||
| 9907408541 | |||
| b3e0d730da | |||
| edfd2ba9b7 | |||
| f2ce47cdcd | |||
| 6b10d365fc | |||
| da756ccffe | |||
| 5e99870a71 | |||
| ed5d6f63b1 | |||
| 89a58c27a0 |
Generated
+631
-711
File diff suppressed because it is too large
Load Diff
+9
-10
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "pwsp"
|
||||
version = "1.0.1"
|
||||
version = "1.3.0"
|
||||
edition = "2024"
|
||||
authors = ["arabian"]
|
||||
description = "PWSP lets you play audio files through your microphone. Has both CLI and GUI clients."
|
||||
@@ -12,23 +12,22 @@ keywords = ["soundpad", "pipewire", "linux", "cli", "gui"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.47.1", features = ["full"] }
|
||||
futures = { version = "0.3.31", features = ["thread-pool"] }
|
||||
tokio = { version = "1.48.0", features = ["full"] }
|
||||
async-trait = "0.1.89"
|
||||
|
||||
serde = { version = "1.0.227", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.148"
|
||||
|
||||
clap = { version = "4.5.48", default-features = false, features = ["std", "suggestions", "help", "usage", "error-context", "derive"] }
|
||||
clap = { version = "4.5.53", default-features = false, features = ["std", "suggestions", "help", "usage", "error-context", "derive"] }
|
||||
dirs = "6.0.0"
|
||||
|
||||
rodio = { version = "0.21.1", default-features = false, features = ["symphonia-all", "playback"] }
|
||||
pipewire = "0.9.2"
|
||||
rfd = "0.15.4"
|
||||
rfd = "0.16.0"
|
||||
|
||||
egui = { version = "0.32.3", default-features = false, features = ["default_fonts", "rayon"] }
|
||||
eframe = { version = "0.32.3", default-features = false, features = ["default_fonts", "glow", "x11", "wayland"] }
|
||||
egui_material_icons = "0.4.0"
|
||||
egui = { version = "0.33.3", default-features = false, features = ["default_fonts", "rayon"] }
|
||||
eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow", "x11", "wayland"] }
|
||||
egui_material_icons = "0.5.0"
|
||||
|
||||
[[bin]]
|
||||
name = "pwsp-daemon"
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
[Unit]
|
||||
Description=Pipewire Soundpad Daemon
|
||||
After=pipewire.service
|
||||
|
||||
[Service]
|
||||
ExecStartPre=/usr/bin/sleep 10
|
||||
ExecStart=/usr/bin/pwsp-daemon
|
||||
Restart=no
|
||||
RuntimeDirectory=pwsp
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
%global cargo_install_lib 0
|
||||
|
||||
Name: pwsp
|
||||
Version: 1.0.1
|
||||
Version: 1.3.0
|
||||
Release: %autorelease
|
||||
Summary: Lets you play audio files through your microphone
|
||||
|
||||
@@ -26,7 +26,7 @@ GUI clients.}
|
||||
%description %{_description}
|
||||
|
||||
%prep
|
||||
%autosetup -n pipewire-soundpad-v%{version} -p1
|
||||
%autosetup -n pipewire-soundpad-%{version} -p1
|
||||
|
||||
%build
|
||||
cargo build --release --locked
|
||||
|
||||
+24
-10
@@ -14,17 +14,17 @@ struct Cli {
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum Commands {
|
||||
/// Perform an action (ping, pause, resume, stop, play)
|
||||
/// Perform an action (ping, pause, resume, toggle-pause, stop, play)
|
||||
Action {
|
||||
#[clap(subcommand)]
|
||||
action: Actions,
|
||||
},
|
||||
/// Get information from the player (is paused, volume, position, state)
|
||||
/// Get information from the player (is paused, volume, position, duration, state, current-file-path, input, inputs)
|
||||
Get {
|
||||
#[clap(subcommand)]
|
||||
parameter: GetCommands,
|
||||
},
|
||||
/// Set information in the player (volume, position)
|
||||
/// Set information in the player (volume, position, input)
|
||||
Set {
|
||||
#[clap(subcommand)]
|
||||
parameter: SetCommands,
|
||||
@@ -39,10 +39,14 @@ enum Actions {
|
||||
Pause,
|
||||
/// Resume audio playback
|
||||
Resume,
|
||||
/// Toggle pause
|
||||
TogglePause,
|
||||
/// Stop audio playback and clear the queue
|
||||
Stop,
|
||||
/// Play a file
|
||||
Play { file_path: PathBuf },
|
||||
/// Toggle loop
|
||||
ToggleLoop,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
@@ -51,11 +55,11 @@ enum GetCommands {
|
||||
IsPaused,
|
||||
/// Playback volume
|
||||
Volume,
|
||||
/// Playback position
|
||||
/// Playback position (in seconds)
|
||||
Position,
|
||||
/// Duration of the current file
|
||||
Duration,
|
||||
/// Player state
|
||||
/// Player state (Playing, Paused or Stopped)
|
||||
State,
|
||||
/// Current playing file path
|
||||
CurrentFilePath,
|
||||
@@ -63,16 +67,20 @@ enum GetCommands {
|
||||
Input,
|
||||
/// All audio inputs
|
||||
Inputs,
|
||||
/// Is loop enabled (true or false)
|
||||
Loop,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum SetCommands {
|
||||
/// Playback volume
|
||||
Volume { volume: f32 },
|
||||
/// Playback position
|
||||
/// Playback position (in seconds)
|
||||
Position { position: f32 },
|
||||
/// Input
|
||||
Input { id: u32 },
|
||||
/// Audio input id (see pwsp-cli get inputs)
|
||||
Input { name: String },
|
||||
/// Enable or disable loop (true or false)
|
||||
Loop { enabled: String },
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -86,8 +94,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
Actions::Ping => Request::ping(),
|
||||
Actions::Pause => Request::pause(),
|
||||
Actions::Resume => Request::resume(),
|
||||
Actions::TogglePause => Request::toggle_pause(),
|
||||
Actions::Stop => Request::stop(),
|
||||
Actions::Play { file_path } => Request::play(file_path.to_str().unwrap()),
|
||||
Actions::ToggleLoop => Request::toggle_loop(),
|
||||
},
|
||||
Commands::Get { parameter } => match parameter {
|
||||
GetCommands::IsPaused => Request::get_is_paused(),
|
||||
@@ -98,15 +108,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
GetCommands::CurrentFilePath => Request::get_current_file_path(),
|
||||
GetCommands::Input => Request::get_input(),
|
||||
GetCommands::Inputs => Request::get_inputs(),
|
||||
GetCommands::Loop => Request::get_loop(),
|
||||
},
|
||||
Commands::Set { parameter } => match parameter {
|
||||
SetCommands::Volume { volume } => Request::set_volume(volume),
|
||||
SetCommands::Position { position } => Request::seek(position),
|
||||
SetCommands::Input { id } => Request::set_input(id),
|
||||
SetCommands::Input { name } => Request::set_input(&name),
|
||||
SetCommands::Loop { enabled } => Request::set_loop(&enabled),
|
||||
},
|
||||
};
|
||||
|
||||
let response = make_request(request).await?;
|
||||
let response = make_request(request)
|
||||
.await
|
||||
.map_err(|e| e as Box<dyn Error>)?;
|
||||
println!("{} : {}", response.status, response.message);
|
||||
|
||||
Ok(())
|
||||
|
||||
+48
-4
@@ -1,5 +1,8 @@
|
||||
use pwsp::{
|
||||
types::socket::{Request, Response},
|
||||
types::{
|
||||
audio_player::PlayerState,
|
||||
socket::{Request, Response},
|
||||
},
|
||||
utils::{
|
||||
commands::parse_command,
|
||||
daemon::{
|
||||
@@ -9,10 +12,11 @@ use pwsp::{
|
||||
pipewire::create_virtual_mic,
|
||||
},
|
||||
};
|
||||
use std::{error::Error, fs};
|
||||
use std::{error::Error, fs, time::Duration};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::UnixListener,
|
||||
time::sleep,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
@@ -44,6 +48,27 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
socket_path.to_str().unwrap_or_default()
|
||||
);
|
||||
|
||||
let commands_loop_handle = tokio::spawn(async {
|
||||
commands_loop(listener).await.ok();
|
||||
});
|
||||
|
||||
let player_loop_handle = tokio::spawn(async {
|
||||
player_loop().await;
|
||||
});
|
||||
|
||||
tokio::select! {
|
||||
_ = commands_loop_handle => {
|
||||
eprint!("Commands loop was finished, stopping program...");
|
||||
}
|
||||
_ = player_loop_handle => {
|
||||
eprint!("Audio Player loop was finished, stopping program...");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn commands_loop(listener: UnixListener) -> Result<(), Box<dyn Error>> {
|
||||
loop {
|
||||
let (mut stream, _addr) = listener.accept().await?;
|
||||
|
||||
@@ -64,7 +89,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
let request: Request = serde_json::from_slice(&buffer).unwrap();
|
||||
println!("Received request: {:?}", request);
|
||||
// ---------- Read request (end) ----------
|
||||
|
||||
// ---------- Generate response (start) ----------
|
||||
@@ -89,8 +113,28 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
eprintln!("Failed to write response to client!");
|
||||
return;
|
||||
}
|
||||
println!("Sent response: {:?}", response);
|
||||
// ---------- Send response (end) ----------
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn player_loop() {
|
||||
loop {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
|
||||
// Start playback again if loop is enabled
|
||||
let should_play = audio_player.get_state() == PlayerState::Stopped
|
||||
&& audio_player.current_file_path.is_some()
|
||||
&& audio_player.looped;
|
||||
|
||||
if should_play {
|
||||
let file_path = audio_player.current_file_path.clone().unwrap();
|
||||
audio_player
|
||||
.play(&file_path)
|
||||
.await
|
||||
.expect("Something went wrong while trying to play the file");
|
||||
}
|
||||
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
||||
+98
-56
@@ -1,7 +1,7 @@
|
||||
use crate::gui::SoundpadGui;
|
||||
use crate::gui::{SUPPORTED_EXTENSIONS, SoundpadGui};
|
||||
use egui::{
|
||||
AtomExt, Button, Color32, ComboBox, FontFamily, Label, RichText, ScrollArea, Slider, TextEdit,
|
||||
Ui, Vec2,
|
||||
Align, AtomExt, Button, Color32, ComboBox, FontFamily, Label, Layout, RichText, ScrollArea,
|
||||
Slider, TextEdit, Ui, Vec2,
|
||||
};
|
||||
use egui_material_icons::icons;
|
||||
use pwsp::types::audio_player::PlayerState;
|
||||
@@ -48,10 +48,15 @@ impl SoundpadGui {
|
||||
&mut self.config.save_scale_factor,
|
||||
"Always remember UI scale factor",
|
||||
);
|
||||
let pause_on_exit_response = ui.checkbox(
|
||||
&mut self.config.pause_on_exit,
|
||||
"Pause audio playback when the window is closed",
|
||||
);
|
||||
|
||||
if save_volume_response.changed()
|
||||
|| save_input_response.changed()
|
||||
|| save_scale_response.changed()
|
||||
|| pause_on_exit_response.changed()
|
||||
{
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
@@ -103,19 +108,35 @@ impl SoundpadGui {
|
||||
}
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Loop Button ----------
|
||||
let loop_button = Button::new(
|
||||
RichText::new(match self.audio_player_state.looped {
|
||||
true => icons::ICON_REPEAT_ONE,
|
||||
false => icons::ICON_REPEAT,
|
||||
})
|
||||
.size(18.0),
|
||||
)
|
||||
.frame(false);
|
||||
|
||||
let loop_button_response = ui.add_sized([15.0, 30.0], loop_button);
|
||||
if loop_button_response.clicked() {
|
||||
self.toggle_loop();
|
||||
}
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Position Slider ----------
|
||||
let position_slider = Slider::new(
|
||||
&mut self.app_state.position_slider_value,
|
||||
0.0..=self.audio_player_state.duration,
|
||||
)
|
||||
.show_value(false)
|
||||
.step_by(1.0);
|
||||
.step_by(0.01);
|
||||
|
||||
let default_slider_width = ui.spacing().slider_width;
|
||||
let position_slider_width = ui.available_width()
|
||||
- (30.0 * 3.0)
|
||||
- default_slider_width
|
||||
- (ui.spacing().item_spacing.x * 5.0);
|
||||
- (ui.spacing().item_spacing.x * 6.0);
|
||||
ui.spacing_mut().slider_width = position_slider_width;
|
||||
let position_slider_response = ui.add_sized([30.0, 30.0], position_slider);
|
||||
if position_slider_response.drag_stopped() {
|
||||
@@ -145,7 +166,7 @@ impl SoundpadGui {
|
||||
icons::ICON_VOLUME_DOWN
|
||||
};
|
||||
let volume_icon = Label::new(RichText::new(volume_icon).size(18.0));
|
||||
ui.add_sized([30.0, 25.0], volume_icon);
|
||||
ui.add_sized([30.0, 30.0], volume_icon);
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Volume Slider ----------
|
||||
@@ -182,6 +203,8 @@ impl SoundpadGui {
|
||||
ui.set_min_height(area_size.y);
|
||||
|
||||
ScrollArea::vertical().id_salt(0).show(ui, |ui| {
|
||||
ui.set_min_width(area_size.x);
|
||||
|
||||
let mut dirs: Vec<PathBuf> = self.app_state.dirs.iter().cloned().collect();
|
||||
dirs.sort();
|
||||
for path in dirs.iter() {
|
||||
@@ -191,28 +214,43 @@ impl SoundpadGui {
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
||||
|
||||
let mut dir_button_text = RichText::new(name.clone());
|
||||
if let Some(current_dir) = &self.app_state.current_dir {
|
||||
if current_dir.eq(path) {
|
||||
dir_button_text = dir_button_text.color(Color32::WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
let dir_button =
|
||||
Button::new(RichText::new(name).atom_max_width(area_size.x))
|
||||
.frame(false);
|
||||
Button::new(dir_button_text.atom_max_width(area_size.x)).frame(false);
|
||||
|
||||
let dir_button_response = ui.add(dir_button);
|
||||
if dir_button_response.clicked() {
|
||||
self.app_state.current_dir = Some(path.clone());
|
||||
self.open_dir(path);
|
||||
}
|
||||
|
||||
let delete_dir_button = Button::new(icons::ICON_DELETE).frame(false);
|
||||
let delete_dir_button_response =
|
||||
ui.add_sized([18.0, 18.0], delete_dir_button);
|
||||
if delete_dir_button_response.clicked() {
|
||||
self.remove_dir(path.clone());
|
||||
self.remove_dir(&path.clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let add_dir_button = egui::Button::new(icons::ICON_ADD).frame(false);
|
||||
let add_dir_button_response = ui.add_sized([18.0, 18.0], add_dir_button);
|
||||
if add_dir_button_response.clicked() {
|
||||
self.add_dir();
|
||||
let add_dirs_button = Button::new(icons::ICON_ADD).frame(false);
|
||||
let add_dirs_button_response = ui.add_sized([18.0, 18.0], add_dirs_button);
|
||||
if add_dirs_button_response.clicked() {
|
||||
self.add_dirs();
|
||||
}
|
||||
});
|
||||
|
||||
ui.with_layout(Layout::bottom_up(Align::Min), |ui| {
|
||||
let play_file_button = Button::new("Play file");
|
||||
let play_file_button_response = ui.add(play_file_button);
|
||||
if play_file_button_response.clicked() {
|
||||
self.open_file();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -220,16 +258,14 @@ impl SoundpadGui {
|
||||
}
|
||||
|
||||
fn draw_files(&mut self, ui: &mut Ui, area_size: Vec2) {
|
||||
let extensions = [
|
||||
"mp3", "wav", "ogg", "flac", "mp4", "m4a", "aac", "mov", "mkv", "webm", "avi",
|
||||
];
|
||||
|
||||
ui.vertical(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(
|
||||
let search_field = ui.add_sized(
|
||||
[ui.available_width(), 22.0],
|
||||
TextEdit::singleline(&mut self.app_state.search_query).hint_text("Search..."),
|
||||
);
|
||||
|
||||
self.app_state.search_field_id = Some(search_field.id);
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
@@ -239,43 +275,49 @@ impl SoundpadGui {
|
||||
ui.set_min_height(area_size.y);
|
||||
|
||||
ui.vertical(|ui| {
|
||||
if let Some(path) = self.app_state.current_dir.clone() {
|
||||
for entry in path.read_dir().unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let entry_path = entry.path();
|
||||
let mut files: Vec<PathBuf> = self.app_state.files.iter().cloned().collect();
|
||||
files.sort();
|
||||
|
||||
if entry_path.is_dir() {
|
||||
continue;
|
||||
for entry_path in files {
|
||||
if entry_path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !SUPPORTED_EXTENSIONS
|
||||
.contains(&entry_path.extension().unwrap_or_default().to_str().unwrap())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_name = entry_path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
let search_query = self
|
||||
.app_state
|
||||
.search_query
|
||||
.to_lowercase()
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
if !file_name.to_lowercase().contains(search_query.as_str()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut file_button_text = RichText::new(file_name);
|
||||
if let Some(current_file) = &self.app_state.selected_file {
|
||||
if current_file.eq(&entry_path) {
|
||||
file_button_text = file_button_text.color(Color32::WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
if !extensions.contains(
|
||||
&entry_path.extension().unwrap_or_default().to_str().unwrap(),
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_name = entry_path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
let search_query = self
|
||||
.app_state
|
||||
.search_query
|
||||
.to_lowercase()
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
if !file_name.to_lowercase().contains(search_query.as_str()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_button = Button::new(file_name).frame(false);
|
||||
let file_button_response = ui.add(file_button);
|
||||
if file_button_response.clicked() {
|
||||
self.play_file(entry_path);
|
||||
}
|
||||
let file_button = Button::new(file_button_text).frame(false);
|
||||
let file_button_response = ui.add(file_button);
|
||||
if file_button_response.clicked() {
|
||||
self.play_file(&entry_path);
|
||||
self.app_state.selected_file = Some(entry_path);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -287,7 +329,7 @@ impl SoundpadGui {
|
||||
ui.add_space(5.0);
|
||||
ui.horizontal_top(|ui| {
|
||||
// ---------- Microphone selection ----------
|
||||
let mut mics: Vec<(&u32, &String)> =
|
||||
let mut mics: Vec<(&String, &String)> =
|
||||
self.audio_player_state.all_inputs.iter().collect();
|
||||
mics.sort_by_key(|(k, _)| *k);
|
||||
|
||||
@@ -301,8 +343,8 @@ impl SoundpadGui {
|
||||
.unwrap_or(&String::new()),
|
||||
)
|
||||
.show_ui(ui, |ui| {
|
||||
for (index, device) in mics {
|
||||
ui.selectable_value(&mut selected_input, index.to_owned(), device);
|
||||
for (name, nick) in mics {
|
||||
ui.selectable_value(&mut selected_input, name.to_owned(), nick);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+90
-7
@@ -1,23 +1,106 @@
|
||||
use crate::gui::SoundpadGui;
|
||||
use egui::{Context, Key};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
impl SoundpadGui {
|
||||
pub fn handle_input(&mut self, ctx: &Context) {
|
||||
if ctx.memory(|reader| { reader.focused() }.is_some()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.input(|i| {
|
||||
// Close app on espace
|
||||
if i.key_pressed(Key::Escape) {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
if !self.app_state.show_settings && i.key_pressed(Key::Space) {
|
||||
self.play_toggle();
|
||||
}
|
||||
|
||||
if i.key_pressed(Key::Slash) {
|
||||
// Open/close settings
|
||||
if i.key_pressed(Key::I) {
|
||||
self.app_state.show_settings = !self.app_state.show_settings;
|
||||
}
|
||||
|
||||
if self.app_state.show_settings && i.key_pressed(Key::Backspace) {
|
||||
self.app_state.show_settings = false;
|
||||
if i.key_pressed(Key::Enter) && self.app_state.selected_file.is_some() {
|
||||
self.play_file(&self.app_state.selected_file.clone().unwrap());
|
||||
}
|
||||
|
||||
if !self.app_state.show_settings {
|
||||
// Pause / resume audio on space
|
||||
if i.key_pressed(Key::Space) {
|
||||
self.play_toggle();
|
||||
}
|
||||
|
||||
// Focus search field
|
||||
if i.key_pressed(Key::Slash) && self.app_state.search_field_id.is_some() {
|
||||
self.app_state.force_focus_id = self.app_state.search_field_id;
|
||||
}
|
||||
|
||||
// Iterate through dirs if there are some
|
||||
if i.modifiers.ctrl {
|
||||
let arrow_up_pressed = i.key_pressed(Key::ArrowUp);
|
||||
let arrow_down_pressed = i.key_pressed(Key::ArrowDown);
|
||||
|
||||
if arrow_up_pressed || arrow_down_pressed {
|
||||
if i.modifiers.shift && !self.app_state.dirs.is_empty() {
|
||||
let mut dirs: Vec<PathBuf> =
|
||||
self.app_state.dirs.iter().cloned().collect();
|
||||
dirs.sort();
|
||||
|
||||
let current_dir_index: i8;
|
||||
if let Some(current_dir) = &self.app_state.current_dir {
|
||||
if let Some(index) = dirs.iter().position(|x| x == current_dir) {
|
||||
current_dir_index = index as i8;
|
||||
} else {
|
||||
current_dir_index = -1;
|
||||
}
|
||||
} else {
|
||||
current_dir_index = -1;
|
||||
}
|
||||
|
||||
let mut new_dir_index: i8;
|
||||
|
||||
new_dir_index = current_dir_index - arrow_up_pressed as i8
|
||||
+ arrow_down_pressed as i8;
|
||||
|
||||
if new_dir_index < 0 {
|
||||
new_dir_index = (dirs.len() - 1) as i8;
|
||||
} else if new_dir_index >= dirs.len() as i8 {
|
||||
new_dir_index = 0;
|
||||
}
|
||||
|
||||
self.open_dir(&dirs[new_dir_index as usize]);
|
||||
} else if self.app_state.current_dir.is_some() {
|
||||
let mut files: Vec<PathBuf> =
|
||||
self.app_state.files.iter().cloned().collect();
|
||||
files.sort();
|
||||
|
||||
let current_files_index: i64;
|
||||
if let Some(selected_file) = &self.app_state.selected_file {
|
||||
if let Some(index) = files.iter().position(|x| x == selected_file) {
|
||||
current_files_index = index as i64;
|
||||
} else {
|
||||
current_files_index = -1;
|
||||
}
|
||||
} else {
|
||||
current_files_index = -1;
|
||||
}
|
||||
|
||||
let mut new_files_index: i64;
|
||||
|
||||
new_files_index = current_files_index - arrow_up_pressed as i64
|
||||
+ arrow_down_pressed as i64;
|
||||
|
||||
if new_files_index < 0 {
|
||||
new_files_index = (files.len() - 1) as i64;
|
||||
} else if new_files_index >= files.len() as i64 {
|
||||
new_files_index = 0;
|
||||
}
|
||||
|
||||
self.app_state.selected_file =
|
||||
Some(files[new_files_index as usize].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+61
-23
@@ -23,6 +23,10 @@ use std::{
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
const SUPPORTED_EXTENSIONS: [&str; 11] = [
|
||||
"mp3", "wav", "ogg", "flac", "mp4", "m4a", "aac", "mov", "mkv", "webm", "avi",
|
||||
];
|
||||
|
||||
struct SoundpadGui {
|
||||
pub app_state: AppState,
|
||||
pub config: GuiConfig,
|
||||
@@ -52,55 +56,83 @@ impl SoundpadGui {
|
||||
}
|
||||
|
||||
pub fn play_toggle(&mut self) {
|
||||
let mut guard = self.audio_player_state_shared.lock().unwrap();
|
||||
guard.state = match guard.state {
|
||||
PlayerState::Playing => {
|
||||
make_request_sync(Request::pause()).ok();
|
||||
guard.new_state = Some(PlayerState::Paused);
|
||||
PlayerState::Paused
|
||||
let (new_state, request) = {
|
||||
let guard = self.audio_player_state_shared.lock().unwrap();
|
||||
match guard.state {
|
||||
PlayerState::Playing => (Some(PlayerState::Paused), Some(Request::pause())),
|
||||
PlayerState::Paused => (Some(PlayerState::Playing), Some(Request::resume())),
|
||||
PlayerState::Stopped => (None, None),
|
||||
}
|
||||
PlayerState::Paused => {
|
||||
make_request_sync(Request::resume()).ok();
|
||||
guard.new_state = Some(PlayerState::Playing);
|
||||
PlayerState::Playing
|
||||
}
|
||||
PlayerState::Stopped => PlayerState::Stopped,
|
||||
};
|
||||
|
||||
if let Some(req) = request {
|
||||
make_request_sync(req).ok();
|
||||
}
|
||||
|
||||
if let Some(state) = new_state {
|
||||
let mut guard = self.audio_player_state_shared.lock().unwrap();
|
||||
guard.new_state = Some(state.clone());
|
||||
guard.state = state;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_dir(&mut self) {
|
||||
pub fn open_file(&mut self) {
|
||||
let file_dialog = FileDialog::new().add_filter("Audio File", &SUPPORTED_EXTENSIONS);
|
||||
if let Some(path) = file_dialog.pick_file() {
|
||||
self.play_file(&path);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_dirs(&mut self) {
|
||||
let file_dialog = FileDialog::new();
|
||||
if let Some(path) = file_dialog.pick_folder() {
|
||||
self.app_state.dirs.insert(path);
|
||||
if let Some(paths) = file_dialog.pick_folders() {
|
||||
for path in paths {
|
||||
self.app_state.dirs.insert(path);
|
||||
}
|
||||
self.config.dirs = self.app_state.dirs.clone();
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_dir(&mut self, path: PathBuf) {
|
||||
self.app_state.dirs.remove(&path);
|
||||
pub fn remove_dir(&mut self, path: &PathBuf) {
|
||||
self.app_state.dirs.remove(path);
|
||||
if let Some(current_dir) = &self.app_state.current_dir
|
||||
&& current_dir == &path
|
||||
&& current_dir == path
|
||||
{
|
||||
self.app_state.current_dir = None;
|
||||
self.app_state.files.clear();
|
||||
}
|
||||
self.config.dirs = self.app_state.dirs.clone();
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
|
||||
pub fn play_file(&mut self, path: PathBuf) {
|
||||
pub fn open_dir(&mut self, path: &PathBuf) {
|
||||
self.app_state.current_dir = Some(path.clone());
|
||||
self.app_state.files = path
|
||||
.read_dir()
|
||||
.unwrap()
|
||||
.filter_map(|res| res.ok())
|
||||
.map(|entry| entry.path())
|
||||
.collect();
|
||||
}
|
||||
|
||||
pub fn play_file(&mut self, path: &PathBuf) {
|
||||
make_request_sync(Request::play(path.to_str().unwrap())).ok();
|
||||
}
|
||||
|
||||
pub fn set_input(&mut self, id: u32) {
|
||||
make_request_sync(Request::set_input(id)).ok();
|
||||
pub fn set_input(&mut self, name: String) {
|
||||
make_request_sync(Request::set_input(&name)).ok();
|
||||
|
||||
if self.config.save_input {
|
||||
let mut daemon_config = get_daemon_config();
|
||||
daemon_config.default_input_id = Some(id);
|
||||
daemon_config.default_input_name = Some(name);
|
||||
daemon_config.save_to_file().ok();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_loop(&mut self) {
|
||||
make_request_sync(Request::toggle_loop()).ok();
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run() -> Result<(), Box<dyn Error>> {
|
||||
@@ -128,7 +160,13 @@ pub async fn run() -> Result<(), Box<dyn Error>> {
|
||||
Ok(Box::new(SoundpadGui::new(&cc.egui_ctx)))
|
||||
}),
|
||||
) {
|
||||
Ok(_) => Ok(()),
|
||||
Ok(_) => {
|
||||
let config = get_gui_config();
|
||||
if config.pause_on_exit {
|
||||
make_request_sync(Request::pause()).ok();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,13 @@ impl App for SoundpadGui {
|
||||
}
|
||||
|
||||
self.draw(ui).ok();
|
||||
|
||||
if let Some(force_focus_id) = self.app_state.force_focus_id {
|
||||
ui.memory_mut(|reder| {
|
||||
reder.request_focus(force_focus_id);
|
||||
});
|
||||
self.app_state.force_focus_id = None;
|
||||
}
|
||||
});
|
||||
|
||||
if self.app_state.position_dragged {
|
||||
|
||||
@@ -33,6 +33,8 @@ pub struct AudioPlayer {
|
||||
pub duration: Option<f32>,
|
||||
|
||||
pub current_file_path: Option<PathBuf>,
|
||||
|
||||
pub looped: bool,
|
||||
}
|
||||
|
||||
impl AudioPlayer {
|
||||
@@ -40,8 +42,8 @@ impl AudioPlayer {
|
||||
let daemon_config = get_daemon_config();
|
||||
let default_volume = daemon_config.default_volume.unwrap_or(1.0);
|
||||
let mut default_input_device: Option<AudioDevice> = None;
|
||||
if let Some(id) = daemon_config.default_input_id
|
||||
&& let Ok(device) = get_device(id).await
|
||||
if let Some(name) = daemon_config.default_input_name
|
||||
&& let Ok(device) = get_device(&name).await
|
||||
&& device.device_type == DeviceType::Input
|
||||
{
|
||||
default_input_device = Some(device);
|
||||
@@ -62,6 +64,8 @@ impl AudioPlayer {
|
||||
duration: None,
|
||||
|
||||
current_file_path: None,
|
||||
|
||||
looped: false,
|
||||
};
|
||||
|
||||
if default_input_device.is_some() {
|
||||
@@ -147,7 +151,7 @@ impl AudioPlayer {
|
||||
self.sink.is_paused()
|
||||
}
|
||||
|
||||
pub fn get_state(&mut self) -> PlayerState {
|
||||
pub fn get_state(&self) -> PlayerState {
|
||||
if self.sink.len() == 0 {
|
||||
return PlayerState::Stopped;
|
||||
}
|
||||
@@ -164,7 +168,7 @@ impl AudioPlayer {
|
||||
self.sink.set_volume(volume);
|
||||
}
|
||||
|
||||
pub fn get_position(&mut self) -> f32 {
|
||||
pub fn get_position(&self) -> f32 {
|
||||
if self.get_state() == PlayerState::Stopped {
|
||||
return 0.0;
|
||||
}
|
||||
@@ -172,7 +176,11 @@ impl AudioPlayer {
|
||||
self.sink.get_pos().as_secs_f32()
|
||||
}
|
||||
|
||||
pub fn seek(&mut self, position: f32) -> Result<(), Box<dyn Error>> {
|
||||
pub fn seek(&mut self, mut position: f32) -> Result<(), Box<dyn Error>> {
|
||||
if position < 0.0 {
|
||||
position = 0.0;
|
||||
}
|
||||
|
||||
match self.sink.try_seek(Duration::from_secs_f32(position)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err.into()),
|
||||
@@ -218,14 +226,14 @@ impl AudioPlayer {
|
||||
}
|
||||
|
||||
pub fn get_current_file_path(&mut self) -> &Option<PathBuf> {
|
||||
if self.get_state() == PlayerState::Stopped {
|
||||
if self.get_state() == PlayerState::Stopped && !self.looped {
|
||||
self.current_file_path = None;
|
||||
}
|
||||
&self.current_file_path
|
||||
}
|
||||
|
||||
pub async fn set_current_input_device(&mut self, id: u32) -> Result<(), Box<dyn Error>> {
|
||||
let input_device = get_device(id).await?;
|
||||
pub async fn set_current_input_device(&mut self, name: &str) -> Result<(), Box<dyn Error>> {
|
||||
let input_device = get_device(name).await?;
|
||||
|
||||
if input_device.device_type != DeviceType::Input {
|
||||
return Err("Selected device is not an input device".into());
|
||||
|
||||
+72
-8
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
types::socket::Response,
|
||||
types::{audio_player::PlayerState, socket::Response},
|
||||
utils::{daemon::get_audio_player, pipewire::get_all_devices},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
@@ -16,6 +16,8 @@ pub struct PauseCommand {}
|
||||
|
||||
pub struct ResumeCommand {}
|
||||
|
||||
pub struct TogglePauseCommand {}
|
||||
|
||||
pub struct StopCommand {}
|
||||
|
||||
pub struct IsPausedCommand {}
|
||||
@@ -47,9 +49,17 @@ pub struct GetCurrentInputCommand {}
|
||||
pub struct GetAllInputsCommand {}
|
||||
|
||||
pub struct SetCurrentInputCommand {
|
||||
pub id: Option<u32>,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
pub struct GetLoopCommand {}
|
||||
|
||||
pub struct SetLoopCommand {
|
||||
pub enabled: Option<bool>,
|
||||
}
|
||||
|
||||
pub struct ToggleLoopCommand {}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for PingCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
@@ -75,6 +85,25 @@ impl Executable for ResumeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for TogglePauseCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
|
||||
if audio_player.get_state() == PlayerState::Stopped {
|
||||
return Response::new(false, "Audio is not playing");
|
||||
}
|
||||
|
||||
if audio_player.is_paused() {
|
||||
audio_player.resume();
|
||||
Response::new(true, "Audio was resumed")
|
||||
} else {
|
||||
audio_player.pause();
|
||||
Response::new(true, "Audio was paused")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for StopCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
@@ -96,7 +125,7 @@ impl Executable for IsPausedCommand {
|
||||
#[async_trait]
|
||||
impl Executable for GetStateCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
let state = audio_player.get_state();
|
||||
Response::new(true, serde_json::to_string(&state).unwrap())
|
||||
}
|
||||
@@ -127,7 +156,7 @@ impl Executable for SetVolumeCommand {
|
||||
#[async_trait]
|
||||
impl Executable for GetPositionCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
let position = audio_player.get_position();
|
||||
Response::new(true, position.to_string())
|
||||
}
|
||||
@@ -192,7 +221,10 @@ impl Executable for GetCurrentInputCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
if let Some(input_device) = &audio_player.current_input_device {
|
||||
Response::new(true, format!("{} - {}", input_device.id, input_device.nick))
|
||||
Response::new(
|
||||
true,
|
||||
format!("{} - {}", input_device.name, input_device.nick),
|
||||
)
|
||||
} else {
|
||||
Response::new(false, "No input device selected")
|
||||
}
|
||||
@@ -209,7 +241,7 @@ impl Executable for GetAllInputsCommand {
|
||||
continue;
|
||||
}
|
||||
|
||||
let string = format!("{} - {}", device.id, device.nick);
|
||||
let string = format!("{} - {}", device.name, device.nick);
|
||||
input_devices_strings.push(string);
|
||||
}
|
||||
let response_message = input_devices_strings.join("; ");
|
||||
@@ -221,9 +253,9 @@ impl Executable for GetAllInputsCommand {
|
||||
#[async_trait]
|
||||
impl Executable for SetCurrentInputCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
if let Some(id) = self.id {
|
||||
if let Some(name) = &self.name {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
match audio_player.set_current_input_device(id).await {
|
||||
match audio_player.set_current_input_device(name).await {
|
||||
Ok(_) => Response::new(true, "Input device was set"),
|
||||
Err(err) => Response::new(false, err.to_string()),
|
||||
}
|
||||
@@ -232,3 +264,35 @@ impl Executable for SetCurrentInputCommand {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetLoopCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
Response::new(true, audio_player.looped.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for SetLoopCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
|
||||
match self.enabled {
|
||||
Some(enabled) => {
|
||||
audio_player.looped = enabled;
|
||||
Response::new(true, format!("Loop was set to {}", enabled))
|
||||
}
|
||||
None => Response::new(false, "Invalid enabled value"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for ToggleLoopCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
audio_player.looped = !audio_player.looped;
|
||||
Response::new(true, format!("Loop was set to {}", audio_player.looped))
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -4,7 +4,7 @@ use std::{collections::HashSet, error::Error, fs, path::PathBuf};
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct DaemonConfig {
|
||||
pub default_input_id: Option<u32>,
|
||||
pub default_input_name: Option<String>,
|
||||
pub default_volume: Option<f32>,
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ pub struct GuiConfig {
|
||||
pub save_volume: bool,
|
||||
pub save_input: bool,
|
||||
pub save_scale_factor: bool,
|
||||
pub pause_on_exit: bool,
|
||||
|
||||
pub dirs: HashSet<PathBuf>,
|
||||
}
|
||||
@@ -48,6 +49,7 @@ impl Default for GuiConfig {
|
||||
save_volume: false,
|
||||
save_input: false,
|
||||
save_scale_factor: false,
|
||||
pause_on_exit: false,
|
||||
|
||||
dirs: HashSet::default(),
|
||||
}
|
||||
|
||||
+12
-2
@@ -1,4 +1,7 @@
|
||||
use crate::types::audio_player::PlayerState;
|
||||
|
||||
use egui::Id;
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
path::PathBuf,
|
||||
@@ -18,6 +21,12 @@ pub struct AppState {
|
||||
|
||||
pub current_dir: Option<PathBuf>,
|
||||
pub dirs: HashSet<PathBuf>,
|
||||
|
||||
pub selected_file: Option<PathBuf>,
|
||||
pub files: HashSet<PathBuf>,
|
||||
|
||||
pub search_field_id: Option<Id>,
|
||||
pub force_focus_id: Option<Id>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
@@ -27,6 +36,7 @@ pub struct AudioPlayerState {
|
||||
pub current_file_path: PathBuf,
|
||||
|
||||
pub is_paused: bool,
|
||||
pub looped: bool,
|
||||
|
||||
pub volume: f32,
|
||||
pub new_volume: Option<f32>,
|
||||
@@ -34,6 +44,6 @@ pub struct AudioPlayerState {
|
||||
pub new_position: Option<f32>,
|
||||
pub duration: f32,
|
||||
|
||||
pub current_input: u32,
|
||||
pub all_inputs: HashMap<u32, String>,
|
||||
pub current_input: String,
|
||||
pub all_inputs: HashMap<String, String>,
|
||||
}
|
||||
|
||||
+18
-2
@@ -32,6 +32,10 @@ impl Request {
|
||||
Request::new("resume", vec![])
|
||||
}
|
||||
|
||||
pub fn toggle_pause() -> Self {
|
||||
Request::new("toggle_pause", vec![])
|
||||
}
|
||||
|
||||
pub fn stop() -> Self {
|
||||
Request::new("stop", vec![])
|
||||
}
|
||||
@@ -80,8 +84,20 @@ impl Request {
|
||||
Request::new("seek", vec![("position", &position.to_string())])
|
||||
}
|
||||
|
||||
pub fn set_input(id: u32) -> Self {
|
||||
Request::new("set_input", vec![("input_id", &id.to_string())])
|
||||
pub fn set_input(name: &str) -> Self {
|
||||
Request::new("set_input", vec![("input_name", name)])
|
||||
}
|
||||
|
||||
pub fn get_loop() -> Self {
|
||||
Request::new("get_loop", vec![])
|
||||
}
|
||||
|
||||
pub fn set_loop(enabled: &str) -> Self {
|
||||
Request::new("set_loop", vec![("enabled", enabled)])
|
||||
}
|
||||
|
||||
pub fn toggle_loop() -> Self {
|
||||
Request::new("toggle_loop", vec![])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-7
@@ -7,6 +7,7 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
"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 {})),
|
||||
"is_paused" => Some(Box::new(IsPausedCommand {})),
|
||||
"get_state" => Some(Box::new(GetStateCommand {})),
|
||||
@@ -44,14 +45,20 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
"get_input" => Some(Box::new(GetCurrentInputCommand {})),
|
||||
"get_inputs" => Some(Box::new(GetAllInputsCommand {})),
|
||||
"set_input" => {
|
||||
let id = request
|
||||
.args
|
||||
.get("input_id")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<u32>()
|
||||
.ok();
|
||||
Some(Box::new(SetCurrentInputCommand { id }))
|
||||
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
|
||||
.get("enabled")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<bool>()
|
||||
.ok();
|
||||
Some(Box::new(SetLoopCommand { enabled }))
|
||||
}
|
||||
"toggle_loop" => Some(Box::new(ToggleLoopCommand {})),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ pub async fn wait_for_daemon() -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn make_request(request: Request) -> Result<Response, Box<dyn Error>> {
|
||||
pub async fn make_request(request: Request) -> Result<Response, Box<dyn Error + Send + Sync>> {
|
||||
let socket_path = get_runtime_dir().join("daemon.sock");
|
||||
let mut stream = UnixStream::connect(socket_path).await?;
|
||||
|
||||
|
||||
+49
-21
@@ -24,7 +24,11 @@ pub fn get_gui_config() -> GuiConfig {
|
||||
}
|
||||
|
||||
pub fn make_request_sync(request: Request) -> Result<Response, Box<dyn Error>> {
|
||||
futures::executor::block_on(make_request(request))
|
||||
tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current()
|
||||
.block_on(make_request(request))
|
||||
.map_err(|e| e as Box<dyn Error>)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn format_time_pair(position: f32, duration: f32) -> String {
|
||||
@@ -40,7 +44,7 @@ pub fn format_time_pair(position: f32, duration: f32) -> String {
|
||||
|
||||
pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerState>>) {
|
||||
tokio::spawn(async move {
|
||||
let sleep_duration = Duration::from_millis(100);
|
||||
let sleep_duration = Duration::from_secs_f32(1.0 / 60.0);
|
||||
|
||||
loop {
|
||||
wait_for_daemon().await.ok();
|
||||
@@ -53,15 +57,39 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
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 = make_request(state_req).await.unwrap_or_default();
|
||||
let file_path_res = make_request(file_path_req).await.unwrap_or_default();
|
||||
let is_paused_res = make_request(is_paused_req).await.unwrap_or_default();
|
||||
let volume_res = make_request(volume_req).await.unwrap_or_default();
|
||||
let position_res = make_request(position_req).await.unwrap_or_default();
|
||||
let duration_res = make_request(duration_req).await.unwrap_or_default();
|
||||
let current_input_res = make_request(current_input_req).await.unwrap_or_default();
|
||||
let all_inputs_res = make_request(all_inputs_req).await.unwrap_or_default();
|
||||
let (
|
||||
state_res,
|
||||
file_path_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(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 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(),
|
||||
@@ -96,10 +124,8 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
.collect::<Vec<&str>>()
|
||||
.first()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.parse::<u32>()
|
||||
.unwrap_or_default(),
|
||||
false => 0,
|
||||
.to_string(),
|
||||
false => String::new(),
|
||||
};
|
||||
let all_inputs = match all_inputs_res.status {
|
||||
true => all_inputs_res
|
||||
@@ -111,16 +137,17 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
if entry.is_empty() {
|
||||
return None;
|
||||
}
|
||||
entry.split_once(" - ").and_then(|(k, v)| {
|
||||
k.trim()
|
||||
.parse::<u32>()
|
||||
.ok()
|
||||
.map(|key| (key, v.trim().to_string()))
|
||||
})
|
||||
entry
|
||||
.split_once(" - ")
|
||||
.map(|(k, v)| (k.trim().to_string(), v.trim().to_string()))
|
||||
})
|
||||
.collect(),
|
||||
.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();
|
||||
@@ -151,6 +178,7 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
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;
|
||||
|
||||
+12
-2
@@ -77,6 +77,8 @@ async fn pw_get_global_objects_thread(
|
||||
main_sender: mpsc::Sender<(Option<AudioDevice>, Option<Port>)>,
|
||||
pw_receiver: pipewire::channel::Receiver<Terminate>,
|
||||
) {
|
||||
pipewire::init();
|
||||
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
|
||||
// Stop main loop on Terminate message
|
||||
@@ -162,6 +164,10 @@ pub async fn get_all_devices() -> Result<(Vec<AudioDevice>, Vec<AudioDevice>), B
|
||||
input_device.input_fl = Some(port.clone());
|
||||
input_device.input_fr = Some(port)
|
||||
}
|
||||
"capture_MONO" => {
|
||||
input_device.input_fl = Some(port.clone());
|
||||
input_device.input_fr = Some(port);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if output_devices.contains_key(&node_id) {
|
||||
@@ -199,12 +205,12 @@ pub async fn get_all_devices() -> Result<(Vec<AudioDevice>, Vec<AudioDevice>), B
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_device(node_id: u32) -> Result<AudioDevice, Box<dyn Error>> {
|
||||
pub async fn get_device(device_name: &str) -> Result<AudioDevice, Box<dyn Error>> {
|
||||
let (mut input_devices, output_devices) = get_all_devices().await?;
|
||||
input_devices.extend(output_devices);
|
||||
|
||||
for device in input_devices {
|
||||
if device.id == node_id {
|
||||
if device.name == device_name {
|
||||
return Ok(device);
|
||||
}
|
||||
}
|
||||
@@ -216,6 +222,8 @@ pub fn create_virtual_mic() -> Result<pipewire::channel::Sender<Terminate>, Box<
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel::<Terminate>();
|
||||
|
||||
let _pw_thread = thread::spawn(move || {
|
||||
pipewire::init();
|
||||
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = ContextRc::new(&main_loop, None).expect("Failed to create pipewire context");
|
||||
let core = context
|
||||
@@ -257,6 +265,8 @@ pub fn create_link(
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel::<Terminate>();
|
||||
|
||||
let _pw_thread = thread::spawn(move || {
|
||||
pipewire::init();
|
||||
|
||||
let main_loop = MainLoopRc::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = ContextRc::new(&main_loop, None).expect("Failed to create pipewire context");
|
||||
let core = context
|
||||
|
||||
Reference in New Issue
Block a user