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
|
|
|
|
|
from core.utils.util import remove_punctuation_and_length, get_string_no_punctuation_or_emoji
|
|
|
|
|
|
2025-02-18 00:07:19 +08:00
|
|
|
TAG = __name__
|
|
|
|
|
logger = setup_logging()
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
|
|
|
|
2025-02-14 23:09:12 +08:00
|
|
|
async def isLLMWantToFinish(conn):
|
|
|
|
|
first_text = conn.tts_first_text
|
|
|
|
|
last_text = conn.tts_last_text
|
|
|
|
|
_, last_text_without_punctuation = remove_punctuation_and_length(last_text)
|
|
|
|
|
if "再见" in last_text_without_punctuation or "拜拜" in last_text_without_punctuation:
|
|
|
|
|
return True
|
|
|
|
|
_, first_text_without_punctuation = remove_punctuation_and_length(first_text)
|
|
|
|
|
if "再见" in first_text_without_punctuation or "拜拜" in first_text_without_punctuation:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2025-02-02 23:01:14 +08:00
|
|
|
async def sendAudioMessage(conn, audios, duration, text):
|
|
|
|
|
base_delay = conn.tts_duration
|
|
|
|
|
|
2025-02-11 12:48:39 +08:00
|
|
|
# 发送 tts.start
|
2025-02-02 23:01:14 +08:00
|
|
|
if text == conn.tts_first_text:
|
2025-02-18 00:07:19 +08:00
|
|
|
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
2025-02-02 23:01:14 +08:00
|
|
|
conn.tts_start_speak_time = time.time()
|
|
|
|
|
|
2025-02-11 12:48:39 +08:00
|
|
|
# 发送 sentence_start(每个音频文件之前发送一次)
|
2025-02-13 21:22:36 +08:00
|
|
|
sentence_task = asyncio.create_task(
|
|
|
|
|
schedule_with_interrupt(base_delay, send_tts_message(conn, "sentence_start", text))
|
|
|
|
|
)
|
|
|
|
|
conn.scheduled_tasks.append(sentence_task)
|
2025-02-11 12:48:39 +08:00
|
|
|
|
|
|
|
|
conn.tts_duration += duration
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
|
|
|
# 发送音频数据
|
2025-02-11 12:48:39 +08:00
|
|
|
for idx, opus_packet in enumerate(audios):
|
2025-02-02 23:01:14 +08:00
|
|
|
await conn.websocket.send(opus_packet)
|
|
|
|
|
|
|
|
|
|
if conn.llm_finish_task and text == conn.tts_last_text:
|
2025-02-11 13:51:44 +08:00
|
|
|
stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time)
|
2025-02-02 23:01:14 +08:00
|
|
|
stop_task = asyncio.create_task(
|
2025-02-11 13:51:44 +08:00
|
|
|
schedule_with_interrupt(stop_duration, send_tts_message(conn, 'stop'))
|
2025-02-02 23:01:14 +08:00
|
|
|
)
|
|
|
|
|
conn.scheduled_tasks.append(stop_task)
|
2025-02-14 23:09:12 +08:00
|
|
|
if await isLLMWantToFinish(conn):
|
|
|
|
|
finish_task = asyncio.create_task(
|
2025-02-26 01:33:05 +08:00
|
|
|
schedule_with_interrupt(stop_duration, await conn.close())
|
2025-02-14 23:09:12 +08:00
|
|
|
)
|
|
|
|
|
conn.scheduled_tasks.append(finish_task)
|
2025-02-02 23:01:14 +08:00
|
|
|
|
2025-02-13 21:22:36 +08:00
|
|
|
|
2025-02-11 12:48:39 +08:00
|
|
|
async def send_tts_message(conn, state, text=None):
|
|
|
|
|
"""发送 TTS 状态消息"""
|
|
|
|
|
message = {
|
2025-02-02 23:01:14 +08:00
|
|
|
"type": "tts",
|
2025-02-11 12:48:39 +08:00
|
|
|
"state": state,
|
2025-02-02 23:01:14 +08:00
|
|
|
"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(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}
|
|
|
|
|
))
|
|
|
|
|
await send_tts_message(conn, "start")
|
|
|
|
|
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
|
|
|
async def schedule_with_interrupt(delay, coro):
|
|
|
|
|
"""可中断的延迟调度"""
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.sleep(delay)
|
|
|
|
|
await coro
|
|
|
|
|
except asyncio.CancelledError:
|
2025-02-15 11:48:02 +08:00
|
|
|
pass
|