Skip to content

A TTS that fits in your CPU (and pocket)

Python

Fade in the first 5 ms of each chunk to remove the onset click (#332)

manu-lm523e8a63a388
diff --git a/pocket_tts/models/tts_model.py b/pocket_tts/models/tts_model.py
index 87804e7..4535ce0 100644
--- a/pocket_tts/models/tts_model.py
+++ b/pocket_tts/models/tts_model.py
@@ -511,6 +511,9 @@ class TTSModel(nn.Module):
try:
audio_chunks = []
mimi_state = init_states(self.mimi, batch_size=1, sequence_length=mimi_sequence_length)
+ # A fresh Mimi decoder state puts a small step (about -41 dBFS) in its first samples,
+ # heard as a click at the start of every chunk: fade the first 5 ms in.
+ fade_in: torch.Tensor | None = torch.linspace(0, 1, self.config.mimi.sample_rate // 200)
while True:
latent = latents_queue.get()
if latent is None:
@@ -537,6 +540,10 @@ class TTSModel(nn.Module):
t = time.monotonic()
audio_frame = self.mimi.decode_from_latent(mimi_decoding_input, mimi_state)
+ if fade_in is not None:
+ n = min(fade_in.numel(), audio_frame.shape[-1])
+ audio_frame[..., :n] *= fade_in[:n].to(audio_frame)
+ fade_in = None
increment_steps(
self.mimi, mimi_state, increment=mimi_steps_per_latent * len(latents)
)
diff --git a/tests/test_generation_regressions.py b/tests/test_generation_regressions.py
index 287d12c..ba18d65 100644
--- a/tests/test_generation_regressions.py
+++ b/tests/test_generation_regressions.py
@@ -2,7 +2,7 @@ import queue
import threading
from collections.abc import Iterator
from types import SimpleNamespace
-from typing import NoReturn, cast
+from typing import Any, NoReturn, cast
import pytest
import torch
@@ -106,3 +106,26 @@ def test_generate_reports_autoregressive_errors_before_decoder_done():
)
def test_is_safetensors_source_handles_revisions_and_query_strings(source: str, expected: bool):
assert _is_safetensors_source(source) is expected
+
+
+def test_decode_audio_worker_fades_in_only_the_first_decoded_frame():
+ # A fresh Mimi decoder state starts with a small step, heard as a click at every chunk start.
+ class FakeMimi(torch.nn.Module):
+ def decode_from_latent(self, latent: torch.Tensor, state: object) -> torch.Tensor:
+ return torch.ones(1, 1, 1920 * latent.shape[1])
+
+ model = object.__new__(TTSModel)
+ torch.nn.Module.__init__(model)
+ model.mimi = cast(Any, FakeMimi())
+ model.flow_lm = cast(Any, SimpleNamespace(emb_std=1.0, emb_mean=0.0))
+ model.config = cast(Any, SimpleNamespace(mimi=SimpleNamespace(sample_rate=24000)))
+ model.max_decoder_frames_per_call = 1
+ latents: queue.Queue[torch.Tensor | None] = queue.Queue()
+ results: queue.Queue[tuple[str, Any]] = queue.Queue()
+ for item in (torch.zeros(1, 1, 32), torch.zeros(1, 1, 32), None):
+ latents.put(item)
+ model._decode_audio_worker(latents, results, mimi_sequence_length=8, mimi_steps_per_latent=1)
+ first, second = results.get()[1], results.get()[1]
+ assert first[..., 0].item() == 0.0 and first[..., 119].item() == 1.0
+ assert torch.all(first[..., 120:] == 1.0) and torch.all(second == 1.0)
+ assert results.get() == ("done", None)