| gTTS (Google Text-to-Speech) is a free and easy-to-use Python library that allows you to convert text into spoken audio using Google's powerful text-to-speech engine. Its benefits include simple implementation, support for multiple languages and accents, customizable speech parameters like speed and pitch, and the ability to save the output as MP3 files, making it ideal for projects requiring audio generation, accessibility features, or interactive applications. gTTS, a text-to-speech library, offers a variety of voice types, but the specific options vary depending on the language selected. While some languages may have a wider range of voices, including male, female, and even different accents, others might only offer a single default voice. You can explore the available voices for a particular language by using the `languages()` method and then selecting the desired voice within the `t2s()` method. To create a voice (speech) file using Google Text-to-Speech (gTTS) in Python, you can use the following code. This will convert text to speech and save it as an audio file (e.g., in MP3 format). First, install gTTS if you haven't already: pip install gTTS
Then, use this code to create a voice file: from gtts import gTTS
# Text you want to convert to audio
text = "Hello, this is a sample text converted to speech using gTTS in Python."
# Specify language (e.g., 'en' for English, 'es' for Spanish)
language = 'en'
# Create a gTTS object
tts = gTTS(text=text, lang=language, slow=False)
# Save the audio file
tts.save("output.mp3")
print("Voice file created successfully!")
Explanation text: The text you want to convert to speech.lang: Set the language code for the text (e.g., 'en' for English).slow=False: Controls the speed; False for normal speed, True for slower speed.save("output.mp3"): Saves the output file as output.mp3 in the working directory.
Once the script is run, you’ll get an output.mp3 file with the spoken text. You can adjust the text and language as needed. If you have an API key for GCP(Google Cloud Platform), you can do more: The gTTS library itself doesn’t directly provide a list of available voices, as it only supports specifying language codes (like 'en' for English, 'fr' for French, etc.) and a speed setting (slow=True/False). It defaults to using Google's standard voice for each language. However, Google Text-to-Speech does support various voices, but to access them, you would need to use the Google Cloud Text-to-Speech API, which provides more control over different voices and accents. If you’d like a sample code for listing voices using the Google Cloud Text-to-Speech API, here’s how: - First, set up the Google Cloud Text-to-Speech API on Google Cloud Console.
Install the google-cloud-texttospeech library: pip install google-cloud-texttospeech
Then, use the following code to list available voices: from google.cloud import texttospeech
# Initialize the client
client = texttospeech.TextToSpeechClient()
# Retrieve the list of voices
voices = client.list_voices()
# Print the available voices
for voice in voices.voices:
print(f"Name: {voice.name}")
print(f"Language Codes: {', '.join(voice.language_codes)}")
print(f"SSML Gender: {texttospeech.SsmlVoiceGender(voice.ssml_gender).name}")
print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n")
Explanation texttospeech.TextToSpeechClient(): Initializes the client for Google Cloud Text-to-Speech.list_voices(): Lists all available voices, with details like name, supported languages, gender, and sample rate.
Output Example The script will output a list of voices available, including the voice name, language codes, SSML gender (e.g., MALE, FEMALE), and sample rate. Note: Using Google Cloud Text-to-Speech requires setting up a Google Cloud project, enabling billing, and authenticating using a service account file. |