@@ -64,8 +64,12 @@ def run():
64
64
type = str ,
65
65
help = "Path to an audio file | 'microphone' | 'microphone:<DEVICE_ID>'" ,
66
66
)
67
- parser .add_argument ("--host" , required = True , type = str , help = "Server host" )
68
- parser .add_argument ("--port" , required = True , type = int , help = "Server port" )
67
+ parser .add_argument (
68
+ "--host" , default = "0.0.0.0" , type = str , help = "Server host. Defaults to 0.0.0.0"
69
+ )
70
+ parser .add_argument (
71
+ "--port" , default = 7007 , type = int , help = "Server port. Defaults to 7007"
72
+ )
69
73
parser .add_argument (
70
74
"--step" , default = 0.5 , type = float , help = f"{ argdoc .STEP } . Defaults to 0.5"
71
75
)
@@ -93,17 +97,18 @@ def run():
93
97
94
98
# Wait for READY signal from server
95
99
print ("Waiting for server to be ready..." , end = "" , flush = True )
96
- while True :
100
+ while not stop_event . is_set () :
97
101
try :
98
102
message = ws .recv ()
99
103
if message .strip () == "READY" :
100
104
print (" OK" )
101
105
break
102
106
print (f"\n Unexpected message while waiting for READY: { message } " )
103
107
except WebSocketException as e :
104
- print (f"\n Error while waiting for server: { e } " )
108
+ print (f"\n WebSocket error while waiting for server: { e } " )
105
109
return
106
110
111
+ # Start threads for sending and receiving audio
107
112
sender = Thread (
108
113
target = send_audio ,
109
114
args = [ws , args .source , args .step , args .sample_rate , stop_event ],
@@ -113,14 +118,25 @@ def run():
113
118
sender .start ()
114
119
receiver .start ()
115
120
121
+ try :
122
+ # Wait for threads to complete or for keyboard interrupt
123
+ sender .join ()
124
+ receiver .join ()
125
+ except KeyboardInterrupt :
126
+ print ("\n Shutting down..." )
127
+ stop_event .set ()
128
+
116
129
except Exception as e :
117
130
print (f"Error: { e } " )
118
- stop_event . set ()
131
+
119
132
finally :
133
+ stop_event .set ()
120
134
try :
121
135
ws .close ()
122
- except :
123
- pass
136
+ except WebSocketException :
137
+ print ("Error closing WebSocket" )
138
+ except Exception as e :
139
+ print (f"Unexpected error closing WebSocket: { e } " )
124
140
125
141
126
142
if __name__ == "__main__" :
0 commit comments