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

260 lines
9.4 KiB
Python
Raw Normal View History

2025-02-02 23:01:14 +08:00
import json
import time
import asyncio
from core.utils import textUtils
2025-09-02 14:34:26 +08:00
from core.utils.util import audio_to_data
from core.providers.tts.dto.dto import SentenceType
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-05-26 02:20:38 +08:00
async def sendAudioMessage(conn, sentenceType, audios, text):
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
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-08-15 16:33:46 +08:00
await sendAudio(conn, audios)
# 发送句子开始消息
if sentenceType is not SentenceType.MIDDLE:
conn.logger.bind(tag=TAG).info(f"发送音频消息: {sentenceType}, {text}")
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()
2025-09-10 22:20:43 +08:00
def calculate_timestamp_and_sequence(conn, start_time, packet_index, frame_duration=60):
"""
计算音频数据包的时间戳和序列号
Args:
conn: 连接对象
start_time: 起始时间(性能计数器值)
packet_index: 数据包索引
frame_duration: 帧时长(毫秒),匹配 Opus 编码
Returns:
tuple: (timestamp, sequence)
"""
# 计算时间戳(使用播放位置计算)
timestamp = int((start_time + packet_index * frame_duration / 1000) * 1000) % (
2**32
)
# 计算序列号
if hasattr(conn, "audio_flow_control"):
sequence = conn.audio_flow_control["sequence"]
else:
sequence = packet_index # 如果没有流控状态,直接使用索引
return timestamp, sequence
2025-09-10 21:40:25 +08:00
async def _send_to_mqtt_gateway(conn, opus_packet, timestamp, sequence):
"""
发送带16字节头部的opus数据包给mqtt_gateway
Args:
conn: 连接对象
opus_packet: opus数据包
timestamp: 时间戳
sequence: 序列号
"""
# 为opus数据包添加16字节头部
header = bytearray(16)
header[0] = 1 # type
header[2:4] = len(opus_packet).to_bytes(2, "big") # payload length
header[4:8] = sequence.to_bytes(4, "big") # sequence
header[8:12] = timestamp.to_bytes(4, "big") # 时间戳
header[12:16] = len(opus_packet).to_bytes(4, "big") # opus长度
# 发送包含头部的完整数据包
complete_packet = bytes(header) + opus_packet
await conn.websocket.send(complete_packet)
2025-05-24 12:11:13 +08:00
# 播放音频
async def sendAudio(conn, audios, frame_duration=60):
"""
发送单个opus包,支持流控
Args:
conn: 连接对象
opus_packet: 单个opus数据包
pre_buffer: 快速发送音频
2025-09-02 11:18:04 +08:00
frame_duration: 帧时长(毫秒),匹配 Opus 编码
"""
if audios is None or len(audios) == 0:
2025-05-26 02:20:38 +08:00
return
2025-09-09 09:37:31 +08:00
# 获取发送延迟配置
send_delay = conn.config.get("tts_audio_send_delay", -1) / 1000.0
2025-08-15 16:33:46 +08:00
if isinstance(audios, bytes):
2025-11-01 17:59:16 +08:00
# 重置流控状态,第一次读取和会话发生转变时
if not hasattr(conn, "audio_flow_control") or conn.audio_flow_control.get("sentence_id") != conn.sentence_id:
conn.audio_flow_control = {
"last_send_time": 0,
"packet_count": 0,
"start_time": time.perf_counter(),
2025-09-08 11:04:42 +08:00
"sequence": 0, # 添加序列号
2025-11-01 17:59:16 +08:00
"sentence_id": conn.sentence_id,
}
2025-11-01 17:59:16 +08:00
if conn.client_abort:
return
conn.last_activity_time = time.time() * 1000
# 预缓冲:前5个包直接发送,不做延迟
pre_buffer_count = 5
flow_control = conn.audio_flow_control
current_time = time.perf_counter()
2025-11-01 17:59:16 +08:00
if flow_control["packet_count"] < pre_buffer_count:
# 预缓冲阶段,直接发送不延迟
pass
elif send_delay > 0:
# 使用固定延迟
await asyncio.sleep(send_delay)
else:
2025-11-01 17:59:16 +08:00
effective_packet = flow_control["packet_count"] - pre_buffer_count
expected_time = flow_control["start_time"] + (
2025-11-01 17:59:16 +08:00
effective_packet * frame_duration / 1000
)
delay = expected_time - current_time
if delay > 0:
await asyncio.sleep(delay)
else:
# 纠正误差
flow_control["start_time"] += abs(delay)
2025-09-10 21:40:25 +08:00
if conn.conn_from_mqtt_gateway:
2025-09-10 22:20:43 +08:00
# 计算时间戳和序列号
timestamp, sequence = calculate_timestamp_and_sequence(
conn,
flow_control["start_time"],
flow_control["packet_count"],
frame_duration,
2025-09-10 21:40:25 +08:00
)
2025-09-10 22:20:43 +08:00
# 调用通用函数发送带头部的数据包
await _send_to_mqtt_gateway(conn, audios, timestamp, sequence)
2025-09-09 09:37:31 +08:00
else:
# 直接发送opus数据包,不添加头部
await conn.websocket.send(audios)
2025-02-02 23:01:14 +08:00
# 更新流控状态
flow_control["packet_count"] += 1
2025-09-08 11:04:42 +08:00
flow_control["sequence"] += 1
flow_control["last_send_time"] = time.perf_counter()
2025-09-02 11:18:04 +08:00
else:
2025-09-02 17:08:56 +08:00
# 文件型音频走普通播放
2025-09-02 11:18:04 +08:00
start_time = time.perf_counter()
play_position = 0
# 执行预缓冲
2025-11-01 17:59:16 +08:00
pre_buffer_frames = min(5, len(audios))
2025-09-02 11:18:04 +08:00
for i in range(pre_buffer_frames):
2025-09-10 21:40:25 +08:00
if conn.conn_from_mqtt_gateway:
2025-09-10 22:20:43 +08:00
# 计算时间戳和序列号
timestamp, sequence = calculate_timestamp_and_sequence(
conn, start_time, i, frame_duration
2025-09-10 21:40:25 +08:00
)
# 调用通用函数发送带头部的数据包
2025-09-10 22:20:43 +08:00
await _send_to_mqtt_gateway(conn, audios[i], timestamp, sequence)
2025-09-09 09:37:31 +08:00
else:
# 直接发送预缓冲包,不添加头部
await conn.websocket.send(audios[i])
2025-09-02 11:18:04 +08:00
remaining_audios = audios[pre_buffer_frames:]
2025-09-10 21:40:25 +08:00
2025-09-02 11:18:04 +08:00
# 播放剩余音频帧
2025-09-08 11:04:42 +08:00
for i, opus_packet in enumerate(remaining_audios):
2025-09-02 11:18:04 +08:00
if conn.client_abort:
break
# 重置没有声音的状态
conn.last_activity_time = time.time() * 1000
if send_delay > 0:
# 固定延迟模式
await asyncio.sleep(send_delay)
else:
# 计算预期发送时间
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-09-10 21:40:25 +08:00
if conn.conn_from_mqtt_gateway:
2025-09-10 22:20:43 +08:00
# 计算时间戳和序列号(使用当前的数据包索引确保连续性)
packet_index = pre_buffer_frames + i
timestamp, sequence = calculate_timestamp_and_sequence(
conn, start_time, packet_index, frame_duration
)
2025-09-10 21:40:25 +08:00
# 调用通用函数发送带头部的数据包
await _send_to_mqtt_gateway(conn, opus_packet, timestamp, sequence)
2025-09-09 09:37:31 +08:00
else:
# 直接发送opus数据包,不添加头部
await conn.websocket.send(opus_packet)
2025-09-10 21:40:25 +08:00
2025-09-02 11:18:04 +08:00
play_position += frame_duration
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}
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播放结束
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"
)
2025-09-02 14:34:26 +08:00
audios = audio_to_data(stop_tts_notify_voice, is_opus=True)
await sendAudio(conn, audios)
2025-05-24 12:11:13 +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-11 13:51:44 +08:00
async def send_stt_message(conn, text):
2025-08-15 16:33:46 +08:00
"""发送 STT 状态消息"""
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-07-09 14:23:47 +08:00
# 解析JSON格式,提取实际的用户说话内容
display_text = text
try:
# 尝试解析JSON格式
if text.strip().startswith("{") and text.strip().endswith("}"):
2025-07-09 14:23:47 +08:00
parsed_data = json.loads(text)
if isinstance(parsed_data, dict) and "content" in parsed_data:
# 如果是包含说话人信息的JSON格式,只显示content部分
display_text = parsed_data["content"]
# 保存说话人信息到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
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")