makaveli commited on
Commit
5cb8342
1 Parent(s): f2683ae

Update client.py with TTS websocket

Browse files
Files changed (1) hide show
  1. whisper_live/client.py +38 -7
whisper_live/client.py CHANGED
@@ -122,6 +122,22 @@ class Client:
122
  self.frames = b""
123
  print("[INFO]: * recording")
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def on_message(self, ws, message):
126
  """
127
  Callback function called when a message is received from the server.
@@ -193,10 +209,10 @@ class Client:
193
  wrapper = textwrap.TextWrapper(width=60)
194
  word_list = wrapper.wrap(text="".join(text))
195
  # Print each line.
196
- if os.name == "nt":
197
- os.system("cls")
198
- else:
199
- os.system("clear")
200
  for element in word_list:
201
  print(element)
202
 
@@ -231,7 +247,22 @@ class Client:
231
  }
232
  )
233
  )
 
 
 
 
 
 
 
 
 
234
 
 
 
 
 
 
 
235
  @staticmethod
236
  def bytes_to_float_array(audio_bytes):
237
  """
@@ -340,7 +371,7 @@ class Client:
340
  """
341
  return self.client_socket
342
 
343
- def write_audio_frames_to_file(self, frames, file_name):
344
  """
345
  Write audio frames to a WAV file.
346
 
@@ -356,7 +387,7 @@ class Client:
356
  wavfile: wave.Wave_write
357
  wavfile.setnchannels(self.channels)
358
  wavfile.setsampwidth(2)
359
- wavfile.setframerate(self.rate)
360
  wavfile.writeframes(frames)
361
 
362
  def process_hls_stream(self, hls_url):
@@ -540,4 +571,4 @@ class TranscriptionClient:
540
  resampled_file = resample(audio)
541
  self.client.play_file(resampled_file)
542
  else:
543
- self.client.record()
 
122
  self.frames = b""
123
  print("[INFO]: * recording")
124
 
125
+ # TTS audio websocket client
126
+ socket_url = f"ws://{host}:8888"
127
+ self.tts_client_socket = websocket.WebSocketApp(
128
+ socket_url,
129
+ on_open=lambda ws: self.on_open_tts(ws),
130
+ on_message=lambda ws, message: self.on_message_tts(ws, message),
131
+ on_error=lambda ws, error: self.on_error_tts(ws, error),
132
+ on_close=lambda ws, close_status_code, close_msg: self.on_close_tts(
133
+ ws, close_status_code, close_msg
134
+ ),
135
+ )
136
+
137
+ self.tts_ws_thread = threading.Thread(target=self.tts_client_socket.run_forever)
138
+ self.tts_ws_thread.setDaemon(True)
139
+ self.tts_ws_thread.start()
140
+
141
  def on_message(self, ws, message):
142
  """
143
  Callback function called when a message is received from the server.
 
209
  wrapper = textwrap.TextWrapper(width=60)
210
  word_list = wrapper.wrap(text="".join(text))
211
  # Print each line.
212
+ # if os.name == "nt":
213
+ # os.system("cls")
214
+ # else:
215
+ # os.system("clear")
216
  for element in word_list:
217
  print(element)
218
 
 
247
  }
248
  )
249
  )
250
+
251
+ def on_open_tts(self):
252
+ pass
253
+
254
+ def on_message_tts(self, ws, message):
255
+ # print(message)
256
+ print(type(message))
257
+ self.write_audio_frames_to_file(message.tobytes(), "tts_out.wav", rate=24000)
258
+ pass
259
 
260
+ def on_error_tts(self, ws, error):
261
+ print(error)
262
+
263
+ def on_close_tts(self, ws, close_status_code, close_msg):
264
+ print(f"[INFO]: Websocket connection closed: {close_status_code}: {close_msg}")
265
+
266
  @staticmethod
267
  def bytes_to_float_array(audio_bytes):
268
  """
 
371
  """
372
  return self.client_socket
373
 
374
+ def write_audio_frames_to_file(self, frames, file_name, rate=None):
375
  """
376
  Write audio frames to a WAV file.
377
 
 
387
  wavfile: wave.Wave_write
388
  wavfile.setnchannels(self.channels)
389
  wavfile.setsampwidth(2)
390
+ wavfile.setframerate(self.rate if rate is None else rate)
391
  wavfile.writeframes(frames)
392
 
393
  def process_hls_stream(self, hls_url):
 
571
  resampled_file = resample(audio)
572
  self.client.play_file(resampled_file)
573
  else:
574
+ self.client.record()