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(" 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 )