mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
feat:新增无需登录的在线播放音乐功能
This commit is contained in:
parent
98bfe863fe
commit
d3a2205859
1 changed files with 184 additions and 26 deletions
|
|
@ -6,6 +6,7 @@ import random
|
|||
import asyncio
|
||||
import difflib
|
||||
import traceback
|
||||
import requests
|
||||
from pathlib import Path
|
||||
from core.utils import p3
|
||||
from core.handle.sendAudioHandle import send_stt_message
|
||||
|
|
@ -26,21 +27,34 @@ play_music_function_desc = {
|
|||
"properties": {
|
||||
"song_name": {
|
||||
"type": "string",
|
||||
"description": "歌曲名称,如果用户没有指定具体歌名则为'random', 明确指定的时返回音乐的名字 示例: ```用户:播放两只老虎\n参数:两只老虎``` ```用户:播放音乐 \n参数:random ```",
|
||||
"description": "歌曲名称,如果用户没有指定具体歌名则为'random', 明确指定的时返回音乐的名字 示例: ```用户:播放两只老虎\n参数:两只老虎``` ```用户:播放音乐 \n参数:random ```"
|
||||
},
|
||||
"artist": {
|
||||
"type": "string",
|
||||
"description": "歌手名称,可选参数。如果指定了歌手名,将使用在线API搜索音乐"
|
||||
},
|
||||
"use_api": {
|
||||
"type": "boolean",
|
||||
"description": "是否使用在线API搜索音乐,默认为false。设置为true时将尝试使用在线API搜索音乐"
|
||||
}
|
||||
},
|
||||
"required": ["song_name"],
|
||||
},
|
||||
},
|
||||
"required": ["song_name"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@register_function("play_music", play_music_function_desc, ToolType.SYSTEM_CTL)
|
||||
def play_music(conn, song_name: str):
|
||||
@register_function('play_music', play_music_function_desc, ToolType.SYSTEM_CTL)
|
||||
def play_music(conn, song_name: str, artist: str = None, use_api: bool = False):
|
||||
try:
|
||||
music_intent = (
|
||||
f"播放音乐 {song_name}" if song_name != "random" else "随机播放音乐"
|
||||
)
|
||||
# 如果指定了歌手或明确使用API,则使用在线API播放
|
||||
if artist or use_api:
|
||||
music_intent = f"在线播放音乐 {song_name}"
|
||||
if artist:
|
||||
music_intent += f" - {artist}"
|
||||
else:
|
||||
music_intent = f"播放音乐 {song_name}" if song_name != "random" else "随机播放音乐"
|
||||
|
||||
|
||||
# 检查事件循环状态
|
||||
if not conn.loop.is_running():
|
||||
|
|
@ -51,7 +65,8 @@ def play_music(conn, song_name: str):
|
|||
|
||||
# 提交异步任务
|
||||
future = asyncio.run_coroutine_threadsafe(
|
||||
handle_music_command(conn, music_intent), conn.loop
|
||||
handle_music_command(conn, music_intent, song_name, artist, use_api),
|
||||
conn.loop
|
||||
)
|
||||
|
||||
# 非阻塞回调处理
|
||||
|
|
@ -125,16 +140,21 @@ def initialize_music_handler(conn):
|
|||
MUSIC_CACHE["music_dir"] = os.path.abspath(
|
||||
MUSIC_CACHE["music_config"].get("music_dir", "./music") # 默认路径修改
|
||||
)
|
||||
MUSIC_CACHE["music_ext"] = MUSIC_CACHE["music_config"].get(
|
||||
"music_ext", (".mp3", ".wav", ".p3")
|
||||
)
|
||||
MUSIC_CACHE["refresh_time"] = MUSIC_CACHE["music_config"].get(
|
||||
"refresh_time", 60
|
||||
)
|
||||
MUSIC_CACHE["music_ext"] = MUSIC_CACHE["music_config"].get("music_ext", (".mp3", ".wav", ".p3"))
|
||||
MUSIC_CACHE["refresh_time"] = MUSIC_CACHE["music_config"].get("refresh_time", 60)
|
||||
# 在线API配置
|
||||
MUSIC_CACHE["api_base_url"] = MUSIC_CACHE["music_config"].get("api_base_url", "http://www.jsrc.top:5566")
|
||||
MUSIC_CACHE["temp_dir"] = os.path.abspath(MUSIC_CACHE["music_config"].get("temp_dir", "./temp"))
|
||||
else:
|
||||
MUSIC_CACHE["music_dir"] = os.path.abspath("./music")
|
||||
MUSIC_CACHE["music_ext"] = (".mp3", ".wav", ".p3")
|
||||
MUSIC_CACHE["refresh_time"] = 60
|
||||
MUSIC_CACHE["api_base_url"] = "http://www.jsrc.top:5566"
|
||||
MUSIC_CACHE["temp_dir"] = os.path.abspath("./temp")
|
||||
|
||||
# 确保临时目录存在
|
||||
os.makedirs(MUSIC_CACHE["temp_dir"], exist_ok=True)
|
||||
|
||||
# 获取音乐文件列表
|
||||
MUSIC_CACHE["music_files"], MUSIC_CACHE["music_file_names"] = get_music_files(
|
||||
MUSIC_CACHE["music_dir"], MUSIC_CACHE["music_ext"]
|
||||
|
|
@ -143,7 +163,7 @@ def initialize_music_handler(conn):
|
|||
return MUSIC_CACHE
|
||||
|
||||
|
||||
async def handle_music_command(conn, text):
|
||||
async def handle_music_command(conn, text, song_name=None, artist=None, use_api=False):
|
||||
initialize_music_handler(conn)
|
||||
global MUSIC_CACHE
|
||||
|
||||
|
|
@ -151,6 +171,12 @@ async def handle_music_command(conn, text):
|
|||
clean_text = re.sub(r"[^\w\s]", "", text).strip()
|
||||
conn.logger.bind(tag=TAG).debug(f"检查是否是音乐命令: {clean_text}")
|
||||
|
||||
# 如果指定了歌手或明确使用API,则使用在线API播放
|
||||
if artist or use_api:
|
||||
conn.logger.bind(tag=TAG).info(f"使用在线API播放音乐: {song_name} - {artist if artist else '未指定歌手'}")
|
||||
await play_api_music(conn, song_name, artist)
|
||||
return True
|
||||
|
||||
# 尝试匹配具体歌名
|
||||
if os.path.exists(MUSIC_CACHE["music_dir"]):
|
||||
if time.time() - MUSIC_CACHE["scan_time"] > MUSIC_CACHE["refresh_time"]:
|
||||
|
|
@ -167,7 +193,15 @@ async def handle_music_command(conn, text):
|
|||
conn.logger.bind(tag=TAG).info(f"找到最匹配的歌曲: {best_match}")
|
||||
await play_local_music(conn, specific_file=best_match)
|
||||
return True
|
||||
# 检查是否是通用播放音乐命令
|
||||
|
||||
# 如果本地没找到匹配的歌曲,且song_name不是random,尝试使用在线API
|
||||
if song_name != "random" and song_name != "":
|
||||
conn.logger.bind(tag=TAG).info(f"本地未找到匹配歌曲,尝试使用在线API: {song_name}")
|
||||
success = await play_api_music(conn, song_name, artist)
|
||||
if success:
|
||||
return True
|
||||
|
||||
# 如果在线API也失败或song_name为random,则随机播放本地音乐
|
||||
await play_local_music(conn)
|
||||
return True
|
||||
|
||||
|
|
@ -194,10 +228,8 @@ async def play_local_music(conn, specific_file=None):
|
|||
"""播放本地音乐文件"""
|
||||
try:
|
||||
if not os.path.exists(MUSIC_CACHE["music_dir"]):
|
||||
conn.logger.bind(tag=TAG).error(
|
||||
f"音乐目录不存在: " + MUSIC_CACHE["music_dir"]
|
||||
)
|
||||
return
|
||||
conn.logger.bind(tag=TAG).error(f"音乐目录不存在: " + MUSIC_CACHE["music_dir"])
|
||||
return False
|
||||
|
||||
# 确保路径正确性
|
||||
if specific_file:
|
||||
|
|
@ -206,13 +238,14 @@ async def play_local_music(conn, specific_file=None):
|
|||
else:
|
||||
if not MUSIC_CACHE["music_files"]:
|
||||
conn.logger.bind(tag=TAG).error("未找到MP3音乐文件")
|
||||
return
|
||||
return False
|
||||
selected_music = random.choice(MUSIC_CACHE["music_files"])
|
||||
music_path = os.path.join(MUSIC_CACHE["music_dir"], selected_music)
|
||||
|
||||
if not os.path.exists(music_path):
|
||||
conn.logger.bind(tag=TAG).error(f"选定的音乐文件不存在: {music_path}")
|
||||
return
|
||||
return False
|
||||
|
||||
text = _get_random_play_prompt(selected_music)
|
||||
await send_stt_message(conn, text)
|
||||
conn.dialogue.put(Message(role="assistant", content=text))
|
||||
|
|
@ -231,9 +264,134 @@ async def play_local_music(conn, specific_file=None):
|
|||
if music_path.endswith(".p3"):
|
||||
opus_packets, _ = p3.decode_opus_from_file(music_path)
|
||||
else:
|
||||
opus_packets, _ = conn.tts.audio_to_opus_data(music_path)
|
||||
conn.audio_play_queue.put((opus_packets, None, conn.tts_last_text_index))
|
||||
opus_packets, duration = conn.tts.audio_to_opus_data(music_path)
|
||||
conn.audio_play_queue.put((opus_packets, selected_music, 0))
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).error(f"播放音乐失败: {str(e)}")
|
||||
conn.logger.bind(tag=TAG).error(f"详细错误: {traceback.format_exc()}")
|
||||
return False
|
||||
|
||||
|
||||
async def play_api_music(conn, song_name, artist=None):
|
||||
"""通过在线API播放音乐"""
|
||||
global MUSIC_CACHE
|
||||
|
||||
try:
|
||||
# 1. 获取音乐信息
|
||||
music_info = await get_music_info(conn, song_name, artist)
|
||||
if not music_info:
|
||||
await send_stt_message(conn, f"抱歉,没有找到歌曲 {song_name}")
|
||||
return False
|
||||
|
||||
# 2. 下载音频文件
|
||||
audio_url = music_info.get("audioUrl")
|
||||
if not audio_url:
|
||||
await send_stt_message(conn, "获取音乐链接失败")
|
||||
return False
|
||||
|
||||
# 生成随机文件名
|
||||
random_name = f"music_{int(time.time())}_{random.randint(1000, 9999)}.mp3"
|
||||
audio_path = os.path.join(MUSIC_CACHE["temp_dir"], random_name)
|
||||
|
||||
# 下载音频文件 - 注意传入conn参数
|
||||
success = await download_file(conn, audio_url, audio_path)
|
||||
if not success:
|
||||
await send_stt_message(conn, "下载音乐失败")
|
||||
return False
|
||||
|
||||
# 3. 播放音乐(使用与本地音乐相同的播放逻辑)
|
||||
display_name = f"{song_name}"
|
||||
if artist:
|
||||
display_name += f" - {artist}"
|
||||
|
||||
# 发送播放消息
|
||||
await send_stt_message(conn, f"正在播放 {display_name}")
|
||||
|
||||
# 重置TTS状态
|
||||
conn.tts_first_text_index = 0
|
||||
conn.tts_last_text_index = 0
|
||||
conn.llm_finish_task = True
|
||||
|
||||
# 解码音频文件
|
||||
if audio_path.endswith(".p3"):
|
||||
opus_packets, duration = p3.decode_opus_from_file(audio_path)
|
||||
else:
|
||||
opus_packets, duration = conn.tts.audio_to_opus_data(audio_path)
|
||||
|
||||
# 将音频帧放入播放队列
|
||||
conn.audio_play_queue.put((opus_packets, display_name, 0))
|
||||
|
||||
# 播放完成后删除临时文件
|
||||
try:
|
||||
os.remove(audio_path)
|
||||
except:
|
||||
pass
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).error(f"API音乐播放出错: {str(e)}")
|
||||
conn.logger.bind(tag=TAG).error(traceback.format_exc())
|
||||
await send_stt_message(conn, f"播放音乐时出错: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
async def get_music_info(conn, song_name, artist=None):
|
||||
"""获取音乐信息(音频URL)"""
|
||||
global MUSIC_CACHE
|
||||
|
||||
try:
|
||||
url_builder = f"{MUSIC_CACHE['api_base_url']}/stream_pcm?song={requests.utils.quote(song_name)}"
|
||||
if artist:
|
||||
url_builder += f"&artist={requests.utils.quote(artist)}"
|
||||
|
||||
# 使用异步方式获取数据
|
||||
response = await asyncio.to_thread(requests.get, url_builder)
|
||||
if response.status_code != 200:
|
||||
conn.logger.bind(tag=TAG).error(f"获取音乐信息失败,响应码: {response.status_code}")
|
||||
return None
|
||||
|
||||
response_data = response.json()
|
||||
result = {}
|
||||
|
||||
# 检查API响应格式,支持两种可能的字段名
|
||||
audio_path = response_data.get("audioPath")
|
||||
audio_url = response_data.get("audio_url")
|
||||
|
||||
# 优先使用直接URL,否则构建URL
|
||||
if audio_url and not audio_url.startswith("http"):
|
||||
audio_url = f"{MUSIC_CACHE['api_base_url']}{audio_url}"
|
||||
elif audio_path:
|
||||
audio_url = f"{MUSIC_CACHE['api_base_url']}/get_file?path={requests.utils.quote(audio_path)}&name={requests.utils.quote(song_name + '.mp3')}"
|
||||
else:
|
||||
conn.logger.bind(tag=TAG).error("API响应中缺少音频URL信息")
|
||||
return None
|
||||
|
||||
result["audioUrl"] = audio_url
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).error(f"获取音乐信息时发生错误: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
async def download_file(conn, file_url, output_path):
|
||||
"""下载文件到指定路径"""
|
||||
try:
|
||||
# 使用异步方式下载文件
|
||||
response = await asyncio.to_thread(requests.get, file_url, stream=True)
|
||||
if response.status_code != 200:
|
||||
conn.logger.bind(tag=TAG).error(f"下载文件失败,响应码: {response.status_code}")
|
||||
return False
|
||||
|
||||
# 异步写入文件
|
||||
with open(output_path, 'wb') as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
return True
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).error(f"下载文件时发生错误: {str(e)}")
|
||||
return False
|
||||
Loading…
Reference in a new issue