Skip to content

Commit efe1ebf

Browse files
committed
[*] refactor
1 parent d5523cb commit efe1ebf

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

lib/record-audio/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ impl RecordedAudio {
9090
*sample = sample.clamp(-1.0, 1.0);
9191
}
9292
}
93+
94+
pub fn convert_to_mono(&mut self) {
95+
if self.channels <= 1 {
96+
return;
97+
}
98+
99+
let num_channels = self.channels as usize;
100+
let num_frames = self.samples.len() / num_channels;
101+
let mut mono_samples = Vec::with_capacity(num_frames);
102+
103+
for frame_idx in 0..num_frames {
104+
let start = frame_idx * num_channels;
105+
let end = start + num_channels;
106+
let frame_samples = &self.samples[start..end.min(self.samples.len())];
107+
let avg = frame_samples.iter().sum::<f32>() / num_channels as f32;
108+
mono_samples.push(avg);
109+
}
110+
111+
self.samples = mono_samples;
112+
self.channels = 1;
113+
}
93114
}
94115

95116
pub struct AudioRecorder {

wayshot/src/logic/video_editor/common_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ pub struct VideoEditorExportAudioConfig {
136136
pub struct VideoEditorRecordAudioConfig {
137137
pub save_dir: String,
138138
pub device: String,
139-
140139
#[derivative(Default(value = "1.0"))]
141140
pub gain: f32,
141+
pub mono: bool,
142142
}
143143

144144
#[derive(Serialize, Deserialize, Debug, Clone, Default)]

wayshot/src/logic/video_editor/record_audio.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,15 @@ fn video_editor_stop_recording_audio(ui: &AppWindow) {
251251
audio.apply_gain(config.gain);
252252
}
253253

254+
if config.mono {
255+
audio.convert_to_mono();
256+
}
257+
254258
let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S");
255259
let filename = format!("recording_{}.wav", timestamp);
256260
let save_path = PathBuf::from(&save_dir).join(&filename);
257261

258-
toast::async_toast_info(ui_weak, format!("Saving {}", save_path.display()));
262+
toast::async_toast_info(ui_weak.clone(), format!("Saving {}", save_path.display()));
259263

260264
match audio.save_to_file(&save_path) {
261265
Ok(()) => {
@@ -306,6 +310,7 @@ fn video_editor_record_audio_select_dir(ui: &AppWindow) {
306310
save_dir: dir_str.clone().into(),
307311
device: current_config.device.clone(),
308312
gain: current_config.gain,
313+
mono: current_config.mono,
309314
};
310315

311316
let config: VideoEditorRecordAudioConfig = new_config.clone().into();

wayshot/ui/panel/desktop/video-editor/record-audio-dialog.slint

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
SettingDetailLabel,
1717
LineInput,
1818
Select,
19+
CheckBtn,
1920
} from "../../../base/widgets.slint";
2021

2122
export component RecordAudioDialog inherits MovingDialog {
@@ -77,6 +78,16 @@ export component RecordAudioDialog inherits MovingDialog {
7778
Logic.video-editor-record-audio-update(cache-config);
7879
}
7980
}
81+
82+
CheckBtn {
83+
text: Logic.tr("Mono");
84+
checked: cache-config.mono;
85+
86+
toggled => {
87+
cache-config.mono = self.checked;
88+
Logic.video-editor-record-audio-update(cache-config);
89+
}
90+
}
8091
}
8192

8293
hbox := HorizontalLayout {

wayshot/ui/store.slint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ export struct VideoEditorRecordAudioConfig {
597597
save-dir: string,
598598
device: string,
599599
gain: float,
600+
mono: bool,
600601
}
601602

602603

0 commit comments

Comments
 (0)