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
45446316de
commit
e58551bfb5
8 changed files with 54 additions and 63 deletions
16
config.yaml
16
config.yaml
|
|
@ -304,3 +304,19 @@ module_test:
|
|||
- "你好,请介绍一下你自己"
|
||||
- "What's the weather like today?"
|
||||
- "请用100字概括量子计算的基本原理和应用前景"
|
||||
|
||||
# 本地音乐播放配置
|
||||
music:
|
||||
music_commands:
|
||||
- "来一首歌"
|
||||
- "唱一首歌"
|
||||
- "播放音乐"
|
||||
- "来点音乐"
|
||||
- "背景音乐"
|
||||
- "放首歌"
|
||||
- "播放歌曲"
|
||||
- "来点背景音乐"
|
||||
- "我想听歌"
|
||||
- "我要听歌"
|
||||
- "放点音乐"
|
||||
music_dir: "./music" # 音乐文件存放路径
|
||||
|
|
@ -3,18 +3,6 @@ import json
|
|||
import asyncio
|
||||
import time
|
||||
from core.utils.util import remove_punctuation_and_length, get_string_no_punctuation_or_emoji
|
||||
import os
|
||||
from pydub import AudioSegment
|
||||
from pydub.playback import play
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import re
|
||||
import uuid
|
||||
import tempfile
|
||||
import winsound # Windows专用
|
||||
import pygame
|
||||
import traceback
|
||||
import random
|
||||
import difflib
|
||||
from core.handle.musicHandler import MusicHandler
|
||||
|
||||
TAG = __name__
|
||||
|
|
|
|||
|
|
@ -9,26 +9,27 @@ import traceback
|
|||
import time
|
||||
import threading
|
||||
import websockets
|
||||
import yaml
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
class MusicHandler:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
# 加载独立音乐配置
|
||||
music_config_path = os.path.abspath("music/music_config.yaml")
|
||||
if os.path.exists(music_config_path):
|
||||
with open(music_config_path, 'r') as f:
|
||||
self.music_config = yaml.safe_load(f)
|
||||
else:
|
||||
self.music_config = {}
|
||||
logger.bind(tag=TAG).warning("未找到独立音乐配置文件,使用默认配置")
|
||||
self.music_related_keywords = []
|
||||
|
||||
if "music" in self.config:
|
||||
self.music_config = self.config["music"]
|
||||
self.music_dir = os.path.abspath(
|
||||
self.music_config.get("music_dir", "./music") # 默认路径修改
|
||||
)
|
||||
self.music_related_keywords = self.music_config.get("music_commands", [])
|
||||
else:
|
||||
self.music_dir = os.path.abspath("./music")
|
||||
self.music_related_keywords = ["来一首歌", "唱一首歌", "播放音乐", "来点音乐", "背景音乐", "放首歌",
|
||||
"播放歌曲", "来点背景音乐", "我想听歌", "我要听歌", "放点音乐"]
|
||||
|
||||
self.music_dir = os.path.abspath(
|
||||
self.music_config.get("music_dir", "./music/mp3") # 默认路径修改
|
||||
)
|
||||
self.is_playing = False
|
||||
self.stop_event = threading.Event()
|
||||
|
||||
|
|
@ -51,8 +52,7 @@ class MusicHandler:
|
|||
return True
|
||||
|
||||
# 检查是否是通用播放音乐命令
|
||||
music_related_keywords = self.music_config.get("music_commands", [])
|
||||
if any(cmd in clean_text for cmd in music_related_keywords):
|
||||
if any(cmd in clean_text for cmd in self.music_related_keywords):
|
||||
await self.play_local_music(conn)
|
||||
return True
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ class MusicHandler:
|
|||
|
||||
def _extract_song_name(self, text):
|
||||
"""从用户输入中提取歌名"""
|
||||
for keyword in ["听", "播放", "放"]:
|
||||
for keyword in ["听", "播放", "放", "唱"]:
|
||||
if keyword in text:
|
||||
parts = text.split(keyword)
|
||||
if len(parts) > 1:
|
||||
|
|
@ -78,7 +78,6 @@ class MusicHandler:
|
|||
if ratio > highest_ratio and ratio > 0.4:
|
||||
highest_ratio = ratio
|
||||
best_match = music_file
|
||||
|
||||
return best_match
|
||||
|
||||
async def play_local_music(self, conn, specific_file=None):
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
# 本地音乐播放配置
|
||||
music_commands:
|
||||
- "播放音乐"
|
||||
- "来点音乐"
|
||||
- "背景音乐"
|
||||
- "放首歌"
|
||||
- "播放歌曲"
|
||||
- "来点背景音乐"
|
||||
- "我想听歌"
|
||||
- "我要听歌"
|
||||
- "放点音乐"
|
||||
music_dir: "./music/mp3" # 音乐文件存放路径
|
||||
|
|
@ -16,4 +16,4 @@ aiohttp_cors==0.7.0
|
|||
ormsgpack==1.7.0
|
||||
ruamel.yaml==0.18.10
|
||||
loguru==0.7.3
|
||||
requests>=2.0.0
|
||||
requests==2.32.3
|
||||
Loading…
Reference in a new issue