mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
* Update util.py解决返回元组报错 (#103) 解决返回元组报错 * 增加播放本地音乐功能 (#105) * Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. On branch Music-playback Your branch is up to date with 'origin/Music-playback'. Changes to be committed: modified: core/handle/audioHandle.py new file: "music/\346\234\210\344\272\256\344\273\243\350\241\250\346\210\221\347\232\204\345\277\203_\351\202\223\344\270\275\345\220\233.mp3" new file: "music/\350\270\217\345\261\261\346\262\263.mp3" * Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. On branch Music-playback Your branch is up to date with 'origin/Music-playback'. Changes to be committed: modified: config.yaml modified: core/connection.py modified: core/handle/abortHandle.py modified: core/handle/audioHandle.py new file: core/handle/musicHandler.py modified: core/handle/textHandle.py modified: core/websocket_server.py new file: "music/\344\270\200\345\277\265\345\215\203\345\271\264_\345\233\275\351\243\216\347\211\210.mp3" new file: "music/\344\270\255\347\247\213\346\234\210.mp3" deleted: "music/\346\234\210\344\272\256\344\273\243\350\241\250\346\210\221\347\232\204\345\277\203_\351\202\223\344\270\275\345\220\233.mp3" deleted: "music/\350\270\217\345\261\261\346\262\263.mp3" * 增加播放本地音乐功能 * 增加播放本地音乐功能 * 让音乐配置变的优雅 On branch Music-playback Your branch is up to date with 'origin/Music-playback'. Changes to be committed: modified: config.yaml modified: core/handle/musicHandler.py renamed: "music/\344\270\200\345\277\265\345\215\203\345\271\264_\345\233\275\351\243\216\347\211\210.mp3" -> "music/mp3/\344\270\200\345\277\265\345\215\203\345\271\264_\345\233\275\351\243\216\347\211\210.mp3" renamed: "music/\344\270\255\347\247\213\346\234\210.mp3" -> "music/mp3/\344\270\255\347\247\213\346\234\210.mp3" renamed: "music/\345\273\211\346\263\242\350\200\201\347\237\243\357\274\214\345\260\232\350\203\275\351\245\255\345\220\246.mp3" -> "music/mp3/\345\273\211\346\263\242\350\200\201\347\237\243\357\274\214\345\260\232\350\203\275\351\245\255\345\220\246.mp3" new file: music/music_config.yaml --------- Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com> * update:优化代码 * update:流控调试 * update:优化音乐播放 * update:优化音乐配置初始化 --------- Co-authored-by: linqingping <linqingping@users.noreply.github.com> Co-authored-by: Chris <119588753+Chris-websketch@users.noreply.github.com> Co-authored-by: hrz <1710360675@qq.com>
96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
from config.logger import setup_logging
|
|
import json
|
|
import asyncio
|
|
import time
|
|
from core.utils.util import remove_punctuation_and_length, get_string_no_punctuation_or_emoji
|
|
|
|
TAG = __name__
|
|
logger = setup_logging()
|
|
|
|
|
|
async def isLLMWantToFinish(conn):
|
|
first_text = conn.tts_first_text
|
|
last_text = conn.tts_last_text
|
|
_, last_text_without_punctuation = remove_punctuation_and_length(last_text)
|
|
if "再见" in last_text_without_punctuation or "拜拜" in last_text_without_punctuation:
|
|
return True
|
|
_, first_text_without_punctuation = remove_punctuation_and_length(first_text)
|
|
if "再见" in first_text_without_punctuation or "拜拜" in first_text_without_punctuation:
|
|
return True
|
|
return False
|
|
|
|
|
|
async def sendAudioMessage(conn, audios, duration, text):
|
|
base_delay = conn.tts_duration
|
|
|
|
# 发送 tts.start
|
|
if text == conn.tts_first_text:
|
|
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
|
conn.tts_start_speak_time = time.time()
|
|
|
|
# 发送 sentence_start(每个音频文件之前发送一次)
|
|
sentence_task = asyncio.create_task(
|
|
schedule_with_interrupt(base_delay, send_tts_message(conn, "sentence_start", text))
|
|
)
|
|
conn.scheduled_tasks.append(sentence_task)
|
|
|
|
conn.tts_duration += duration
|
|
|
|
# 发送音频数据
|
|
for idx, opus_packet in enumerate(audios):
|
|
await conn.websocket.send(opus_packet)
|
|
|
|
if conn.llm_finish_task and text == conn.tts_last_text:
|
|
stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time)
|
|
stop_task = asyncio.create_task(
|
|
schedule_with_interrupt(stop_duration, send_tts_message(conn, 'stop'))
|
|
)
|
|
conn.scheduled_tasks.append(stop_task)
|
|
if await isLLMWantToFinish(conn):
|
|
finish_task = asyncio.create_task(
|
|
schedule_with_interrupt(stop_duration, await conn.close())
|
|
)
|
|
conn.scheduled_tasks.append(finish_task)
|
|
|
|
|
|
async def send_tts_message(conn, state, text=None):
|
|
"""发送 TTS 状态消息"""
|
|
message = {
|
|
"type": "tts",
|
|
"state": state,
|
|
"session_id": conn.session_id
|
|
}
|
|
if text is not None:
|
|
message["text"] = text
|
|
|
|
await conn.websocket.send(json.dumps(message))
|
|
if state == "stop":
|
|
conn.clearSpeakStatus()
|
|
|
|
|
|
async def send_stt_message(conn, text):
|
|
"""发送 STT 状态消息"""
|
|
stt_text = get_string_no_punctuation_or_emoji(text)
|
|
await conn.websocket.send(json.dumps({
|
|
"type": "stt",
|
|
"text": stt_text,
|
|
"session_id": conn.session_id}
|
|
))
|
|
await conn.websocket.send(
|
|
json.dumps({
|
|
"type": "llm",
|
|
"text": "😊",
|
|
"emotion": "happy",
|
|
"session_id": conn.session_id}
|
|
))
|
|
await send_tts_message(conn, "start")
|
|
|
|
|
|
async def schedule_with_interrupt(delay, coro):
|
|
"""可中断的延迟调度"""
|
|
try:
|
|
await asyncio.sleep(delay)
|
|
await coro
|
|
except asyncio.CancelledError:
|
|
pass
|