File size: 2,612 Bytes
d4101a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | """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=("<bos>", "o", "l", "a", "<eos>"),
ids=tuple(SYMBOL_TO_ID[s] for s in ("<bos>", "o", "l", "a", "<eos>")),
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()
|