From a30953f75997d31fbc1b18fedf4ec7f82b2f58cf Mon Sep 17 00:00:00 2001
From: zhaomingwork <zhaomingwork@qq.com>
Date: 星期五, 12 五月 2023 11:19:19 +0800
Subject: [PATCH] cpp python websocket compatible
---
funasr/runtime/python/websocket/ws_client.py | 56 +++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 41 insertions(+), 15 deletions(-)
diff --git a/funasr/runtime/python/websocket/ws_client.py b/funasr/runtime/python/websocket/ws_client.py
index 9de31a4..f8632ce 100644
--- a/funasr/runtime/python/websocket/ws_client.py
+++ b/funasr/runtime/python/websocket/ws_client.py
@@ -10,6 +10,10 @@
from multiprocessing import Process
from funasr.fileio.datadir_writer import DatadirWriter
+import logging
+
+logging.basicConfig(level=logging.ERROR)
+
parser = argparse.ArgumentParser()
parser.add_argument("--host",
type=str,
@@ -80,12 +84,13 @@
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
+ message = json.dumps({"chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, "wav_name": wav_name,"is_speaking": True})
+ voices.put(message)
is_speaking = True
while True:
data = stream.read(CHUNK)
- data = data.decode('ISO-8859-1')
- message = json.dumps({"chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, "audio": data, "is_speaking": is_speaking, "is_finished": is_finished})
+ message = data
voices.put(message)
@@ -118,15 +123,21 @@
stride = int(60*args.chunk_size[1]/args.chunk_interval/1000*16000*2)
chunk_num = (len(audio_bytes)-1)//stride + 1
# print(stride)
+
+ # send first time
+ message = json.dumps({"chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, "wav_name": wav_name,"is_speaking": True})
+ voices.put(message)
is_speaking = True
for i in range(chunk_num):
- if i == chunk_num-1:
- is_speaking = False
+
beg = i*stride
data = audio_bytes[beg:beg+stride]
- data = data.decode('ISO-8859-1')
- message = json.dumps({"chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, "is_speaking": is_speaking, "audio": data, "is_finished": is_finished, "wav_name": wav_name})
+ message = data
voices.put(message)
+ if i == chunk_num-1:
+ is_speaking = False
+ message = json.dumps({"is_speaking": is_speaking})
+ voices.put(message)
# print("data_chunk: ", len(data_chunk))
# print(voices.qsize())
sleep_duration = 0.001 if args.send_without_sleep else 60*args.chunk_size[1]/args.chunk_interval/1000
@@ -158,25 +169,40 @@
async def message(id):
global websocket
text_print = ""
+ text_print_2pass_online = ""
+ text_print_2pass_offline = ""
while True:
try:
meg = await websocket.recv()
meg = json.loads(meg)
- # print(meg, end = '')
- # print("\r")
- # print(meg)
wav_name = meg.get("wav_name", "demo")
- print(wav_name)
+ # print(wav_name)
text = meg["text"]
if ibest_writer is not None:
ibest_writer["text"][wav_name] = text
+
if meg["mode"] == "online":
text_print += " {}".format(text)
- else:
+ text_print = text_print[-args.words_max_print:]
+ os.system('clear')
+ print("\rpid"+str(id)+": "+text_print)
+ elif meg["mode"] == "online":
text_print += "{}".format(text)
- text_print = text_print[-args.words_max_print:]
- os.system('clear')
- print("\rpid"+str(id)+": "+text_print)
+ text_print = text_print[-args.words_max_print:]
+ os.system('clear')
+ print("\rpid"+str(id)+": "+text_print)
+ else:
+ if meg["mode"] == "2pass-online":
+ text_print_2pass_online += " {}".format(text)
+ text_print = text_print_2pass_offline + text_print_2pass_online
+ else:
+ text_print_2pass_online = ""
+ text_print = text_print_2pass_offline + "{}".format(text)
+ text_print_2pass_offline += "{}".format(text)
+ text_print = text_print[-args.words_max_print:]
+ os.system('clear')
+ print("\rpid" + str(id) + ": " + text_print)
+
except Exception as e:
print("Exception:", e)
traceback.print_exc()
@@ -207,7 +233,7 @@
await asyncio.gather(task, task2, task3)
def one_thread(id):
- asyncio.get_event_loop().run_until_complete(ws_client(id)) # 鍚姩鍗忕▼
+ asyncio.get_event_loop().run_until_complete(ws_client(id))
asyncio.get_event_loop().run_forever()
--
Gitblit v1.9.1