104 lines
3.7 KiB
Python
104 lines
3.7 KiB
Python
|
|
"""已配置真实 TTS 服务的 WAV 输出适配。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import math
|
|||
|
|
import wave
|
|||
|
|
|
|||
|
|
from app.adapters.media import inspect_media
|
|||
|
|
from app.domain.execution import CpuResult
|
|||
|
|
from app.domain.errors import MediaError as ProviderError
|
|||
|
|
from .contracts import _invalid, _keys, _float, _string, _voices
|
|||
|
|
from .transport import _url
|
|||
|
|
|
|||
|
|
from typing import TYPE_CHECKING
|
|||
|
|
|
|||
|
|
if TYPE_CHECKING:
|
|||
|
|
from .client import Provider
|
|||
|
|
|
|||
|
|
|
|||
|
|
async def _speech(provider: Provider, inputs, p, work):
|
|||
|
|
if (
|
|||
|
|
not provider.settings.tts_base_url
|
|||
|
|
or not provider.settings.tts_model
|
|||
|
|
or not _voices(provider.settings)
|
|||
|
|
):
|
|||
|
|
raise ProviderError("capability_unavailable", "中文配音服务尚未配置。")
|
|||
|
|
_keys(inputs, {"prompt"})
|
|||
|
|
_keys(p, {"voice", "speed", "model"})
|
|||
|
|
model = p.get("model", provider.settings.tts_model)
|
|||
|
|
if model != provider.settings.tts_model:
|
|||
|
|
raise ProviderError("capability_changed", "配音模型配置已变化,请重新生成执行计划")
|
|||
|
|
text = _string(inputs.get("prompt"), 2000)
|
|||
|
|
voice = p.get("voice", _voices(provider.settings)[0])
|
|||
|
|
if voice not in _voices(provider.settings):
|
|||
|
|
raise _invalid("请选择已配置的中文音色。")
|
|||
|
|
speed = _float(p.get("speed", 1.0), 0.5, 2)
|
|||
|
|
target = work / "speech.wav"
|
|||
|
|
await provider._download_url(
|
|||
|
|
_url(provider.settings.tts_base_url, "/audio/speech"),
|
|||
|
|
target,
|
|||
|
|
provider._headers("tts"),
|
|||
|
|
timeout=getattr(provider.settings, "speech_timeout_seconds", 330.0),
|
|||
|
|
total_timeout=getattr(provider.settings, "speech_total_timeout_seconds", 360.0),
|
|||
|
|
payload={
|
|||
|
|
"model": model,
|
|||
|
|
"input": text,
|
|||
|
|
"voice": voice,
|
|||
|
|
"speed": speed,
|
|||
|
|
"response_format": "wav",
|
|||
|
|
},
|
|||
|
|
)
|
|||
|
|
info = await inspect_media(target, provider.settings)
|
|||
|
|
if info["kind"] != "audio" or info["mime_type"] != "audio/wav":
|
|||
|
|
target.unlink(missing_ok=True)
|
|||
|
|
raise ProviderError("invalid_speech_response", "配音服务未返回有效的 WAV 音频。", True)
|
|||
|
|
# 时间线用48kHz整数采样,避免5.02秒被round成5秒后错误准入视频生成。
|
|||
|
|
try:
|
|||
|
|
with wave.open(str(target), "rb") as source:
|
|||
|
|
samples = (
|
|||
|
|
source.getnframes() * 48000 + source.getframerate() - 1
|
|||
|
|
) // source.getframerate()
|
|||
|
|
except (wave.Error, EOFError):
|
|||
|
|
samples = math.ceil(info["duration_seconds"] * 48000)
|
|||
|
|
return CpuResult(
|
|||
|
|
[target],
|
|||
|
|
{
|
|||
|
|
"text": text,
|
|||
|
|
"voice": voice,
|
|||
|
|
"duration_seconds": info["duration_seconds"],
|
|||
|
|
"sample_count": samples,
|
|||
|
|
"sample_rate": 48000,
|
|||
|
|
},
|
|||
|
|
{"provider": "openai-compatible-tts", "model": model, "speed": speed},
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
|
|||
|
|
async def health(provider: Provider):
|
|||
|
|
configured = bool(
|
|||
|
|
provider.settings.tts_base_url
|
|||
|
|
and provider.settings.tts_model
|
|||
|
|
and _voices(provider.settings)
|
|||
|
|
)
|
|||
|
|
result = {
|
|||
|
|
"configured": configured,
|
|||
|
|
"reachable": False,
|
|||
|
|
"model": provider.settings.tts_model,
|
|||
|
|
"validation": "requires_deployment_acceptance",
|
|||
|
|
}
|
|||
|
|
if not configured:
|
|||
|
|
return result
|
|||
|
|
try:
|
|||
|
|
base = provider.settings.tts_base_url.rstrip("/").removesuffix("/v1")
|
|||
|
|
value = await provider._json(
|
|||
|
|
"GET", _url(base, "/health"), headers=provider._headers("tts"), timeout=5
|
|||
|
|
)
|
|||
|
|
result["reachable"] = (
|
|||
|
|
value.get("status") == "ok"
|
|||
|
|
and value.get("model") == provider.settings.tts_model
|
|||
|
|
and set(_voices(provider.settings)).issubset(value.get("voices", []))
|
|||
|
|
)
|
|||
|
|
except (ProviderError, TypeError, ValueError):
|
|||
|
|
pass
|
|||
|
|
return result
|