Skip to content

A TTS that fits in your CPU (and pocket)

Python

End voice prompts on a short pause (#334)

manu-lmbdac15691170
diff --git a/pocket_tts/data/audio_utils.py b/pocket_tts/data/audio_utils.py
index a01839d..4c469d1 100644
--- a/pocket_tts/data/audio_utils.py
+++ b/pocket_tts/data/audio_utils.py
@@ -26,3 +26,33 @@ def convert_audio(
assert wav.shape[-2] == to_channels
return wav
+
+
+def end_on_pause(
+ wav: torch.Tensor,
+ sample_rate: int,
+ pause_sec: float = 0.08,
+ fade_sec: float = 0.02,
+ floor_db: float = 35.0,
+) -> torch.Tensor:
+ """End a voice prompt [..., T] on exactly `pause_sec` of silence.
+
+ Training prompts end inside the pause between two words. A prompt that stops on speech
+ makes the model continue that utterance (a burst at the start of every chunk, or a wrong
+ first word), and one that ends on a long silence delays the onset. The trailing silence
+ (20 ms frames more than `floor_db` below the loudest one) is cut, the last `fade_sec` of
+ what remains is faded out, and `pause_sec` of zeros is appended.
+ """
+ frame = max(1, int(0.02 * sample_rate))
+ n = wav.shape[-1] // frame
+ if n == 0:
+ return wav
+ rms = wav[..., : n * frame].reshape(-1, n, frame).square().mean(dim=(0, 2)).sqrt()
+ db = 20 * torch.log10(rms + 1e-12)
+ loud = torch.nonzero(db > db.max() - floor_db).flatten()
+ end = (int(loud[-1]) + 1) * frame
+ wav = wav[..., :end].clone()
+ fade = min(int(fade_sec * sample_rate), end)
+ wav[..., end - fade :] *= torch.linspace(1, 0, fade, device=wav.device, dtype=wav.dtype)
+ pause = wav.new_zeros(*wav.shape[:-1], int(pause_sec * sample_rate))
+ return torch.cat([wav, pause], dim=-1)
diff --git a/pocket_tts/models/tts_model.py b/pocket_tts/models/tts_model.py
index 2c10f2c..3f7d37b 100644
--- a/pocket_tts/models/tts_model.py
+++ b/pocket_tts/models/tts_model.py
@@ -20,7 +20,7 @@ from torch.nn import functional as F
from typing_extensions import Self
from pocket_tts.data.audio import audio_read
-from pocket_tts.data.audio_utils import convert_audio
+from pocket_tts.data.audio_utils import convert_audio, end_on_pause
from pocket_tts.default_parameters import (
DEFAULT_EOS_THRESHOLD,
DEFAULT_LANGUAGE,
@@ -1028,6 +1028,7 @@ class TTSModel(nn.Module):
audio, conditioning_sample_rate, self.config.mimi.sample_rate, 1
)
+ audio_conditioning = end_on_pause(audio_conditioning, self.config.mimi.sample_rate)
with display_execution_time("Encoding audio prompt"):
prompt = self._encode_audio(audio_conditioning.unsqueeze(0).to(self.device))
diff --git a/pocket_tts/utils/utils.py b/pocket_tts/utils/utils.py
index 9936831..a1cbe4b 100644
--- a/pocket_tts/utils/utils.py
+++ b/pocket_tts/utils/utils.py
@@ -48,7 +48,7 @@ _ORIGINS_OF_PREDEFINED_VOICES = {
def get_predefined_voice(language: str, name: str) -> str:
- return f"hf://kyutai/pocket-tts-without-voice-cloning/languages/{language}/embeddings/{name}.safetensors@4e1e0a3e611c51c0b4ed8174fc10f32a54644303"
+ return f"hf://kyutai/pocket-tts-without-voice-cloning/languages/{language}/embeddings/{name}.safetensors@069025daa1d6a8a9640bd52581e2a2252f8bede4"
def make_cache_directory() -> Path:
diff --git a/tests/test_end_on_pause.py b/tests/test_end_on_pause.py
new file mode 100644
index 0000000..3a78d72
--- /dev/null
+++ b/tests/test_end_on_pause.py
@@ -0,0 +1,22 @@
+import torch
+
+from pocket_tts.data.audio_utils import end_on_pause
+
+SR = 24000
+
+
+def test_end_on_pause_trims_fades_and_pads():
+ speech = torch.sin(torch.arange(SR) / 5.0)[None] # 1 s of tone
+ fade = int(0.02 * SR)
+ for tail in (0, SR // 2): # a prompt stopping on speech, and one with 0.5 s of silence
+ wav = torch.cat([speech, torch.zeros(1, tail)], dim=-1)
+ out = end_on_pause(wav, SR, pause_sec=0.2)
+ assert out.shape == (1, SR + int(0.2 * SR))
+ assert torch.equal(out[:, : SR - fade], speech[:, : SR - fade])
+ assert out[:, SR - 1].abs() < 1e-6
+ assert out[:, SR:].abs().max() == 0
+
+
+def test_end_on_pause_silent_input_is_returned_whole():
+ wav = torch.zeros(1, 100)
+ assert end_on_pause(wav, SR).shape[-1] >= 100