"""End-to-end synthetic tests for Synthesizer and audio post-processing.""" from __future__ import annotations from pathlib import Path import tempfile import types import unittest from unittest.mock import patch import numpy as np import soundfile as sf import torch from lua_nova_v1.config import CFMConfig, Config from lua_nova_v1.inference import Synthesizer from lua_nova_v1.models import LuaNovaCFM from lua_nova_v1.text.phonemizer import Phonemized, SYMBOL_TO_ID class _DummyVocoder(torch.nn.Module): def decode_raw_mel(self, phys_mel: torch.Tensor, lengths: torch.Tensor | None = None) -> torch.Tensor: b, t, _ = phys_mel.shape out = torch.sin(torch.linspace(0.0, 20.0, t * 256, device=phys_mel.device))[None].expand(b, -1) * 0.25 return out.contiguous() class SynthesizerTests(unittest.TestCase): def test_synthesizer_speak_and_save_wav(self) -> None: base = Config( channels=32, heads=2, feedforward=64, encoder_layers=1, prosody_layers=1, acoustic_layers=1, predictor_channels=16, vocos_n_mels=100, ) cfg = CFMConfig( base=base, flow_channels=32, flow_layers=1, flow_heads=2, flow_feedforward=64, flow_kernel=3, mel_space="bigvgan_24k_100", ) cfm = LuaNovaCFM(cfg).eval() synth = Synthesizer(cfm=cfm, vocoder=_DummyVocoder(), device="cpu") # type: ignore[arg-type] fake_ph = Phonemized( symbols=("", "o", "l", "a", ""), ids=tuple(SYMBOL_TO_ID[s] for s in ("", "o", "l", "a", "")), stress=(0, 0, 0, 1, 0), unknown=(), ) with patch("lua_nova_v1.inference.synthesizer.phonemize", return_value=fake_ph): wav = synth.speak("Dia 22/03 custou R$ 10,00. Vamos testar?", n_steps=2, sway_coef=-0.5) self.assertEqual(wav.dtype, np.float32) self.assertGreater(len(wav), 256) self.assertTrue(np.isfinite(wav).all()) self.assertGreater(float(np.abs(wav).max()), 0.05) with tempfile.TemporaryDirectory() as tmp: out = Path(tmp) / "out.wav" synth.save_wav(out, wav) read_wav, sr = sf.read(out, dtype="float32") self.assertEqual(sr, 24000) self.assertEqual(len(read_wav), len(wav)) if __name__ == "__main__": unittest.main()