Skip to main content

Overview

The voice cloning workflow involves extracting features from reference audio and reusing them for multiple generations. The create_voice_clone_prompt() method and VoiceClonePromptItem dataclass provide this functionality. Key benefits:
  • Extract voice features once, generate many times
  • Avoid redundant audio processing
  • Enable batch voice cloning with different voices

VoiceClonePromptItem

Container for one sample’s voice-clone prompt information that can be fed to the model. Fields are aligned with Qwen3TTSForConditionalGeneration.generate(..., voice_clone_prompt=...). Source: qwen_tts/inference/qwen3_tts_model.py:40-52

Fields

Optional[torch.Tensor]
Reference audio codes extracted by the speech tokenizer.
  • Shape: (T, Q) or (T,) depending on tokenizer (25Hz/12Hz)
  • None when x_vector_only_mode=True
torch.Tensor
Speaker embedding vector extracted from reference audio.Shape: (D,) where D is the embedding dimension.
bool
Whether to use speaker embedding only (ignores ref_code and ref_text).
  • True: X-vector only mode - only speaker embedding is used
  • False: ICL mode - uses both embedding and reference codes/text
bool
Whether ICL (In-Context Learning) mode is enabled.Always the inverse of x_vector_only_mode:
  • True when x_vector_only_mode=False
  • False when x_vector_only_mode=True
Optional[str]
Transcription of the reference audio.
  • Required when icl_mode=True (x_vector_only_mode=False)
  • Ignored when x_vector_only_mode=True

Example


create_voice_clone_prompt

Build voice-clone prompt items from reference audio (and optionally reference text) using the Base model. Model Type: Base only Source: qwen_tts/inference/qwen3_tts_model.py:355-458

Modes

X-vector Only Mode (x_vector_only_mode=True)

  • Only speaker embedding is used to clone voice
  • ref_text and ref_code are ignored
  • Mutually exclusive with ICL mode
  • Faster and simpler, but may be less accurate

ICL Mode (x_vector_only_mode=False)

  • ICL (In-Context Learning) mode is enabled automatically
  • Both speaker embedding and reference codes/text are used
  • ref_text is required in this mode
  • More accurate voice cloning

Parameters

Union[AudioLike, List[AudioLike]]
required
Reference audio(s) used to extract:
  • ref_code via model.speech_tokenizer.encode(...)
  • ref_spk_embedding via model.extract_speaker_embedding(...) (resampled to 24kHz)
Supported formats:
  • str: Local wav path, URL, or base64 audio string
  • (np.ndarray, sr): Tuple of waveform + sampling rate
  • List of the above
Example:
  • "reference.wav"
  • "https://example.com/audio.wav"
  • (audio_array, 24000)
  • ["ref1.wav", "ref2.wav"]
Optional[Union[str, List[Optional[str]]]]
default:"None"
Reference transcript(s) - transcription of the reference audio.Required when x_vector_only_mode=False (ICL mode).Can be:
  • Single string (applied to all audio)
  • List of strings matching the length of ref_audio
  • None or empty string when x_vector_only_mode=True
Union[bool, List[bool]]
default:"False"
Whether to use speaker embedding only.
  • True: X-vector only mode (no ref_text required)
  • False: ICL mode (requires ref_text)
Can be:
  • Single boolean (applied to all audio)
  • List of booleans matching the length of ref_audio

Batch Behavior

  • ref_audio can be a single item or a list
  • ref_text and x_vector_only_mode can be scalars or lists
  • If any of them are lists with length > 1, all lists must match in length

Returns

List[VoiceClonePromptItem]
List of prompt items that can be passed to generate_voice_clone(voice_clone_prompt=...).Each item contains extracted voice features ready for synthesis.

Raises

  • ValueError - If x_vector_only_mode=False but ref_text is missing
  • ValueError - If batch lengths mismatch
  • ValueError - If the model is not a Base model

Example

Audio Input Formats

Both create_voice_clone_prompt() and generate_voice_clone() support flexible audio input:

Local File Path

URL

Base64

NumPy Array

Best Practices

1. Reuse Prompts

Create prompts once and reuse them for multiple generations:

2. Choose the Right Mode

  • ICL Mode (x_vector_only_mode=False): Better quality, requires reference text
  • X-vector Only Mode (x_vector_only_mode=True): Faster, no reference text needed

3. Reference Audio Quality

  • Use clean, high-quality reference audio (minimal background noise)
  • 3-10 seconds of speech is usually sufficient
  • Ensure the reference text exactly matches the audio (for ICL mode)

See Also