A research project focused on developing a lightweight neural voice cloning and speech synthesis system, optimized for deployment on edge devices.
Python
Fix unusualy silent references handling
IgorSwat164526034e32
diff --git a/reverb_tts/audio.py b/reverb_tts/audio.pyindex 0ee5ea2..75cea7f 100644--- a/reverb_tts/audio.py+++ b/reverb_tts/audio.py@@ -18,6 +18,10 @@ RESAMPLE = {"lowpass_filter_width": 64, "rolloff": 0.9475937167399596, "resampliAudioInput = Union[str, Path, np.ndarray, torch.Tensor]SILENCE_DB = -50.0 # a 10 ms window quieter than this (dBFS) is silence+# A reference quieter than QUIET_DB (RMS, dBFS) is raised to LEVEL_DB before it is encoded: its takes come out as quiet+# as it is, and trim_silence would then take their soft speech for silence and cut words away.+QUIET_DB = -40.0+LEVEL_DB = -20.0def load_audio(audio: AudioInput, sample_rate: Optional[int], target_rate: int) -> torch.Tensor:@@ -42,6 +46,16 @@ def load_audio(audio: AudioInput, sample_rate: Optional[int], target_rate: int)return wav+def raise_quiet(audio: torch.Tensor) -> torch.Tensor:+ """The recording raised to LEVEL_DB if it is quieter than QUIET_DB (RMS), otherwise as it is."""++ rms = float(audio.square().mean().sqrt()) if audio.numel() else 0.0+ if rms == 0.0 or 20 * np.log10(rms) >= QUIET_DB:+ return audio++ return audio * (10 ** (LEVEL_DB / 20) / rms)++def trim_silence(audio: np.ndarray, sample_rate: int, keep: float = 0.1) -> np.ndarray:"""Cuts leading and trailing silence (10 ms windows under SILENCE_DB), keeping `keep` seconds of it on each side.diff --git a/reverb_tts/tts.py b/reverb_tts/tts.pyindex dc47a07..bf4197d 100644--- a/reverb_tts/tts.py+++ b/reverb_tts/tts.py@@ -18,7 +18,7 @@ import torchfrom huggingface_hub import hf_hub_download, list_repo_files, snapshot_downloadfrom transformers import HiggsAudioV2TokenizerModel, Qwen2Tokenizer-from reverb_tts.audio import AudioInput, load_audio, speech_length, trim_silence+from reverb_tts.audio import AudioInput, load_audio, raise_quiet, speech_length, trim_silencefrom reverb_tts.model import CODEBOOKS, MASK, Networkfrom reverb_tts.sampler import Batch, Prompt, SamplerConfig, samplefrom reverb_tts.text import check_language, estimate_frames, prepare_text, split_text@@ -183,7 +183,7 @@ class TTS:"""check_language(language)- wav = load_audio(audio, sample_rate, self.sample_rate)+ wav = raise_quiet(load_audio(audio, sample_rate, self.sample_rate))# Whole codec frames, as the training references were: otherwise the codes can come out misaligned.wav = torch.nn.functional.pad(wav, (0, -wav.numel() % self.codec.config.hop_length))codes = self.codec.encode(wav[None, None].to(self.device)).audio_codes[0].long().cpu()diff --git a/tests/test_audio.py b/tests/test_audio.pyindex 4127a26..d738e97 100644--- a/tests/test_audio.py+++ b/tests/test_audio.py@@ -2,7 +2,7 @@ import numpy as npimport pytestimport torch-from reverb_tts.audio import load_audio, speech_length, trim_silence+from reverb_tts.audio import LEVEL_DB, load_audio, raise_quiet, speech_length, trim_silenceRATE = 24000@@ -60,3 +60,13 @@ def test_speech_length_levels_a_quiet_recording_first() -> None:def test_speech_length_counts_a_silent_or_tiny_recording_whole() -> None:assert speech_length(quiet(2), RATE) == 2assert speech_length(np.zeros(10, np.float32), RATE) == 10 / RATE+++def test_raise_quiet_levels_only_a_quiet_recording() -> None:+ tone = torch.sin(2 * torch.pi * 220 * torch.arange(RATE) / RATE) * np.sqrt(2) # RMS 1+ def db(x: torch.Tensor) -> float:+ return 20 * float(torch.log10(x.square().mean().sqrt()))+ assert db(raise_quiet(tone * 10 ** (-60 / 20))) == pytest.approx(LEVEL_DB, abs=0.01)+ typical = tone * 10 ** (-30 / 20)+ assert torch.equal(raise_quiet(typical), typical)+ assert torch.equal(raise_quiet(torch.zeros(RATE)), torch.zeros(RATE))