Skip to content

AI-based Speech Synthesis

Abstract

AI-based Speech Synthesis is a Python project that uses AI to convert text into natural-sounding speech. The application features voice customization, error handling, and a CLI interface, demonstrating speech synthesis and audio processing techniques.

Prerequisites

  • Python 3.8 or above
  • A code editor or IDE
  • Basic understanding of speech synthesis
  • Required libraries: pyttsx3pyttsx3, gttsgtts

Before you Start

Install Python and the required libraries:

Install dependencies
pip install pyttsx3 gtts
Install dependencies
pip install pyttsx3 gtts

Getting Started

Create a Project

  1. Create a folder named ai-based-speech-synthesisai-based-speech-synthesis.
  2. Open the folder in your code editor or IDE.
  3. Create a file named ai_based_speech_synthesis.pyai_based_speech_synthesis.py.
  4. Copy the code below into your file.

Write the Code

⚙️ AI-based Speech Synthesis
AI-based Speech Synthesis
"""
AI-based Speech Synthesis
 
Features:
- Speech synthesis using deep learning
- Text-to-speech
- Modular design
- CLI interface
- Error handling
"""
import sys
try:
    import pyttsx3
except ImportError:
    pyttsx3 = None
 
class SpeechSynthesizer:
    def __init__(self):
        self.engine = pyttsx3.init() if pyttsx3 else None
    def synthesize(self, text):
        if self.engine:
            self.engine.say(text)
            self.engine.runAndWait()
        else:
            print("Speech synthesis library not available.")
 
class CLI:
    @staticmethod
    def run():
        print("AI-based Speech Synthesis")
        synthesizer = SpeechSynthesizer()
        while True:
            cmd = input('> ')
            if cmd.startswith('speak'):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Usage: speak <text>")
                    continue
                text = parts[1]
                synthesizer.synthesize(text)
            elif cmd == 'exit':
                break
            else:
                print("Unknown command. Type 'speak <text>' or 'exit'.")
 
if __name__ == "__main__":
    try:
        CLI.run()
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)
 
AI-based Speech Synthesis
"""
AI-based Speech Synthesis
 
Features:
- Speech synthesis using deep learning
- Text-to-speech
- Modular design
- CLI interface
- Error handling
"""
import sys
try:
    import pyttsx3
except ImportError:
    pyttsx3 = None
 
class SpeechSynthesizer:
    def __init__(self):
        self.engine = pyttsx3.init() if pyttsx3 else None
    def synthesize(self, text):
        if self.engine:
            self.engine.say(text)
            self.engine.runAndWait()
        else:
            print("Speech synthesis library not available.")
 
class CLI:
    @staticmethod
    def run():
        print("AI-based Speech Synthesis")
        synthesizer = SpeechSynthesizer()
        while True:
            cmd = input('> ')
            if cmd.startswith('speak'):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Usage: speak <text>")
                    continue
                text = parts[1]
                synthesizer.synthesize(text)
            elif cmd == 'exit':
                break
            else:
                print("Unknown command. Type 'speak <text>' or 'exit'.")
 
if __name__ == "__main__":
    try:
        CLI.run()
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)
 

Example Usage

Run speech synthesis
python ai_based_speech_synthesis.py
Run speech synthesis
python ai_based_speech_synthesis.py

Explanation

Key Features

  • Text-to-Speech: Converts text to audio output.
  • Voice Customization: Supports different voices and languages.
  • Error Handling: Validates inputs and manages exceptions.
  • CLI Interface: Interactive command-line usage.

Code Breakdown

  1. Import Libraries and Setup Engine
ai_based_speech_synthesis.py
import pyttsx3
from gtts import gTTS
ai_based_speech_synthesis.py
import pyttsx3
from gtts import gTTS
  1. Text-to-Speech Function
ai_based_speech_synthesis.py
def speak_text(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()
 
def save_speech(text, lang='en', filename='output.mp3'):
    tts = gTTS(text=text, lang=lang)
    tts.save(filename)
ai_based_speech_synthesis.py
def speak_text(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()
 
def save_speech(text, lang='en', filename='output.mp3'):
    tts = gTTS(text=text, lang=lang)
    tts.save(filename)
  1. CLI Interface and Error Handling
ai_based_speech_synthesis.py
def main():
    print("AI-based Speech Synthesis")
    while True:
        cmd = input('> ')
        if cmd == 'speak':
            text = input("Text: ")
            try:
                speak_text(text)
            except Exception as e:
                print(f"Error: {e}")
        elif cmd == 'save':
            text = input("Text: ")
            lang = input("Language (default 'en'): ") or 'en'
            filename = input("Filename (default 'output.mp3'): ") or 'output.mp3'
            try:
                save_speech(text, lang, filename)
                print(f"Saved to {filename}")
            except Exception as e:
                print(f"Error: {e}")
        elif cmd == 'exit':
            break
        else:
            print("Unknown command. Type 'speak', 'save', or 'exit'.")
 
if __name__ == "__main__":
    main()
ai_based_speech_synthesis.py
def main():
    print("AI-based Speech Synthesis")
    while True:
        cmd = input('> ')
        if cmd == 'speak':
            text = input("Text: ")
            try:
                speak_text(text)
            except Exception as e:
                print(f"Error: {e}")
        elif cmd == 'save':
            text = input("Text: ")
            lang = input("Language (default 'en'): ") or 'en'
            filename = input("Filename (default 'output.mp3'): ") or 'output.mp3'
            try:
                save_speech(text, lang, filename)
                print(f"Saved to {filename}")
            except Exception as e:
                print(f"Error: {e}")
        elif cmd == 'exit':
            break
        else:
            print("Unknown command. Type 'speak', 'save', or 'exit'.")
 
if __name__ == "__main__":
    main()

Features

  • AI-Based Speech Synthesis: High-quality text-to-speech
  • Voice Customization: Supports multiple voices and languages
  • Error Handling: Manages invalid inputs and exceptions
  • Production-Ready: Scalable and maintainable code

Next Steps

Enhance the project by:

  • Supporting batch synthesis
  • Creating a GUI with Tkinter or a web app with Flask
  • Adding voice selection and speed control
  • Unit testing for reliability

Educational Value

This project teaches:

  • Speech Synthesis Fundamentals: Text-to-speech and audio processing
  • Software Design: Modular, maintainable code
  • Error Handling: Writing robust Python code

Real-World Applications

  • Accessibility Tools
  • Voice Assistants
  • Content Creation
  • Educational Tools

Conclusion

AI-based Speech Synthesis demonstrates how to build a scalable and accurate text-to-speech tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in accessibility, education, and more. For more advanced projects, visit Python Central Hub.

Was this page helpful?

Let us know how we did