2025-02-02 23:01:14 +08:00
|
|
|
import json
|
|
|
|
|
import asyncio
|
|
|
|
|
import time
|
2025-05-26 02:20:38 +08:00
|
|
|
from core.providers.tts.dto.dto import SentenceType
|
2025-07-21 09:30:23 +08:00
|
|
|
from core.utils import textUtils
|
2025-02-02 23:01:14 +08:00
|
|
|
|
2025-02-18 00:07:19 +08:00
|
|
|
TAG = __name__
|
2025-05-24 12:11:13 +08:00
|
|
|
|
2025-03-13 15:30:00 +08:00
|
|
|
|
2025-05-26 02:20:38 +08:00
|
|
|
async def sendAudioMessage(conn, sentenceType, audios, text):
|
2025-03-05 17:26:32 +08:00
|
|
|
# 发送句子开始消息
|
2025-06-08 11:50:09 +08:00
|
|
|
conn.logger.bind(tag=TAG).info(f"发送音频消息: {sentenceType}, {text}")
|
2025-07-21 09:30:23 +08:00
|
|
|
|
2025-05-26 16:12:38 +08:00
|
|
|
pre_buffer = False
|
2025-07-23 14:35:59 +08:00
|
|
|
if conn.tts.tts_audio_first_sentence:
|
2025-05-26 16:12:38 +08:00
|
|
|
conn.logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
|
|
|
|
conn.tts.tts_audio_first_sentence = False
|
|
|
|
|
pre_buffer = True
|
2025-08-05 18:35:37 +08:00
|
|
|
await send_tts_message(conn, "start", None)
|
2025-02-02 23:01:14 +08:00
|
|
|
|
2025-08-05 18:35:37 +08:00
|
|
|
if sentenceType == SentenceType.FIRST:
|
|
|
|
|
await send_tts_message(conn, "sentence_start", text)
|
2025-05-24 12:11:13 +08:00
|
|
|
|
2025-05-26 16:12:38 +08:00
|
|
|
await sendAudio(conn, audios, pre_buffer)
|
2025-05-24 12:11:13 +08:00
|
|
|
|
|
|
|
|
# 发送结束消息(如果是最后一个文本)
|
2025-05-26 02:20:38 +08:00
|
|
|
if conn.llm_finish_task and sentenceType == SentenceType.LAST:
|
2025-05-24 12:11:13 +08:00
|
|
|
await send_tts_message(conn, "stop", None)
|
2025-05-30 15:47:32 +08:00
|
|
|
conn.client_is_speaking = False
|
2025-05-24 12:11:13 +08:00
|
|
|
if conn.close_after_chat:
|
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 播放音频
|
|
|
|
|
async def sendAudio(conn, audios, pre_buffer=True):
|
2025-08-05 14:10:55 +08:00
|
|
|
if audios is None:
|
2025-05-26 02:20:38 +08:00
|
|
|
return
|
2025-08-05 14:10:55 +08:00
|
|
|
# 如果audios不是opus数组,则不需要进行遍历,可以直接发送;这里需要进行流控管理,防止发送过快引发客户端溢出
|
|
|
|
|
if isinstance(audios ,bytes):
|
|
|
|
|
await conn.websocket.send(audios)
|
2025-05-24 12:11:13 +08:00
|
|
|
else:
|
2025-08-05 14:10:55 +08:00
|
|
|
if audios is None or len(audios) == 0:
|
|
|
|
|
return
|
|
|
|
|
# 流控参数优化
|
|
|
|
|
frame_duration = 60 # 帧时长(毫秒),匹配 Opus 编码
|
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
|
play_position = 0
|
|
|
|
|
# 仅当第一句话时执行预缓冲
|
|
|
|
|
if pre_buffer:
|
|
|
|
|
pre_buffer_frames = min(3, len(audios))
|
|
|
|
|
for i in range(pre_buffer_frames):
|
|
|
|
|
await conn.websocket.send(audios[i])
|
|
|
|
|
remaining_audios = audios[pre_buffer_frames:]
|
|
|
|
|
else:
|
|
|
|
|
remaining_audios = audios
|
2025-05-24 12:11:13 +08:00
|
|
|
|
2025-08-05 14:10:55 +08:00
|
|
|
# 播放剩余音频帧
|
|
|
|
|
for opus_packet in remaining_audios:
|
|
|
|
|
if conn.client_abort:
|
|
|
|
|
break
|
2025-03-01 17:09:01 +08:00
|
|
|
|
2025-08-05 14:10:55 +08:00
|
|
|
# 重置没有声音的状态
|
|
|
|
|
conn.last_activity_time = time.time() * 1000
|
2025-05-24 12:11:13 +08:00
|
|
|
|
2025-08-05 14:10:55 +08:00
|
|
|
# 计算预期发送时间
|
|
|
|
|
expected_time = start_time + (play_position / 1000)
|
|
|
|
|
current_time = time.perf_counter()
|
|
|
|
|
delay = expected_time - current_time
|
|
|
|
|
if delay > 0:
|
|
|
|
|
await asyncio.sleep(delay)
|
2025-03-01 17:09:01 +08:00
|
|
|
|
2025-08-05 14:10:55 +08:00
|
|
|
await conn.websocket.send(opus_packet)
|
2025-03-10 15:56:28 +08:00
|
|
|
|
2025-08-05 14:10:55 +08:00
|
|
|
play_position += frame_duration
|
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-08-05 18:35:37 +08:00
|
|
|
if text is None and state == "sentence_start":
|
2025-08-05 14:10:55 +08:00
|
|
|
return
|
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:
|
2025-07-23 14:35:59 +08:00
|
|
|
message["text"] = textUtils.check_emoji(text)
|
2025-02-02 23:01:14 +08:00
|
|
|
|
2025-05-24 12:11:13 +08:00
|
|
|
# TTS播放结束
|
2025-02-11 12:48:39 +08:00
|
|
|
if state == "stop":
|
2025-05-24 12:11:13 +08:00
|
|
|
# 播放提示音
|
|
|
|
|
tts_notify = conn.config.get("enable_stop_tts_notify", False)
|
|
|
|
|
if tts_notify:
|
|
|
|
|
stop_tts_notify_voice = conn.config.get(
|
|
|
|
|
"stop_tts_notify_voice", "config/assets/tts_notify.mp3"
|
|
|
|
|
)
|
|
|
|
|
audios, _ = conn.tts.audio_to_opus_data(stop_tts_notify_voice)
|
|
|
|
|
await sendAudio(conn, audios)
|
|
|
|
|
# 清除服务端讲话状态
|
2025-02-11 12:48:39 +08:00
|
|
|
conn.clearSpeakStatus()
|
2025-02-02 23:01:14 +08:00
|
|
|
|
2025-05-24 12:11:13 +08:00
|
|
|
# 发送消息到客户端
|
|
|
|
|
await conn.websocket.send(json.dumps(message))
|
|
|
|
|
|
2025-02-13 21:22:36 +08:00
|
|
|
|
2025-02-11 13:51:44 +08:00
|
|
|
async def send_stt_message(conn, text):
|
2025-05-29 23:56:34 +08:00
|
|
|
end_prompt_str = conn.config.get("end_prompt", {}).get("prompt")
|
|
|
|
|
if end_prompt_str and end_prompt_str == text:
|
|
|
|
|
await send_tts_message(conn, "start")
|
|
|
|
|
return
|
|
|
|
|
|
2025-02-11 13:51:44 +08:00
|
|
|
"""发送 STT 状态消息"""
|
2025-07-09 14:23:47 +08:00
|
|
|
|
|
|
|
|
# 解析JSON格式,提取实际的用户说话内容
|
|
|
|
|
display_text = text
|
|
|
|
|
try:
|
|
|
|
|
# 尝试解析JSON格式
|
|
|
|
|
if text.strip().startswith('{') and text.strip().endswith('}'):
|
|
|
|
|
parsed_data = json.loads(text)
|
|
|
|
|
if isinstance(parsed_data, dict) and "content" in parsed_data:
|
|
|
|
|
# 如果是包含说话人信息的JSON格式,只显示content部分
|
|
|
|
|
display_text = parsed_data["content"]
|
2025-07-18 11:34:14 +08:00
|
|
|
# 保存说话人信息到conn对象
|
|
|
|
|
if "speaker" in parsed_data:
|
|
|
|
|
conn.current_speaker = parsed_data["speaker"]
|
2025-07-09 14:23:47 +08:00
|
|
|
except (json.JSONDecodeError, TypeError):
|
|
|
|
|
# 如果不是JSON格式,直接使用原始文本
|
|
|
|
|
display_text = text
|
2025-07-21 09:30:23 +08:00
|
|
|
stt_text = textUtils.get_string_no_punctuation_or_emoji(display_text)
|
2025-02-11 13:51:44 +08:00
|
|
|
await conn.websocket.send(
|
2025-03-31 23:26:23 +08:00
|
|
|
json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id})
|
|
|
|
|
)
|
2025-05-30 15:47:32 +08:00
|
|
|
conn.client_is_speaking = True
|
2025-02-11 13:51:44 +08:00
|
|
|
await send_tts_message(conn, "start")
|