11//! Offers audio recording capabilities via being a middleware on [`cpal`]. 
22
3- use  std:: { sync:: Arc ,  thread:: JoinHandle } ; 
3+ use  std:: { collections :: VecDeque ,   sync:: Arc ,  thread:: { sleep ,   JoinHandle } ,  time :: Duration } ; 
44
55use  cpal:: { 
66    traits:: { DeviceTrait ,  StreamTrait } , 
@@ -22,8 +22,58 @@ use super::InputDevice;
2222/// The `err_callback` callback is called if an error occurs in the [`DeviceTrait::build_input_stream`] whilst recording. 
2323/// The function returns an error if an error occured outside of the [`DeviceTrait::build_input_stream`] function. 
2424pub  fn  record_audio_with_interrupt < E > ( 
25+     input_device :  InputDevice , 
2526    interrupt :  tokio:: sync:: oneshot:: Receiver < ( ) > , 
27+     err_callback :  E , 
28+     config :  StreamConfig , 
29+ )  -> anyhow:: Result < Arc < Mutex < VecDeque < f32 > > > > 
30+ where 
31+     E :  FnMut ( StreamError )  + Send  + ' static , 
32+ { 
33+     let  buffer_handle:  Arc < parking_lot:: lock_api:: Mutex < parking_lot:: RawMutex ,  VecDeque < f32 > > >  =
34+         Arc :: new ( parking_lot:: Mutex :: new ( VecDeque :: new ( ) ) ) ; 
35+     let  buffer_handle_clone = buffer_handle. clone ( ) ; 
36+ 
37+     let  _:  JoinHandle < anyhow:: Result < ( ) > >  = std:: thread:: spawn ( move  || { 
38+         let  stream:  cpal:: Stream  = input_device. build_input_stream ( 
39+             & config, 
40+             move  |data :  & [ f32 ] ,  _info :  & cpal:: InputCallbackInfo | { 
41+                 let  mut  buffer_handle = buffer_handle_clone. lock ( ) ; 
42+                 for  sample in  data. iter ( )  { 
43+                     buffer_handle. push_back ( * sample) ; 
44+                 } 
45+             } , 
46+             err_callback, 
47+             None , 
48+         ) ?; 
49+ 
50+         //Start stream 
51+         stream. play ( ) ?; 
52+ 
53+         //Wait for interrupt 
54+         interrupt. blocking_recv ( ) ?; 
55+ 
56+         //Return from thread 
57+         Ok ( ( ) ) 
58+     } ) ; 
59+ 
60+     Ok ( buffer_handle) 
61+ } 
62+ 
63+ /// 
64+ /// Records audio until the user interrupts it. 
65+ /// 
66+ /// # Behavior 
67+ /// The recording thread is automaticly started at the creation of the (Input)[`cpal::Stream`]. 
68+ /// Records audio until (Pushes the samples into the [`Arc<Mutex<Vec<f32>>>`]) the [`tokio::sync::oneshot::Receiver`] receives a message. 
69+ /// The [`Sync`] buffer is returned and can be accessed. 
70+ /// 
71+ /// # Error 
72+ /// The `err_callback` callback is called if an error occurs in the [`DeviceTrait::build_input_stream`] whilst recording. 
73+ /// The function returns an error if an error occured outside of the [`DeviceTrait::build_input_stream`] function. 
74+ pub  fn  record_audio_with_duration < E > ( 
2675    input_device :  InputDevice , 
76+     duration :  Duration , 
2777    err_callback :  E , 
2878    config :  StreamConfig , 
2979)  -> anyhow:: Result < Arc < Mutex < Vec < f32 > > > > 
50100        //Start stream 
51101        stream. play ( ) ?; 
52102
53-         //Wait for interrupt  
54-         interrupt . blocking_recv ( ) ? ; 
103+         //Sleep the thread  
104+         sleep ( duration ) ; 
55105
56106        //Return from thread 
57107        Ok ( ( ) ) 
0 commit comments