Auto-generating Audio Like HackerNoon In Python

Written by h3avren | Published 2022/01/18
Tech Story Tags: python | python-tutorials | python-programming | learn-python | python-development | python3 | learn-to-code-python | auto-generating-audio | web-monetization

TLDRimport pyttsx3 engine = pyttsx3.init() engine.setProperty('rate',125) text = '''And I have been seeing an audio file embedded with each article on the website which is auto-generated. It’s kinda cool. At least I think it is.''' engine.save_to_file(text,'text.mp3') # this command queues the text to be saved as an audio file engine.runAndWait() via the TL;DR App

Why am I even Writing this Article?

Well, It hasn’t been a long since I joined HackerNoon. And I have been seeing an audio file embedded with each article on the website which is auto-generated. It’s kinda cool. At least I think it is 🤪.

So, I thought why not try getting an audio file using Python which is somewhat similar to the one auto generated by HackerNoon for all it’s articles. Kinda learn by doing thing.So, let’s proceed.

What are we gonna use?

Legends say Python has a plethora of libraries for everything but for this I could find only one in working state.

Simply put pyttsx3 cause that’s the only library I could put to work. Others are namely gtts (Google text to speech API) and python-espeak (Python C extension for the eSpeak speech synthesizer).

I wasn’t able to get gtts to work and python-espeak didn’t even install properly on my system.

Installation

pip install pyttsx3

That’s all it takes to install, Python is beautiful you know?

Let’s get started

Steps we are gonna follow:

  • import pyttsx3
  • pass it the required text
  • save the speech as an audio file

Getting hands dirty with code

Firstly, let’s get the code to output something through the speakers:

import pyttsx3    # importing the library

engine = pyttsx3.init()    # initialising the pyttsx3 engine

# setting engine properties
engine.setProperty('rate',125)    # setting the rate of words per minute

text = '''And I have been seeing an audio file embedded with 
        each article on the website which is auto-generated.
        It’s kinda cool. At least I think it is.'''

engine.say(text)    # this line queues the text for conversion
engine.runAndWait()    # this command processes the commands in the queue

# the say command outputs the speech via the sound output device

Now, let’s save that as an audio file:

import pyttsx3
engine = pyttsx3.init()

engine.setProperty('rate',125)
text = '''And I have been seeing an audio file embedded with 
        each article on the website which is auto-generated.
        It’s kinda cool. At least I think it is.'''

engine.save_to_file(text,'text.mp3')    # this command queues the text to be saved as an audio file
engine.runAndWait()

The above code saves the audio as an .mp3 file with the name text. Here’s how it sound.

Now, let’s try to include some female voice:

There is No Female Voice included with the core pyttsx3 package.The following is for linux users as they can use espeak (a C library pre-installed on linux).

I learnt about this from this stackoverflow question which also references to this github issue.

import pyttsx3
engine = pyttsx3.init()

engine.setProperty('rate',125)
engine.setProperty('voice','english+f3')
text = '''And I have been seeing an audio file embedded with 
        each article on the website which is auto-generated.
        It’s kinda cool. At least I think it is.'''
engine.save_to_file(text,'text_female.mp3')
engine.runAndWait()

Here’s how it sounds.

Resources

The best resource I can suggest to learn from is the pyttsx3 official documentation. I taught myself using the same and hail lord Google for it’s quick answers to my problems.

Conclusion

Well, I couldn’t reach any closer to creating the audio files as good in quality as HackerNoon but at least we tried it using our favourite language Python.

So, we used pyttsx3 for converting text to speech in Python. We didn’t do much other than using an existing API but it can be used as a tinier application for larger programs if we use our brains right enough for a large scale Python project. See you fellas, later on, it was fun learning all this with you.


Written by h3avren | Dreaming of Python... Under a sky in India...
Published by HackerNoon on 2022/01/18