From 1f8645340b62f9b57d90b42c4dec8266c8ad30f6 Mon Sep 17 00:00:00 2001 From: Jad Date: Thu, 13 Mar 2025 02:35:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89tts=E6=8E=A5=E5=8F=A3=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config.yaml | 14 ++++++++ .../core/providers/tts/custom.py | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 main/xiaozhi-server/core/providers/tts/custom.py diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index da98f1b40..47a8cfa62 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -412,6 +412,20 @@ TTS: # 语速范围0.25-4.0 speed: 1 output_file: tmp/ + CustomTTS: + # 自定义的TTS接口服务,请求参数可自定义 + # 要求接口使用GET方式请求,返回wav格式音频 + type: custom + url: "http://127.0.0.1:9880/tts" + params: # 自定义请求参数 + text: "{prompt_text}" # {prompt_text}会被替换为实际的提示词内容 + speaker: jok老师 + speed: 1 + foo: bar + testabc: 123456 + headers: # 自定义请求头 + Authorization: Bearer xxxx + output_file: tmp/ # 模块测试配置 module_test: test_sentences: # 自定义测试语句 diff --git a/main/xiaozhi-server/core/providers/tts/custom.py b/main/xiaozhi-server/core/providers/tts/custom.py new file mode 100644 index 000000000..6334f3167 --- /dev/null +++ b/main/xiaozhi-server/core/providers/tts/custom.py @@ -0,0 +1,34 @@ +import os +import uuid +import requests +from config.logger import setup_logging +from datetime import datetime +from core.providers.tts.base import TTSProviderBase + +TAG = __name__ +logger = setup_logging() + +class TTSProvider(TTSProviderBase): + def __init__(self, config, delete_audio_file): + super().__init__(config, delete_audio_file) + self.url = config.get("url") + self.headers = config.get("headers", {}) + self.params = config.get("params") + self.output_file = config.get("output_file", "tmp/") + + def generate_filename(self, extension=".wav"): + return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}") + + async def text_to_speak(self, text, output_file): + request_params = {} + for k, v in self.params.items(): + if isinstance(v, str) and "{prompt_text}" in v: + v = v.replace("{prompt_text}", text) + request_params[k] = v + + resp = requests.get(self.url, params=request_params, headers=self.headers) + if resp.status_code == 200: + with open(output_file, "wb") as file: + file.write(resp.content) + else: + logger.bind(tag=TAG).error(f"Custom TTS请求失败: {resp.status_code} - {resp.text}")