mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:优化音乐配置初始化
This commit is contained in:
parent
60aa3a2fd4
commit
9d24a56bf1
3 changed files with 11 additions and 14 deletions
|
|
@ -19,14 +19,14 @@ 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.handle.musicHandler import MusicHandler # 添加这行
|
||||
from core.utils.auth_code_gen import AuthCodeGenerator
|
||||
|
||||
|
||||
TAG = __name__
|
||||
|
||||
|
||||
class ConnectionHandler:
|
||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, websocket_server=None):
|
||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _music):
|
||||
self.config = config
|
||||
self.logger = setup_logging()
|
||||
self.auth = AuthMiddleware(config)
|
||||
|
|
@ -88,8 +88,7 @@ class ConnectionHandler:
|
|||
self.private_config = None
|
||||
self.auth_code_gen = AuthCodeGenerator.get_instance()
|
||||
self.is_device_verified = False # 添加设备验证状态标志
|
||||
self.music_handler = MusicHandler(config) # 确保这行存在
|
||||
self.websocket_server = websocket_server # 添加server引用
|
||||
self.music_handler = _music
|
||||
|
||||
async def handle_connection(self, ws):
|
||||
try:
|
||||
|
|
@ -335,10 +334,6 @@ class ConnectionHandler:
|
|||
await self.websocket.close()
|
||||
self.logger.bind(tag=TAG).info("连接资源已释放")
|
||||
|
||||
# 确保从活跃连接集合中移除
|
||||
if self.websocket_server and self in self.websocket_server.active_connections:
|
||||
self.websocket_server.active_connections.discard(self)
|
||||
|
||||
def reset_vad_states(self):
|
||||
self.client_audio_buffer = bytes()
|
||||
self.client_have_voice = False
|
||||
|
|
|
|||
|
|
@ -53,12 +53,12 @@ class MusicHandler:
|
|||
async def handle_music_command(self, conn, text):
|
||||
"""处理音乐播放指令"""
|
||||
clean_text = re.sub(r'[^\w\s]', '', text).strip()
|
||||
logger.bind(tag=TAG).info(f"检查是否是音乐命令: {clean_text}")
|
||||
logger.bind(tag=TAG).debug(f"检查是否是音乐命令: {clean_text}")
|
||||
|
||||
# 尝试匹配具体歌名
|
||||
if os.path.exists(self.music_dir):
|
||||
music_files = [f for f in os.listdir(self.music_dir) if f.endswith('.mp3')]
|
||||
logger.bind(tag=TAG).info(f"找到的音乐文件: {music_files}")
|
||||
logger.bind(tag=TAG).debug(f"找到的音乐文件: {music_files}")
|
||||
|
||||
potential_song = _extract_song_name(clean_text)
|
||||
if potential_song:
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import asyncio
|
|||
import websockets
|
||||
from config.logger import setup_logging
|
||||
from core.connection import ConnectionHandler
|
||||
from core.handle.musicHandler import MusicHandler
|
||||
from core.utils.util import get_local_ip
|
||||
from core.utils import asr, vad, llm, tts
|
||||
|
||||
|
|
@ -12,7 +13,7 @@ class WebSocketServer:
|
|||
def __init__(self, config: dict):
|
||||
self.config = config
|
||||
self.logger = setup_logging()
|
||||
self._vad, self._asr, self._llm, self._tts = self._create_processing_instances()
|
||||
self._vad, self._asr, self._llm, self._tts, self._music = self._create_processing_instances()
|
||||
self.active_connections = set() # 添加全局连接记录
|
||||
|
||||
def _create_processing_instances(self):
|
||||
|
|
@ -44,7 +45,8 @@ class WebSocketServer:
|
|||
self.config["TTS"][self.config["selected_module"]["TTS"]]["type"],
|
||||
self.config["TTS"][self.config["selected_module"]["TTS"]],
|
||||
self.config["delete_audio"]
|
||||
)
|
||||
),
|
||||
MusicHandler(self.config)
|
||||
)
|
||||
|
||||
async def start(self):
|
||||
|
|
@ -64,7 +66,7 @@ class WebSocketServer:
|
|||
async def _handle_connection(self, websocket):
|
||||
"""处理新连接,每次创建独立的ConnectionHandler"""
|
||||
# 创建ConnectionHandler时传入当前server实例
|
||||
handler = ConnectionHandler(self.config, self._vad, self._asr, self._llm, self._tts, self)
|
||||
handler = ConnectionHandler(self.config, self._vad, self._asr, self._llm, self._tts, self._music)
|
||||
self.active_connections.add(handler)
|
||||
try:
|
||||
await handler.handle_connection(websocket)
|
||||
|
|
|
|||
Loading…
Reference in a new issue