mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
fix: 流式流畅度问题,英文符号切分,空格切分
This commit is contained in:
@@ -257,7 +257,7 @@ class ConnectionHandler:
|
|||||||
current_text = full_text[processed_chars:] # 从未处理的位置开始
|
current_text = full_text[processed_chars:] # 从未处理的位置开始
|
||||||
|
|
||||||
# 查找最后一个有效标点
|
# 查找最后一个有效标点
|
||||||
punctuations = ("。", "?", "!", ";", ":")
|
punctuations = ("。", "?", "!", ";", ":", ".", "?", "!", ";", ":"," ")
|
||||||
last_punct_pos = -1
|
last_punct_pos = -1
|
||||||
for punct in punctuations:
|
for punct in punctuations:
|
||||||
pos = current_text.rfind(punct)
|
pos = current_text.rfind(punct)
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ from core.utils.util import remove_punctuation_and_length, get_string_no_punctua
|
|||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
async def sendAudioMessageStream(conn, audios_queue, text, text_index=0):
|
|
||||||
|
async def sendAudioMessageStream(conn, audios_queue, text, text_index=0, llm_finish_task=False):
|
||||||
# 发送句子开始消息
|
# 发送句子开始消息
|
||||||
if text_index == conn.tts_first_text_index:
|
if text_index == conn.tts_first_text_index:
|
||||||
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
||||||
@@ -18,8 +19,10 @@ async def sendAudioMessageStream(conn, audios_queue, text, text_index=0):
|
|||||||
# 初始化流控参数
|
# 初始化流控参数
|
||||||
frame_duration = 60 # 毫秒
|
frame_duration = 60 # 毫秒
|
||||||
start_time = time.time() # 使用高精度计时器
|
start_time = time.time() # 使用高精度计时器
|
||||||
|
# 初始化流控参数
|
||||||
|
frame_duration = 60 # 毫秒
|
||||||
|
start_time_chunk = time.perf_counter() # 使用高精度计时器
|
||||||
play_position = 0 # 已播放的时长(毫秒)
|
play_position = 0 # 已播放的时长(毫秒)
|
||||||
time_out_stop = False
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
start_get_queue = time.time()
|
start_get_queue = time.time()
|
||||||
@@ -45,13 +48,21 @@ async def sendAudioMessageStream(conn, audios_queue, text, text_index=0):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if audio_opus_datas:
|
if audio_opus_datas:
|
||||||
queue_duration = time.time() - start_get_queue
|
|
||||||
last_duration = conn.tts_duration - queue_duration
|
|
||||||
if last_duration <= 0:
|
|
||||||
last_duration = 0
|
|
||||||
conn.tts_duration = duration + last_duration
|
|
||||||
for opus_packet in audio_opus_datas:
|
for opus_packet in audio_opus_datas:
|
||||||
|
if conn.client_abort:
|
||||||
|
return
|
||||||
|
# 计算当前包的预期发送时间
|
||||||
|
# 计算当前包的预期发送时间
|
||||||
|
expected_time = start_time_chunk + (play_position / 1000)
|
||||||
|
current_time = time.perf_counter()
|
||||||
|
|
||||||
|
# 等待直到预期时间
|
||||||
|
delay = expected_time - current_time
|
||||||
|
if delay > 0:
|
||||||
|
await asyncio.sleep(delay)
|
||||||
|
logger.bind(tag=TAG).info(f'发送数据长度:{len(opus_packet)}')
|
||||||
await conn.websocket.send(opus_packet)
|
await conn.websocket.send(opus_packet)
|
||||||
|
play_position += frame_duration # 更新播放位置
|
||||||
start_time = time.time() # 更新获取数据的时间
|
start_time = time.time() # 更新获取数据的时间
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"发生错误: {e}")
|
logger.bind(tag=TAG).error(f"发生错误: {e}")
|
||||||
@@ -60,11 +71,16 @@ async def sendAudioMessageStream(conn, audios_queue, text, text_index=0):
|
|||||||
|
|
||||||
print(f'{text_index}-{conn.tts_last_text_index}')
|
print(f'{text_index}-{conn.tts_last_text_index}')
|
||||||
# 发送结束消息(如果是最后一个文本)
|
# 发送结束消息(如果是最后一个文本)
|
||||||
|
logger.bind(tag=TAG).info(f"{conn.llm_finish_task},{text_index},{conn.tts_last_text_index}")
|
||||||
if conn.llm_finish_task and text_index == conn.tts_last_text_index:
|
if conn.llm_finish_task and text_index == conn.tts_last_text_index:
|
||||||
if conn.tts_duration and conn.tts_duration > 0:
|
expected_time = start_time_chunk + (play_position / 1000)
|
||||||
await asyncio.sleep(conn.tts_duration)
|
current_time = time.perf_counter()
|
||||||
|
# 等待直到预期时间
|
||||||
|
delay = expected_time - current_time
|
||||||
|
if delay > 0:
|
||||||
|
await asyncio.sleep(delay)
|
||||||
await send_tts_message(conn, 'stop', None)
|
await send_tts_message(conn, 'stop', None)
|
||||||
if await isLLMWantToFinish(text):
|
if conn.close_after_chat or "拜拜" in text or "再见" in text:
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -230,7 +230,7 @@ class TTSProvider(TTSProviderBase):
|
|||||||
if (len(audio_raw) % 1920) > 0:
|
if (len(audio_raw) % 1920) > 0:
|
||||||
duration += 60
|
duration += 60
|
||||||
duration = duration / 1000.0
|
duration = duration / 1000.0
|
||||||
logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}')
|
# logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}')
|
||||||
opus_datas = self.wav_to_opus_data_audio_raw(audio_raw)
|
opus_datas = self.wav_to_opus_data_audio_raw(audio_raw)
|
||||||
queue.put({
|
queue.put({
|
||||||
"data": opus_datas,
|
"data": opus_datas,
|
||||||
@@ -248,7 +248,7 @@ class TTSProvider(TTSProviderBase):
|
|||||||
duration += 60
|
duration += 60
|
||||||
duration = duration / 1000.0
|
duration = duration / 1000.0
|
||||||
# 把 audio 转成 opus
|
# 把 audio 转成 opus
|
||||||
logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}')
|
# logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}')
|
||||||
opus_datas = self.wav_to_opus_data_audio_raw(audio_raw)
|
opus_datas = self.wav_to_opus_data_audio_raw(audio_raw)
|
||||||
queue.put({
|
queue.put({
|
||||||
"data": opus_datas,
|
"data": opus_datas,
|
||||||
|
|||||||
Reference in New Issue
Block a user