Instant Free Text to Speech in Python
Text to Speech is converting any given text to a audible speech format.
Adding Speech as a feature in your application can make it more interactive as easier for the user to get response from the machine.
There are many free and paid websites available on the web which can help you convert any text into speech.
With the advancement in machine learning and deep learning algorithms text to speech has become more and more sophisticated in producing human like voice.
Also, There are many Text to speech engine APIs which can be by a developer in there application to enable Text to speech (TTS).
Lets start with a basic off the shelf TTS implementation on Python and later dive into alternate methods.
To start with TTS we need to install the gTTS library.
- Install gTTS by using command “pip install gTTS“.
- Now we can import the library using “from gtts import gTTS” and use it to convert text into speech and save it as a mp3 file or even play it back to back.
- Below is a sample program which takes input text from user and plays the output in speech.
from gtts import gTTS from playsound import playsound text_to_speech = gTTS('I will say what you write') text_to_speech.save('TTS.mp3')
Futher you can play the saved mp3 file by using the playsound library
To install the playsound library use command “pip install playsound“.
Use the function playsound with parameter as your mp3 file name, playsound will play the mp3 file.
from gtts import gTTS from playsound import playsound text_to_speech = gTTS('I will say what you write') text_to_speech.save('TTS.mp3') playsound('TTS.mp3')