c840f37a44
CI/CD Pipeline / Validate Code Quality And Tests (push) Successful in 33s
CI/CD Pipeline / Frontend Lint (push) Successful in 1m12s
CI/CD Pipeline / Build Production Runtime Images (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Integration Tests (push) Failing after 1m8s
CI/CD Pipeline / Unit Tests (push) Successful in 2m52s
CI/CD Pipeline / Build & Push Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
79 lines
1.7 KiB
Python
Executable File
79 lines
1.7 KiB
Python
Executable File
"""TTS 服务抽象接口 (Port).
|
||
|
||
新增 TTS 供应商时,实现本接口即可。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from abc import ABC, abstractmethod
|
||
from pathlib import Path
|
||
|
||
|
||
class TtsService(ABC):
|
||
"""TTS 服务抽象基类.
|
||
|
||
所有 TTS 供应商(Mock / 阿里云 / 讯飞 等)都需要实现本接口。
|
||
"""
|
||
|
||
@abstractmethod
|
||
def synthesize(
|
||
self,
|
||
text: str,
|
||
*,
|
||
voice_id: str = "",
|
||
speed: float = 1.0,
|
||
pitch: float = 0.0,
|
||
output_path: Path | None = None,
|
||
sample_rate: int = 22050,
|
||
format: str = "wav",
|
||
) -> Path:
|
||
"""文本转语音合成.
|
||
|
||
Args:
|
||
text: 输入文本
|
||
voice_id: 音色 ID
|
||
speed: 语速 (0.5 ~ 2.0)
|
||
pitch: 语调(半音,-12 ~ 12)
|
||
output_path: 输出文件路径(None 则自动生成)
|
||
sample_rate: 采样率
|
||
format: 输出格式 (wav/mp3)
|
||
|
||
Returns:
|
||
输出音频文件路径
|
||
|
||
Raises:
|
||
TtsError: 合成失败
|
||
"""
|
||
...
|
||
|
||
@abstractmethod
|
||
def estimate_duration(self, text: str, *, speed: float = 1.0) -> float:
|
||
"""预估音频时长(秒).
|
||
|
||
用于在实际合成前估算时长,方便时间轴对齐。
|
||
|
||
Args:
|
||
text: 输入文本
|
||
speed: 语速
|
||
|
||
Returns:
|
||
预估时长(秒)
|
||
"""
|
||
...
|
||
|
||
@property
|
||
@abstractmethod
|
||
def provider_name(self) -> str:
|
||
"""供应商名称."""
|
||
...
|
||
|
||
def available_voices(self) -> list[str]:
|
||
"""支持的音色 ID 列表."""
|
||
return []
|
||
|
||
|
||
class TtsError(Exception):
|
||
"""TTS 合成异常."""
|
||
|
||
pass
|