To conclude, if you want to use more reliable synthesis, Google TTS API is your choice, if you just want to make it work a lot faster and without Internet connection, you should use pyttsx3 library. Great, that's it for this tutorial, I hope that will help you build your application, or maybe your own virtual assistant in Python. You can also save the audio as file using the save_to_file() method, instead of playing the sound using say() method: # saving speech audio into a fileĪ new MP3 file will appear in the current directory, check it out! Conclusion For instance, let's get the details of speaking rate: # get details of speaking rateĪlright, let's change this to 300 (make the speaking rate much faster): # setting new voice rate (faster)Īnother useful property is voices, which allow us to get details of all voices available on your machine: # get details of all voices availableĪs you can see, my machine has three voice speakers, let's use the second, for example: # set another voiceĮtProperty("voice", voices.id)
This library provides us with some properties that we can tweak based on our needs. So you can call multiple times the say() method and run a single runAndWait() method in the end, in order to hear the synthesis, try it out! Say() method adds an utterance to speak to the event queue, while runAndWait() method runs the actual event loop until all commands queued up. Text = "Python is a great programming language" Now to convert some text, we need to use say() and runAndWait() methods: # convert this text to speech Now we need to initialize the TTS engine: # initialize Text-to-speech engine To get started with this library, open up a new Python file and import it: import pyttsx3
Note: If you're on a Linux system and the voice output is not working with this library, then you should install espeak, ffmpeg and libespeak1: $ sudo apt update & sudo apt install espeak ffmpeg libespeak1
SAPI5 on Windows XP, Windows Vista, 8, 8.1 and 10.Well, pyttsx3 library comes into the rescue, it is a text to speech conversion library in Python, it looks for TTS engines pre-installed in your platform and uses them, here are the text-to-speech synthesizers that this library uses:
Now you know how to use Google's API, but what if you want to use text to speech technologies offline ? Here are the supported languages: Offline Text to Speech To get the list of available languages, use this: # all available languages along with their IETF tag If you don't want to save it to a file and just play it directly, then you should use tts.write_to_fp() which accepts io.BytesIO() object to write into, check this link for more information. It isn't available only in English, you can use other languages as well by passing the lang parameter: # in spanish
Up to this point, we have sent the text and retrieved the actual audio speech from the API, let's save this audio to a file: # save the audio fileĪwesome, you'll see a new file appear in the current directory, let's play it using playsound module installed previously: # play the audio fileĪnd that's it ! You'll hear a robot talking what you just told him to say! It's pretty straightforward to use this library, you just need to pass text to gTTS object that is an interface to Google Translate's Text to Speech API: # make request to google to get synthesis Open up a new Python file and import: import gtts
It requires Internet connection and it's pretty easy to use. To get started, let's install required modules: pip3 install gTTS pyttsx3 playsound Online Text to SpeechĪs you may guess, gTTS stands for Google Text To Speech, it is a Python library to interface with Google Translate's text to speech API. To make things clear, this tutorial is about converting text to speech and not the other way around, if you want to convert speech to text instead, check this tutorial. There are a lot of APIs out there that offers this service, one of the commonly used services is Google Text to Speech, in this tutorial, we will play around with it along with another offline library: pyttsx3. Instead, we gonna use some APIs and engines that offer it. In this tutorial, we won't be building neural networks and train the model in order to achieve results, as it is pretty complex and hard to do it. In this tutorial, you will learn how you can convert text to speech in Python. It converts human language text into human-like speech audio. Speech synthesis (or Text to Speech) is the computer-generated simulation of human speech.