Skip to content

Commit 0bc95de

Browse files
committed
[*] update normally
1 parent 4f9c635 commit 0bc95de

File tree

9 files changed

+275
-80
lines changed

9 files changed

+275
-80
lines changed

lib/recorder/src/record_speaker.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use pipewire::{
1111
stream::{StreamBox, StreamFlags, StreamListener},
1212
};
1313
use std::{
14+
ops::Mul,
1415
path::PathBuf,
1516
sync::{
1617
Arc, Mutex,
17-
atomic::{AtomicBool, Ordering},
18+
atomic::{AtomicBool, AtomicI32, Ordering},
1819
},
1920
time::Duration,
2021
};
@@ -118,6 +119,9 @@ pub struct SpeakerRecorder {
118119
level_sender: Option<Arc<Sender<f32>>>,
119120
/// Whether to run in preview mode (no file output)
120121
enable_preview_mode: bool,
122+
123+
// [0, infinity]
124+
amplification: Option<Arc<AtomicI32>>,
121125
}
122126

123127
impl SpeakerRecorder {
@@ -170,9 +174,15 @@ impl SpeakerRecorder {
170174
stop_sig,
171175
level_sender,
172176
enable_preview_mode,
177+
amplification: None,
173178
})
174179
}
175180

181+
pub fn with_amplification(mut self, v: Arc<AtomicI32>) -> Self {
182+
self.amplification = Some(v);
183+
self
184+
}
185+
176186
/// Start recording system audio output to WAV file.
177187
///
178188
/// This method begins capturing system audio output using PipeWire monitor ports.
@@ -232,8 +242,12 @@ impl SpeakerRecorder {
232242

233243
// Create an input stream to record the monitoring port of the output device.
234244
let stream = self.create_stream()?;
235-
let _stream_listener =
236-
Self::stream_register(&stream, writer.clone(), self.level_sender.clone())?;
245+
let _stream_listener = Self::stream_register(
246+
&stream,
247+
writer.clone(),
248+
self.level_sender.clone(),
249+
self.amplification.clone(),
250+
)?;
237251
Self::stream_connect(&stream, node_id)?;
238252

239253
while !self.stop_sig.load(Ordering::Relaxed) {
@@ -368,6 +382,7 @@ impl SpeakerRecorder {
368382
stream: &StreamBox,
369383
writer: Arc<Mutex<Option<WavWriterType>>>,
370384
level_sender: Option<Arc<Sender<f32>>>,
385+
amplification: Option<Arc<AtomicI32>>,
371386
) -> Result<StreamListener<()>, SpeakerError> {
372387
let stream_listener = stream
373388
.add_local_listener::<()>()
@@ -392,6 +407,18 @@ impl SpeakerRecorder {
392407
)
393408
};
394409

410+
let mut f32_sample_amplification = vec![0.0; f32_samples.len()];
411+
let f32_samples = if let Some(ref amplification) = amplification {
412+
for (index, v) in f32_samples.iter().enumerate() {
413+
f32_sample_amplification[index] =
414+
v.mul(amplification.load(Ordering::Relaxed) as f32 / 100.0);
415+
}
416+
417+
&f32_sample_amplification[..]
418+
} else {
419+
f32_samples
420+
};
421+
395422
if let Ok(mut writer_opt) = writer.lock()
396423
&& let Some(ref mut wav_writer) = *writer_opt
397424
{

wayshot/src/config.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
//! configuration file creation.
66
77
use crate::slint_generatedAppWindow::{
8-
SettingControl as UISettingControl, SettingRecorder as UISettingRecorder,
8+
Fps as UIFps, Resolution as UIResolution, SettingControl as UISettingControl,
9+
SettingRecorder as UISettingRecorder,
910
};
1011
use anyhow::{Context, Result, bail};
1112
use log::debug;
@@ -114,13 +115,23 @@ pub struct Preference {
114115
pub is_dark: bool,
115116
}
116117

117-
#[derive(Serialize, Deserialize, Default, Debug, Clone, SlintFromConvert)]
118+
#[derive(Serialize, Deserialize, Derivative, Debug, Clone, SlintFromConvert)]
119+
#[derivative(Default)]
118120
#[from("UISettingRecorder")]
119121
pub struct Recorder {
120122
pub save_dir: String,
121123
pub remove_temporary_files: bool,
124+
125+
#[derivative(Default(value = "fps_default()"))]
126+
pub fps: UIFps,
127+
128+
#[derivative(Default(value = "resolution_default()"))]
129+
pub resolution: UIResolution,
122130
}
123131

132+
crate::impl_slint_enum_serde!(UIFps, Fps24, Fps25, Fps30);
133+
crate::impl_slint_enum_serde!(UIResolution, Original, P720, P1080, P2K, P4K);
134+
124135
#[derive(Serialize, Deserialize, Debug, Clone, Derivative, SlintFromConvert)]
125136
#[derivative(Default)]
126137
#[from("UISettingControl")]
@@ -274,6 +285,14 @@ fn appid_default() -> String {
274285
Uuid::new_v4().to_string()
275286
}
276287

288+
fn fps_default() -> UIFps {
289+
UIFps::Fps25
290+
}
291+
292+
fn resolution_default() -> UIResolution {
293+
UIResolution::Original
294+
}
295+
277296
/// Initializes the global configuration
278297
///
279298
/// This should be called once at application startup.

wayshot/src/logic/recorder.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ use crate::{
99
toast_warn,
1010
};
1111
use anyhow::{Result, bail};
12+
use once_cell::sync::Lazy;
1213
use recorder::{AudioRecorder, SpeakerRecorder, bounded};
1314
use slint::{ComponentHandle, Model, SharedString, ToSharedString, VecModel, Weak};
1415
use std::{
1516
path::PathBuf,
16-
sync::{Arc, atomic::AtomicBool},
17+
sync::{
18+
Arc, Mutex,
19+
atomic::{AtomicBool, AtomicI32, Ordering},
20+
},
1721
thread,
1822
};
1923

20-
// static CACHE: Lazy<Mutex<Cache>> = Lazy::new(|| Mutex::new(Cache::default()));
24+
#[derive(Default, Debug)]
25+
struct Cache {
26+
desktop_speaker_amplification: Option<Arc<AtomicI32>>,
27+
input_audio_amplification: Option<Arc<AtomicI32>>,
28+
}
29+
30+
static CACHE: Lazy<Mutex<Cache>> = Lazy::new(|| Mutex::new(Cache::default()));
2131

2232
#[macro_export]
2333
macro_rules! store_audio_sources {
@@ -56,7 +66,8 @@ pub fn init(ui: &AppWindow) {
5666
inner_init(&ui);
5767

5868
logic_cb!(choose_save_dir, ui);
59-
logic_cb!(screen_changed, ui, name);
69+
logic_cb!(input_audio_amplification_changed, ui, v);
70+
logic_cb!(desktop_speaker_amplification_changed, ui, v);
6071
logic_cb!(input_audio_changed, ui, name);
6172
logic_cb!(start_recording, ui);
6273
logic_cb!(stop_recording, ui);
@@ -70,15 +81,15 @@ fn inner_init(ui: &AppWindow) {
7081
store_video_sources!(ui).set_vec(vec![]);
7182

7283
if let Err(e) = init_input_audio(&ui) {
73-
toast_warn!(ui, "{e}");
84+
toast_warn!(ui, format!("{e}"));
7485
}
7586

7687
if let Err(e) = init_desktop_speaker(&ui) {
77-
toast_warn!(ui, "{e}");
88+
toast_warn!(ui, format!("{e}"));
7889
}
7990

8091
if let Err(e) = init_video(&ui) {
81-
toast_warn!(ui, "{e}");
92+
toast_warn!(ui, format!("{e}"));
8293
}
8394
}
8495

@@ -172,6 +183,16 @@ fn init_desktop_speaker(ui: &AppWindow) -> Result<()> {
172183
}
173184

174185
Ok(recorder) => {
186+
let amplification = Arc::new(AtomicI32::new(
187+
config::all().control.desktop_speaker_sound.max(0.0) as i32,
188+
));
189+
let recorder = recorder.with_amplification(amplification.clone());
190+
191+
{
192+
let mut cache = CACHE.lock().unwrap();
193+
cache.desktop_speaker_amplification = Some(amplification);
194+
}
195+
175196
if let Err(e) = recorder.start_recording() {
176197
async_toast_warn(
177198
ui_weak.clone(),
@@ -224,8 +245,6 @@ fn init_video(ui: &AppWindow) -> Result<()> {
224245
name: name.clone(),
225246
});
226247

227-
global_logic!(ui).invoke_screen_changed(name);
228-
229248
Ok(())
230249
}
231250

@@ -243,7 +262,16 @@ fn choose_save_dir(ui: &AppWindow) {
243262
});
244263
}
245264

246-
fn screen_changed(ui: &AppWindow, name: SharedString) {}
265+
fn input_audio_amplification_changed(ui: &AppWindow, v: f32) {}
266+
267+
fn desktop_speaker_amplification_changed(_ui: &AppWindow, v: f32) {
268+
let amplification = CACHE.lock().unwrap().desktop_speaker_amplification.clone();
269+
if let Some(amplification) = amplification {
270+
amplification.store(v as i32, Ordering::Relaxed);
271+
} else {
272+
log::warn!("desktop speaker amplification is None");
273+
}
274+
}
247275

248276
fn input_audio_changed(ui: &AppWindow, name: SharedString) {}
249277

wayshot/ui/logic.slint

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Store, SettingPreference, TabIndex, SettingDetailIndex, MobileSettingDetailIndex, PopupIndex, MobileTabIndex, SettingBackup, SettingRecorder, SettingControl } from "store.slint";
1+
import { Store, SettingPreference, TabIndex, SettingDetailIndex, MobileSettingDetailIndex, PopupIndex, MobileTabIndex, SettingBackup, SettingRecorder, SettingControl, Fps, Resolution } from "store.slint";
22

33
import { SideBarEntry } from "base/def.slint";
44

@@ -75,11 +75,63 @@ export global Logic {
7575
callback set-setting-control(setting: SettingControl);
7676

7777
callback choose-save-dir();
78-
callback screen-changed(value: string);
7978
callback input-audio-changed(value: string);
79+
callback input-audio-amplification-changed(value: float);
80+
callback desktop-speaker-amplification-changed(value: float);
8081
callback start-recording();
8182
callback stop-recording();
8283
callback stop-merge-tracks();
8384

85+
pure public function fps-to-int(fps: Fps) -> int {
86+
if (fps == Fps.Fps24) {
87+
return 24;
88+
} else if (fps == Fps.Fps25) {
89+
return 25;
90+
} else {
91+
return 30;
92+
}
93+
}
94+
95+
pure public function fps-from-int(fps: int) -> Fps {
96+
if (fps == 24) {
97+
return Fps.Fps24;
98+
} else if (fps == 25) {
99+
return Fps.Fps25;
100+
} else {
101+
return Fps.Fps30;
102+
}
103+
}
104+
105+
pure public function resolution-to-string(res: Resolution) -> string {
106+
if (res == Resolution.Original) {
107+
return "Original";
108+
} else if (res == Resolution.P720) {
109+
return "720P";
110+
} else if (res == Resolution.P1080) {
111+
return "1080P";
112+
} else if (res == Resolution.P2K) {
113+
return "2K";
114+
} else if (res == Resolution.P4K) {
115+
return "4K";
116+
} else {
117+
return "Original";
118+
}
119+
}
120+
121+
pure public function resolution-from-string(res: string) -> Resolution {
122+
if (res == "Original") {
123+
return Resolution.Original;
124+
} else if (res == "720P") {
125+
return Resolution.P720;
126+
} else if (res == "1080P") {
127+
return Resolution.P1080;
128+
} else if (res == "2K") {
129+
return Resolution.P2K;
130+
} else if (res == "4K") {
131+
return Resolution.P4K;
132+
} else {
133+
return Resolution.Original;
134+
}
135+
}
84136
//////////////////////////////// Logic End ////////////////////////////////
85137
}

0 commit comments

Comments
 (0)