@@ -294,7 +294,7 @@ impl AudioRecorder {
294294 & self ,
295295 device_name : & str ,
296296 callback : impl FnMut ( & [ f32 ] , & InputCallbackInfo ) + Send + ' static ,
297- ) -> Result < AudioRecordingSession , AudioError > {
297+ ) -> Result < Arc < Mutex < Option < Stream > > > , AudioError > {
298298 let ( stream_config, _) = self . get_config ( device_name) ?;
299299
300300 let physical_device = self
@@ -319,54 +319,7 @@ impl AudioRecorder {
319319 . play ( )
320320 . map_err ( |e| AudioError :: StreamError ( e. to_string ( ) ) ) ?;
321321
322- Ok ( AudioRecordingSession {
323- stream : Arc :: new ( Mutex :: new ( Some ( stream) ) ) ,
324- } )
325- }
326- }
327-
328- /// Active audio recording session
329- pub struct AudioRecordingSession {
330- stream : Arc < Mutex < Option < Stream > > > ,
331- }
332-
333- impl std:: fmt:: Debug for AudioRecordingSession {
334- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
335- f. debug_struct ( "AudioRecordingSession" )
336- . field ( "is_active" , & self . is_active ( ) )
337- . finish ( )
338- }
339- }
340-
341- impl AudioRecordingSession {
342- /// Stop the audio recording session
343- pub fn stop ( & self ) -> Result < ( ) , AudioError > {
344- let mut stream_guard = self
345- . stream
346- . lock ( )
347- . map_err ( |_| AudioError :: StreamError ( "Failed to lock stream" . to_string ( ) ) ) ?;
348-
349- if let Some ( stream) = stream_guard. take ( ) {
350- drop ( stream) ;
351- }
352-
353- Ok ( ( ) )
354- }
355-
356- /// Check if the recording session is active
357- pub fn is_active ( & self ) -> bool {
358- let stream_guard = match self . stream . lock ( ) {
359- Ok ( guard) => guard,
360- Err ( _) => return false ,
361- } ;
362-
363- stream_guard. is_some ( )
364- }
365- }
366-
367- impl Drop for AudioRecordingSession {
368- fn drop ( & mut self ) {
369- _ = self . stop ( ) ;
322+ Ok ( Arc :: new ( Mutex :: new ( Some ( stream) ) ) )
370323 }
371324}
372325
@@ -375,14 +328,6 @@ pub struct AudioFileWriter {
375328 writer : Option < WavWriter < BufWriter < File > > > ,
376329}
377330
378- impl std:: fmt:: Debug for AudioFileWriter {
379- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
380- f. debug_struct ( "AudioFileWriter" )
381- . field ( "is_active" , & self . writer . is_some ( ) )
382- . finish ( )
383- }
384- }
385-
386331impl AudioFileWriter {
387332 /// Create a new WAV file writer
388333 pub fn new < P : AsRef < Path > > (
@@ -437,10 +382,10 @@ impl Drop for AudioFileWriter {
437382}
438383
439384/// Streaming audio recorder that writes to file in real-time
440- #[ derive( Debug ) ]
385+ // #[derive(Debug)]
441386pub struct StreamingAudioRecorder {
442387 file_writer : Arc < Mutex < Option < AudioFileWriter > > > ,
443- recording_session : AudioRecordingSession ,
388+ recording_session : Arc < Mutex < Option < Stream > > > ,
444389 audio_level_receiver : Option < Arc < Receiver < f32 > > > ,
445390}
446391
@@ -505,22 +450,19 @@ impl StreamingAudioRecorder {
505450
506451 /// Stop the streaming audio recording
507452 pub fn stop ( self ) -> Result < ( ) , AudioError > {
508- self . recording_session . stop ( ) ?;
509-
510- if let Ok ( mut writer) = self . file_writer . lock ( )
511- && let Some ( ref mut writer) = * writer
512453 {
454+ if let Some ( stream) = self . recording_session . lock ( ) . unwrap ( ) . take ( ) {
455+ drop ( stream) ;
456+ }
457+ }
458+
459+ if let Some ( mut writer) = self . file_writer . lock ( ) . unwrap ( ) . take ( ) {
513460 writer. finalize ( ) ?;
514461 }
515462
516463 Ok ( ( ) )
517464 }
518465
519- /// Check if the streaming recorder is active
520- pub fn is_active ( & self ) -> bool {
521- self . recording_session . is_active ( )
522- }
523-
524466 pub fn get_audio_level_receiver ( & self ) -> Option < Arc < Receiver < f32 > > > {
525467 self . audio_level_receiver . clone ( )
526468 }
0 commit comments