Make your program read texts aloud (Python quick tip #1)

image by Tim Mossholder on Unsplash

link to this article Portuguese translation here.

In this article, you will find a custom-built Python function that makes your code read text aloud. This resource can be helpful, for example, when you want your code to play a voice message to warn you if it reaches an exception. Or maybe you intend to transform your print statements into something more fun by making your computer “talk” to the user. Possibilities are plenty.

In order to use this function, you need to install both the gtts and the pygame modules with pip or another similar tool. If your operational system is Linux or macOS, installing the pydub module is necessary too. You will also need the os and time built-in modules.

I present the complete code below. If you understand it well enough, fell free to finish this article here and test the code by yourself. However, if you want some more detailed explanation about the function components, I invite you to keep reading.

PART 1: THE PARAMETERS

After the import statements, the function is defined with five parameters:

- text => the str to be read aloud;

- filename (default: “temp.mp3”) => a str with the name of the mp3 file generated with the voice message;

- delete_audio_file (default: True) => a bool that determines if the audio file with the voice message should be deleted or not. On Linux or Mac-OS, a .ogg file will also be created and that’s the one that will be played;

- language (default: “en”) => a str with the text language acronym (check the gtts documentation for more info);

- slow (default: False) => a bool to determine whether the program should pronounce the message slowly or not.

PART 2: CREATE THE AUDIO FILE

The gTTS object will convert the text into a voice message, which will be read according to the chosen language phonetic rules. If you define the parameter slow as True, the voice message will be pronounced more slowly. This object is saved in the audio variable. Then, the custom-built function calls the gTTS method to save the voice message as an mp3 file, whose name will be the same as the filename parameter.

On Linux and Mac-OS, the mp3 file will be converted to an .ogg file. Unless the delete_audio_file parameter was set to False, the mp3 file will be deleted as soon as it is no longer needed.

PART 3: LOAD AND PLAY THE AUDIO FILE

mixer.init() initializes the mixer module from pygame. Then, the code calls the music submodule functions load() and play().

An important note here: when mixer.music.play() is called, the code will continue executing quickly towards its end. So, if you don’t set a pause here in the code execution, the audio file will probably stop playing even before anyone can hear anything. This is the reason for part 4 of the custom-built function.

PART 4: WAIT FOR THE END OF THE AUDIO FILE

This part calls mixer.music.get_busy() as the condition to the while loop. This function from pygame will return 1 if the mixer object is playing and 0 if not. So, the loop will run every 0.25 seconds until the audio file execution stops and the get_busy() function returns the value 0. The seconds variable will save the total time of the played mp3 audio.

PART 5: CLEANING UP AND LOG MESSAGE

Finally, we need to stop the mixer module, calling mixer.quit(), and remove the created audio file. However, we will keep this file if the delete_audio_file parameter was set to False.

A print message with the total number of seconds will be displayed in the terminal. You can comment out this line of code if you want. The seconds variable value will also be returned by the function, which allows to use it in later code, if needed.

ABOUT SOURCES USED TO BUILD THIS CODE:

I first learned how to transform text to speech with Python from this quite didactic and straightforward program published by Geeks for Geeks: https://www.geeksforgeeks.org/convert-text-speech-python/

And I discovered about the pygame mixer module from this Stack Overflow post by shad0w_wa1k3r:

https://stackoverflow.com/questions/20021457/playing-mp3-song-on-python

Finally, I learned how to transform a mp3 file into an .ogg with this response by RDK on Stack Overflow:

https://stackoverflow.com/questions/50720152/gtts-error-saving-as-wav-but-saved-as-mpeg

Thank you so much for having honored my text with your reading.

Happy coding!

P.S.1: You’ll find this code on my Github repository (still under construction though…), in the duckytie.py file.

P.S.2: Look me up on LinkedIn: https://www.linkedin.com/in/fabriciobrasil/