From 7699753a7c2716c6456becabcc0fc1ab60f9dbdd Mon Sep 17 00:00:00 2001 From: Cutshion Date: Fri, 23 Oct 2026 17:50:56 +0900 Subject: [PATCH] Fix voice changing between sentences Co-Authored-By: Claude Opus 5.5 --- audio/mouth.py | 19 +++++++++++++++++++ docs/TUNING.md | 1 + tests/test_mouth.py | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/audio/mouth.py b/audio/mouth.py index 77b31bd..b7a2cfd 100644 --- a/audio/mouth.py +++ b/audio/mouth.py @@ -28,6 +28,9 @@ ROOT = pathlib.Path(__file__).resolve().parents[1] # the project folder DEVICE = "plughw:CARD=Device,DEV=0" # "USB Audio Device" VOICE = "F1" # F1-F5 female, M1-M5 male STEPS = 4 # quality/speed: 4 fast ... 8 best +NOISE_SEED = 7 # Supertonic starts every sentence from random noise; with 4 steps that + # noise sets the pitch (one sentence rendered 4 times: up to 24 Hz apart, + # 2026-10-23). One fixed noise for every sentence: the same voice always SPEED = 0.92 # < 1 = slower; 1.05 sounded rushed SPEED_MIN, SPEED_MAX = 0.72, 1.2 # what "speak slower / faster" may set (Supertonic takes 0.7 to 2.0) SPLIT_WORDS = 8 # TTS on the CPU only: a first sentence this long is said in two parts, @@ -110,6 +113,21 @@ def _set_volume(): _volume_set = True +def _fixed_noise(model): + """Every sentence starts from the same noise (the first frames of one fixed bank), not np.random's.""" + from supertonic.core import get_latent_mask + bank = np.random.default_rng(NOISE_SEED).standard_normal( + (model.ldim * model.chunk_compress_factor, 2048)).astype(np.float32) # 2048 latent frames: ~140 s of speech + + def sample_noisy_latent(duration): + wav_lengths = (duration * model.sample_rate).astype(np.int64) + chunk = model.base_chunk_size * model.chunk_compress_factor + n = int((duration.max() * model.sample_rate + chunk - 1) / chunk) + mask = get_latent_mask(wav_lengths, model.base_chunk_size, model.chunk_compress_factor) + return np.repeat(bank[None, :, :n], len(duration), 0) * mask, mask + model.sample_noisy_latent = sample_noisy_latent + + def _load(): global _tts, _style, _gpu _set_volume() @@ -119,6 +137,7 @@ def _load(): _tts = TTS(model="supertonic-3", model_dir=ROOT / "models/supertonic3", auto_download=False, intra_op_num_threads=8) _style = _tts.get_voice_style(VOICE) + _fixed_noise(_tts.model) try: tts_gpu.use(_tts.model) _gpu = True diff --git a/docs/TUNING.md b/docs/TUNING.md index 66589a1..d1ef15a 100644 --- a/docs/TUNING.md +++ b/docs/TUNING.md @@ -155,6 +155,7 @@ Every value here is a constant at the top of its file, with a comment on what it | `audio/mouth.py` | `SPEED`, `SPEED_MIN`, `SPEED_MAX` | 0.92, 0.72, 1.2 | Speaking speed without a stored value, and the limits (Supertonic takes 0.7–2.0) | | `audio/mouth.py` | `SPLIT_WORDS` | 8 words | TTS on the CPU only (GPU engines not loaded): a first sentence this long is said in two parts, cut after its first comma (3 words in or later), so the voice starts ~1 s sooner. On the GPU a sentence is said whole | | `audio/mouth.py` | `STEPS` | 4 | Supertonic synthesis steps (quality / speed). On the GPU a step takes a few ms, on the CPU 0.1–0.27 s | +| `audio/mouth.py` | `NOISE_SEED` | 7 | The fixed noise every sentence starts from (one voice for every sentence; another seed is a slightly different voice) | | `audio/mouth.py` | `ECHO_MIN`, `ECHO_TOP_DB`, `ECHO_MIN_FRAMES`, `ECHO_LAG_S`, `ECHO_KEEP_S` | 0.6, 15 dB, 12, −0.3…1.0 s, 30 s | Lumi's own voice in the clip mic: a clip-mic utterance whose spectrogram (16 bands, its frames within `ECHO_TOP_DB` of its loudest, at least `ECHO_MIN_FRAMES`) correlates at least `ECHO_MIN` with what the speaker played `ECHO_LAG_S` earlier is dropped before whisper (`mouth.own_voice`; check: `tests/echo_check.py`). Estimate | | `audio/tts_gpu.py` | `MAX_LEN` | 640 | Longest text / latent (~44 s of speech) the TensorRT engines take; longer goes to the CPU. A change needs `tts_gpu.py build` | | `audio/ears.py` | `WHISPER_MODEL` | `ggml-large-v3-turbo.bin` (f16) | Speech-to-text model; takes effect when the whisper-server is started again (check: `tests/asr_check.py`) | diff --git a/tests/test_mouth.py b/tests/test_mouth.py index 8600496..1176c6a 100644 --- a/tests/test_mouth.py +++ b/tests/test_mouth.py @@ -53,4 +53,35 @@ assert mouth.echo((heard * 32767).astype(np.int16), 102.9) >= mouth.ECHO_MIN visitor = other * 0.5 + played[8000:40000] * 0.05 # a visitor close to the mic, Lumi 20 dB lower assert mouth.echo((visitor * 32767).astype(np.int16), 102.9) < mouth.ECHO_MIN assert mouth.echo((heard * 32767).astype(np.int16), 106.0) is None # said 1 s after Lumi stopped -print(f"ok: {len(CUTS)} cuts, the prompt, the prefill, own voice") + +# one voice: every sentence (English, Korean, short, long) is synthesised with the same style, steps and speed, and +# starts from the same noise, so the same sentence always sounds the same and sentences don't change pitch +calls = [] + + +class FakeTTS: + def synthesize(self, text, **kw): + calls.append(kw) + return np.zeros((1, 10), np.float32), None + + +mouth._tts, mouth._style = FakeTTS(), object() +for s in ["Hello.", "I can talk with you in English and Korean, answer questions, and guide you.", "네, 갈게요.", + "오늘은 날씨가 맑고 따뜻해요, 그래서 산책하기 좋은 날이에요."]: + mouth._synth(s, mouth.lang_of(s)) +assert all({k: v for k, v in c.items() if k != "lang"} == {k: v for k, v in calls[0].items() if k != "lang"} + for c in calls), calls +assert all(c["voice_style"] is mouth._style for c in calls) + + +class FakeModel: + ldim, chunk_compress_factor, base_chunk_size, sample_rate = 4, 2, 512, 44100 + + +m = FakeModel() +mouth._fixed_noise(m) +a, _ = m.sample_noisy_latent(np.array([1.0])) +b, _ = m.sample_noisy_latent(np.array([1.0])) +c, _ = m.sample_noisy_latent(np.array([2.5])) +assert np.array_equal(a, b) and np.array_equal(a, c[:, :, :a.shape[2]]) # same noise, a longer one goes on from it +print(f"ok: {len(CUTS)} cuts, the prompt, the prefill, own voice, one voice")