mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 16:43:55 +08:00
Music play (#142)
* 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>
This commit is contained in:
co-authored by
linqingping
Chris
hrz
parent
73803f49fc
commit
87cb0b484d
+20
-13
@@ -15,15 +15,18 @@ from core.utils.dialogue import Message, Dialogue
|
||||
from core.handle.textHandle import handleTextMessage
|
||||
from core.utils.util import get_string_no_punctuation_or_emoji
|
||||
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
||||
from core.handle.audioHandle import handleAudioMessage, sendAudioMessage
|
||||
from core.handle.sendAudioHandle import sendAudioMessage
|
||||
from core.handle.receiveAudioHandle import handleAudioMessage
|
||||
from config.private_config import PrivateConfig
|
||||
from core.auth import AuthMiddleware, AuthenticationError
|
||||
from core.utils.auth_code_gen import AuthCodeGenerator # 添加导入
|
||||
from core.utils.auth_code_gen import AuthCodeGenerator
|
||||
|
||||
|
||||
TAG = __name__
|
||||
|
||||
|
||||
class ConnectionHandler:
|
||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts):
|
||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _music):
|
||||
self.config = config
|
||||
self.logger = setup_logging()
|
||||
self.auth = AuthMiddleware(config)
|
||||
@@ -81,23 +84,25 @@ class ConnectionHandler:
|
||||
for cmd in self.cmd_exit:
|
||||
if len(cmd) > self.max_cmd_length:
|
||||
self.max_cmd_length = len(cmd)
|
||||
|
||||
|
||||
self.private_config = None
|
||||
self.auth_code_gen = AuthCodeGenerator.get_instance()
|
||||
self.is_device_verified = False # 添加设备验证状态标志
|
||||
|
||||
self.music_handler = _music
|
||||
|
||||
async def handle_connection(self, ws):
|
||||
try:
|
||||
# 获取并验证headers
|
||||
self.headers = dict(ws.request.headers)
|
||||
self.logger.bind(tag=TAG).info(f"New connection request - Headers: {self.headers}")
|
||||
# 获取客户端ip地址
|
||||
client_ip = ws.remote_address[0]
|
||||
self.logger.bind(tag=TAG).info(f"{client_ip} conn - Headers: {self.headers}")
|
||||
|
||||
# 进行认证
|
||||
await self.auth.authenticate(self.headers)
|
||||
|
||||
device_id = self.headers.get("device-id", None)
|
||||
|
||||
|
||||
# Load private configuration if device_id is provided
|
||||
bUsePrivateConfig = self.config.get("use_private_config", False)
|
||||
self.logger.bind(tag=TAG).info(f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}")
|
||||
@@ -108,10 +113,10 @@ class ConnectionHandler:
|
||||
# 判断是否已经绑定
|
||||
owner = self.private_config.get_owner()
|
||||
self.is_device_verified = owner is not None
|
||||
|
||||
|
||||
if self.is_device_verified:
|
||||
await self.private_config.update_last_chat_time()
|
||||
|
||||
await self.private_config.update_last_chat_time()
|
||||
|
||||
llm, tts = self.private_config.create_private_instances()
|
||||
if all([llm, tts]):
|
||||
self.llm = llm
|
||||
@@ -171,7 +176,7 @@ class ConnectionHandler:
|
||||
date_time = time.strftime("%Y-%m-%d %H:%M", time.localtime())
|
||||
self.prompt = self.prompt.replace("{date_time}", date_time)
|
||||
self.dialogue.put(Message(role="system", content=self.prompt))
|
||||
|
||||
|
||||
async def _check_and_broadcast_auth_code(self):
|
||||
"""检查设备绑定状态并广播认证码"""
|
||||
if not self.private_config.get_owner():
|
||||
@@ -191,7 +196,7 @@ class ConnectionHandler:
|
||||
# 如果不使用私有配置,就不需要验证
|
||||
return False
|
||||
return not self.is_device_verified
|
||||
|
||||
|
||||
def chat(self, query):
|
||||
# 如果设备未验证,就发送验证码
|
||||
if self.isNeedAuth():
|
||||
@@ -204,7 +209,7 @@ class ConnectionHandler:
|
||||
finally:
|
||||
loop.close()
|
||||
return True
|
||||
|
||||
|
||||
self.dialogue.put(Message(role="user", content=query))
|
||||
response_message = []
|
||||
start = 0
|
||||
@@ -321,6 +326,8 @@ class ConnectionHandler:
|
||||
|
||||
async def close(self):
|
||||
"""资源清理方法"""
|
||||
|
||||
# 清理其他资源
|
||||
self.stop_event.set()
|
||||
self.executor.shutdown(wait=False)
|
||||
if self.websocket:
|
||||
|
||||
Reference in New Issue
Block a user