feature/share-libs
rs 102 lines 2.88 KB
Raw
1 use super::player_set_volume::Mode;
2 use crate::{
3 command::player_prepare::prepare_players,
4 players::{BASS_SINK, DRUMS_SINK, OTHER_SINK, VOCALS_SINK},
5 };
6 use log::info;
7 use serde::{Deserialize, Serialize};
8 use std::fmt::Display;
9
10 #[derive(Deserialize, Serialize, Debug, PartialEq)]
11 pub struct PlayerVolume {
12 mode: Mode,
13 volume: String,
14 }
15
16 impl Display for PlayerVolume {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 write!(f, "{:?} {}", self.mode, self.volume)
19 }
20 }
21
22 #[tauri::command]
23 pub async fn player_paused() {
24 info!("Player paused.");
25
26 // Stop all sinks
27 if let Some(sink) = unsafe { VOCALS_SINK.get() } {
28 sink.pause();
29 }
30 if let Some(sink) = unsafe { BASS_SINK.get() } {
31 sink.pause();
32 }
33 if let Some(sink) = unsafe { OTHER_SINK.get() } {
34 sink.pause();
35 }
36 if let Some(sink) = unsafe { DRUMS_SINK.get() } {
37 sink.pause();
38 }
39 }
40
41 #[tauri::command]
42 pub async fn player_play(player_volumes: Vec<PlayerVolume>) {
43 info!("Player resumed. {:?}", player_volumes);
44
45 // Stop all sinks
46 if let Some(sink) = unsafe { VOCALS_SINK.get() } {
47 player_volumes.iter().for_each(|player_volume| {
48 if player_volume.mode == Mode::Vocals {
49 let volume = player_volume.volume.parse::<f32>().unwrap() / 100.0;
50 sink.set_volume(volume);
51 }
52 });
53 sink.play();
54 }
55 if let Some(sink) = unsafe { BASS_SINK.get() } {
56 player_volumes.iter().for_each(|player_volume| {
57 if player_volume.mode == Mode::Bass {
58 let volume = player_volume.volume.parse::<f32>().unwrap() / 100.0;
59 sink.set_volume(volume);
60 }
61 });
62 sink.play();
63 }
64 if let Some(sink) = unsafe { OTHER_SINK.get() } {
65 player_volumes.iter().for_each(|player_volume| {
66 if player_volume.mode == Mode::Other {
67 let volume = player_volume.volume.parse::<f32>().unwrap() / 100.0;
68 sink.set_volume(volume);
69 }
70 });
71 sink.play();
72 }
73 if let Some(sink) = unsafe { DRUMS_SINK.get() } {
74 player_volumes.iter().for_each(|player_volume| {
75 if player_volume.mode == Mode::Drums {
76 let volume = player_volume.volume.parse::<f32>().unwrap() / 100.0;
77 sink.set_volume(volume);
78 }
79 });
80 sink.play();
81 }
82 }
83
84 #[tauri::command]
85 pub async fn player_stop(audio_id: String) {
86 info!("Stopping player.");
87
88 // Stop all sinks
89 if let Some(sink) = unsafe { VOCALS_SINK.take() } {
90 sink.stop();
91 }
92 if let Some(sink) = unsafe { BASS_SINK.take() } {
93 sink.stop();
94 }
95 if let Some(sink) = unsafe { OTHER_SINK.take() } {
96 sink.stop();
97 }
98 if let Some(sink) = unsafe { DRUMS_SINK.take() } {
99 sink.stop()
100 }
101 prepare_players(audio_id).await;
102 }