Files
javis_bot/tests/test_voice_worker_melo_speed.py
javis-bot 27eb1315b9 fix(tts): MeloTTS speaks up to 1.25 itself so fast speech sounds natural
Stretching all of the speed-up from 1.0 kept every sound but sounded
awkward (UTMOS 3.13 at 1.5, against 3.73 for MeloTTS's own 1.5), while
MeloTTS's own speed past 1.25 drops sounds (sentence-final "요" and first
syllables, 4.6% character errors at 1.5). MeloTTS now speaks up to 1.25
and the WSOLA stretch does only the rest, with a 5 ms search: 2.6% and
UTMOS 3.56 at 1.5 through the real child. Version 0.1.6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 00:06:52 +09:00

90 lines
3.0 KiB
Python

"""How the MeloTTS child speeds speech up: the model never speaks faster than MODEL_SPEED_MAX (faster
drops sounds), and the rest is a time stretch that keeps the pitch. And the engine sends its current speed
with every sentence."""
from __future__ import annotations
import json
import os
import sys
import textwrap
import numpy as np
import pytest
from voice_worker import melo_child, tts_melo
from voice_worker.melo_child import MODEL_SPEED_MAX, split_speed, stretch
SR = 44100
def sine(hz: float, seconds: float) -> np.ndarray:
return np.sin(2 * np.pi * hz * np.arange(int(SR * seconds)) / SR)
@pytest.mark.unit
def test_the_model_never_speaks_faster_than_its_limit_and_the_stretch_does_the_rest():
for speed in (0.5, 0.8, MODEL_SPEED_MAX):
assert split_speed(speed) == (speed, 1.0)
for speed in (1.5, 1.75, 2.0):
model, factor = split_speed(speed)
assert model == MODEL_SPEED_MAX and factor > 1.0
assert model * factor == pytest.approx(speed)
@pytest.mark.unit
def test_stretch_shortens_by_the_factor_and_keeps_the_pitch():
x = sine(220.0, 2.0)
for factor in (1.2, 1.6, 2.0):
y = stretch(x, factor, SR)
assert len(y) == pytest.approx(len(x) / factor, abs=2)
spectrum = np.abs(np.fft.rfft(y * np.hanning(len(y))))
assert np.argmax(spectrum) * SR / len(y) == pytest.approx(220.0, abs=2.0)
@pytest.mark.unit
def test_stretched_sound_stays_even_without_dips_or_clicks():
# Badly joined pieces cancel each other (the level dips) or jump (clicks).
x = 0.5 * sine(180.0, 1.5)
y = stretch(x, 1.6, SR)
middle = y[SR // 10: -SR // 10]
frame = SR // 100
levels = [np.sqrt(np.mean(middle[i:i + frame] ** 2)) for i in range(0, len(middle) - frame, frame)]
assert min(levels) > 0.5 / np.sqrt(2) * 0.9 and max(levels) < 0.5 / np.sqrt(2) * 1.1
assert np.max(np.abs(np.diff(middle))) < 2 * np.pi * 180.0 / SR * 0.5 * 1.5
@pytest.mark.unit
def test_no_stretch_leaves_the_sound_as_it_is():
x = sine(300.0, 0.5)
assert np.array_equal(stretch(x, 1.0, SR), x)
assert len(stretch(np.zeros(0), 1.5, SR)) == 0
FAKE_CHILD = textwrap.dedent("""
import json, struct, sys
out = sys.stdout.buffer
def reply(h, payload=b""):
raw = json.dumps(h).encode()
out.write(struct.pack("<I", len(raw)) + raw + payload); out.flush()
reply({"ready": True, "rate": 44100})
for line in sys.stdin:
req = json.loads(line)
n = int(req["speed"] * 100)
reply({"ok": True, "samples": n}, b"\\x00\\x00" * n)
""")
@pytest.mark.unit
def test_the_engine_sends_its_current_speed_with_every_sentence(tmp_path, monkeypatch):
child = tmp_path / "fake_child.py"
child.write_text(FAKE_CHILD)
monkeypatch.setattr(tts_melo, "CHILD", str(child))
engine = tts_melo.MeloEngine(sys.executable, "cpu", 1.5)
try:
assert len(engine.synthesize("하나", 48000)) == 150
engine.speed = 1.8
assert len(engine.synthesize("둘", 48000)) == 180
finally:
engine.close()