Files
xiaozhi-esp32-server/main/xiaozhi-server/core/handle/ttsReportHandle.py
T

60 lines
1.6 KiB
Python
Raw Normal View History

2025-04-30 17:29:27 +08:00
"""
TTS上报功能已集成到ConnectionHandler类中。
上报功能包括:
1. 每个连接对象拥有自己的上报队列和处理线程
2. 上报线程的生命周期与连接对象绑定
3. 使用ConnectionHandler.enqueue_tts_report方法进行上报
具体实现请参考core/connection.py中的相关代码。
"""
from config.logger import setup_logging
from config.manage_api_client import report
TAG = __name__
logger = setup_logging()
2025-05-02 00:07:36 +08:00
def report_tts(conn, type, text, opus_data):
2025-04-30 17:29:27 +08:00
"""执行TTS上报操作
2025-05-02 00:07:36 +08:00
2025-04-30 17:29:27 +08:00
Args:
conn: 连接对象
2025-05-02 00:07:36 +08:00
type: 上报类型,1为用户,2为智能体
2025-04-30 17:29:27 +08:00
text: 合成文本
2025-05-02 00:07:36 +08:00
opus_data: opus音频数据
2025-04-30 17:29:27 +08:00
"""
try:
# 执行上报
2025-05-02 00:07:36 +08:00
report(
2025-04-30 17:29:27 +08:00
mac_address=conn.device_id,
session_id=conn.session_id,
2025-05-02 00:07:36 +08:00
chat_type=type,
2025-04-30 17:29:27 +08:00
content=text,
2025-05-02 00:07:36 +08:00
opus_data=opus_data,
2025-04-30 17:29:27 +08:00
)
except Exception as e:
logger.bind(tag=TAG).error(f"TTS上报失败: {e}")
2025-05-02 00:07:36 +08:00
def enqueue_tts_report(conn, type, text, opus_data):
if not conn.read_config_from_api:
return
2025-04-30 17:29:27 +08:00
"""将TTS数据加入上报队列
2025-05-02 00:07:36 +08:00
2025-04-30 17:29:27 +08:00
Args:
conn: 连接对象
text: 合成文本
2025-05-02 00:07:36 +08:00
opus_data: opus音频数据
2025-04-30 17:29:27 +08:00
"""
try:
# 使用连接对象的队列,传入文本和二进制数据而非文件路径
2025-05-02 00:07:36 +08:00
conn.tts_report_queue.put((type, text, opus_data))
logger.bind(tag=TAG).info(
f"TTS数据已加入上报队列: {conn.device_id}, 音频大小: {len(opus_data)} "
)
2025-04-30 17:29:27 +08:00
except Exception as e:
2025-05-02 00:07:36 +08:00
logger.bind(tag=TAG).error(f"加入TTS上报队列失败: {text}, {e}")