364 lines
13 KiB
Python
364 lines
13 KiB
Python
from abc import ABC, abstractmethod
|
|
from loguru import logger
|
|
import re
|
|
import asyncio
|
|
import websockets
|
|
import base64
|
|
import struct
|
|
from array import array
|
|
import sys
|
|
import wave
|
|
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)
|
|
|
|
|
|
# 自动切分函数,避免TOKEN耗尽
|
|
|
|
|
|
def smart_split(text: str, max_length: int = 300) -> list[str]:
|
|
if len(text) <= max_length:
|
|
return [text]
|
|
text = re.sub(r"\s+", " ", text).strip()
|
|
|
|
chunks = []
|
|
while len(text) > max_length:
|
|
# 1. 截取前 max_length 个字作为候选区
|
|
candidate = text[:max_length]
|
|
|
|
# 2. 优先找一句话结束的绝对标点(句号、问号、感叹号、省略号、换行)
|
|
# [。!?…\n] 后面可能跟着引号,所以用 [」”]? 容错
|
|
end_punc_match = list(re.finditer(r"[。!?…\n][」”]?", candidate))
|
|
|
|
if end_punc_match:
|
|
# 取候选区里最后一个句号的位置
|
|
split_idx = end_punc_match[-1].end()
|
|
else:
|
|
# 3. 没找到句号,则退而求其次寻找停顿标点(逗号、分号、冒号、顿号)
|
|
pause_punc_match = list(re.finditer(r"[,;:、,;:][」”]?", candidate))
|
|
if pause_punc_match:
|
|
split_idx = pause_punc_match[-1].end()
|
|
else:
|
|
# 4. 如果 600 字内极其奇葩地连一个标点都没有,则在空格处切分
|
|
space_match = list(re.finditer(r"\s", candidate))
|
|
if space_match:
|
|
split_idx = space_match[-1].end()
|
|
else:
|
|
# 5. 实在啥都没有,无情强行按字数切断(保底机制)
|
|
split_idx = max_length
|
|
|
|
# 提取切分出来的段落
|
|
chunk = text[:split_idx].strip()
|
|
if chunk:
|
|
chunks.append(chunk)
|
|
|
|
# 剩下的文本继续循环
|
|
text = text[split_idx:].strip()
|
|
|
|
if text:
|
|
chunks.append(text)
|
|
|
|
return chunks
|
|
|
|
|
|
class TTSBackend(ABC):
|
|
@abstractmethod
|
|
def say(self, voice: str, text: str, instruct: str = "", output_file: str = ""):
|
|
pass
|
|
|
|
def stream_say(self, voice: str, text: str, instruct: str = ""):
|
|
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 = "",
|
|
split_length: int = 450,
|
|
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} 的语音"
|
|
):
|
|
output_file = (
|
|
f"{output_file_prefix}_{i}.wav"
|
|
if len(chunks) > 1
|
|
else f"{output_file_prefix}.wav"
|
|
)
|
|
logger.debug(f"正在合成 chunk {i+1}/{len(chunks)}: {chunk}")
|
|
self.say(
|
|
voice=voice, text=chunk, instruct=instruct, output_file=output_file
|
|
)
|
|
|
|
|
|
class CrispASRCLIBackend(TTSBackend):
|
|
def __init__(
|
|
self,
|
|
model: str,
|
|
codec_model: str,
|
|
executable: str = "./crispasr",
|
|
gpu_backend: str = "auto",
|
|
):
|
|
self.executable = executable
|
|
self.model = model
|
|
self.gpu_backend = gpu_backend
|
|
self.codec_model = codec_model
|
|
|
|
def say(
|
|
self,
|
|
voice: str,
|
|
text: str,
|
|
instruct: str = "",
|
|
output_file: str = "./output.wav",
|
|
):
|
|
import subprocess
|
|
|
|
cmd = [
|
|
self.executable,
|
|
"--voice",
|
|
voice,
|
|
"--tts",
|
|
text,
|
|
"--model",
|
|
self.model,
|
|
"--gpu-backend",
|
|
self.gpu_backend,
|
|
"--codec-model",
|
|
self.codec_model,
|
|
]
|
|
if instruct:
|
|
cmd.extend(["--instruct", instruct])
|
|
if output_file:
|
|
cmd.extend(["--tts-output", output_file])
|
|
logger.debug(f"命令: {' '.join(cmd)}")
|
|
|
|
result = subprocess.run(cmd, capture_output=True)
|
|
logger.debug(f"stdout: {result.stdout.decode()}")
|
|
logger.debug(f"stderr: {result.stderr.decode()}")
|
|
if result.returncode != 0:
|
|
raise RuntimeError(f"Command failed: {result.stderr.decode()}")
|
|
|
|
|
|
# stream还不是很稳定,默认使用http接口,stream接口留作测试和特殊需求使用
|
|
class QTRTTSBackend(TTSBackend):
|
|
"""实现基于 WebSocket 的流式 TTS 后端。
|
|
|
|
stream_say 会阻塞直到流结束并将接收到的样本封装为 WAV 文件,返回 WAV 路径。
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
mode: str = "http",
|
|
http_url: str = "http://127.0.0.1:3001/api/tts",
|
|
ws_uri: str = "ws://127.0.0.1:3001/api/tts/stream",
|
|
sample_rate: int = 24000,
|
|
max_retries: int = 5,
|
|
retry_delay: float = 0.5,
|
|
):
|
|
self.mode = mode
|
|
self.http_url = http_url
|
|
self.ws_uri = ws_uri
|
|
self.sample_rate = sample_rate
|
|
self.max_retries = max(1, max_retries)
|
|
self.retry_delay = max(0.0, retry_delay)
|
|
|
|
def stream_say(self, voice: str, text: str, instruct: str = "") -> Optional[str]:
|
|
return asyncio.run(
|
|
self._stream_say_async(voice=voice, text=text, instruct=instruct)
|
|
)
|
|
|
|
async def _stream_say_async(
|
|
self, voice: str, text: str, instruct: str = ""
|
|
) -> Optional[str]:
|
|
pcm_path = "qtr_backend_output_stream.pcm"
|
|
wav_path = "qtr_backend_output_stream.wav"
|
|
|
|
# remove existing temp
|
|
try:
|
|
if os.path.exists(pcm_path):
|
|
os.remove(pcm_path)
|
|
except Exception:
|
|
pass
|
|
|
|
req = {"text": text, "speaker": voice, "instruction": instruct}
|
|
last_error: Optional[Exception] = None
|
|
|
|
for attempt in range(1, self.max_retries + 1):
|
|
ws = None
|
|
try:
|
|
# 重试时清理上一次失败留下的中间文件,避免混入损坏片段
|
|
if attempt > 1 and os.path.exists(pcm_path):
|
|
os.remove(pcm_path)
|
|
|
|
ws = await websockets.connect(self.ws_uri, close_timeout=1)
|
|
# send request then end marker as server expects
|
|
await ws.send(json.dumps(req, ensure_ascii=False))
|
|
await ws.send("end")
|
|
|
|
while True:
|
|
try:
|
|
message = await ws.recv()
|
|
except websockets.exceptions.ConnectionClosedOK:
|
|
break
|
|
except websockets.exceptions.ConnectionClosedError as e:
|
|
# 交给外层重试
|
|
raise e
|
|
|
|
if message == "end":
|
|
break
|
|
|
|
# 二进制帧:float32 小端样本
|
|
if isinstance(message, (bytes, bytearray)):
|
|
try:
|
|
arr = array("f")
|
|
arr.frombytes(bytes(message))
|
|
if sys.byteorder == "big":
|
|
arr.byteswap()
|
|
with open(pcm_path, "ab") as pcm_f:
|
|
for v in arr:
|
|
v = max(-1.0, min(1.0, v))
|
|
pcm_f.write(struct.pack("<h", int(v * 32767.0)))
|
|
except Exception as e:
|
|
logger.error(f"处理二进制帧失败: {e}")
|
|
continue
|
|
|
|
# 文本帧:控制或 base64
|
|
if isinstance(message, str):
|
|
if message == "done":
|
|
break
|
|
if message == "segment_done":
|
|
continue
|
|
try:
|
|
audio = base64.b64decode(message, validate=True)
|
|
except Exception:
|
|
logger.error(f"收到非 base64 文本消息: {message!r}")
|
|
continue
|
|
|
|
# 尝试把 base64 解码视为 f32 bytes
|
|
try:
|
|
arr = array("f")
|
|
arr.frombytes(audio)
|
|
if sys.byteorder == "big":
|
|
arr.byteswap()
|
|
with open(pcm_path, "ab") as f:
|
|
for v in arr:
|
|
v = max(-1.0, min(1.0, v))
|
|
f.write(struct.pack("<h", int(v * 32767.0)))
|
|
except Exception:
|
|
# 不是 f32 bytes,直接写入原始解码数据
|
|
with open(pcm_path, "ab") as f:
|
|
f.write(audio)
|
|
|
|
last_error = None
|
|
break
|
|
except (
|
|
websockets.exceptions.WebSocketException,
|
|
OSError,
|
|
asyncio.TimeoutError,
|
|
EOFError,
|
|
) as e:
|
|
last_error = e
|
|
logger.warning(
|
|
f"QTR 连接/流处理失败,第 {attempt}/{self.max_retries} 次: {e}"
|
|
)
|
|
if attempt < self.max_retries:
|
|
await asyncio.sleep(self.retry_delay * attempt)
|
|
finally:
|
|
if ws is not None:
|
|
try:
|
|
await ws.close()
|
|
except Exception as close_err:
|
|
# 服务端未返回 close frame 时不应导致整体失败
|
|
logger.debug(f"忽略 websocket 关闭异常: {close_err}")
|
|
|
|
if last_error is not None:
|
|
raise RuntimeError(
|
|
f"QTR 流式合成失败,重试 {self.max_retries} 次后仍失败: {last_error}"
|
|
)
|
|
|
|
# 封装为 WAV
|
|
if os.path.exists(pcm_path):
|
|
try:
|
|
with open(pcm_path, "rb") as pcm_f:
|
|
pcm_data = pcm_f.read()
|
|
with wave.open(wav_path, "wb") as wf:
|
|
wf.setnchannels(1)
|
|
wf.setsampwidth(2)
|
|
wf.setframerate(self.sample_rate)
|
|
wf.writeframes(pcm_data)
|
|
logger.info(f"已生成 WAV: {wav_path}")
|
|
return wav_path
|
|
except Exception as e:
|
|
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 = ""
|
|
):
|
|
import requests
|
|
|
|
response = requests.post(
|
|
self.http_url,
|
|
json={
|
|
"text": text,
|
|
"speaker": voice,
|
|
"instruction": instruct,
|
|
},
|
|
)
|
|
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)
|
|
with open(output_file, "wb") as f:
|
|
f.write(audio)
|
|
return output_file if output_file else None
|
|
|
|
def say(
|
|
self,
|
|
voice: str,
|
|
text: str,
|
|
instruct: str = "",
|
|
output_file: str = "./output.wav",
|
|
) -> Optional[str]:
|
|
# 默认行为:调用 stream_say 或 http_say,并返回 output_file
|
|
match self.mode:
|
|
case "stream":
|
|
wav = self.stream_say(voice=voice, text=text, instruct=instruct)
|
|
if wav and output_file:
|
|
try:
|
|
os.replace(wav, output_file)
|
|
return output_file
|
|
except Exception:
|
|
return wav
|
|
return wav
|
|
case "http":
|
|
return self.http_say(
|
|
voice=voice, text=text, instruct=instruct, output_file=output_file
|
|
)
|