mirror of
https://github.com/debpalash/VoiceStudio.git
synced 2026-12-04 01:18:16 +00:00
Review fixes for #4564: a clip a few hundredths over the cap skipped the cut and kept its transcript; the capped marker is published before the cache entry; prep logs name files, not full paths; the eviction's ignored OSError says why.
133 lines
4.3 KiB
Python
133 lines
4.3 KiB
Python
"""#4559: CosyVoice's speech tokenizer asserts on prompt audio over 30 s."""
|
|
import importlib
|
|
import os
|
|
|
|
import soundfile as sf
|
|
import torch
|
|
|
|
os.environ.setdefault("OMNIVOICE_MODEL", "test")
|
|
os.environ.setdefault("OMNIVOICE_DISABLE_FILE_LOG", "1")
|
|
|
|
SR = 16_000
|
|
|
|
|
|
def _tts():
|
|
return importlib.import_module("services.tts_backend")
|
|
|
|
|
|
def _wav(path, seconds):
|
|
sf.write(path, torch.full((int(seconds * SR),), 0.1).numpy(), SR)
|
|
return str(path)
|
|
|
|
|
|
def _dur(path):
|
|
info = sf.info(path)
|
|
return info.frames / info.samplerate
|
|
|
|
|
|
def test_long_reference_is_capped_and_transcript_dropped(tmp_path):
|
|
kw = {"ref_audio": _wav(tmp_path / "long.wav", 45), "ref_text": "whole clip"}
|
|
_tts().prepare_cosyvoice_reference(kw)
|
|
assert _dur(kw["ref_audio"]) < 30
|
|
assert kw["ref_text"] is None
|
|
|
|
|
|
def test_short_reference_untouched(tmp_path):
|
|
raw = _wav(tmp_path / "short.wav", 10)
|
|
kw = {"ref_audio": raw, "ref_text": "short"}
|
|
_tts().prepare_cosyvoice_reference(kw)
|
|
assert kw == {"ref_audio": raw, "ref_text": "short"}
|
|
|
|
|
|
def test_prep_failure_is_fail_open(tmp_path):
|
|
kw = {"ref_audio": str(tmp_path / "missing.wav"), "ref_text": "x"}
|
|
_tts().prepare_cosyvoice_reference(kw)
|
|
assert kw["ref_audio"] == str(tmp_path / "missing.wav")
|
|
assert kw["ref_text"] == "x"
|
|
|
|
|
|
def test_voxcpm_cap_unchanged_by_cosyvoice_cache(tmp_path):
|
|
raw = _wav(tmp_path / "long.wav", 45)
|
|
tts = _tts()
|
|
tts._prepare_voxcpm_ref(raw, max_s=25.0)
|
|
assert 29 < _dur(tts._prepare_voxcpm_ref(raw)) <= 30.1
|
|
|
|
|
|
def test_inprocess_backend_caps_before_inference(tmp_path):
|
|
tts = _tts()
|
|
seen = {}
|
|
|
|
class Model:
|
|
sample_rate = 24000
|
|
|
|
def inference_cross_lingual(self, text, ref, stream=False):
|
|
seen["ref"] = ref
|
|
yield {"tts_speech": torch.zeros(1, 10)}
|
|
|
|
def inference_zero_shot(self, *a, **k):
|
|
seen["zero_shot"] = True
|
|
return iter(())
|
|
|
|
b = tts.CosyVoiceBackend()
|
|
b._model = Model()
|
|
b.generate("hello", ref_audio=_wav(tmp_path / "long.wav", 45), ref_text="t",
|
|
language="en")
|
|
assert "zero_shot" not in seen
|
|
assert _dur(seen["ref"]) < 30
|
|
|
|
|
|
def test_sidecar_adapter_caps_before_exchange(tmp_path, monkeypatch):
|
|
mod = importlib.import_module("engines.cosyvoice_subprocess")
|
|
sent = {}
|
|
from services.subprocess_backend import SubprocessBackend
|
|
|
|
def fake(self, text, **kw):
|
|
sent.update(kw)
|
|
return "ok"
|
|
|
|
monkeypatch.setattr(SubprocessBackend, "generate", fake)
|
|
b = mod.CosyVoiceSubprocessBackend.__new__(mod.CosyVoiceSubprocessBackend)
|
|
b.generate("hi", ref_audio=_wav(tmp_path / "long.wav", 45), ref_text="t")
|
|
assert _dur(sent["ref_audio"]) < 30
|
|
assert sent["ref_text"] is None
|
|
|
|
|
|
def test_long_reference_soundfile_cannot_read_is_still_capped(tmp_path, monkeypatch):
|
|
"""M4A/AAC: soundfile fails, so the clip decodes through audio_io like the sidecar does."""
|
|
tts = _tts()
|
|
import services.audio_io as audio_io
|
|
|
|
clip = tmp_path / "long.m4a"
|
|
clip.write_bytes(b"not-a-wav")
|
|
monkeypatch.setattr(audio_io, "load_audio",
|
|
lambda path: (torch.full((1, 45 * SR), 0.1), SR))
|
|
kw = {"ref_audio": str(clip), "ref_text": "whole clip"}
|
|
tts.prepare_cosyvoice_reference(kw)
|
|
assert kw["ref_audio"] != str(clip) and _dur(kw["ref_audio"]) < 30
|
|
assert kw["ref_text"] is None
|
|
|
|
|
|
def test_prepared_copies_are_bounded(tmp_path, monkeypatch):
|
|
tts = _tts()
|
|
monkeypatch.setattr(tts, "_PREPARED_REF_LIMIT", 2)
|
|
monkeypatch.setattr(tts, "_VOXCPM_REF_PREP_CACHE", {})
|
|
monkeypatch.setattr(tts, "_VOXCPM_CAPPED_REFS", set())
|
|
prepared = []
|
|
for i in range(4):
|
|
kw = {"ref_audio": _wav(tmp_path / f"long{i}.wav", 40)}
|
|
tts.prepare_cosyvoice_reference(kw)
|
|
prepared.append(kw["ref_audio"])
|
|
assert [os.path.exists(p) for p in prepared] == [False, False, True, True]
|
|
assert len(tts._VOXCPM_REF_PREP_CACHE) == 2
|
|
for path in prepared[2:]:
|
|
os.remove(path)
|
|
|
|
|
|
def test_reference_just_over_the_cap_is_still_cut_and_transcript_dropped(tmp_path):
|
|
"""A clip 0.05 s over the cap used to hit the "nothing to cut" no-op path."""
|
|
tts = _tts()
|
|
kw = {"ref_audio": _wav(tmp_path / "edge.wav", tts._COSYVOICE_REF_MAX_S + 0.05), "ref_text": "whole clip"}
|
|
tts.prepare_cosyvoice_reference(kw)
|
|
assert _dur(kw["ref_audio"]) <= tts._COSYVOICE_REF_MAX_S + 0.01
|
|
assert kw["ref_text"] is None
|