@@ -51,25 +51,15 @@ def start(self):
5151
5252 # Wait for the WebSocket to be ready
5353 self .ws_ready .wait ()
54+ self .start_audio_streaming ()
5455
55- send_thread = threading .Thread (target = self .send_audio )
56- play_thread = threading .Thread (target = self .play_audio )
56+ def start_audio_streaming (self ):
57+ self .send_thread = threading .Thread (target = self .send_audio )
58+ self .play_thread = threading .Thread (target = self .play_audio )
5759
5860 with sd .InputStream (samplerate = self .args .sample_rate , channels = 1 , dtype = 'int16' , callback = self .audio_input_callback , blocksize = self .args .chunk_size ):
59- send_thread .start ()
60- play_thread .start ()
61-
62- try :
63- input ("Press Enter to stop streaming..." )
64- except KeyboardInterrupt :
65- print ("\n Streaming interrupted by user." )
66- finally :
67- self .stop_event .set ()
68- send_thread .join ()
69- play_thread .join ()
70- self .ws .close ()
71- ws_thread .join ()
72- print ("Audio streaming stopped." )
61+ self .send_thread .start ()
62+ self .play_thread .start ()
7363
7464 def on_open (self , ws ):
7565 print ("WebSocket connection opened." )
@@ -92,6 +82,14 @@ def on_error(self, ws, error):
9282 def on_close (self , ws , close_status_code , close_msg ):
9383 print ("WebSocket connection closed." )
9484
85+ def on_shutdown (self ):
86+ self .stop_event .set ()
87+ self .send_thread .join ()
88+ self .play_thread .join ()
89+ self .ws .close ()
90+ self .ws_thread .join ()
91+ print ("Service shutdown." )
92+
9593 def send_audio (self ):
9694 while not self .stop_event .is_set ():
9795 if not self .send_queue .empty ():
0 commit comments