# How I built An Audio Reader CLI App In Python

Hey there 👋, welcome back if you are a usual suspect! And If you are new here, sit tight! I am friendly and hope you enjoy my article today. 

So I wanted to listen to some articles instead of reading them while I am doing some work. Yeah, multitasking...

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1633592890054/ZDmS1gYL6.png)
And I thought about building my own audio reader because I like building my own programs nowadays given the python skills I have on top of Python's huge library.

### Let's Get Started 🚀

An Audio Reader App is basically text to speech translator and there are tons of apps out there you could get, some are freemium & others premium.

### Tools 🛠
📌 pyttsx3 - Text-to-speech x-platform<br>
📌 requests -Make HTTP requests to any API in the world<br>
📌 bs4 -Pulling data out of HTML and XML files<br>

#### Let's build it together:

I created an audible.py file on my desktop<br>
Import dependencies <br>

```
import pyttsx3
import requests
from bs4 import BeautifulSoup
``` 
Main Engine Initialising 🛫: 
``` 
engine = pyttsx3.init('sapi5')
``` 
The ```init``` command helps to get a reference to a ```pyttsx3.Engine``` instance. <br>
It takes a parameter which is a ```driverName```  – ``` sapi5``` <br>

Some of the ``` pyttsx3.drivers```  module to load and use currently are:<br>
⚓```sapi5``` - SAPI5 on Windows<br>
⚓```nsss``` - NSSpeechSynthesizer on Mac OS X<br>
⚓```espeak``` - eSpeak on every other platform<br>
Read  [More](https://pyttsx3.readthedocs.io/en/latest/engine.html) <br>

Voice Getter & Setter 🔊
``` 
# Get voices list property from the engine
voices = engine.getProperty('voices')

# setting voice to the first one in the list(female)
engine.setProperty('voice', voices[0].id)
``` 
Create a function to handle audio output 🎵
``` 
def speak(audio):
    engine.say(audio)
    engine.runAndWait()
``` 
The ``` say()```  queues a command to speak an utterance. <br>
The ``` runAndWait()```  blocks while processing all currently queued commands. <br>

Get Article URL from user input 🖍
``` 
url = str(input("Paste article URL : \n"))
print("\n -------------------------------")

# Get text using requests
data = requests.get(url).text

# print(data) -> This outputs html markup
``` 
Test URL:  Freecode Camp  [Link](https://www.freecodecamp.org/news/become-a-developer-and-get-your-first-job-fast-7b8ac26d84c6/)  

Getting the soup {text} from the html page(data) 🍳
``` 
soup = BeautifulSoup(data,'html.parser')
article = soup.get_text()

print(article)
``` 
And the golden pie 🍕. Let's get our audio
``` 
print(speak(article))

# If you want to save the speech as an audio file
engine.save_to_file(article, 'article.mp3')

engine.runAndWait()
``` 
The ``` mp3```  is saved at the end of the program & is located in the ``` cwd``` .

Github Repo: Audio Reader  [App](https://github.com/Ronlin1/audio-reader-app) 

Watch Video Implementation:  Audio Reader [App](https://www.youtube.com/watch?v=1pw7b8VBc9Y) 

#### Conclusion
That's It you have built your own audio reader in just a few lines of python using only three libraries. My challenge to you will be: find a way how to implement audio sync with text being logged out in the console to match line by line.

Tip: Try to search for ```time``` or ```await``` or ```async``` or ```anything related to this:```

Now I am thinking of the GUI app. I wanna built it too. Stay In touch >>>

If you enjoyed this simple article, kindly share the love! Subscribe & comment for any issues. 
See You!

**Ronnie Atuhaire 😎**



