mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
第一句语音index处理,歌曲播放index处理
This commit is contained in:
parent
5abeee16eb
commit
65f8279f0b
3 changed files with 16 additions and 10 deletions
|
|
@ -71,7 +71,9 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# tts相关变量
|
# tts相关变量
|
||||||
self.tts_first_text = None
|
self.tts_first_text = None
|
||||||
|
self.tts_first_text_index = -1
|
||||||
self.tts_last_text = None
|
self.tts_last_text = None
|
||||||
|
self.tts_last_text_index = -1
|
||||||
self.tts_start_speak_time = None
|
self.tts_start_speak_time = None
|
||||||
self.tts_duration = 0
|
self.tts_duration = 0
|
||||||
|
|
||||||
|
|
@ -328,22 +330,22 @@ class ConnectionHandler:
|
||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
text = None
|
text = None
|
||||||
try:
|
try:
|
||||||
opus_datas, text, text_id = self.audio_play_queue.get()
|
opus_datas, text, text_index = self.audio_play_queue.get()
|
||||||
future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_id), self.loop)
|
future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index), self.loop)
|
||||||
future.result()
|
future.result()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"audio_play_priority priority_thread: {text} {e}")
|
self.logger.bind(tag=TAG).error(f"audio_play_priority priority_thread: {text} {e}")
|
||||||
|
|
||||||
def speak_and_play(self, text, text_id=0):
|
def speak_and_play(self, text, text_index=0):
|
||||||
if text is None or len(text) <= 0:
|
if text is None or len(text) <= 0:
|
||||||
self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}")
|
self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}")
|
||||||
return None, text, text_id
|
return None, text, text_index
|
||||||
tts_file = self.tts.to_tts(text)
|
tts_file = self.tts.to_tts(text)
|
||||||
if tts_file is None:
|
if tts_file is None:
|
||||||
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
|
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
|
||||||
return None, text, text_id
|
return None, text, text_index
|
||||||
self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}")
|
self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}")
|
||||||
return tts_file, text, text_id
|
return tts_file, text, text_index
|
||||||
|
|
||||||
def clearSpeakStatus(self):
|
def clearSpeakStatus(self):
|
||||||
self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态")
|
self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态")
|
||||||
|
|
@ -351,13 +353,15 @@ class ConnectionHandler:
|
||||||
self.tts_last_text = None
|
self.tts_last_text = None
|
||||||
self.tts_last_text_index = -1
|
self.tts_last_text_index = -1
|
||||||
self.tts_first_text = None
|
self.tts_first_text = None
|
||||||
|
self.tts_first_text_index = -1
|
||||||
self.tts_duration = 0
|
self.tts_duration = 0
|
||||||
self.tts_start_speak_time = None
|
self.tts_start_speak_time = None
|
||||||
|
|
||||||
def recode_first_last_text(self, text, text_index=0):
|
def recode_first_last_text(self, text, text_index=0):
|
||||||
if not self.tts_first_text:
|
if self.tts_first_text_index == -1:
|
||||||
self.logger.bind(tag=TAG).info(f"大模型说出第一句话: {text}")
|
self.logger.bind(tag=TAG).info(f"大模型说出第一句话: {text}")
|
||||||
self.tts_first_text = text
|
self.tts_first_text = text
|
||||||
|
self.tts_first_text_index = text_index
|
||||||
self.tts_last_text = text
|
self.tts_last_text = text
|
||||||
self.tts_last_text_index = text_index
|
self.tts_last_text_index = text_index
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,9 @@ class MusicHandler:
|
||||||
text = f"正在播放{selected_music}"
|
text = f"正在播放{selected_music}"
|
||||||
await send_stt_message(conn, text)
|
await send_stt_message(conn, text)
|
||||||
conn.tts_first_text = selected_music
|
conn.tts_first_text = selected_music
|
||||||
|
conn.tts_first_text_index = 0
|
||||||
conn.tts_last_text = selected_music
|
conn.tts_last_text = selected_music
|
||||||
|
conn.tts_last_text_index = 0
|
||||||
conn.llm_finish_task = True
|
conn.llm_finish_task = True
|
||||||
if music_path.endswith(".p3"):
|
if music_path.endswith(".p3"):
|
||||||
opus_packets, duration = p3.decode_opus_from_file(music_path)
|
opus_packets, duration = p3.decode_opus_from_file(music_path)
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ async def isLLMWantToFinish(last_text):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def sendAudioMessage(conn, audios, text, text_id=0):
|
async def sendAudioMessage(conn, audios, text, text_index=0):
|
||||||
# 发送句子开始消息
|
# 发送句子开始消息
|
||||||
if text == conn.tts_first_text:
|
if text_index == conn.tts_first_text_index:
|
||||||
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
||||||
conn.tts_start_speak_time = time.perf_counter()
|
conn.tts_start_speak_time = time.perf_counter()
|
||||||
await send_tts_message(conn, "sentence_start", text)
|
await send_tts_message(conn, "sentence_start", text)
|
||||||
|
|
@ -45,7 +45,7 @@ async def sendAudioMessage(conn, audios, text, text_id=0):
|
||||||
play_position += frame_duration # 更新播放位置
|
play_position += frame_duration # 更新播放位置
|
||||||
await send_tts_message(conn, "sentence_end", text)
|
await send_tts_message(conn, "sentence_end", text)
|
||||||
# 发送结束消息(如果是最后一个文本)
|
# 发送结束消息(如果是最后一个文本)
|
||||||
if conn.llm_finish_task and text_id == conn.tts_last_text_index:
|
if conn.llm_finish_task and text_index == conn.tts_last_text_index:
|
||||||
await send_tts_message(conn, 'stop', None)
|
await send_tts_message(conn, 'stop', None)
|
||||||
if await isLLMWantToFinish(text):
|
if await isLLMWantToFinish(text):
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue