mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
fix: 修复选用seed-tts-2.0文本缺失
fix: 设备端说话状态与服务端同步
This commit is contained in:
@@ -21,7 +21,6 @@ async def sendAudioMessage(conn: "ConnectionHandler", sentenceType, audios, text
|
|||||||
if conn.tts.tts_audio_first_sentence:
|
if conn.tts.tts_audio_first_sentence:
|
||||||
conn.logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
conn.logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
||||||
conn.tts.tts_audio_first_sentence = False
|
conn.tts.tts_audio_first_sentence = False
|
||||||
await send_tts_message(conn, "start", None)
|
|
||||||
|
|
||||||
if sentenceType == SentenceType.FIRST:
|
if sentenceType == SentenceType.FIRST:
|
||||||
# 同一句子的后续消息加入流控队列,其他情况立即发送
|
# 同一句子的后续消息加入流控队列,其他情况立即发送
|
||||||
@@ -204,7 +203,6 @@ def _start_background_sender(conn: "ConnectionHandler", rate_controller, flow_co
|
|||||||
|
|
||||||
conn.last_activity_time = time.time() * 1000
|
conn.last_activity_time = time.time() * 1000
|
||||||
await _do_send_audio(conn, packet, flow_control)
|
await _do_send_audio(conn, packet, flow_control)
|
||||||
conn.client_is_speaking = True
|
|
||||||
|
|
||||||
# 使用 start_sending 启动后台循环
|
# 使用 start_sending 启动后台循环
|
||||||
rate_controller.start_sending(send_callback)
|
rate_controller.start_sending(send_callback)
|
||||||
@@ -232,12 +230,10 @@ async def _send_audio_with_rate_control(
|
|||||||
# 预缓冲:前N个包直接发送
|
# 预缓冲:前N个包直接发送
|
||||||
if flow_control["packet_count"] < PRE_BUFFER_COUNT:
|
if flow_control["packet_count"] < PRE_BUFFER_COUNT:
|
||||||
await _do_send_audio(conn, packet, flow_control)
|
await _do_send_audio(conn, packet, flow_control)
|
||||||
conn.client_is_speaking = True
|
|
||||||
elif send_delay > 0:
|
elif send_delay > 0:
|
||||||
# 固定延迟模式
|
# 固定延迟模式
|
||||||
await asyncio.sleep(send_delay)
|
await asyncio.sleep(send_delay)
|
||||||
await _do_send_audio(conn, packet, flow_control)
|
await _do_send_audio(conn, packet, flow_control)
|
||||||
conn.client_is_speaking = True
|
|
||||||
else:
|
else:
|
||||||
# 动态流控模式:仅添加到队列,由后台循环负责发送
|
# 动态流控模式:仅添加到队列,由后台循环负责发送
|
||||||
rate_controller.add_audio(packet)
|
rate_controller.add_audio(packet)
|
||||||
@@ -318,3 +314,5 @@ async def send_stt_message(conn: "ConnectionHandler", text):
|
|||||||
json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id})
|
json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id})
|
||||||
)
|
)
|
||||||
await send_tts_message(conn, "start")
|
await send_tts_message(conn, "start")
|
||||||
|
# 发送start消息后客户端状态会处于说话中状态,同步服务端状态
|
||||||
|
conn.client_is_speaking = True
|
||||||
@@ -148,6 +148,7 @@ class TTSProvider(TTSProviderBase):
|
|||||||
self.access_token = config.get("access_token")
|
self.access_token = config.get("access_token")
|
||||||
self.cluster = config.get("cluster")
|
self.cluster = config.get("cluster")
|
||||||
self.resource_id = config.get("resource_id")
|
self.resource_id = config.get("resource_id")
|
||||||
|
self.resource_type = True if self.resource_id == "seed-tts-2.0" else False
|
||||||
self.activate_session = False
|
self.activate_session = False
|
||||||
if config.get("private_voice"):
|
if config.get("private_voice"):
|
||||||
self.voice = config.get("private_voice")
|
self.voice = config.get("private_voice")
|
||||||
@@ -511,7 +512,7 @@ class TTSProvider(TTSProviderBase):
|
|||||||
if res.optional.event == EVENT_SessionCanceled:
|
if res.optional.event == EVENT_SessionCanceled:
|
||||||
logger.bind(tag=TAG).debug(f"释放服务端资源成功~~")
|
logger.bind(tag=TAG).debug(f"释放服务端资源成功~~")
|
||||||
self.activate_session = False
|
self.activate_session = False
|
||||||
elif res.optional.event == EVENT_TTSSentenceStart:
|
elif not self.resource_type and res.optional.event == EVENT_TTSSentenceStart:
|
||||||
json_data = json.loads(res.payload.decode("utf-8"))
|
json_data = json.loads(res.payload.decode("utf-8"))
|
||||||
self.tts_text = json_data.get("text", "")
|
self.tts_text = json_data.get("text", "")
|
||||||
logger.bind(tag=TAG).debug(f"句子语音生成开始: {self.tts_text}")
|
logger.bind(tag=TAG).debug(f"句子语音生成开始: {self.tts_text}")
|
||||||
@@ -522,8 +523,17 @@ class TTSProvider(TTSProviderBase):
|
|||||||
res.optional.event == EVENT_TTSResponse
|
res.optional.event == EVENT_TTSResponse
|
||||||
and res.header.message_type == AUDIO_ONLY_RESPONSE
|
and res.header.message_type == AUDIO_ONLY_RESPONSE
|
||||||
):
|
):
|
||||||
|
# 处理seed-tts-2.0文本字幕
|
||||||
|
if self.resource_type and self.conn.tts_MessageText:
|
||||||
|
logger.bind(tag=TAG).info(
|
||||||
|
f"句子语音生成成功: {self.conn.tts_MessageText}"
|
||||||
|
)
|
||||||
|
self.tts_audio_queue.put(
|
||||||
|
(SentenceType.FIRST, [], self.conn.tts_MessageText)
|
||||||
|
)
|
||||||
|
self.conn.tts_MessageText = None
|
||||||
self.wav_to_opus_data_audio_raw_stream(res.payload, callback=self.handle_opus)
|
self.wav_to_opus_data_audio_raw_stream(res.payload, callback=self.handle_opus)
|
||||||
elif res.optional.event == EVENT_TTSSentenceEnd:
|
elif not self.resource_type and res.optional.event == EVENT_TTSSentenceEnd:
|
||||||
logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}")
|
logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}")
|
||||||
elif res.optional.event == EVENT_SessionFinished:
|
elif res.optional.event == EVENT_SessionFinished:
|
||||||
logger.bind(tag=TAG).debug(f"会话结束~~")
|
logger.bind(tag=TAG).debug(f"会话结束~~")
|
||||||
|
|||||||
Reference in New Issue
Block a user