This commit is contained in:
:NoBody: committed 2026-07-26 16:49:38 +08:00
1 parent 122c47dd20
commit bf68444629
11 files changed
+316 -1707

No files matched your search

+21 -1
View File
@@ -12,6 +12,7 @@ import os
import json
from typing import Optional
from tqdm import tqdm
from retry import retry
logger.remove()
logger.add(lambda msg: tqdm.write(msg, end=""), colorize=True)
@@ -74,8 +75,11 @@ class TTSBackend(ABC):
raise NotImplementedError("stream_say is not implemented for this backend")
# 自动分块的说话接口,适用于长文本,是默认实现
# @retry(delay=0.5, tries=3, backoff=2)
def smart_say(
self,
from_chunk: int,
to_chunk: int,
voice: str,
text: str,
instruct: str = "",
@@ -83,6 +87,9 @@ class TTSBackend(ABC):
output_file_prefix: str = "./output",
):
chunks = smart_split(text, split_length)
chunks = (
chunks[from_chunk - 1 : to_chunk] if from_chunk and to_chunk else chunks
)
for i, chunk in tqdm(
enumerate(chunks), total=len(chunks), desc=f"合成 {voice} 的语音"
):
@@ -298,6 +305,7 @@ class QTRTTSBackend(TTSBackend):
logger.error(f"封装 WAV 失败: {e}")
return None
@retry(delay=0.5, tries=3, backoff=2)
def http_say(
self, voice: str, text: str, instruct: str = "", output_file: str = ""
):
@@ -311,7 +319,19 @@ class QTRTTSBackend(TTSBackend):
"instruction": instruct,
},
)
audio_base64 = response.json().get("audio_base64")
response.raise_for_status()
try:
payload = response.json()
except ValueError as e:
raise RuntimeError(
f"HTTP TTS 接口返回了非 JSON 响应: {response.text[:200]!r}"
) from e
audio_base64 = payload.get("audio_base64")
if not audio_base64:
raise RuntimeError(f"HTTP TTS 响应缺少 audio_base64 字段: {payload!r}")
audio = base64.b64decode(audio_base64)
if output_file:
os.makedirs(os.path.dirname(output_file) or ".", exist_ok=True)