mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat: tts first frame latency
This commit is contained in:
parent
41a06e29b0
commit
0ce527498f
4 changed files with 32 additions and 5 deletions
|
|
@ -16,14 +16,22 @@ async def sendAudioMessage(conn, sentenceType, audios, text):
|
||||||
# 记录首句 TTS 播放时间(端到端延迟的终点)
|
# 记录首句 TTS 播放时间(端到端延迟的终点)
|
||||||
first_audio_time = time.time() * 1000
|
first_audio_time = time.time() * 1000
|
||||||
|
|
||||||
|
# 计算 TTS 首包延迟(输入到输出)
|
||||||
|
tts_first_package_delay = 0
|
||||||
|
if hasattr(conn, '_latency_tts_first_text_time') and conn._latency_tts_first_text_time:
|
||||||
|
tts_first_package_delay = first_audio_time - conn._latency_tts_first_text_time
|
||||||
|
|
||||||
# 计算端到端延迟
|
# 计算端到端延迟
|
||||||
|
e2e_total_delay = 0
|
||||||
if hasattr(conn, '_latency_voice_end_time'):
|
if hasattr(conn, '_latency_voice_end_time'):
|
||||||
e2e_total_delay = first_audio_time - conn._latency_voice_end_time
|
e2e_total_delay = first_audio_time - conn._latency_voice_end_time
|
||||||
conn.logger.bind(tag=TAG).info(
|
|
||||||
f"🔊 [延迟追踪] 首句TTS开始播放 | "
|
conn.logger.bind(tag=TAG).info(
|
||||||
f"⏱️ 端到端总延迟: {e2e_total_delay:.0f}ms (用户说完→首句播放) | "
|
f"🔊 [延迟追踪] 首句TTS开始播放 | "
|
||||||
f"文本: {text if text else '(无文本)'}"
|
f"TTS首包延迟: {tts_first_package_delay:.0f}ms | "
|
||||||
)
|
f"⏱️ 端到端总延迟: {e2e_total_delay:.0f}ms (用户说完→首句播放) | "
|
||||||
|
f"文本: {text if text else '(无文本)'}"
|
||||||
|
)
|
||||||
|
|
||||||
if sentenceType == SentenceType.FIRST:
|
if sentenceType == SentenceType.FIRST:
|
||||||
await send_tts_message(conn, "sentence_start", text)
|
await send_tts_message(conn, "sentence_start", text)
|
||||||
|
|
|
||||||
|
|
@ -288,10 +288,16 @@ class TTSProviderBase(ABC):
|
||||||
self.tts_text_buff = []
|
self.tts_text_buff = []
|
||||||
self.is_first_sentence = True
|
self.is_first_sentence = True
|
||||||
self.tts_audio_first_sentence = True
|
self.tts_audio_first_sentence = True
|
||||||
|
self.conn._latency_tts_first_text_time = None # Reset TTS input time
|
||||||
elif ContentType.TEXT == message.content_type:
|
elif ContentType.TEXT == message.content_type:
|
||||||
self.tts_text_buff.append(message.content_detail)
|
self.tts_text_buff.append(message.content_detail)
|
||||||
segment_text = self._get_segment_text()
|
segment_text = self._get_segment_text()
|
||||||
if segment_text:
|
if segment_text:
|
||||||
|
# Record TTS first text input time (for latency tracking)
|
||||||
|
if not hasattr(self.conn, '_latency_tts_first_text_time') or self.conn._latency_tts_first_text_time is None:
|
||||||
|
import time
|
||||||
|
self.conn._latency_tts_first_text_time = time.time() * 1000
|
||||||
|
logger.bind(tag=TAG).debug("📝 [延迟追踪] TTS首次接收文本")
|
||||||
self.to_tts_stream(segment_text, opus_handler=self.handle_opus)
|
self.to_tts_stream(segment_text, opus_handler=self.handle_opus)
|
||||||
elif ContentType.FILE == message.content_type:
|
elif ContentType.FILE == message.content_type:
|
||||||
self._process_remaining_text_stream(opus_handler=self.handle_opus)
|
self._process_remaining_text_stream(opus_handler=self.handle_opus)
|
||||||
|
|
|
||||||
|
|
@ -161,6 +161,7 @@ class TTSProvider(TTSProviderBase):
|
||||||
self.tts_audio_first_sentence = True
|
self.tts_audio_first_sentence = True
|
||||||
self._first_sent = False
|
self._first_sent = False
|
||||||
self._session_text_buffer = []
|
self._session_text_buffer = []
|
||||||
|
self.conn._latency_tts_first_text_time = None # Reset TTS input time
|
||||||
|
|
||||||
elif self.conn.client_abort:
|
elif self.conn.client_abort:
|
||||||
# ========== Interruption ==========
|
# ========== Interruption ==========
|
||||||
|
|
@ -195,6 +196,12 @@ class TTSProvider(TTSProviderBase):
|
||||||
# Accumulate text for reporting (supports voice_opening/closing and LLM content)
|
# Accumulate text for reporting (supports voice_opening/closing and LLM content)
|
||||||
self._session_text_buffer.append(message.content_detail)
|
self._session_text_buffer.append(message.content_detail)
|
||||||
|
|
||||||
|
# Record TTS first text input time (for latency tracking)
|
||||||
|
if not hasattr(self.conn, '_latency_tts_first_text_time') or self.conn._latency_tts_first_text_time is None:
|
||||||
|
import time
|
||||||
|
self.conn._latency_tts_first_text_time = time.time() * 1000
|
||||||
|
logger.bind(tag=TAG).debug("📝 [延迟追踪] TTS首次接收文本")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.bind(tag=TAG).debug(
|
logger.bind(tag=TAG).debug(
|
||||||
f"Sending TTS text: {message.content_detail}"
|
f"Sending TTS text: {message.content_detail}"
|
||||||
|
|
|
||||||
|
|
@ -97,10 +97,16 @@ class TTSProvider(TTSProviderBase):
|
||||||
self.tts_audio_first_sentence = True
|
self.tts_audio_first_sentence = True
|
||||||
self._first_chunk_logged = False
|
self._first_chunk_logged = False
|
||||||
self.before_stop_play_files.clear()
|
self.before_stop_play_files.clear()
|
||||||
|
self.conn._latency_tts_first_text_time = None # Reset TTS input time
|
||||||
elif ContentType.TEXT == message.content_type:
|
elif ContentType.TEXT == message.content_type:
|
||||||
self.tts_text_buff.append(message.content_detail)
|
self.tts_text_buff.append(message.content_detail)
|
||||||
segment_text = self._get_segment_text()
|
segment_text = self._get_segment_text()
|
||||||
if segment_text:
|
if segment_text:
|
||||||
|
# Record TTS first text input time (for latency tracking)
|
||||||
|
if not hasattr(self.conn, '_latency_tts_first_text_time') or self.conn._latency_tts_first_text_time is None:
|
||||||
|
import time
|
||||||
|
self.conn._latency_tts_first_text_time = time.time() * 1000
|
||||||
|
logger.bind(tag=TAG).debug("📝 [延迟追踪] TTS首次接收文本")
|
||||||
self.to_tts_single_stream(segment_text)
|
self.to_tts_single_stream(segment_text)
|
||||||
|
|
||||||
elif ContentType.FILE == message.content_type:
|
elif ContentType.FILE == message.content_type:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue