1.0
This commit is contained in:
1 parent
dbda6edc00
commit
122c47dd20
15 files changed
+3765
-73
No files matched your search
+221
-2
@@ -1,6 +1,21 @@
|
||||
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
|
||||
|
||||
logger.remove()
|
||||
logger.add(lambda msg: tqdm.write(msg, end=""), colorize=True)
|
||||
|
||||
|
||||
# 自动切分函数,避免TOKEN耗尽
|
||||
|
||||
@@ -55,16 +70,22 @@ class TTSBackend(ABC):
|
||||
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")
|
||||
|
||||
# 自动分块的说话接口,适用于长文本,是默认实现
|
||||
def smart_say(
|
||||
self,
|
||||
voice: str,
|
||||
text: str,
|
||||
instruct: str = "",
|
||||
split_length: int = 450,
|
||||
output_file_prefix: str = "./output",
|
||||
):
|
||||
chunks = smart_split(text)
|
||||
for i, chunk in enumerate(chunks):
|
||||
chunks = smart_split(text, split_length)
|
||||
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
|
||||
@@ -122,3 +143,201 @@ class CrispASRCLIBackend(TTSBackend):
|
||||
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
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
audio_base64 = response.json().get("audio_base64")
|
||||
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
|
||||
)
|
||||
Reference in new issue
Block a user