Skip to main content

Speech Generation

Note

Speech generation supports multiple model families, including SpeechT5, Kokoro, and Qwen3-TTS. The generated audio signal is a single-channel (mono) waveform, and the sampling rate depends on the model (for example, SpeechT5 outputs 16 kHz and Qwen3-TTS outputs 24 kHz).

Convert and Optimize Model​

Download and convert model (for example, speecht5_tts) to OpenVINO format from Hugging Face. The command below uses SpeechT5 as an example; SpeechT5 requires specifying a vocoder via --model-kwargs:

optimum-cli export openvino --model microsoft/speecht5_tts --weight-format int4 --model-kwargs '{"vocoder":"microsoft/speecht5_hifigan"}' speecht5_tts

See all supported Speech Generation Models.

info

Refer to the Model Preparation guide for detailed instructions on how to download, convert and optimize models for OpenVINO GenAI.

Run Model Using OpenVINO GenAI​

The Text2SpeechPipeline is the main object for generating speech from text. It automatically loads the TTS model and vocoder from the converted model directory. The output sample rate is model-dependent and is returned in the generation result.

import openvino_genai
import numpy as np
import soundfile as sf

pipeline = openvino_genai.Text2SpeechPipeline(model_path, "CPU")
generation_config = pipeline.get_generation_config()

# Generate audio using the default speaker
result = pipeline.generate("Hello OpenVINO GenAI", generation_config=generation_config)

# speech tensor contains the waveform of the spoken phrase
speech_data = np.array(result.speeches[0].data).reshape(-1)
sf.write("output_audio.wav", speech_data, samplerate=result.output_sample_rate)
tip

Use CPU or GPU as devices without any other code change.

Additional Usage Options​

tip

Check out the Python, C++, C, and Node.js speech generation samples. The C sample writes a WAV file; the C example above writes raw float32 samples.

Use Speaker Embedding File​

To generate speech using the SpeechT5 model, you can specify a target voice by providing a speaker embedding file.

This file must contain 512 32-bit floating-point values that represent the voice characteristics of the target speaker. The model will use these characteristics to synthesize the input text in the specified voice.

If no speaker embedding is provided, the model uses the default built-in speaker.

You can generate a speaker embedding using the create_speaker_embedding.py script. This script records 5 seconds of audio from your microphone and extracts a speaker embedding vector from the recording.

python create_speaker_embedding.py
import openvino_genai
import openvino as ov
import numpy as np
import soundfile as sf

pipeline = openvino_genai.Text2SpeechPipeline(model_path, "CPU")

speaker_embedding = np.fromfile(args.speaker_embedding_file_path, dtype=np.float32).reshape(1, 512)
speaker_embedding = ov.Tensor(speaker_embedding)
result = pipeline.generate("Hello OpenVINO GenAI", speaker_embedding)

speech = result.speeches[0]
sf.write("output_audio.wav", speech.data[0], samplerate=result.output_sample_rate)

Performance Metrics​

Text2SpeechPipeline returns Text2SpeechDecodedResults with a perf_metrics field of type SpeechGenerationPerfMetrics, which extends PerfMetrics.

In addition to common metrics, SpeechGenerationPerfMetrics provides:

MetricTypeDescription
num_generated_samplesintegerNumber of audio samples generated