| | |
| | | import argparse |
| | | import json |
| | | import traceback |
| | | from multiprocessing import Process |
| | | 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, |
| | |
| | | type=int, |
| | | default=100, |
| | | help="chunk") |
| | | parser.add_argument("--output_dir", |
| | | type=str, |
| | | default=None, |
| | | help="output_dir") |
| | | |
| | | args = parser.parse_args() |
| | | args.chunk_size = [int(x) for x in args.chunk_size.split(",")] |
| | |
| | | # voices = asyncio.Queue() |
| | | from queue import Queue |
| | | voices = Queue() |
| | | |
| | | ibest_writer = None |
| | | if args.output_dir is not None: |
| | | writer = DatadirWriter(args.output_dir) |
| | | ibest_writer = writer[f"1best_recog"] |
| | | |
| | | async def record_microphone(): |
| | | is_finished = False |
| | |
| | | wavs = [args.audio_in] |
| | | for wav in wavs: |
| | | wav_splits = wav.strip().split() |
| | | 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] |
| | | |
| | | # bytes_f = open(wav_path, "rb") |
| | | # bytes_data = bytes_f.read() |
| | | with wave.open(wav_path, "rb") as wav_file: |
| | |
| | | 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}) |
| | | 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}) |
| | | voices.put(message) |
| | | # print("data_chunk: ", len(data_chunk)) |
| | | # print(voices.qsize()) |
| | |
| | | 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) |
| | | text = meg["text"][0] |
| | | wav_name = meg.get("wav_name", "demo") |
| | | # 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("\r"+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() |
| | |
| | | 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() |
| | | |
| | | |