@@ -51,25 +51,15 @@ def start(self):
51
51
52
52
# Wait for the WebSocket to be ready
53
53
self .ws_ready .wait ()
54
+ self .start_audio_streaming ()
54
55
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 )
57
59
58
60
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 ()
73
63
74
64
def on_open (self , ws ):
75
65
print ("WebSocket connection opened." )
@@ -92,6 +82,14 @@ def on_error(self, ws, error):
92
82
def on_close (self , ws , close_status_code , close_msg ):
93
83
print ("WebSocket connection closed." )
94
84
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
+
95
93
def send_audio (self ):
96
94
while not self .stop_event .is_set ():
97
95
if not self .send_queue .empty ():
0 commit comments