"""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("