Text-to-speech code in python

How to write a text-to-speech code in python?

Introduction and overview

We will write a text-to-speech code in python and learn how to turn human language text into human-like speech in this tutorial.

We sometimes prefer to listen to information rather than read it. While listening to the crucial file data, we can multitask. Python has a number of APIs for converting text to speech. The Google Text to Speech API, or gTTS API, is a well-known and widely used API.

The tool is simple to use and comes with a number of built-in features, including the ability to save a text file as an mp3 file.

We don’t need to employ a neural network or train a model to convert the file to voice because that is likewise difficult. Instead, we’ll finish a task using these APIs.

There are various APIs for converting text to speech in Python. The Google Text to Speech API, often known as the gTTS API, is one of the best APIs.

gTTS is a straightforward application that converts text into audio files that may be saved as mp3 files.

The gTTS API supports English, Hindi, Tamil, French, German, and a variety of additional languages. The speech can be delivered at one of two different audio speeds: quick or slow. Changing the voice of the generated audio is not possible as of the most recent release.

text-to-speech code in Python

Text-to-speech code in python Using gTTS

Google Text-to-Speech (gTTS) is a Python library and command-line tool for interacting with Google Translate’s text-to-speech API. Writes spoken mp3 data to a file, a file-like object (bytestring), or stdout for additional audio manipulation. It has a configurable pre-processing and tokenization feature.

pip install gTTS

Write a simple code with gTTS

# Import the gTTs to speech conversion
from gtts import gTTS
import os
# The text that you want to convert to audio
sample_txt = 'This is a text to speech test.'
tts = gTTS(text=sample_txt)
# Saving the converted audio in a mp3 file named outout_text_to_speech.mp3
# Playing the converted file
os.system("mpg321 outout_text_to_speech.mp3")

The main drawback of gTTS is it depends on your network. If you want to generate a speech from text offline then you can use “pyttsx3“. That’s all, although you can learn more about the module gTTS here.

Text-to-speech code in python Using pyttsx3

pyttsx3 is a Python-based text-to-speech conversion library. It operates offline, unlike other libraries, and is compatible with Python 2 and 3.

pip install pyttsx3

A simple program to generate speech from text is as follows.

# import the module
import pyttsx3
engine = pyttsx3.init()
engine.say("This voice is generated in python.")

If you want to create complex logic and improve the voice quality then check out the official documentation of pyttsx3 here.