@@ -10,7 +10,7 @@ use crate::{
1010} ;
1111use anyhow:: { Result , bail} ;
1212use once_cell:: sync:: Lazy ;
13- use recorder:: { AudioRecorder , SpeakerRecorder , bounded} ;
13+ use recorder:: { AudioRecorder , SpeakerRecorder , StreamingAudioRecorder , bounded} ;
1414use slint:: { ComponentHandle , Model , SharedString , ToSharedString , VecModel , Weak } ;
1515use std:: {
1616 path:: PathBuf ,
@@ -25,6 +25,7 @@ use std::{
2525struct Cache {
2626 desktop_speaker_amplification : Option < Arc < AtomicI32 > > ,
2727 input_audio_amplification : Option < Arc < AtomicI32 > > ,
28+ input_streaming_audio_recorder : Option < StreamingAudioRecorder > ,
2829}
2930
3031static 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
267283fn 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
278329fn start_recording ( ui : & AppWindow ) { }
279330
0 commit comments