Sure! Below is a comprehensive Python script that utilizes Coqui TTS with the XTTS v2 model for cross-lingual text-to-speech synthesis. The script sets up a Gradio interface that allows users to enter text, select a language, and provide an audio reference (mp3 or wav file) to mimic the voice characteristics of the reference audio. The synthesized speech is then output for playback.
# Import necessary libraries
import gradio as gr
from TTS.api import TTS
# Initialize the TTS model
# Using the XTTS v2 model 'YourTTS' which supports cross-lingual synthesis with speaker adaptation
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=True, gpu=False)
# Retrieve the list of supported languages
languages = tts.languages
def synthesize(text, language, audio_reference):
"""
Synthesizes speech from text using a reference audio for speaker adaptation.
Args:
text (str): The text to be synthesized.
language (str): The language code for synthesis.
audio_reference (str): Filepath to the reference audio.
Returns:
Tuple (int, numpy.ndarray): The sample rate and the synthesized audio waveform.
"""
# Check if an audio reference is provided
if audio_reference is not None:
# The audio reference is provided as a file path
audio_path = audio_reference
# Extract the speaker embedding from the reference audio
speaker_embedding = tts.get_speaker_embedding(audio_path)
else:
# If no reference audio is provided, default speaker embedding is used
speaker_embedding = None
# Perform the speech synthesis
wav = tts.tts(text, speaker_embedding=speaker_embedding, language=language)
# Retrieve the sample rate for the synthesized audio
sample_rate = tts.synthesizer.output_sample_rate
# Return the sample rate and audio waveform for playback
return sample_rate, wav
# Create the Gradio interface
iface = gr.Interface(
fn=synthesize,
inputs=[
gr.Textbox(lines=4, label="Text to Synthesize", placeholder="Type the text here..."),
gr.Dropdown(choices=languages, label="Language"),
gr.Audio(source="upload", type="filepath", label="Reference Audio (mp3 or wav)", optional=True)
],
outputs=gr.Audio(type="numpy", label="Synthesized Audio"),
title="Multilingual TTS with Speaker Adaptation",
description=(
"This application uses Coqui TTS to synthesize speech from text in multiple languages. "
"You can provide an audio reference to imitate the speaker's voice. "
"Enter the text you wish to synthesize, select the language, and optionally upload a reference audio file."
),
examples=[
["Hello, how are you?", "en", None],
["Bonjour tout le monde", "fr-fr", None],
["Hola, ¿cómo estás?", "es-es", None]
],
allow_flagging="never"
)
# Launch the Gradio interface
iface.launch()
Explanation:
-
Importing Libraries:
gradio is used for creating the web interface.
TTS.api from Coqui TTS is used to interact with the TTS models.
-
Initializing the TTS Model:
- We use the
YourTTS model (tts_models/multilingual/multi-dataset/your_tts) which supports cross-lingual speech synthesis with speaker adaptation.
progress_bar is set to True for visual feedback during processing.
gpu is set to False to run on CPU. Set to True if you have a compatible GPU for faster processing.
-
Retrieving Supported Languages:
tts.languages provides a list of language codes that the model supports.
-
Defining the Synthesis Function:
- The
synthesize function takes in text, language, and audio_reference.
- If an audio reference is provided, it extracts the speaker embedding using
get_speaker_embedding().
- The
tts() method synthesizes the speech with the provided text, speaker embedding, and language.
- The function returns a tuple of the sample rate and the audio waveform, which Gradio uses to play back the audio.
-
Setting Up the Gradio Interface:
- Inputs:
Textbox for inputting the text.
Dropdown for selecting the language from the supported list.
Audio component for uploading the reference audio file. It is marked as optional.
- Output:
Audio component that plays back the synthesized speech.
- Additional Parameters:
title, description, and examples are provided for a better user experience.
-
Launching the Interface:
iface.launch() starts the Gradio server and opens the interface in a web browser.
Notes:
-
Reference Audio:
- The reference audio should be a clear recording of a single speaker.
- Supported formats are typically
.mp3 and .wav.
- The quality of the speaker adaptation depends on the quality of the reference audio.
-
Language Codes:
- The language codes correspond to the ones supported by the model, such as
'en' for English, 'fr-fr' for French, 'es-es' for Spanish, etc.
- You can print
languages to see the full list of supported languages.
-
Dependencies:
-
Running on GPU:
- If you have a CUDA-compatible GPU, you can set
gpu=True in the TTS initialization for faster synthesis.
-
Error Handling:
- The code assumes that the inputs are valid and doesn't include extensive error handling.
- You might want to add checks for empty text input or unsupported languages for a production environment.
Conclusion:
This script sets up a user-friendly interface for performing cross-lingual text-to-speech synthesis using Coqui TTS. By providing an audio reference, you can adapt the synthesized voice to mimic a specific speaker across different languages supported by the model.
Feel free to run the script and experiment with different texts, languages, and reference audios!