RVC-Boss/GPT-SoVITS · error · OSError

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

Error message

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

What it means

Webui duplicate of the TTS.py reference-audio length guard: before extracting prompt semantic tokens with the SSL model, it verifies the 16 kHz reference waveform is 3-10 s (48000-160000 samples) and raises OSError (with a gr.Warning toast) when out of range. Quality of zero-shot cloning collapses outside this window, so it is enforced hard.

Source

Thrown at GPT_SoVITS/inference_webui.py:855

    text = text.strip("\n")
    # if (text[0] not in splits and len(get_first(text)) < 4): text = "。" + text if text_language != "en" else "." + text

    print(i18n("实际输入的目标文本:"), text)
    zero_wav = np.zeros(
        int(hps.data.sampling_rate * pause_second),
        dtype=np.float16 if is_half == True else np.float32,
    )
    zero_wav_torch = torch.from_numpy(zero_wav)
    if is_half == True:
        zero_wav_torch = zero_wav_torch.half().to(device)
    else:
        zero_wav_torch = zero_wav_torch.to(device)
    if not ref_free:
        with torch.no_grad():
            wav16k, sr = librosa.load(ref_wav_path, sr=16000)
            if wav16k.shape[0] > 160000 or wav16k.shape[0] < 48000:
                gr.Warning(i18n("参考音频在3~10秒范围外,请更换!"))
                raise OSError(i18n("参考音频在3~10秒范围外,请更换!"))
            wav16k = torch.from_numpy(wav16k)
            if is_half == True:
                wav16k = wav16k.half().to(device)
            else:
                wav16k = wav16k.to(device)
            wav16k = torch.cat([wav16k, zero_wav_torch])
            ssl_content = ssl_model.model(wav16k.unsqueeze(0))["last_hidden_state"].transpose(1, 2)  # .float()
            codes = vq_model.extract_latent(ssl_content)
            prompt_semantic = codes[0, 0]
            prompt = prompt_semantic.unsqueeze(0).to(device)

    t1 = ttime()
    t.append(t1 - t0)

    if how_to_cut == i18n("凑四句一切"):
        text = cut1(text)
    elif how_to_cut == i18n("凑50字一切"):
        text = cut2(text)

View on GitHub (pinned to d523079fc0)

Solutions

  1. Re-select or trim the reference audio to 3-10 seconds of actual speech (5-8 s ideal).
  2. Use the built-in audio-slicing tool (tools/slicer2.py) to cut long material into valid reference clips.
  3. Remove silence at head/tail so all of the counted duration is speech.
  4. Pre-validate duration in your own scripts with soundfile/librosa before invoking the webui function.

Example fix

# before
# in webui: paste 20s song as 参考音频 -> OSError 参考音频在3~10秒范围外

# after: trim with ffmpeg to 6s of speech
# ffmpeg -i long.wav -ss 5 -t 6 -af silenceremove=start_periods=1:start_threshold=-45dB ref.wav
Defensive patterns

Strategy: validation

Validate before calling

import librosa
dur = librosa.get_duration(path=ref_wav_path)
if not (3.0 <= dur <= 10.0):
    raise ValueError(f"reference audio {dur:.1f}s outside 3-10s window — trim or replace")

Try / catch

try:
    prompt = get_cirm(wav16k)  # internal webui path
except OSError as e:
    if "3~10" in str(e):
        gr.Warning("请更换3~10秒的参考音频")
        return

Prevention

When it happens

Trigger: Running inference in the Gradio webui (or the get_phones_and_bert pipeline in inference_webui.py) with a reference/prompt wav whose 16 kHz sample count is <48000 or >160000 and ref_free is False.

Common situations: Short 1-2 s mic recording used as reference; whole song / long narration used as reference; silent padding counted in duration; reference file replaced with a long one but UI not refreshed.

Related errors


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