mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
fix: 兼容intent
This commit is contained in:
parent
0545122cb5
commit
4e19841d83
2 changed files with 15 additions and 9 deletions
|
|
@ -33,9 +33,9 @@ class JellyfinHandler:
|
|||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.music_related_keywords = []
|
||||
|
||||
default_music_related_keywords = ["来一首歌", "唱一首歌", "播放音乐", "来点音乐", "背景音乐", "放首歌",
|
||||
"播放歌曲", "来点背景音乐", "我想听歌", "我要听歌", "放点音乐"]
|
||||
self.music_files = ['播放音乐']
|
||||
if "music" in self.config:
|
||||
self.music_config = self.config["music"]
|
||||
self.jellyfin_endpoint = self.music_config['jellyfin']['endpoint']
|
||||
|
|
@ -49,12 +49,18 @@ class JellyfinHandler:
|
|||
|
||||
async def handle_music_command(self, conn, text):
|
||||
"""处理音乐播放指令"""
|
||||
clean_text = re.sub(r'[^\w\s]', '', text).strip()
|
||||
logger.bind(tag=TAG).debug(f"检查是否是音乐命令: {clean_text}")
|
||||
try:
|
||||
content = (re.findall(r"'([^']*)'", text) or [text])[0]
|
||||
clean_text = re.sub(r'[^\w\s]', '', content).strip()
|
||||
potential_song = clean_text.split(' ')[-1]
|
||||
except Exception as e:
|
||||
logger.bind(tag=TAG).error(f"非intent命令: {str(e)}")
|
||||
clean_text = re.sub(r'[^\w\s]', '', text).strip()
|
||||
logger.bind(tag=TAG).debug(f"检查是否是音乐命令: {clean_text}")
|
||||
potential_song = _extract_song_name(clean_text)
|
||||
|
||||
# 尝试匹配具体歌名
|
||||
if self.jellyfin_endpoint:
|
||||
potential_song = _extract_song_name(clean_text)
|
||||
if potential_song:
|
||||
best_match_item = self._find_best_match(potential_song)
|
||||
if best_match_item:
|
||||
|
|
|
|||
|
|
@ -47,18 +47,18 @@ class TTSProviderBase(ABC):
|
|||
if not wav_bytes:
|
||||
raise IOError('stream null')
|
||||
audio = AudioSegment.from_file(BytesIO(wav_bytes))
|
||||
return self.audio_to_opus_data(audio)
|
||||
return self.audio_seg_to_opus_data(audio)
|
||||
|
||||
def audio_to_opus_data(self, audio_file_path:str):
|
||||
def audio_to_opus_data(self, wav_file_path:str):
|
||||
"""音频文件转换为Opus编码"""
|
||||
# 获取文件后缀名
|
||||
file_type = os.path.splitext(audio_file_path)[1]
|
||||
file_type = os.path.splitext(wav_file_path)[1]
|
||||
if file_type:
|
||||
file_type = file_type.lstrip('.')
|
||||
audio = AudioSegment.from_file(wav_file_path, format=file_type)
|
||||
return self.audio_to_opus_data(audio)
|
||||
return self.audio_seg_to_opus_data(audio)
|
||||
|
||||
def audio_to_opus_data(self, audio:AudioSegment):
|
||||
def audio_seg_to_opus_data(self, audio:AudioSegment):
|
||||
# 转换为单声道/16kHz采样率/16位小端编码(确保与编码器匹配)
|
||||
audio = audio.set_channels(1).set_frame_rate(16000).set_sample_width(2)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue