zhaomingwork
2023-05-12 6c5271b91101113ebb998e77443d1536ddf929ad
modified ws_clinet.py to parallel handle wavs in diff process
2个文件已修改
41 ■■■■ 已修改文件
funasr/runtime/python/websocket/ws_client.py 37 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/websocket/websocketsrv.h 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/websocket/ws_client.py
@@ -84,6 +84,7 @@
                    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
@@ -96,7 +97,7 @@
        await asyncio.sleep(0.005)
async def record_from_scp():
async def record_from_scp(chunk_begin,chunk_size):
    import wave
    global voices
    is_finished = False
@@ -105,6 +106,8 @@
        wavs = f_scp.readlines()
    else:
        wavs = [args.audio_in]
    if chunk_size>0:
        wavs=wavs[chunk_begin:chunk_begin+chunk_size]
    for wav in wavs:
        wav_splits = wav.strip().split()
        wav_name = wav_splits[0] if len(wav_splits) > 1 else "demo"
@@ -220,27 +223,47 @@
            traceback.print_exc()
            exit(0)
async def ws_client(id):
async def ws_client(id,chunk_begin,chunk_size):
    global websocket
    uri = "ws://{}:{}".format(args.host, args.port)
    async for websocket in websockets.connect(uri, subprotocols=["binary"], ping_interval=None):
        if args.audio_in is not None:
            task = asyncio.create_task(record_from_scp())
            task = asyncio.create_task(record_from_scp(chunk_begin,chunk_size))
        else:
            task = asyncio.create_task(record_microphone())
        task2 = asyncio.create_task(ws_send())
        task3 = asyncio.create_task(message(id))
        await asyncio.gather(task, task2, task3)
def one_thread(id):
   asyncio.get_event_loop().run_until_complete(ws_client(id))
def one_thread(id,chunk_begin,chunk_size):
   asyncio.get_event_loop().run_until_complete(ws_client(id,chunk_begin,chunk_size))
   asyncio.get_event_loop().run_forever()
if __name__ == '__main__':
    # calculate the number of wavs for each preocess
    if args.audio_in.endswith(".scp"):
        f_scp = open(args.audio_in)
        wavs = f_scp.readlines()
    else:
        wavs = [args.audio_in]
    total_len=len(wavs)
    if total_len>=args.test_thread_num:
         chunk_size=int((total_len)/args.test_thread_num)
         remain_wavs=total_len-chunk_size*args.test_thread_num
    else:
         chunk_size=0
    process_list = []
    for i in range(args.test_thread_num):
        p = Process(target=one_thread,args=(i,))
    chunk_begin=0
    for i in range(args.test_thread_num):
        now_chunk_size= chunk_size
        if remain_wavs>0:
            now_chunk_size=chunk_size+1
            remain_wavs=remain_wavs-1
        # process i handle wavs at chunk_begin and size of now_chunk_size
        p = Process(target=one_thread,args=(i,chunk_begin,now_chunk_size))
        chunk_begin=chunk_begin+now_chunk_size
        p.start()
        process_list.append(p)
funasr/runtime/websocket/websocketsrv.h
@@ -89,9 +89,7 @@
  // use map to keep the received samples data from one connection in offline
  // engine. if for online engline, a data struct is needed(TODO)
  std::map<websocketpp::connection_hdl, std::shared_ptr<std::vector<char>>,
           std::owner_less<websocketpp::connection_hdl>>
      sample_map;
  std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,
           std::owner_less<websocketpp::connection_hdl>>