游雁
2023-08-30 c2e4e3c2e9be855277d9f4fa9cd0544892ff829a
funasr/runtime/python/websocket/funasr_wss_client.py
@@ -8,11 +8,10 @@
import json
import traceback
from multiprocessing import Process
from funasr.fileio.datadir_writer import DatadirWriter
# from funasr.fileio.datadir_writer import DatadirWriter
import logging
SUPPORT_AUDIO_TYPE_SETS = ['.wav', '.pcm']
logging.basicConfig(level=logging.ERROR)
parser = argparse.ArgumentParser()
@@ -72,11 +71,13 @@
voices = Queue()
offline_msg_done=False
ibest_writer = None
if args.output_dir is not None:
    writer = DatadirWriter(args.output_dir)
    ibest_writer = writer[f"1best_recog"]
    # if os.path.exists(args.output_dir):
    #     os.remove(args.output_dir)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)
async def record_microphone():
@@ -136,8 +137,17 @@
                frames = wav_file.readframes(wav_file.getnframes())
                audio_bytes = bytes(frames)
        else:
            raise NotImplementedError(
                f'Not supported audio type')
            import ffmpeg
            try:
                # This launches a subprocess to decode audio while down-mixing and resampling as necessary.
                # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
                audio_bytes, _ = (
                    ffmpeg.input(wav_path, threads=0)
                    .output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=16000)
                    .run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
                )
            except ffmpeg.Error as e:
                raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
        # stride = int(args.chunk_size/1000*16000*2)
        stride = int(60 * args.chunk_size[1] / args.chunk_interval / 1000 * 16000 * 2)
@@ -166,10 +176,9 @@
            sleep_duration = 0.001 if args.mode == "offline" else 60 * args.chunk_size[1] / args.chunk_interval / 1000
            
            await asyncio.sleep(sleep_duration)
    # when all data sent, we need to close websocket
    while not voices.empty():
         await asyncio.sleep(1)
    await asyncio.sleep(3)
    if not args.mode=="offline":
        await asyncio.sleep(2)
    # offline model need to wait for message recved
    
    if args.mode=="offline":
@@ -186,27 +195,38 @@
    text_print = ""
    text_print_2pass_online = ""
    text_print_2pass_offline = ""
    if args.output_dir is not None:
        ibest_writer = open(os.path.join(args.output_dir, "text.{}".format(id)), "a", encoding="utf-8")
    else:
        ibest_writer = None
    try:
       while True:
        
            meg = await websocket.recv()
            meg = json.loads(meg)
            # print(meg)
            wav_name = meg.get("wav_name", "demo")
            text = meg["text"]
            if ibest_writer is not None:
                ibest_writer["text"][wav_name] = text
            if ibest_writer is not None:
                text_write_line = "{}\t{}\n".format(wav_name, text)
                ibest_writer.write(text_write_line)
            if meg["mode"] == "online":
                text_print += "{}".format(text)
                text_print = text_print[-args.words_max_print:]
                # os.system('clear')
                os.system('clear')
                print("\rpid" + str(id) + ": " + text_print)
            elif meg["mode"] == "offline":
                text_print += "{}".format(text)
                text_print = text_print[-args.words_max_print:]
                # text_print = text_print[-args.words_max_print:]
                # os.system('clear')
                print("\rpid" + str(id) + ": " + text_print)
                offline_msg_done=True
                print("\rpid" + str(id) + ": " + wav_name + ": " + text_print)
                if ("is_final" in meg and meg["is_final"]==False):
                    offline_msg_done = True
                if not "is_final" in meg:
                    offline_msg_done = True
            else:
                if meg["mode"] == "2pass-online":
                    text_print_2pass_online += "{}".format(text)
@@ -216,7 +236,7 @@
                    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')
                os.system('clear')
                print("\rpid" + str(id) + ": " + text_print)
                offline_msg_done=True
@@ -227,17 +247,6 @@
 
async def print_messge():
    global websocket
    while True:
        try:
            meg = await websocket.recv()
            meg = json.loads(meg)
            print(meg)
        except Exception as e:
            print("Exception:", e)
            #traceback.print_exc()
            exit(0)
async def ws_client(id, chunk_begin, chunk_size):
  if args.audio_in is None:
@@ -290,9 +299,7 @@
            wav_name = wav_splits[0] if len(wav_splits) > 1 else "demo"
            wav_path = wav_splits[1] if len(wav_splits) > 1 else wav_splits[0]
            audio_type = os.path.splitext(wav_path)[-1].lower()
            if audio_type not in SUPPORT_AUDIO_TYPE_SETS:
                raise NotImplementedError(
                    f'Not supported audio type: {audio_type}')
        total_len = len(wavs)
        if total_len >= args.thread_num: