pwsp 1.7.4

PWSP lets you play audio files through your microphone. Has both CLI and GUI clients.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use crate::{
    types::pipewire::{DeviceType, Terminate},
    utils::{
        daemon::get_daemon_config,
        pipewire::{create_link, get_device, link_player_to_virtual_mic},
    },
};
use rodio::{Decoder, DeviceSinkBuilder, MixerDeviceSink, Player, Source};
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    error::Error,
    fs,
    path::{Path, PathBuf},
    time::Duration,
};

#[derive(Debug, Eq, PartialEq, Default, Clone, Serialize, Deserialize)]
pub enum PlayerState {
    #[default]
    Stopped,
    Paused,
    Playing,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrackInfo {
    pub id: u32,
    pub path: PathBuf,
    pub duration: Option<f32>,
    pub position: f32,
    pub volume: f32,
    pub looped: bool,
    pub paused: bool,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct FullState {
    pub state: PlayerState,
    pub tracks: Vec<TrackInfo>,
    pub volume: f32,
    pub current_input: String,
    pub all_inputs: HashMap<String, String>,
}

pub struct PlayingSound {
    pub id: u32,
    pub sink: Player,
    pub path: PathBuf,
    pub duration: Option<f32>,
    pub looped: bool,
    pub volume: f32,
}

pub struct AudioPlayer {
    stream_handle: Option<MixerDeviceSink>,
    pub tracks: HashMap<u32, PlayingSound>,
    pub next_id: u32,

    input_link_sender: Option<pipewire::channel::Sender<Terminate>>,
    player_link_sender: Option<pipewire::channel::Sender<Terminate>>,
    pub input_device_name: Option<String>,

    pub volume: f32, // Master volume
}

impl AudioPlayer {
    pub async fn new() -> Result<Self, Box<dyn Error>> {
        let daemon_config = get_daemon_config();
        let default_volume = daemon_config.default_volume.unwrap_or(1.0);

        let mut audio_player = AudioPlayer {
            stream_handle: None,
            tracks: HashMap::new(),
            next_id: 1,

            input_link_sender: None,
            player_link_sender: None,
            input_device_name: daemon_config.default_input_name.clone(),

            volume: default_volume,
        };

        if audio_player.input_device_name.is_some() {
            audio_player.link_devices().await?;
        }

        Ok(audio_player)
    }

    fn ensure_stream(&mut self) -> Result<&MixerDeviceSink, Box<dyn Error>> {
        if self.stream_handle.is_none() {
            let mut sink = DeviceSinkBuilder::open_default_sink()?;
            sink.log_on_drop(false);
            self.stream_handle = Some(sink);
        }
        Ok(self.stream_handle.as_ref().unwrap())
    }

    fn drop_stream(&mut self) {
        if self.stream_handle.is_some() {
            self.stream_handle = None;
            self.abort_player_link_thread();
        }
    }

    fn abort_link_thread(&mut self) {
        if let Some(sender) = &self.input_link_sender {
            if sender.send(Terminate {}).is_ok() {
                println!("Sent terminate signal to input link thread");
                self.input_link_sender = None;
            } else {
                eprintln!("Failed to send terminate signal to input link thread");
            }
        }
    }

    fn abort_player_link_thread(&mut self) {
        if let Some(sender) = &self.player_link_sender {
            if sender.send(Terminate {}).is_ok() {
                println!("Sent terminate signal to player link thread");
                self.player_link_sender = None;
            } else {
                eprintln!("Failed to send terminate signal to player link thread");
            }
        }
    }

    async fn link_player(&mut self) -> Result<(), Box<dyn Error>> {
        if self.player_link_sender.is_some() {
            return Ok(());
        }

        match link_player_to_virtual_mic().await {
            Ok(sender) => {
                self.player_link_sender = Some(sender);
                Ok(())
            }
            Err(_) => Ok(()),
        }
    }

    async fn link_devices(&mut self) -> Result<(), Box<dyn Error>> {
        self.abort_link_thread();

        let input_device;
        if let Some(input_device_name) = &self.input_device_name {
            if let Ok(device) = get_device(input_device_name).await {
                input_device = device;
            } else {
                eprintln!(
                    "Could not find selected input device {}, skipping device linking",
                    input_device_name
                );
                return Ok(());
            }
        } else {
            eprintln!("No input device selected, skipping device linking");
            return Ok(());
        }

        let daemon_input;
        if let Ok(device) = get_device("pwsp-virtual-mic").await {
            daemon_input = device;
        } else {
            eprintln!("Could not find pwsp-virtual-mic device, skipping device linking");
            return Ok(());
        }

        let Some(output_fl) = input_device.output_fl.clone() else {
            eprintln!("Failed to get pwsp-daemon output_fl");
            return Ok(());
        };
        let Some(output_fr) = input_device.output_fr.clone() else {
            eprintln!("Failed to get pwsp-daemon output_fr");
            return Ok(());
        };
        let Some(input_fl) = daemon_input.input_fl.clone() else {
            eprintln!("Failed to get pwsp-daemon input_fl");
            return Ok(());
        };
        let Some(input_fr) = daemon_input.input_fr.clone() else {
            eprintln!("Failed to get pwsp-daemon input_fr");
            return Ok(());
        };

        self.input_link_sender = Some(create_link(output_fl, output_fr, input_fl, input_fr)?);

        Ok(())
    }

    pub fn pause(&mut self, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.sink.pause();
            }
        } else {
            for sound in self.tracks.values_mut() {
                sound.sink.pause();
            }
        }
    }

    pub fn resume(&mut self, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.sink.play();
            }
        } else {
            for sound in self.tracks.values_mut() {
                sound.sink.play();
            }
        }
    }

    pub fn stop(&mut self, id: Option<u32>) {
        if let Some(id) = id {
            self.tracks.remove(&id);
        } else {
            self.tracks.clear();
        }
        if self.tracks.is_empty() {
            self.drop_stream();
        }
    }

    pub fn is_paused(&self) -> bool {
        if self.tracks.is_empty() {
            return false;
        }
        self.tracks.values().all(|s| s.sink.is_paused())
    }

    pub fn get_state(&self) -> PlayerState {
        if self.tracks.is_empty() {
            return PlayerState::Stopped;
        }

        if self
            .tracks
            .values()
            .any(|s| !s.sink.is_paused() && !s.sink.empty())
        {
            return PlayerState::Playing;
        }

        if self.is_paused() {
            return PlayerState::Paused;
        }

        PlayerState::Stopped
    }

    pub fn get_volume(&mut self, id: Option<u32>) -> Option<f32> {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                Some(sound.sink.volume())
            } else {
                None
            }
        } else {
            Some(self.volume)
        }
    }

    pub fn set_volume(&mut self, volume: f32, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.volume = volume;
                sound.sink.set_volume(self.volume * volume);
            }
        } else {
            self.volume = volume;
            for sound in self.tracks.values_mut() {
                sound.sink.set_volume(self.volume * sound.volume);
            }
        }
    }

    pub fn get_position(&self, id: Option<u32>) -> f32 {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get(&id) {
                return sound.sink.get_pos().as_secs_f32();
            }
        } else if let Some(sound) = self.tracks.values().last() {
            // Fallback to last added track if no ID
            return sound.sink.get_pos().as_secs_f32();
        }
        0.0
    }

    pub fn seek(&mut self, position: f32, id: Option<u32>) -> Result<(), Box<dyn Error>> {
        let position = if position < 0.0 { 0.0 } else { position };

        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.sink.try_seek(Duration::from_secs_f32(position))?;
            }
        } else {
            // Seek all? Or last? Let's seek all for now if no ID provided
            for sound in self.tracks.values_mut() {
                sound.sink.try_seek(Duration::from_secs_f32(position)).ok();
            }
        }
        Ok(())
    }

    pub fn get_duration(&mut self, id: Option<u32>) -> Result<f32, Box<dyn Error>> {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get(&id) {
                return sound.duration.ok_or("Unknown duration".into());
            }
        } else if let Some(sound) = self.tracks.values().last() {
            return sound.duration.ok_or("Unknown duration".into());
        }
        Err("No track playing".into())
    }

    pub async fn play(
        &mut self,
        file_path: &Path,
        concurrent: bool,
    ) -> Result<u32, Box<dyn Error>> {
        let path_buf = file_path.to_path_buf();

        let decoder_result =
            tokio::task::spawn_blocking(move || -> Result<_, Box<dyn Error + Send + Sync>> {
                if !path_buf.exists() {
                    return Err(format!("File does not exist: {}", path_buf.display()).into());
                }

                let file = fs::File::open(&path_buf)?;
                let decoder = Decoder::try_from(file)
                    .map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)?;
                Ok(decoder)
            })
            .await?;

        match decoder_result {
            Ok(source) => {
                if !concurrent {
                    self.tracks.clear();
                }

                self.ensure_stream()?;
                self.link_player().await.ok();

                let id = self.next_id;
                self.next_id += 1;

                let duration = source.total_duration().map(|d| d.as_secs_f32());

                let mixer = self.stream_handle.as_ref().unwrap().mixer();
                let sink = Player::connect_new(mixer);
                sink.set_volume(self.volume); // Default volume is 1.0 * master
                sink.append(source);
                sink.play();

                let sound = PlayingSound {
                    id,
                    sink,
                    path: file_path.to_path_buf(),
                    duration,
                    looped: false,
                    volume: 1.0,
                };

                self.tracks.insert(id, sound);

                Ok(id)
            }
            Err(err) => Err(err as Box<dyn Error>),
        }
    }

    pub fn set_loop(&mut self, enabled: bool, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.looped = enabled;
            }
        } else {
            // Set loop for all? Or just last?
            // Let's set for all.
            for sound in self.tracks.values_mut() {
                sound.looped = enabled;
            }
        }
    }

    pub fn get_tracks(&self) -> Vec<TrackInfo> {
        let mut tracks: Vec<_> = self
            .tracks
            .values()
            .map(|sound| TrackInfo {
                id: sound.id,
                path: sound.path.clone(),
                duration: sound.duration,
                position: sound.sink.get_pos().as_secs_f32(),
                volume: sound.volume,
                looped: sound.looped,
                paused: sound.sink.is_paused(),
            })
            .collect();
        tracks.sort_by_key(|t| t.id);
        tracks
    }

    pub async fn update(&mut self, check_devices: bool) {
        if check_devices {
            if let Some(input_device_name) = &self.input_device_name {
                // Unlink devices if selected input device was removed
                if self.input_link_sender.is_some() && get_device(input_device_name).await.is_err()
                {
                    eprintln!(
                        "Selected input device {} was removed, unlinking devices",
                        input_device_name
                    );
                    self.abort_link_thread();
                }
                // Link devices if not linked
                else if self.input_link_sender.is_none() {
                    self.link_devices().await.ok();
                }
            }

            if self.stream_handle.is_some() && self.player_link_sender.is_none() {
                self.link_player().await.ok();
            }
        }

        // Handle looped sounds
        let mut restarts = vec![];

        for (id, sound) in &self.tracks {
            if sound.sink.empty() && sound.looped {
                restarts.push(*id);
            }
        }

        let mut restart_futures = vec![];

        for id in restarts {
            if let Some(sound) = self.tracks.get(&id) {
                let path = sound.path.clone();
                let handle = tokio::task::spawn_blocking(move || {
                    if let Ok(file) = fs::File::open(&path)
                        && let Ok(source) = Decoder::try_from(file)
                    {
                        return Some((id, source));
                    }
                    None
                });
                restart_futures.push(handle);
            }
        }

        for handle in restart_futures {
            if let Ok(res) = handle.await
                && let Some((id, source)) = res
                && let Some(sound) = self.tracks.get_mut(&id)
            {
                sound.sink.append(source);
                sound.sink.play();
            }
        }

        self.tracks
            .retain(|_, sound| !sound.sink.empty() || sound.looped);

        if self.tracks.is_empty() {
            self.drop_stream();
        }
    }

    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());
        }

        self.input_device_name = Some(name.to_string());

        self.link_devices().await?;

        Ok(())
    }
}