@@ -17,7 +17,7 @@ use thiserror::Error;
1717static DENOISE_MODEL : Lazy < RnnModel > = Lazy :: new ( || denoise_model ( ) ) ;
1818
1919#[ derive( Debug , Error ) ]
20- pub enum AudioError {
20+ pub enum AudioRecorderError {
2121 #[ error( "Audio host error: {0}" ) ]
2222 HostError ( String ) ,
2323
@@ -66,15 +66,15 @@ impl AudioRecorder {
6666 }
6767 }
6868
69- pub fn get_available_devices ( & self ) -> Result < Vec < AudioDeviceInfo > , AudioError > {
69+ pub fn get_available_devices ( & self ) -> Result < Vec < AudioDeviceInfo > , AudioRecorderError > {
7070 self . get_input_devices ( )
7171 }
7272
73- pub fn get_input_devices ( & self ) -> Result < Vec < AudioDeviceInfo > , AudioError > {
73+ pub fn get_input_devices ( & self ) -> Result < Vec < AudioDeviceInfo > , AudioRecorderError > {
7474 let devices = self
7575 . host
7676 . input_devices ( )
77- . map_err ( |e| AudioError :: HostError ( e. to_string ( ) ) ) ?;
77+ . map_err ( |e| AudioRecorderError :: HostError ( e. to_string ( ) ) ) ?;
7878
7979 let mut input_devices = Vec :: new ( ) ;
8080 for device in devices {
@@ -86,20 +86,20 @@ impl AudioRecorder {
8686 Ok ( input_devices)
8787 }
8888
89- pub fn get_default_input_device ( & self ) -> Result < Option < AudioDeviceInfo > , AudioError > {
89+ pub fn get_default_input_device ( & self ) -> Result < Option < AudioDeviceInfo > , AudioRecorderError > {
9090 if let Some ( device) = self . host . default_input_device ( ) {
9191 self . get_device_info ( & device)
9292 . map ( Some )
93- . map_err ( |e| AudioError :: DeviceError ( e. to_string ( ) ) )
93+ . map_err ( |e| AudioRecorderError :: DeviceError ( e. to_string ( ) ) )
9494 } else {
9595 Ok ( None )
9696 }
9797 }
9898
99- fn get_device_info ( & self , device : & Device ) -> Result < AudioDeviceInfo , AudioError > {
99+ fn get_device_info ( & self , device : & Device ) -> Result < AudioDeviceInfo , AudioRecorderError > {
100100 let name = device
101101 . name ( )
102- . map_err ( |e| AudioError :: DeviceError ( e. to_string ( ) ) ) ?;
102+ . map_err ( |e| AudioRecorderError :: DeviceError ( e. to_string ( ) ) ) ?;
103103
104104 let default_config = device
105105 . default_input_config ( )
@@ -118,19 +118,27 @@ impl AudioRecorder {
118118 } )
119119 }
120120
121- pub fn find_device_by_name ( & self , name : & str ) -> Result < Option < AudioDeviceInfo > , AudioError > {
121+ pub fn find_device_by_name (
122+ & self ,
123+ name : & str ,
124+ ) -> Result < Option < AudioDeviceInfo > , AudioRecorderError > {
122125 let devices = self . get_available_devices ( ) ?;
123126 Ok ( devices. into_iter ( ) . find ( |device| device. name == name) )
124127 }
125128
126- pub fn get_config ( & self , name : & str ) -> Result < ( StreamConfig , Vec < SampleFormat > ) , AudioError > {
129+ pub fn get_config (
130+ & self ,
131+ name : & str ,
132+ ) -> Result < ( StreamConfig , Vec < SampleFormat > ) , AudioRecorderError > {
127133 let device_infos = self . find_device_by_name ( name) ?;
128134 let Some ( device_info) = device_infos else {
129- return Err ( AudioError :: DeviceError ( format ! ( "no found device `{name}`" ) ) ) ;
135+ return Err ( AudioRecorderError :: DeviceError ( format ! (
136+ "no found device `{name}`"
137+ ) ) ) ;
130138 } ;
131139
132140 let Some ( default_config) = device_info. default_config else {
133- return Err ( AudioError :: DeviceError ( format ! (
141+ return Err ( AudioRecorderError :: DeviceError ( format ! (
134142 "no found default_config for device `{name}`"
135143 ) ) ) ;
136144 } ;
@@ -142,16 +150,16 @@ impl AudioRecorder {
142150 & self ,
143151 device_name : & str ,
144152 callback : impl FnMut ( & [ f32 ] , & InputCallbackInfo ) + Send + ' static ,
145- ) -> Result < Stream , AudioError > {
153+ ) -> Result < Stream , AudioRecorderError > {
146154 let ( stream_config, _) = self . get_config ( device_name) ?;
147155
148156 let physical_device = self
149157 . host
150158 . input_devices ( )
151- . map_err ( |e| AudioError :: HostError ( e. to_string ( ) ) ) ?
159+ . map_err ( |e| AudioRecorderError :: HostError ( e. to_string ( ) ) ) ?
152160 . find ( |d| d. name ( ) . map ( |name| name == device_name) . unwrap_or ( false ) )
153161 . ok_or_else ( || {
154- AudioError :: DeviceError ( format ! ( "Device '{}' not found" , device_name) )
162+ AudioRecorderError :: DeviceError ( format ! ( "Device '{}' not found" , device_name) )
155163 } ) ?;
156164
157165 let stream = physical_device
@@ -161,16 +169,16 @@ impl AudioRecorder {
161169 |err| eprintln ! ( "Audio stream error: {}" , err) ,
162170 None ,
163171 )
164- . map_err ( |e| AudioError :: StreamError ( e. to_string ( ) ) ) ?;
172+ . map_err ( |e| AudioRecorderError :: StreamError ( e. to_string ( ) ) ) ?;
165173
166174 stream
167175 . play ( )
168- . map_err ( |e| AudioError :: StreamError ( e. to_string ( ) ) ) ?;
176+ . map_err ( |e| AudioRecorderError :: StreamError ( e. to_string ( ) ) ) ?;
169177
170178 Ok ( stream)
171179 }
172180
173- pub fn spec ( & self , device_name : & str ) -> Result < WavSpec , AudioError > {
181+ pub fn spec ( & self , device_name : & str ) -> Result < WavSpec , AudioRecorderError > {
174182 let ( stream_config, _) = self . get_config ( device_name) ?;
175183
176184 Ok ( WavSpec {
@@ -181,14 +189,14 @@ impl AudioRecorder {
181189 } )
182190 }
183191
184- pub fn start_recording ( & mut self , device_name : & str ) -> Result < ( ) , AudioError > {
192+ pub fn start_recording ( & mut self , device_name : & str ) -> Result < ( ) , AudioRecorderError > {
185193 // Note:
186194 // Without calling `denoise.flush` is not a problem.
187195 // Just losing the last frame of real-time samples.
188196 let mut denoiser = if self . enable_denoise {
189197 let spec = self . spec ( device_name) ?;
190198 let denoiser = RealTimeDenoise :: new ( & DENOISE_MODEL , spec)
191- . map_err ( |e| AudioError :: DenoiseError ( e. to_string ( ) ) ) ?;
199+ . map_err ( |e| AudioRecorderError :: DenoiseError ( e. to_string ( ) ) ) ?;
192200 Some ( denoiser)
193201 } else {
194202 None
0 commit comments