Hackers Realm

Jul 13, 20223 min

Convert Text to Speech using Python | Speech Synthesis | TTS Tutorial

Updated: May 31, 2023

Elevate your projects with text-to-speech conversion using Python! Dive into speech synthesis and learn how to transform written text into natural-sounding speech. Explore various techniques and tools, and harness the power of TTS technology. Enhance accessibility, create interactive voice applications, and unlock new possibilities with this comprehensive tutorial. #TextToSpeech #Python #SpeechSynthesis #TTS #Accessibility #VoiceApplications

Convert Text to Speech using Speech Synthesis

For this project tutorial we will install the Google text to speech API and the pyttsx3 API and convert a text sentence into and audio file.

You can watch the step by step explanation video tutorial down below

Project Information

The objective of the project is to convert text to speech in real time and convert text to audio file. It uses google text to speech API to convert the text to audio. It also uses pyttsx3 (offline module) that converts text to audio with various customizations.

Google Text to Speech API

We will proceed to install the gTTS module

# install the module
 
!pip install gTTS

Collecting gTTS
 
Downloading gTTS-2.2.3-py3-none-any.whl (25 kB)
 
Requirement already satisfied: six in c:\programdata\anaconda3\lib\site-packages (from gTTS) (1.15.0)
 
Requirement already satisfied: requests in c:\programdata\anaconda3\lib\site-packages (from gTTS) (2.24.0)
 
Requirement already satisfied: click in c:\programdata\anaconda3\lib\site-packages (from gTTS) (7.1.2)
 
Requirement already satisfied: idna<3,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests->gTTS) (2.10)
 
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests->gTTS) (1.25.11)
 
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests->gTTS) (2020.6.20)
 
Requirement already satisfied: chardet<4,>=3.0.2 in c:\programdata\anaconda3\lib\site-packages (from requests->gTTS) (3.0.4)
 
Installing collected packages: gTTS
 
Successfully installed gTTS-2.2.3

from gtts import gTTS
 
import os
 

 
# input
 
text = 'Welcome to Hackers Realm. It contains tutorial videos on technology'
 

 
# convert text to speech
 
text_to_speech = gTTS(text=text, lang='en', slow=False)
 

 
# save the audio file
 
text_to_speech.save('test.mp3')
 

 
# play the audio
 
os.system('test.mp3')

0

  • os - used to handle files using system commands

  • lang='en' - Language conf. to English, you may change the language by preference

  • slow=False - Parameter to change audio speed

Pyttsx - Offline

We will install the pyttsx3 module

# install the module
 
!pip install pyttsx3

Collecting pyttsx3
 
Downloading pyttsx3-2.90-py3-none-any.whl (39 kB)
 
Requirement already satisfied: comtypes in c:\programdata\anaconda3\lib\site-packages (from pyttsx3) (1.1.7)
 
Collecting pypiwin32
 
Downloading pypiwin32-223-py3-none-any.whl (1.7 kB)
 
Requirement already satisfied: pywin32 in c:\programdata\anaconda3\lib\site-packages (from pyttsx3) (227)
 
Installing collected packages: pypiwin32, pyttsx3
 
Successfully installed pypiwin32-223 pyttsx3-2.90

import pyttsx3
 

 
# input
 
text = 'Welcome to Hackers Realm. It contains tutorial videos on technology'
 

 
# initialize the module
 
text_to_speech = pyttsx3.init()
 

 
# adjust the speed
 
text_to_speech.setProperty('rate', 150)
 

 
# change the voice
 
voices = text_to_speech.getProperty('voices')
 
text_to_speech.setProperty('voice', voices[1].id) # 0 for male and 1 for female
 

 
# convert text to speech
 
text_to_speech.say(text)
 

 
# save the audio file
 
text_to_speech.save_to_file(text, 'test.mp3')
 

 
# listen to audio
 
text_to_speech.runAndWait()

  • pyttsx3.init() - Initialization of the module

  • setProperty('rate', 150) - Set speed of audio

  • setProperty('voice', voices[1].id) - Change voice type to male or female

  • save_to_file() - stores the audio file

  • runAndWait() - stream the converted audio for real time listening

  • Check the documentation to see more customization available for the audio

Final Thoughts

  • Two best modules to convert any text to speech, you may use other similar modules by your preference.

  • The pyttsx3 module has many customization tools for the audio.

  • Very useful tool for educational purposes for learning or fortifying a specific language.

In this project tutorial, we have explored the Convert Text to Speech process using the Google Text to Speech module and the pyttsx3 module. A basic text was converted into an audio file and customization of the audio is also done with the modules.

Get the project notebook from here

Thanks for reading the article!!!

Check out more project videos from the YouTube channel Hackers Realm

    1963
    0