close when end of audio from client

This commit is contained in:
makaveli10
2024-02-15 18:07:19 +05:30
parent 9bb92b9bb2
commit 01dc69e068
+10 -2
View File
@@ -184,6 +184,8 @@ class TranscriptionServer:
A numpy array containing the audio. A numpy array containing the audio.
""" """
frame_data = websocket.recv() frame_data = websocket.recv()
if frame_data == b"END_OF_AUDIO":
return False
return np.frombuffer(frame_data, dtype=np.float32) return np.frombuffer(frame_data, dtype=np.float32)
def handle_new_connection(self, websocket, faster_whisper_custom_model_path, def handle_new_connection(self, websocket, faster_whisper_custom_model_path,
@@ -212,6 +214,10 @@ class TranscriptionServer:
def process_audio_frames(self, websocket): def process_audio_frames(self, websocket):
frame_np = self.get_audio_from_websocket(websocket) frame_np = self.get_audio_from_websocket(websocket)
client = self.client_manager.get_client(websocket) client = self.client_manager.get_client(websocket)
if frame_np is False:
if self.backend == "tensorrt":
client.set_eos(True)
return False
if self.backend == "tensorrt": if self.backend == "tensorrt":
voice_active = self.voice_activity(websocket, frame_np) voice_active = self.voice_activity(websocket, frame_np)
@@ -219,9 +225,10 @@ class TranscriptionServer:
self.no_voice_activity_chunks = 0 self.no_voice_activity_chunks = 0
client.set_eos(False) client.set_eos(False)
if self.use_vad and not voice_active: if self.use_vad and not voice_active:
return return True
client.add_frames(frame_np) client.add_frames(frame_np)
return True
def recv_audio(self, def recv_audio(self,
websocket, websocket,
@@ -260,7 +267,8 @@ class TranscriptionServer:
try: try:
while not self.client_manager.is_client_timeout(websocket): while not self.client_manager.is_client_timeout(websocket):
self.process_audio_frames(websocket) if not self.process_audio_frames(websocket):
break
except ConnectionClosed: except ConnectionClosed:
logging.info("Connection closed by client") logging.info("Connection closed by client")
except Exception as e: except Exception as e: