Skip to content

Commit 1a7c86e

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

File tree

12 files changed

+117
-17
lines changed

12 files changed

+117
-17
lines changed

lib/recorder/examples/input_recording_demo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2828
input_filename
2929
);
3030

31-
let streaming_recorder = StreamingAudioRecorder::start_recording_to_file(
31+
let streaming_recorder = StreamingAudioRecorder::start(
3232
recorder,
3333
"default", // 使用默认输入设备
3434
input_filename,

lib/recorder/src/record_audio.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use std::{
99
fs::File,
1010
io::BufWriter,
1111
path::Path,
12-
sync::{Arc, Mutex},
12+
sync::{
13+
Arc, Mutex,
14+
atomic::{AtomicI32, Ordering},
15+
},
1316
};
1417
use thiserror::Error;
1518

@@ -179,6 +182,9 @@ pub struct AudioRecorder {
179182
audio_level_sender: Option<Arc<Sender<f32>>>,
180183
/// Optional receiver for audio level data (if monitoring enabled)
181184
audio_level_receiver: Option<Arc<Receiver<f32>>>,
185+
186+
// [0, infinity]
187+
amplification: Option<Arc<AtomicI32>>,
182188
}
183189

184190
impl AudioRecorder {
@@ -221,9 +227,15 @@ impl AudioRecorder {
221227

222228
audio_level_sender,
223229
audio_level_receiver,
230+
amplification: None,
224231
})
225232
}
226233

234+
pub fn with_amplification(mut self, v: Arc<AtomicI32>) -> Self {
235+
self.amplification = Some(v);
236+
self
237+
}
238+
227239
/// Get the audio level receiver for real-time monitoring.
228240
///
229241
/// This method returns the receiver end of the audio level channel,
@@ -526,7 +538,7 @@ pub struct StreamingAudioRecorder {
526538

527539
impl StreamingAudioRecorder {
528540
/// Start recording audio to a file
529-
pub fn start_recording_to_file<P: AsRef<Path>>(
541+
pub fn start<P: AsRef<Path>>(
530542
recorder: AudioRecorder,
531543
device_name: &str,
532544
file_path: P,
@@ -546,8 +558,22 @@ impl StreamingAudioRecorder {
546558

547559
let file_writer_clone = file_writer.clone();
548560
let audio_level_sender = recorder.audio_level_sender.clone();
549-
let recording_session =
550-
recorder.start_input_recording(device_name, move |data: &[f32], _info: &_| {
561+
let amplification = recorder.amplification.clone();
562+
let recording_session = recorder.start_input_recording(
563+
device_name,
564+
move |f32_samples: &[f32], _info: &_| {
565+
let mut f32_sample_amplification = vec![0.0; f32_samples.len()];
566+
let data = if let Some(ref amplification) = amplification {
567+
for (index, v) in f32_samples.iter().enumerate() {
568+
f32_sample_amplification[index] =
569+
v * (amplification.load(Ordering::Relaxed) as f32 / 100.0);
570+
}
571+
572+
&f32_sample_amplification[..]
573+
} else {
574+
f32_samples
575+
};
576+
551577
if let Some(ref tx) = audio_level_sender
552578
&& let Some(db) = calc_rms_level(data)
553579
&& let Err(e) = tx.try_send(db)
@@ -560,7 +586,8 @@ impl StreamingAudioRecorder {
560586
{
561587
_ = writer.write_samples(data);
562588
}
563-
})?;
589+
},
590+
)?;
564591

565592
Ok(Self {
566593
file_writer: file_writer,

lib/recorder/src/record_speaker.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use pipewire::{
1111
stream::{StreamBox, StreamFlags, StreamListener},
1212
};
1313
use std::{
14-
ops::Mul,
1514
path::PathBuf,
1615
sync::{
1716
Arc, Mutex,
@@ -411,7 +410,7 @@ impl SpeakerRecorder {
411410
let f32_samples = if let Some(ref amplification) = amplification {
412411
for (index, v) in f32_samples.iter().enumerate() {
413412
f32_sample_amplification[index] =
414-
v.mul(amplification.load(Ordering::Relaxed) as f32 / 100.0);
413+
v * (amplification.load(Ordering::Relaxed) as f32 / 100.0);
415414
}
416415

417416
&f32_sample_amplification[..]

lib/recorder/src/recorder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl RecordingSession {
392392
.output_path
393393
.with_extension(INPUT_AUDIO_EXTENSION);
394394

395-
let streaming_recorder = StreamingAudioRecorder::start_recording_to_file(
395+
let streaming_recorder = StreamingAudioRecorder::start(
396396
audio_recorder,
397397
device_name,
398398
audio_file_path,

wayshot/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ pub struct Recorder {
122122
pub save_dir: String,
123123
pub remove_temporary_files: bool,
124124

125+
#[derivative(Default(value = "true"))]
126+
pub show_cursor: bool,
127+
125128
#[derivative(Default(value = "fps_default()"))]
126129
pub fps: UIFps,
127130

wayshot/src/logic/recorder.rs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
};
1111
use anyhow::{Result, bail};
1212
use once_cell::sync::Lazy;
13-
use recorder::{AudioRecorder, SpeakerRecorder, bounded};
13+
use recorder::{AudioRecorder, SpeakerRecorder, StreamingAudioRecorder, bounded};
1414
use slint::{ComponentHandle, Model, SharedString, ToSharedString, VecModel, Weak};
1515
use std::{
1616
path::PathBuf,
@@ -25,6 +25,7 @@ use std::{
2525
struct Cache {
2626
desktop_speaker_amplification: Option<Arc<AtomicI32>>,
2727
input_audio_amplification: Option<Arc<AtomicI32>>,
28+
input_streaming_audio_recorder: Option<StreamingAudioRecorder>,
2829
}
2930

3031
static CACHE: Lazy<Mutex<Cache>> = Lazy::new(|| Mutex::new(Cache::default()));
@@ -113,6 +114,10 @@ fn init_input_audio(ui: &AppWindow) -> Result<()> {
113114

114115
store_audio_sources!(ui).set_vec(names.clone());
115116

117+
if names.is_empty() {
118+
bail!("available input device no found");
119+
}
120+
116121
let control_config = config::all().control;
117122
if control_config.input_audio.is_empty()
118123
|| names
@@ -134,7 +139,7 @@ fn init_input_audio(ui: &AppWindow) -> Result<()> {
134139
global_store!(ui).set_setting_control(control_setting.clone());
135140
global_logic!(ui).invoke_set_setting_control(control_setting);
136141
} else {
137-
log::warn!("No default input device found");
142+
bail!("Default input device no found");
138143
}
139144
}
140145

@@ -144,7 +149,7 @@ fn init_input_audio(ui: &AppWindow) -> Result<()> {
144149
name: name.clone(),
145150
});
146151

147-
global_logic!(ui).invoke_input_audio_changed(name);
152+
input_audio_changed(&ui, name);
148153

149154
Ok(())
150155
}
@@ -224,6 +229,10 @@ fn init_video(ui: &AppWindow) -> Result<()> {
224229

225230
store_video_sources!(ui).set_vec(names.clone());
226231

232+
if names.is_empty() {
233+
bail!("available screens no found");
234+
}
235+
227236
let control_config = config::all().control;
228237
if control_config.screen.is_empty()
229238
|| names
@@ -262,7 +271,14 @@ fn choose_save_dir(ui: &AppWindow) {
262271
});
263272
}
264273

265-
fn input_audio_amplification_changed(ui: &AppWindow, v: f32) {}
274+
fn input_audio_amplification_changed(_ui: &AppWindow, v: f32) {
275+
let amplification = CACHE.lock().unwrap().input_audio_amplification.clone();
276+
if let Some(amplification) = amplification {
277+
amplification.store(v as i32, Ordering::Relaxed);
278+
} else {
279+
log::warn!("input audio amplification is None");
280+
}
281+
}
266282

267283
fn desktop_speaker_amplification_changed(_ui: &AppWindow, v: f32) {
268284
let amplification = CACHE.lock().unwrap().desktop_speaker_amplification.clone();
@@ -273,7 +289,42 @@ fn desktop_speaker_amplification_changed(_ui: &AppWindow, v: f32) {
273289
}
274290
}
275291

276-
fn input_audio_changed(ui: &AppWindow, name: SharedString) {}
292+
fn input_audio_changed(ui: &AppWindow, name: SharedString) {
293+
if let Err(e) = inner_input_audio_changed(ui, name) {
294+
toast_warn!(ui, format!("{e}"));
295+
}
296+
}
297+
298+
fn inner_input_audio_changed(ui: &AppWindow, name: SharedString) -> Result<()> {
299+
let amplification = Arc::new(AtomicI32::new(
300+
config::all().control.input_audio_sound.max(0.0) as i32,
301+
));
302+
303+
let recorder = AudioRecorder::new(Some(1024))?.with_amplification(amplification.clone());
304+
let streaming_recorder = StreamingAudioRecorder::start(recorder, &name, PathBuf::new(), true)?;
305+
306+
let receiver = streaming_recorder.get_audio_level_receiver();
307+
308+
{
309+
let mut cache = CACHE.lock().unwrap();
310+
cache.input_audio_amplification = Some(amplification);
311+
cache.input_streaming_audio_recorder = Some(streaming_recorder);
312+
}
313+
314+
if let Some(receiver) = receiver {
315+
let ui_weak = ui.as_weak();
316+
thread::spawn(move || {
317+
while let Ok(db) = receiver.recv() {
318+
// log::debug!("input_audio_level_receiver db level: {db:.0}",);
319+
_ = ui_weak.upgrade_in_event_loop(move |ui| {
320+
global_store!(ui).set_input_audio_db(db as i32);
321+
});
322+
}
323+
});
324+
}
325+
326+
Ok(())
327+
}
277328

278329
fn start_recording(ui: &AppWindow) {}
279330

wayshot/ui/base/icon.slint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export global Icons {
235235
out property <image> monitor-recorder-fill: @image-url("../images/icons/monitor-recorder-fill.svg");
236236
out property <image> monitor-recorder-light: @image-url("../images/icons/monitor-recorder-light.svg");
237237
out property <image> clear-fill: @image-url("../images/icons/clear-fill.svg");
238+
out property <image> cursor-light: @image-url("../images/icons/cursor-light.svg");
238239

239240
out property <image> landing-account: @image-url("../images/landing/landing-account.svg");
240241
out property <image> landing-language-switch: @image-url("../images/landing/landing-language-switch.svg");
Lines changed: 1 addition & 0 deletions
Loading

wayshot/ui/logic.slint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export global Logic {
7575
callback set-setting-control(setting: SettingControl);
7676

7777
callback choose-save-dir();
78-
callback input-audio-changed(value: string);
78+
callback input-audio-changed(name: string);
7979
callback input-audio-amplification-changed(value: float);
8080
callback desktop-speaker-amplification-changed(value: float);
8181
callback start-recording();

wayshot/ui/panel/desktop/home.slint

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ component ControlPanel inherits HorizontalLayout {
215215
input-audio-slider := Slider {
216216
value: Store.setting-control.input-audio-sound;
217217
minimum: 0;
218-
maximum: 200;
218+
maximum: 100;
219219
indicator-size: self.has-hover ? Theme.icon-size * 2 / 3 : 0;
220220
height: Theme.icon-size / 3;
221221
width: root.slider-width;
@@ -284,7 +284,7 @@ component ControlPanel inherits HorizontalLayout {
284284
desktop-speaker-slider := Slider {
285285
value: Store.setting-control.desktop-speaker-sound;
286286
minimum: 0;
287-
maximum: 200;
287+
maximum: 100;
288288
indicator-size: self.has-hover ? Theme.icon-size * 2 / 3 : 0;
289289
width: root.slider-width;
290290

0 commit comments

Comments
 (0)