diff --git a/app/controllers/chat.py b/app/controllers/chat.py index 434287d..dc0d0c1 100644 --- a/app/controllers/chat.py +++ b/app/controllers/chat.py @@ -69,6 +69,11 @@ def split_string_with_punctuation(current_sentence,text,is_first): result.append(current_sentence) current_sentence = '' return result, current_sentence, is_first + +def vad_preprocess(audio): + if len(audio)<1280: + return ('A'*1280) + return audio[:1280],audio[1280:] #-------------------------------------------------------- # 创建新聊天 @@ -461,16 +466,23 @@ async def streaming_chat_lasting_handler(ws,db,redis): async def voice_call_audio_producer(ws,audio_queue,future,input_finished_event): logger.debug("音频数据生产函数启动") is_future_done = False - while not input_finished_event.is_set(): - voice_call_data_json = json.loads(await ws.receive_text()) - if not is_future_done: #在第一次循环中读取session_id - future.set_result(voice_call_data_json['meta_info']['session_id']) - is_future_done = True - if voice_call_data_json["is_close"]: - input_finished_event.set() - break - else: - await audio_queue.put(voice_call_data_json["audio"]) #将音频数据存入audio_q + audio_data = "" + try: + while not input_finished_event.is_set(): + voice_call_data_json = json.loads(await ws.receive_text()) + if not is_future_done: #在第一次循环中读取session_id + future.set_result(voice_call_data_json['meta_info']['session_id']) + is_future_done = True + if voice_call_data_json["is_close"]: + input_finished_event.set() + break + else: + audio_data += voice_call_data_json["audio"] + while len(audio_data) > 1280: + vad_frame,audio_data = vad_preprocess(audio_data) + await audio_queue.put(vad_frame) #将音频数据存入audio_q + except KeyError as ke: + logger.info(f"收到心跳包") #音频数据消费函数 async def voice_call_audio_consumer(audio_q,asr_result_q,input_finished_event,asr_finished_event):