ASR_client.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- encoding: utf-8 -*-
  2. import time
  3. import websockets
  4. import asyncio
  5. # import threading
  6. import argparse
  7. import json
  8. parser = argparse.ArgumentParser()
  9. parser.add_argument("--host",
  10. type=str,
  11. default="localhost",
  12. required=False,
  13. help="host ip, localhost, 0.0.0.0")
  14. parser.add_argument("--port",
  15. type=int,
  16. default=10095,
  17. required=False,
  18. help="grpc server port")
  19. parser.add_argument("--chunk_size",
  20. type=int,
  21. default=300,
  22. help="ms")
  23. parser.add_argument("--audio_in",
  24. type=str,
  25. default=None,
  26. help="audio_in")
  27. args = parser.parse_args()
  28. # voices = asyncio.Queue()
  29. from queue import Queue
  30. voices = Queue()
  31. # 其他函数可以通过调用send(data)来发送数据,例如:
  32. async def record_microphone():
  33. import pyaudio
  34. #print("2")
  35. global voices
  36. FORMAT = pyaudio.paInt16
  37. CHANNELS = 1
  38. RATE = 16000
  39. CHUNK = int(RATE / 1000 * args.chunk_size)
  40. p = pyaudio.PyAudio()
  41. stream = p.open(format=FORMAT,
  42. channels=CHANNELS,
  43. rate=RATE,
  44. input=True,
  45. frames_per_buffer=CHUNK)
  46. is_speaking = True
  47. while True:
  48. data = stream.read(CHUNK)
  49. data = data.decode('ISO-8859-1')
  50. message = json.dumps({"chunk": args.chunk_size, "is_speaking": is_speaking, "audio": data})
  51. voices.put(message)
  52. #print(voices.qsize())
  53. await asyncio.sleep(0.01)
  54. # 其他函数可以通过调用send(data)来发送数据,例如:
  55. async def record_from_scp():
  56. import wave
  57. global voices
  58. if args.audio_in.endswith(".scp"):
  59. f_scp = open(args.audio_in)
  60. wavs = f_scp.readlines()
  61. else:
  62. wavs = [args.audio_in]
  63. for wav in wavs:
  64. wav_splits = wav.strip().split()
  65. wav_path = wav_splits[1] if len(wav_splits) > 1 else wav_splits[0]
  66. # bytes_f = open(wav_path, "rb")
  67. # bytes_data = bytes_f.read()
  68. with wave.open(wav_path, "rb") as wav_file:
  69. # 获取音频参数
  70. params = wav_file.getparams()
  71. # 获取头信息的长度
  72. # header_length = wav_file.getheaders()[0][1]
  73. # 读取音频帧数据,跳过头信息
  74. # wav_file.setpos(header_length)
  75. frames = wav_file.readframes(wav_file.getnframes())
  76. # 将音频帧数据转换为字节类型的数据
  77. audio_bytes = bytes(frames)
  78. stride = int(args.chunk_size/1000*16000*2)
  79. chunk_num = (len(audio_bytes)-1)//stride + 1
  80. print(stride)
  81. is_speaking = True
  82. for i in range(chunk_num):
  83. if i == chunk_num-1:
  84. is_speaking = False
  85. beg = i*stride
  86. data = audio_bytes[beg:beg+stride]
  87. data = data.decode('ISO-8859-1')
  88. message = json.dumps({"chunk": args.chunk_size, "is_speaking": is_speaking, "audio": data})
  89. voices.put(message)
  90. # print("data_chunk: ", len(data_chunk))
  91. # print(voices.qsize())
  92. await asyncio.sleep(args.chunk_size/1000)
  93. async def ws_send():
  94. global voices
  95. global websocket
  96. print("started to sending data!")
  97. while True:
  98. while not voices.empty():
  99. data = voices.get()
  100. voices.task_done()
  101. try:
  102. await websocket.send(data) # 通过ws对象发送数据
  103. except Exception as e:
  104. print('Exception occurred:', e)
  105. await asyncio.sleep(0.01)
  106. await asyncio.sleep(0.01)
  107. async def message():
  108. global websocket
  109. while True:
  110. try:
  111. meg = await websocket.recv()
  112. meg = json.loads(meg)
  113. print(meg)
  114. except Exception as e:
  115. print("Exception:", e)
  116. async def ws_client():
  117. global websocket # 定义一个全局变量ws,用于保存websocket连接对象
  118. # uri = "ws://11.167.134.197:8899"
  119. uri = "ws://{}:{}".format(args.host, args.port)
  120. #ws = await websockets.connect(uri, subprotocols=["binary"]) # 创建一个长连接
  121. async for websocket in websockets.connect(uri, subprotocols=["binary"], ping_interval=None):
  122. if args.audio_in is not None:
  123. task = asyncio.create_task(record_from_scp()) # 创建一个后台任务录音
  124. else:
  125. task = asyncio.create_task(record_microphone()) # 创建一个后台任务录音
  126. task2 = asyncio.create_task(ws_send()) # 创建一个后台任务发送
  127. task3 = asyncio.create_task(message()) # 创建一个后台接收消息的任务
  128. await asyncio.gather(task, task2, task3)
  129. asyncio.get_event_loop().run_until_complete(ws_client()) # 启动协程
  130. asyncio.get_event_loop().run_forever()