2025-03-05 17:26:32 +08:00
|
|
|
|
import traceback
|
|
|
|
|
|
|
2025-02-18 00:07:19 +08:00
|
|
|
|
from config.logger import setup_logging
|
2025-02-02 23:01:14 +08:00
|
|
|
|
import json
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import time
|
2025-03-29 14:17:09 +08:00
|
|
|
|
|
|
|
|
|
|
from core.providers.tts.dto.dto import TTSMessageDTO, SentenceType, MsgType
|
2025-03-31 23:26:23 +08:00
|
|
|
|
from core.utils.util import (
|
|
|
|
|
|
remove_punctuation_and_length,
|
|
|
|
|
|
get_string_no_punctuation_or_emoji,
|
|
|
|
|
|
)
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
2025-02-18 00:07:19 +08:00
|
|
|
|
TAG = __name__
|
|
|
|
|
|
logger = setup_logging()
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
2025-03-13 15:30:00 +08:00
|
|
|
|
|
2025-03-29 14:17:09 +08:00
|
|
|
|
async def sendAudioMessage(conn, ttsMessageDTO: TTSMessageDTO):
|
2025-03-31 15:15:59 +08:00
|
|
|
|
if ttsMessageDTO.u_id != conn.u_id:
|
2025-04-01 14:27:17 +08:00
|
|
|
|
logger.bind(tag=TAG).info(
|
|
|
|
|
|
f"msg id:{ttsMessageDTO.u_id},不是当前对话,当前对话id:{conn.u_id}"
|
|
|
|
|
|
)
|
2025-03-31 15:15:59 +08:00
|
|
|
|
return
|
2025-03-05 17:26:32 +08:00
|
|
|
|
# 发送句子开始消息
|
2025-03-29 14:17:09 +08:00
|
|
|
|
if SentenceType.SENTENCE_START == ttsMessageDTO.sentence_type:
|
|
|
|
|
|
logger.bind(tag=TAG).info(f"发送第一段语音: {ttsMessageDTO.tts_finish_text}")
|
|
|
|
|
|
await send_tts_message(conn, "sentence_start", ttsMessageDTO.tts_finish_text)
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
2025-03-10 15:56:28 +08:00
|
|
|
|
# 流控参数优化
|
|
|
|
|
|
original_frame_duration = 60 # 原始帧时长(毫秒)
|
2025-04-09 14:32:32 +08:00
|
|
|
|
adjusted_frame_duration = int(original_frame_duration * 1) # 缩短20%
|
2025-03-29 14:17:09 +08:00
|
|
|
|
total_frames = len(ttsMessageDTO.content) # 获取总帧数
|
2025-04-01 14:27:17 +08:00
|
|
|
|
compensation = (
|
|
|
|
|
|
total_frames * (original_frame_duration - adjusted_frame_duration) / 1000
|
|
|
|
|
|
) # 补偿时间(秒)
|
2025-03-10 15:56:28 +08:00
|
|
|
|
|
|
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
|
|
play_position = 0 # 已播放时长(毫秒)
|
2025-03-01 17:09:01 +08:00
|
|
|
|
|
2025-03-29 14:17:09 +08:00
|
|
|
|
for opus_packet in ttsMessageDTO.content:
|
2025-03-01 01:54:55 +08:00
|
|
|
|
if conn.client_abort:
|
|
|
|
|
|
return
|
2025-03-01 17:09:01 +08:00
|
|
|
|
|
2025-03-10 15:56:28 +08:00
|
|
|
|
# 计算带加速因子的预期时间
|
2025-03-01 17:09:01 +08:00
|
|
|
|
expected_time = start_time + (play_position / 1000)
|
|
|
|
|
|
current_time = time.perf_counter()
|
|
|
|
|
|
|
2025-03-10 15:56:28 +08:00
|
|
|
|
# 流控等待(使用加速后的帧时长)
|
2025-03-01 17:09:01 +08:00
|
|
|
|
delay = expected_time - current_time
|
|
|
|
|
|
if delay > 0:
|
|
|
|
|
|
await asyncio.sleep(delay)
|
|
|
|
|
|
|
2025-02-02 23:01:14 +08:00
|
|
|
|
await conn.websocket.send(opus_packet)
|
2025-03-10 15:56:28 +08:00
|
|
|
|
play_position += adjusted_frame_duration # 使用调整后的帧时长
|
|
|
|
|
|
|
|
|
|
|
|
# 补偿因加速损失的时长
|
2025-04-09 14:32:32 +08:00
|
|
|
|
# if compensation > 0:
|
|
|
|
|
|
# await asyncio.sleep(compensation)
|
2025-03-29 14:17:09 +08:00
|
|
|
|
if SentenceType.SENTENCE_END == ttsMessageDTO.sentence_type:
|
|
|
|
|
|
logger.bind(tag=TAG).info(f"发送最后一段语音: {ttsMessageDTO.tts_finish_text}")
|
|
|
|
|
|
await send_tts_message(conn, "sentence_end", ttsMessageDTO.tts_finish_text)
|
2025-03-10 15:56:28 +08:00
|
|
|
|
|
2025-03-01 17:09:01 +08:00
|
|
|
|
# 发送结束消息(如果是最后一个文本)
|
2025-03-29 14:17:09 +08:00
|
|
|
|
if conn.llm_finish_task and MsgType.STOP_TTS_RESPONSE == ttsMessageDTO.msg_type:
|
2025-04-01 14:27:17 +08:00
|
|
|
|
await send_tts_message(conn, "stop", None)
|
2025-03-09 21:33:45 +08:00
|
|
|
|
if conn.close_after_chat:
|
2025-03-01 01:54:55 +08:00
|
|
|
|
await conn.close()
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
2025-03-29 14:17:09 +08:00
|
|
|
|
|
2025-02-11 12:48:39 +08:00
|
|
|
|
async def send_tts_message(conn, state, text=None):
|
|
|
|
|
|
"""发送 TTS 状态消息"""
|
2025-03-31 23:26:23 +08:00
|
|
|
|
message = {"type": "tts", "state": state, "session_id": conn.session_id}
|
2025-02-11 12:48:39 +08:00
|
|
|
|
if text is not None:
|
|
|
|
|
|
message["text"] = text
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
2025-02-11 12:48:39 +08:00
|
|
|
|
await conn.websocket.send(json.dumps(message))
|
|
|
|
|
|
if state == "stop":
|
|
|
|
|
|
conn.clearSpeakStatus()
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
2025-02-13 21:22:36 +08:00
|
|
|
|
|
2025-02-11 13:51:44 +08:00
|
|
|
|
async def send_stt_message(conn, text):
|
|
|
|
|
|
"""发送 STT 状态消息"""
|
|
|
|
|
|
stt_text = get_string_no_punctuation_or_emoji(text)
|
|
|
|
|
|
await conn.websocket.send(
|
2025-03-31 23:26:23 +08:00
|
|
|
|
json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id})
|
|
|
|
|
|
)
|
|
|
|
|
|
await conn.websocket.send(
|
|
|
|
|
|
json.dumps(
|
|
|
|
|
|
{
|
|
|
|
|
|
"type": "llm",
|
|
|
|
|
|
"text": "😊",
|
|
|
|
|
|
"emotion": "happy",
|
|
|
|
|
|
"session_id": conn.session_id,
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2025-02-11 13:51:44 +08:00
|
|
|
|
await send_tts_message(conn, "start")
|