"""Motores de TTS. Cada motor devolve PCM int16 mono; quem codifica é app.audio. Contrato: `synthesize(voice, text, speed, max_seconds)` levanta AudioTooLong assim que o áudio acumulado passa do limite — o custo de CPU para no limite, não depois. Motores: - `PiperEngine`: offline (ONNX, CPU). Licença clara; voz mais "lida". - `EdgeEngine`: Edge TTS (online, Microsoft). Melhor prosódia em pt-BR; precisa de internet. - `KokoroEngine`: Kokoro-82M (offline, ONNX, CPU, Apache-2.0). Um modelo, várias vozes (estilos). - `GoogleEngine`: "voz do Google" (gTTS, online, API não oficial do Google Tradutor). - `AzureEngine`: Azure Speech (online, chave do usuário). Vozes neurais pt-BR que o Edge não expõe. - `CloneEngine`: voz clonada (Chatterbox) pelo processo de clonagem local, num venv próprio (porta 4511). - `RoutingEngine`: escolhe o motor pelo campo `engine` da voz do catálogo. """ from __future__ import annotations import asyncio import threading import time from dataclasses import dataclass from typing import Protocol import numpy as np from .catalog import Voice from .errors import AudioTooLong, CloneUnavailable, EngineUnavailable, InvalidRequest, ServiceError, VoiceNotFound @dataclass(frozen=True) class Pcm: samples: np.ndarray # int16 mono sample_rate: int @property def duration_s(self) -> float: return len(self.samples) / self.sample_rate class TtsEngine(Protocol): name: str def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: ... class PiperEngine: """Piper (ONNX, CPU). Sessões carregadas uma vez por voz, com número de threads fixo.""" name = "piper" def __init__(self, onnx_threads: int): self._threads = onnx_threads self._voices: dict[str, object] = {} self._lock = threading.Lock() def warmup(self, voice: Voice) -> None: self._get(voice) def _get(self, voice: Voice): with self._lock: loaded = self._voices.get(voice.id) if loaded is None: import onnxruntime from piper import PiperVoice loaded = PiperVoice.load(voice.model, config_path=voice.config) options = onnxruntime.SessionOptions() options.intra_op_num_threads = self._threads options.inter_op_num_threads = 1 # Troca a sessão padrão (que usa todos os núcleos) por uma com threads limitadas. loaded.session = onnxruntime.InferenceSession( str(voice.model), sess_options=options, providers=["CPUExecutionProvider"] ) self._voices[voice.id] = loaded return loaded def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: from piper import SynthesisConfig piper_voice = self._get(voice) config = SynthesisConfig(length_scale=1.0 / speed) max_samples = int(max_seconds * voice.sample_rate) chunks: list[np.ndarray] = [] total = 0 rate = voice.sample_rate # O Piper gera por frase; paramos assim que o acumulado passa do limite. for chunk in piper_voice.synthesize(text, syn_config=config): rate = chunk.sample_rate data = np.frombuffer(chunk.audio_int16_bytes, dtype=np.int16) total += len(data) if total > max_samples: raise AudioTooLong() chunks.append(data) if not chunks: raise InvalidRequest("Texto não gerou áudio.") return Pcm(np.concatenate(chunks), rate) def edge_rate(speed: float) -> str: """Velocidade (0,5–2,0) → taxa do Edge TTS ("+25%", "-10%").""" return f"{round((speed - 1.0) * 100):+d}%" class EdgeEngine: """Edge TTS: o texto vai para o serviço da Microsoft e volta como MP3 24 kHz, decodificado em memória. Roda dentro da thread do WorkQueue, que não tem event loop: `asyncio.run` cria um só para esta síntese. """ name = "edge" def __init__(self, timeout_s: float = 60.0): self._timeout_s = timeout_s def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: from .decode import decode_audio try: mp3 = asyncio.run(asyncio.wait_for(self._fetch(voice, text, speed), self._timeout_s)) except ServiceError: raise except Exception as exc: # noqa: BLE001 - rede, DNS, recusa do serviço raise EngineUnavailable() from exc if not mp3: raise InvalidRequest("Texto não gerou áudio.") samples = decode_audio(mp3, voice.sample_rate, max_seconds, sample_format="s16") return Pcm(np.ascontiguousarray(samples), voice.sample_rate) async def _fetch(self, voice: Voice, text: str, speed: float) -> bytes: import edge_tts communicate = edge_tts.Communicate(text, voice.edge_voice, rate=edge_rate(speed)) chunks: list[bytes] = [] async for chunk in communicate.stream(): if chunk["type"] == "audio": chunks.append(chunk["data"]) return b"".join(chunks) class KokoroEngine: """Kokoro-82M via kokoro-onnx. Um só modelo para todas as vozes; a voz é um estilo do voices.bin. Fonemas pelo espeak-ng embutido (`lang` = pt-br). Velocidade nativa (0,5–2,0). """ name = "kokoro" def __init__(self, onnx_threads: int): self._threads = onnx_threads self._models: dict[tuple[str, str], object] = {} self._lock = threading.Lock() def warmup(self, voice: Voice) -> None: self._get(voice) def _get(self, voice: Voice): key = (str(voice.model), str(voice.config)) with self._lock: loaded = self._models.get(key) if loaded is None: import onnxruntime from kokoro_onnx import Kokoro options = onnxruntime.SessionOptions() options.intra_op_num_threads = self._threads options.inter_op_num_threads = 1 # O modelo fp16 gera centenas de avisos inofensivos de "constant folding"; só erros vão ao log. options.log_severity_level = 3 session = onnxruntime.InferenceSession( str(voice.model), sess_options=options, providers=["CPUExecutionProvider"] ) loaded = Kokoro.from_session(session, str(voice.config)) self._models[key] = loaded return loaded def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: kokoro = self._get(voice) try: samples, rate = kokoro.create(text, voice=voice.engine_voice, speed=speed, lang=voice.engine_lang or "pt-br") except (KeyError, ValueError) as exc: raise InvalidRequest(f"Kokoro recusou o texto ou a voz: {exc}") from exc if samples is None or len(samples) == 0: raise InvalidRequest("Texto não gerou áudio.") if len(samples) / rate > max_seconds: raise AudioTooLong() pcm = (np.clip(samples, -1.0, 1.0) * 32767).astype(np.int16) return Pcm(pcm, rate) class GoogleEngine: """gTTS ("voz do Google"): MP3 do Google Tradutor, decodificado em memória. Só tem velocidade normal ou lenta: `speed` ≤ 0,75 usa o modo lento; outros valores saem na velocidade normal. """ name = "google" def __init__(self, timeout_s: float = 60.0): self._timeout_s = timeout_s def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: import io from .decode import decode_audio try: from gtts import gTTS buffer = io.BytesIO() gTTS(text, lang=voice.engine_lang or "pt", tld=voice.engine_voice or "com.br", slow=speed <= 0.75, timeout=self._timeout_s).write_to_fp(buffer) except Exception as exc: # noqa: BLE001 - rede, DNS, recusa do serviço raise EngineUnavailable() from exc mp3 = buffer.getvalue() if not mp3: raise InvalidRequest("Texto não gerou áudio.") samples = decode_audio(mp3, voice.sample_rate, max_seconds, sample_format="s16") return Pcm(np.ascontiguousarray(samples), voice.sample_rate) class AzureEngine: """Azure Speech pela API REST (sem SDK): SSML → WAV PCM 24 kHz mono, decodificado em memória. A chave nunca vai para log nem para resposta. 401/403 = chave ou região erradas; 429 = cota do mês ou do minuto. """ name = "azure" def __init__(self, key: str, region: str, timeout_s: float = 60.0): self._key = key self._region = region self._timeout_s = timeout_s def ssml(self, voice: Voice, text: str, speed: float) -> bytes: from xml.sax.saxutils import escape, quoteattr lang = (voice.engine_voice or "pt-BR-x").rsplit("-", 1)[0] body = ( f"" f"" f"{escape(text)}" ) return body.encode("utf-8") def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: import urllib.error import urllib.request from .decode import decode_audio if not self._key: raise EngineUnavailable() request = urllib.request.Request( f"https://{self._region}.tts.speech.microsoft.com/cognitiveservices/v1", data=self.ssml(voice, text, speed), method="POST", headers={ "Ocp-Apim-Subscription-Key": self._key, "Content-Type": "application/ssml+xml", "X-Microsoft-OutputFormat": "riff-24khz-16bit-mono-pcm", "User-Agent": "cenaforge-voice", }, ) try: with urllib.request.urlopen(request, timeout=self._timeout_s) as response: # noqa: S310 - URL fixa https wav = response.read() except urllib.error.HTTPError as exc: if exc.code == 400: raise InvalidRequest("O Azure recusou o texto ou a voz (confira se a voz existe na sua região).") from exc raise EngineUnavailable() from exc except Exception as exc: # noqa: BLE001 - rede, DNS, timeout raise EngineUnavailable() from exc if not wav: raise InvalidRequest("Texto não gerou áudio.") samples = decode_audio(wav, voice.sample_rate, max_seconds, sample_format="s16") return Pcm(np.ascontiguousarray(samples), voice.sample_rate) class CloneEngine: """Voz clonada: repassa texto + áudio de referência ao processo de clonagem (voice/clone/worker.py). O Chatterbox não tem velocidade; `speed` é ignorado. O processo devolve WAV 24 kHz mono. """ name = "clone" def __init__(self, port: int, key: str, timeout_s: float = 90.0): self._url = f"http://127.0.0.1:{port}/synthesize" self._key = key self._timeout_s = timeout_s def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: import json import urllib.error import urllib.request from .decode import decode_audio body = {"text": text, "reference": str(voice.model), "language": voice.engine_lang or "pt", **(voice.clone or {})} request = urllib.request.Request( self._url, data=json.dumps(body).encode("utf-8"), method="POST", headers={"Content-Type": "application/json", "X-Service-Key": self._key}, ) try: with urllib.request.urlopen(request, timeout=self._timeout_s) as response: # noqa: S310 - 127.0.0.1 fixo wav = response.read() except urllib.error.HTTPError as exc: if exc.code == 400: raise InvalidRequest("A clonagem recusou o texto ou o áudio de referência.") from exc raise CloneUnavailable() from exc except Exception as exc: # noqa: BLE001 - processo fora do ar, porta fechada, timeout raise CloneUnavailable() from exc samples = decode_audio(wav, voice.sample_rate, max_seconds, sample_format="s16") return Pcm(np.ascontiguousarray(samples), voice.sample_rate) class RoutingEngine: """Despacha para o motor da voz (`voice.engine`). Motor ausente = voz fora do catálogo.""" name = "cenaforge-tts" def __init__(self, engines: dict[str, TtsEngine]): self.engines = engines def warmup(self, voice: Voice) -> None: engine = self.engines.get(voice.engine) if engine is not None and hasattr(engine, "warmup"): engine.warmup(voice) def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: engine = self.engines.get(voice.engine) if engine is None: raise VoiceNotFound() return engine.synthesize(voice, text, speed, max_seconds) class FakeEngine: """Motor de teste: tom senoidal, ~15 caracteres por segundo. Pode ser bloqueado para testar a fila.""" name = "fake" def __init__(self, delay_s: float = 0.0): self.delay_s = delay_s self.calls = 0 self.gate: threading.Event | None = None def warmup(self, voice: Voice) -> None: # pragma: no cover - nada a carregar return None def synthesize(self, voice: Voice, text: str, speed: float, max_seconds: float) -> Pcm: self.calls += 1 if self.gate is not None: self.gate.wait(10) if self.delay_s: time.sleep(self.delay_s) seconds = max(0.3, len(text) / (15.0 * speed)) if seconds > max_seconds: raise AudioTooLong() t = np.arange(int(seconds * voice.sample_rate)) / voice.sample_rate samples = (np.sin(2 * np.pi * 220 * t) * 8000).astype(np.int16) return Pcm(samples, voice.sample_rate)