Multiple AI Voice Generators. In recent years, AI-powered voice generators have become increasingly popular, allowing developers to generate high-quality, natural-sounding speech for various applications.
Whether you’re developing a virtual assistant, an AI-driven voice-over system, or creating custom voices for different use cases, Python offers excellent libraries for generating speech using artificial intelligence.
In this article, we will explore how to create multiple AI voice generators in Python. We will focus on using the pyttsx3 library for offline voice generation and gTTS (Google Text-to-Speech) for generating AI-powered voices online. By combining these libraries, you can create diverse voice options for your project. Multiple AI Voice Generators – How to create it using Python.
Libraries and Tools Needed
To build a voice generator system, we will use:
- pyttsx3: A text-to-speech conversion library in Python that works offline.
- gTTS: Google’s Text-to-Speech API, which converts text into speech.
- Pydub: For audio manipulation, converting the speech into different formats.
- os: For interacting with the operating system, mainly for saving and playing generated audio files.
Installation of Required Libraries
Before we begin coding, let’s install the necessary Python packages:
pip install pyttsx3
pip install gtts
pip install pydub
Now, let’s explore how to use these libraries to generate multiple AI voices.
Creating AI Voice Generator with pyttsx3 (Offline)
The pyttsx3 library allows you to generate speech from text offline, making it a great option for projects where internet access is limited or not available.
Example Code for pyttsx3
import pyttsx3
# Function to initialize and configure pyttsx3 voice engine
def pyttsx3_voice_generator(text, voice_id):
engine = pyttsx3.init()
# Getting the available voices
voices = engine.getProperty('voices')
# Set the voice ID (Male/Female)
engine.setProperty('voice', voices[voice_id].id)
# Set speaking rate
engine.setProperty('rate', 150)
# Generate speech
engine.say(text)
engine.runAndWait()
# Example of using pyttsx3 for multiple voice types
text_input = "Hello! I am an AI voice created using pyttsx3."
pyttsx3_voice_generator(text_input, 0) # Male voice
pyttsx3_voice_generator(text_input, 1) # Female voice
Output:
Explanation:
- pyttsx3.init() initializes the text-to-speech engine.
- voices returns a list of available voices. By default, you usually get two voices: one male and one female.
- engine.setProperty(‘voice’, voices[voice_id].id) allows us to select the voice. We pass either
0for a male voice or1for a female voice. - The
engine.say()function converts the input text into speech, andengine.runAndWait()ensures the speech is played.
With this simple code, you can easily generate multiple voices using pyttsx3. This library does not require an internet connection, which makes it ideal for offline use.
Creating AI Voice Generator with gTTS (Google Text-to-Speech API)
The gTTS library is a Google-powered text-to-speech conversion tool that generates high-quality voices using AI models. Since it requires an internet connection, it can generate more advanced and natural-sounding voices than pyttsx3.
Example Code for gTTS
from gtts import gTTS
import os
# Function to generate voice using gTTS
def gtts_voice_generator(text, lang='en'):
# Initialize gTTS object
tts = gTTS(text=text, lang=lang, slow=False)
# Save the generated audio to a file
tts.save("output.mp3")
# Play the generated audio
os.system("start output.mp3")
# Example of using gTTS for voice generation
text_input = "Hello! I am an AI voice generated using Google Text-to-Speech.Welcome to Codemagnet learn coding in easiest way"
gtts_voice_generator(text_input, 'en')
Output:
Explanation:
- gTTS(text=text, lang=lang, slow=False) initializes the text-to-speech object. We can specify the language and the speed of the generated speech.
- tts.save(“output.mp3”) saves the generated speech as an MP3 file.
- os.system(“start output.mp3”) plays the generated audio file.
Advantages of gTTS:
- Natural Sounding Speech: gTTS provides more natural-sounding speech than pyttsx3 since it is cloud-based and leverages Google’s advanced AI models.
- Multiple Language Support: With gTTS, you can generate speech in multiple languages such as English, French, Spanish, etc., by changing the
langparameter.
Combining Multiple AI Voices with gTTS and pyttsx3
You can combine both pyttsx3 and gTTS to create a voice generation system that offers both online and offline options. Let’s build a Python function that can switch between offline and online voice generation based on user input.
Example Code Combining pyttsx3 and gTTS
from gtts import gTTS
import pyttsx3
import os
# Function to generate voice using pyttsx3 (Offline)
def pyttsx3_voice_generator(text, voice_id):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[voice_id].id)
engine.setProperty('rate', 150)
engine.say(text)
engine.runAndWait()
# Function to generate voice using gTTS (Online)
def gtts_voice_generator(text, lang='en'):
tts = gTTS(text=text, lang=lang, slow=False)
tts.save("output.mp3")
os.system("start output.mp3")
# Main function to handle multiple voice generators
def generate_ai_voice(text, engine_type='offline'):
if engine_type == 'offline':
pyttsx3_voice_generator(text, 0) # Use pyttsx3 for offline voice
elif engine_type == 'online':
gtts_voice_generator(text, 'en') # Use gTTS for online voice
# Example usage
text_input = "Welcome to AI voice generation! we can genrate various voice"
generate_ai_voice(text_input, 'offline') # Generate offline voice
generate_ai_voice(text_input, 'online') # Generate online voice
Output:
Explanation:
- This code provides a flexible solution where you can generate voices either using pyttsx3 (offline) or gTTS (online) depending on the
engine_typeparameter. - The user can choose between offline or online voice generation, allowing the system to work in both environments (with or without internet).
Advanced Features
To make the system more dynamic, you can add features like:
- Voice Customization: Allow users to change voice speed, volume, or pitch using the pyttsx3 library.
- Language Options: Expand language support by allowing users to input different languages for gTTS.
- Batch Voice Generation: Generate multiple voice files in a loop for bulk tasks like narration, voice-over projects, etc.
Example Code for Customizing Voice Settings
def custom_pyttsx3_voice_generator(text, voice_id, rate=150, volume=1.0):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[voice_id].id)
engine.setProperty('rate', rate)
engine.setProperty('volume', volume)
engine.say(text)
engine.runAndWait()
# Example usage with custom voice settings
text_input = "This is a customizable voice."
custom_pyttsx3_voice_generator(text_input, 0, rate=200, volume=0.8)
Explanation:
- engine.setProperty(‘rate’, rate) allows you to set the speed of the speech.
- engine.setProperty(‘volume’, volume) lets you adjust the volume of the speech (range from 0.0 to 1.0).
Conclusion
In this article, we have explored how to create multiple AI voice generators in Python using two powerful libraries: pyttsx3 for offline voice generation and gTTS for online, cloud-powered voice generation. We covered basic and advanced examples, showing how you can generate speech in multiple voices, languages, and formats. The ability to switch between offline and online options makes the system highly versatile, providing flexibility based on the project’s requirements.
Whether you’re working on a virtual assistant, voice-over automation, or any project requiring synthesized speech, Python’s rich ecosystem of text-to-speech libraries opens up many possibilities. By combining these libraries and customizing voice settings, you can create a highly personalized AI voice system to suit various applications.





Leave a Reply