feat(tts): 增加使用文件服务提供 TTS 语音文件的功能

This commit is contained in:
Raven95676
2025-05-14 19:17:28 +08:00
parent 40b9aa3a4c
commit d88683f498
2 changed files with 40 additions and 14 deletions

View File

@@ -66,6 +66,7 @@ DEFAULT_CONFIG = {
"enable": False,
"provider_id": "",
"dual_output": False,
"use_file_service": False,
},
"provider_ltm_settings": {
"group_icl_enable": False,
@@ -1307,6 +1308,11 @@ CONFIG_METADATA_2 = {
"hint": "启用后Bot 将同时输出语音和文字消息。",
"obvious_hint": True,
},
"use_file_service": {
"description": "使用文件服务提供 TTS 语音文件",
"type": "bool",
"hint": "启用后,如已配置 callback_api_base 将会使用文件服务提供TTS语音文件",
},
},
},
"provider_ltm_settings": {

View File

@@ -3,7 +3,7 @@ import time
import traceback
from typing import AsyncGenerator, Union
from astrbot.core import html_renderer, logger
from astrbot.core import html_renderer, logger, file_token_service
from astrbot.core.message.components import At, File, Image, Node, Plain, Record, Reply
from astrbot.core.message.message_event_result import ResultContentType
from astrbot.core.platform.astr_message_event import AstrMessageEvent
@@ -178,23 +178,43 @@ class ResultDecorateStage(Stage):
for comp in result.chain:
if isinstance(comp, Plain) and len(comp.text) > 1:
try:
logger.info("TTS 请求: " + comp.text)
logger.info(f"TTS 请求: {comp.text}")
audio_path = await tts_provider.get_audio(comp.text)
logger.info("TTS 结果: " + audio_path)
if audio_path:
new_chain.append(
Record(file=audio_path, url=audio_path)
)
if self.ctx.astrbot_config["provider_tts_settings"][
"dual_output"
]:
new_chain.append(comp)
else:
logger.info(f"TTS 结果: {audio_path}")
if not audio_path:
logger.error(
f"由于 TTS 音频文件找到,消息段转语音失败: {comp.text}"
f"由于 TTS 音频文件找到,消息段转语音失败: {comp.text}"
)
new_chain.append(comp)
except BaseException:
continue
use_file_service = self.ctx.astrbot_config[
"provider_tts_settings"
]["use_file_service"]
callback_api_base = self.ctx.astrbot_config[
"callback_api_base"
]
dual_output = self.ctx.astrbot_config[
"provider_tts_settings"
]["dual_output"]
url = None
if use_file_service and callback_api_base:
token = await file_token_service.register_file(
audio_path
)
url = f"{callback_api_base}/api/file/{token}"
logger.debug(f"已注册:{url}")
new_chain.append(
Record(
file=url if url else audio_path,
url=url if url else audio_path,
)
)
if dual_output:
new_chain.append(comp)
except Exception:
logger.error(traceback.format_exc())
logger.error("TTS 失败,使用文本发送。")
new_chain.append(comp)