Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz, Clipador de lives e Publicador para YouTube, TikTok e Instagram. Inclui a leitura das visualizações dos posts para o Dashboard e o botão "Publicar no <rede>" nos cartões das publicações. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
"""Etapa RVC (Applio 3.7.0 em voice/vendor/applio, instalado pelo setup.bat).
|
|
|
|
Envolve um motor de TTS: gera a fala com a voz base e converte o timbre com o `VoiceConverter` do Applio.
|
|
Só roda para vozes com o bloco `rvc` (vozes de personagem adicionadas pelo painel) e com VOICE_ENABLE_RVC=1.
|
|
O Applio escolhe a GPU sozinho (torch.cuda); com VOICE_DEVICE=cpu o CUDA fica invisível para ele.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import soundfile
|
|
|
|
from .catalog import Voice
|
|
from .engine import Pcm, TtsEngine
|
|
from .errors import RvcFailed, RvcUnavailable
|
|
|
|
log = logging.getLogger("cenaforge_voice")
|
|
|
|
|
|
class ApplioRvcEngine:
|
|
def __init__(self, inner: TtsEngine, applio_dir: Path):
|
|
self.inner = inner
|
|
self.name = f"{inner.name}+rvc"
|
|
self._applio_dir = applio_dir
|
|
self._converter = None
|
|
self._lock = threading.Lock()
|
|
|
|
def warmup(self, voice: Voice) -> None:
|
|
if hasattr(self.inner, "warmup"):
|
|
self.inner.warmup(voice)
|
|
|
|
def _get_converter(self):
|
|
with self._lock:
|
|
if self._converter is None:
|
|
if not (self._applio_dir / "rvc" / "infer" / "infer.py").is_file():
|
|
raise RvcUnavailable()
|
|
# O Applio resolve rvc/models/... relativo ao diretório atual.
|
|
os.chdir(self._applio_dir)
|
|
if str(self._applio_dir) not in sys.path:
|
|
sys.path.insert(0, str(self._applio_dir))
|
|
from rvc.infer.infer import VoiceConverter # type: ignore[import-not-found]
|
|
|
|
self._converter = VoiceConverter()
|
|
return self._converter
|
|
|
|
def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm:
|
|
pcm = self.inner.synthesize(voice, text, speed, max_seconds)
|
|
if not voice.rvc:
|
|
return pcm
|
|
rvc = voice.rvc
|
|
converter = self._get_converter()
|
|
with tempfile.TemporaryDirectory(prefix="rvc-") as tmp:
|
|
src, dst = Path(tmp, "in.wav"), Path(tmp, "out.wav")
|
|
soundfile.write(src, pcm.samples, pcm.sample_rate, subtype="PCM_16")
|
|
try:
|
|
self._convert(converter, rvc, src, dst)
|
|
data, rate = soundfile.read(dst, dtype="int16")
|
|
except Exception as exc: # noqa: BLE001 - .pth sem "config"/"weight", índice quebrado, saída ausente
|
|
log.error("rvc: voz=%s falhou (%s: %s)", voice.id, type(exc).__name__, str(exc)[:160])
|
|
raise RvcFailed() from exc
|
|
if data.ndim > 1:
|
|
data = data[:, 0]
|
|
return Pcm(np.ascontiguousarray(data), rate)
|
|
|
|
@staticmethod
|
|
def _convert(converter, rvc: dict, src: Path, dst: Path) -> None:
|
|
converter.convert_audio(
|
|
audio_input_path=str(src),
|
|
audio_output_path=str(dst),
|
|
model_path=str(rvc["modelPath"]),
|
|
index_path=str(rvc["indexPath"]) if rvc.get("indexPath") else "",
|
|
pitch=int(rvc.get("pitch", 0)),
|
|
f0_method=rvc.get("f0Method", "rmvpe"),
|
|
index_rate=float(rvc.get("indexRate", 0.5)),
|
|
protect=float(rvc.get("protect", 0.33)),
|
|
export_format="WAV",
|
|
)
|