RVC-Boss/GPT-SoVITS · error · OSError

参考音频在3~10秒范围外,请更换!

Error message

参考音频在3~10秒范围外,请更换!

What it means

Raised in _set_prompt_semantic() after loading the reference audio at 16 kHz: the HuBERT/CnHuBERT SSL frontend requires the prompt audio to be between 48000 samples (3 s) and 160000 samples (10 s). Outside this window the extracted semantic tokens degrade badly (too short = not enough voice identity; too long = memory/quality issues), so the loader rejects it with an OSError instead of producing bad audio.

Source

Thrown at GPT_SoVITS/TTS_infer_pack/TTS.py:816

        if self.configs.is_half:
            spec = spec.half()
        if self.is_v2pro == True:
            audio = resample(audio, self.configs.sampling_rate, 16000, self.configs.device)
            if self.configs.is_half:
                audio = audio.half()
        else:
            audio = None
        return spec, audio

    def _set_prompt_semantic(self, ref_wav_path: str):
        zero_wav = np.zeros(
            int(self.configs.sampling_rate * 0.3),
            dtype=np.float16 if self.configs.is_half else np.float32,
        )
        with torch.no_grad():
            wav16k, sr = librosa.load(ref_wav_path, sr=16000)
            if wav16k.shape[0] > 160000 or wav16k.shape[0] < 48000:
                raise OSError(i18n("参考音频在3~10秒范围外,请更换!"))
            wav16k = torch.from_numpy(wav16k)
            zero_wav_torch = torch.from_numpy(zero_wav)
            wav16k = wav16k.to(self.configs.device)
            zero_wav_torch = zero_wav_torch.to(self.configs.device)
            if self.configs.is_half:
                wav16k = wav16k.half()
                zero_wav_torch = zero_wav_torch.half()

            wav16k = torch.cat([wav16k, zero_wav_torch])
            hubert_feature = self.cnhuhbert_model.model(wav16k.unsqueeze(0))["last_hidden_state"].transpose(
                1, 2
            )  # .float()
            codes = self.vits_model.extract_latent(hubert_feature)

            prompt_semantic = codes[0, 0].to(self.configs.device)
            self.prompt_cache["prompt_semantic"] = prompt_semantic

    def batch_sequences(self, sequences: List[torch.Tensor], axis: int = 0, pad_value: int = 0, max_length: int = None):

View on GitHub (pinned to d523079fc0)

Solutions

  1. Trim or re-select the reference audio to be between 3 and 10 seconds of speech (aim for 5-8 s of clean speech).
  2. Use tools/slicer2.py (or the webui audio slicing) to cut a long recording into valid 3-10 s segments and pick the cleanest one.
  3. If your pipeline controls the input, pre-check duration with librosa.get_duration(path=..., ) or soundfile before calling set_ref_audio and reject/trim early.
  4. Strip leading/trailing silence (e.g. with ffmpeg silenceremove or sox) so a '10 second' file actually contains ~10 s of speech within the limit.

Example fix

# before
handler.set_ref_audio("ref.wav")  # OSError: 参考音频在3~10秒范围外

# after
import librosa
dur = librosa.get_duration(path="ref.wav")
assert 3 <= dur <= 10, f"ref audio is {dur:.1f}s, must be 3-10s"
handler.set_ref_audio("ref.wav")
Defensive patterns

Strategy: validation

Validate before calling

import librosa

def valid_ref_duration(path: str, lo: float = 3.0, hi: float = 10.0) -> bool:
    dur = librosa.get_duration(path=path)
    return lo <= dur <= hi

if not valid_ref_duration(ref_wav):
    ref_wav = auto_trim_to(ref_wav, target=6.0)  # or prompt the user

Try / catch

try:
    handler.set_ref_audio(ref_wav)
except OSError as e:
    if "3~10" in str(e):
        ref_wav = trim_or_pick_another(ref_wav)
        handler.set_ref_audio(ref_wav)
    else:
        raise

Prevention

When it happens

Trigger: Calling set_ref_audio(ref_wav_path) (directly or via infer_batch with a new ref_audio_path) where librosa.load(ref_wav_path, sr=16000) returns wav16k.shape[0] > 160000 or < 48000 — audio shorter than 3 seconds or longer than 10 seconds at 16 kHz.

Common situations: User records a 2-second voice sample; user points at a full 30-second song/utterance as reference; trailing silence trimmed too aggressively; wrong file selected (e.g. an hour-long podcast).

Related errors


AI-assisted analysis of RVC-Boss/GPT-SoVITS@d523079fc0 (2026-08-15). Data as JSON: /api/errors/5c05eec75a3868a3. Report an issue: GitHub.