| | |
| | | |
| | | import logging |
| | | |
| | | SUPPORT_AUDIO_TYPE_SETS = ['.wav', '.pcm'] |
| | | logging.basicConfig(level=logging.ERROR) |
| | | |
| | | parser = argparse.ArgumentParser() |
| | |
| | | print(args) |
| | | # 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 |
| | |
| | | input=True, |
| | | frames_per_buffer=CHUNK) |
| | | |
| | | message = json.dumps({"mode": args.mode, "chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, "wav_name": "microphone", "is_speaking": True}) |
| | | message = json.dumps({"mode": args.mode, "chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, |
| | | "wav_name": "microphone", "is_speaking": True}) |
| | | voices.put(message) |
| | | while True: |
| | | |
| | | data = stream.read(CHUNK) |
| | | message = data |
| | | |
| | | voices.put(message) |
| | | |
| | | await asyncio.sleep(0.005) |
| | | |
| | | async def record_from_scp(chunk_begin,chunk_size): |
| | | import wave |
| | | global voices |
| | | is_finished = False |
| | | if args.audio_in.endswith(".scp"): |
| | |
| | | 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() |
| | | if wav_path.endswith(".pcm"): |
| | | with open(wav_path, "rb") as f: |
| | | audio_bytes = f.read() |
| | | elif wav_path.endswith(".wav"): |
| | | import wave |
| | | with wave.open(wav_path, "rb") as wav_file: |
| | | params = wav_file.getparams() |
| | | # header_length = wav_file.getheaders()[0][1] |
| | | # wav_file.setpos(header_length) |
| | | frames = wav_file.readframes(wav_file.getnframes()) |
| | | |
| | | audio_bytes = bytes(frames) |
| | | else: |
| | | raise NotImplementedError( |
| | | f'Not supported audio type') |
| | | |
| | | # stride = int(args.chunk_size/1000*16000*2) |
| | | 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({"mode": args.mode, "chunk_size": args.chunk_size, "chunk_interval": args.chunk_interval, "wav_name": wav_name,"is_speaking": True}) |
| | | message = json.dumps({"mode": args.mode, "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): |
| | |
| | | sleep_duration = 0.001 if args.send_without_sleep else 60*args.chunk_size[1]/args.chunk_interval/1000 |
| | | await asyncio.sleep(sleep_duration) |
| | | |
| | | |
| | | async def ws_send(): |
| | | global voices |
| | | global websocket |
| | |
| | | exit(0) |
| | | await asyncio.sleep(0.005) |
| | | await asyncio.sleep(0.005) |
| | | |
| | | |
| | | |
| | | async def message(id): |
| | | global websocket |
| | |
| | | traceback.print_exc() |
| | | exit(0) |
| | | |
| | | |
| | | async def print_messge(): |
| | | global websocket |
| | | while True: |
| | |
| | | asyncio.get_event_loop().run_until_complete(ws_client(id,chunk_begin,chunk_size)) |
| | | asyncio.get_event_loop().run_forever() |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | # for microphone |
| | | if args.audio_in is None: |
| | |
| | | wavs = f_scp.readlines() |
| | | else: |
| | | 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] |
| | | 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.test_thread_num: |
| | | chunk_size=int((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=1 |
| | |
| | | p.join() |
| | | |
| | | print('end') |
| | | |
| | | |