Skip to content

A research project focused on developing a lightweight neural voice cloning and speech synthesis system, optimized for deployment on edge devices.

Python

Keep a raised reference's peak under 0.99

benITo471b6a8f974ba8
diff --git a/reverb_tts/audio.py b/reverb_tts/audio.py
index 75cea7f..aad7f8d 100644
--- a/reverb_tts/audio.py
+++ b/reverb_tts/audio.py
@@ -22,6 +22,7 @@ SILENCE_DB = -50.0 # a 10 ms window quieter than this (dBFS) is silence
# 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.0
+PEAK = 0.99 # the raise stops short of taking the loudest sample past this: the codec was trained on audio within ±1
def load_audio(audio: AudioInput, sample_rate: Optional[int], target_rate: int) -> torch.Tensor:
@@ -47,13 +48,16 @@ def load_audio(audio: AudioInput, sample_rate: Optional[int], target_rate: int)
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."""
+ """
+ The recording raised to LEVEL_DB if it is quieter than QUIET_DB (RMS), otherwise as it is. A loud click in a quiet
+ recording limits the raise, so its peak stays at PEAK.
+ """
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)
+ return audio * min(10 ** (LEVEL_DB / 20) / rms, PEAK / float(audio.abs().max()))
def trim_silence(audio: np.ndarray, sample_rate: int, keep: float = 0.1) -> np.ndarray:
diff --git a/tests/test_audio.py b/tests/test_audio.py
index d738e97..163eeb5 100644
--- a/tests/test_audio.py
+++ b/tests/test_audio.py
@@ -2,7 +2,7 @@ import numpy as np
import pytest
import torch
-from reverb_tts.audio import LEVEL_DB, load_audio, raise_quiet, speech_length, trim_silence
+from reverb_tts.audio import LEVEL_DB, PEAK, load_audio, raise_quiet, speech_length, trim_silence
RATE = 24000
@@ -70,3 +70,12 @@ def test_raise_quiet_levels_only_a_quiet_recording() -> None:
typical = tone * 10 ** (-30 / 20)
assert torch.equal(raise_quiet(typical), typical)
assert torch.equal(raise_quiet(torch.zeros(RATE)), torch.zeros(RATE))
+
+
+def test_raise_quiet_keeps_a_loud_click_under_the_peak() -> None:
+ # A -60 dBFS tone with one click at 0.5: raising it to LEVEL_DB would take the click to ~50, so the peak limits it.
+ tone = torch.sin(2 * torch.pi * 220 * torch.arange(RATE) / RATE) * np.sqrt(2) * 10 ** (-60 / 20)
+ tone[RATE // 2] = 0.5
+ raised = raise_quiet(tone)
+ assert float(raised.abs().max()) == pytest.approx(PEAK, abs=1e-6)
+ assert float(raised.square().mean().sqrt()) > float(tone.square().mean().sqrt()) # still raised, as far as the peak allows