1- use tokio:: net:: UdpSocket ;
21use cpal:: traits:: { DeviceTrait , HostTrait , StreamTrait } ;
32use hound:: WavWriter ;
43use rosc:: { encoder:: encode, OscMessage , OscPacket , OscType } ;
@@ -8,6 +7,7 @@ use std::fs;
87use std:: io:: { self , Cursor , Write } ;
98use std:: sync:: { Arc , Mutex } ;
109use std:: time:: { Duration , Instant } ;
10+ use tokio:: net:: UdpSocket ;
1111use tokio:: sync:: mpsc;
1212use tokio:: time:: sleep;
1313
@@ -158,7 +158,10 @@ impl TypingIndicator {
158158 args : vec ! [ OscType :: Bool ( is_typing) ] ,
159159 } ;
160160 if let Ok ( buf) = encode ( & OscPacket :: Message ( typing_message) ) {
161- let osc_address = format ! ( "{}:{}" , self . config. osc. address, self . config. osc. output_port) ;
161+ let osc_address = format ! (
162+ "{}:{}" ,
163+ self . config. osc. address, self . config. osc. output_port
164+ ) ;
162165 if let Err ( e) = self . socket . send_to ( & buf, osc_address. as_str ( ) ) . await {
163166 eprintln ! ( "Error sending typing indicator: {}" , e) ;
164167 }
@@ -188,7 +191,11 @@ async fn ask_chatgpt(prompt: &str, config: &OpenAiConfig) -> Result<String, Box<
188191 Ok ( res_body. choices [ 0 ] . message . content . clone ( ) )
189192}
190193
191- async fn send_to_chatbox ( message : & str , config : & Config , socket : & UdpSocket ) -> Result < ( ) , Box < dyn Error > > {
194+ async fn send_to_chatbox (
195+ message : & str ,
196+ config : & Config ,
197+ socket : & UdpSocket ,
198+ ) -> Result < ( ) , Box < dyn Error > > {
192199 // Set typing indicator to true
193200 let typing_on = OscMessage {
194201 addr : "/chatbox/typing" . to_string ( ) ,
@@ -207,13 +214,17 @@ async fn send_to_chatbox(message: &str, config: &Config, socket: &UdpSocket) ->
207214 . collect ( ) ;
208215
209216 // Send each chunk as a separate message
210- for ( i, chunk) in chunks. iter ( ) . enumerate ( ) . take ( config. osc . max_message_chunks ) {
217+ for ( i, chunk) in chunks
218+ . iter ( )
219+ . enumerate ( )
220+ . take ( config. osc . max_message_chunks )
221+ {
211222 let osc_message = OscMessage {
212223 addr : "/chatbox/input" . to_string ( ) ,
213224 args : vec ! [
214225 OscType :: String ( chunk. to_string( ) ) ,
215- OscType :: Bool ( true ) , // Send immediately
216- OscType :: Bool ( i == 0 ) , // Trigger notification only for the first chunk
226+ OscType :: Bool ( true ) , // Send immediately
227+ OscType :: Bool ( i == 0 ) , // Trigger notification only for the first chunk
217228 ] ,
218229 } ;
219230
@@ -235,8 +246,15 @@ async fn send_to_chatbox(message: &str, config: &Config, socket: &UdpSocket) ->
235246 Ok ( ( ) )
236247}
237248
238- async fn transcribe_audio ( audio_data : Vec < u8 > , config : & OpenAiConfig , rate_limiter : & mut RateLimiter ) -> Result < String , Box < dyn Error > > {
239- println ! ( "Starting audio transcription. Audio data size: {} bytes" , audio_data. len( ) ) ;
249+ async fn transcribe_audio (
250+ audio_data : Vec < u8 > ,
251+ config : & OpenAiConfig ,
252+ rate_limiter : & mut RateLimiter ,
253+ ) -> Result < String , Box < dyn Error > > {
254+ println ! (
255+ "Starting audio transcription. Audio data size: {} bytes" ,
256+ audio_data. len( )
257+ ) ;
240258
241259 if audio_data. is_empty ( ) {
242260 return Err ( "Audio data is empty" . into ( ) ) ;
@@ -294,8 +312,11 @@ async fn process_audio(
294312 // Check if audio is shorter than the minimum transcription duration
295313 let min_duration = Duration :: from_secs_f32 ( config. audio . min_transcription_duration ) ;
296314 if audio_duration < min_duration {
297- println ! ( "Audio too short ({:.2}s). Minimum duration is {:.2}s. Skipping transcription." ,
298- audio_duration. as_secs_f32( ) , min_duration. as_secs_f32( ) ) ;
315+ println ! (
316+ "Audio too short ({:.2}s). Minimum duration is {:.2}s. Skipping transcription." ,
317+ audio_duration. as_secs_f32( ) ,
318+ min_duration. as_secs_f32( )
319+ ) ;
299320 typing_indicator. set_typing ( false ) . await ;
300321 return Ok ( ( ) ) ;
301322 }
@@ -312,7 +333,7 @@ async fn process_audio(
312333 println ! ( "Translation: {}" , response) ;
313334 println ! ( "---" ) ;
314335
315- typing_indicator. set_typing ( false ) . await ; // Stop typing indicator
336+ typing_indicator. set_typing ( false ) . await ; // Stop typing indicator
316337
317338 send_to_chatbox ( & response, & config, socket) . await ?;
318339
@@ -337,7 +358,9 @@ fn start_audio_recording(
337358 tx : mpsc:: Sender < AudioEvent > ,
338359) -> Result < ( ) , Box < dyn Error > > {
339360 let host = cpal:: default_host ( ) ;
340- let device = host. default_input_device ( ) . expect ( "No input device available" ) ;
361+ let device = host
362+ . default_input_device ( )
363+ . expect ( "No input device available" ) ;
341364 let device_config = device. default_input_config ( ) ?;
342365
343366 let sample_rate = device_config. sample_rate ( ) . 0 as f32 ;
@@ -385,7 +408,9 @@ fn start_audio_recording(
385408
386409 let mut buffer = audio_data_clone. lock ( ) . unwrap ( ) ;
387410 if !buffer. is_empty ( ) {
388- println ! ( "Silence detected. Stopping recording and processing audio..." ) ;
411+ println ! (
412+ "Silence detected. Stopping recording and processing audio..."
413+ ) ;
389414 let mut wav_buffer = Vec :: new ( ) ;
390415 {
391416 let mut writer = WavWriter :: new (
@@ -396,7 +421,8 @@ fn start_audio_recording(
396421 bits_per_sample : 32 ,
397422 sample_format : hound:: SampleFormat :: Float ,
398423 } ,
399- ) . unwrap ( ) ;
424+ )
425+ . unwrap ( ) ;
400426
401427 for & sample in buffer. iter ( ) {
402428 writer. write_sample ( sample) . unwrap ( ) ;
@@ -419,7 +445,7 @@ fn start_audio_recording(
419445 err_fn,
420446 None ,
421447 ) ?
422- } ,
448+ }
423449 _ => return Err ( "Unsupported sample format" . into ( ) ) ,
424450 } ;
425451
@@ -463,7 +489,10 @@ async fn run_main() -> Result<(), Box<dyn Error>> {
463489
464490 println ! ( "Starting continuous audio recording..." ) ;
465491 println ! ( "Translating to: {}" , config. translation. target_language) ;
466- println ! ( "Rate limit: {} requests per minute" , config. rate_limit. requests_per_minute) ;
492+ println ! (
493+ "Rate limit: {} requests per minute" ,
494+ config. rate_limit. requests_per_minute
495+ ) ;
467496
468497 let ( tx, mut rx) = mpsc:: channel :: < AudioEvent > ( 100 ) ;
469498
@@ -483,16 +512,24 @@ async fn run_main() -> Result<(), Box<dyn Error>> {
483512 match event {
484513 AudioEvent :: StartRecording => {
485514 typing_indicator. set_typing ( true ) . await ;
486- } ,
515+ }
487516 AudioEvent :: StopRecording => {
488517 typing_indicator. set_typing ( false ) . await ;
489- } ,
518+ }
490519 AudioEvent :: AudioData ( audio_data) => {
491- match process_audio ( audio_data, & config, & socket, & mut rate_limiter, & typing_indicator) . await {
492- Ok ( _) => { } ,
520+ match process_audio (
521+ audio_data,
522+ & config,
523+ & socket,
524+ & mut rate_limiter,
525+ & typing_indicator,
526+ )
527+ . await
528+ {
529+ Ok ( _) => { }
493530 Err ( e) => eprintln ! ( "Error processing audio: {}" , e) ,
494531 }
495- } ,
532+ }
496533 }
497534 }
498535
0 commit comments