TEXT-TO-SPEECH Using Pyttsx3
Dhanushkumar R
Microsoft Learn Student Ambassador - GOLD |Data Science @BigTapp Analytics|Ex-Intern @IIT Kharagpur|Machine Learning|Deep Learning|Data Science|Gen AI|LLM | Azure AI&Data |Databricks |Technical Blogger ??????
Pyttsx3 :
It is a text to speech conversion library in Python which is worked in offline and is compatible with both Python 2 and 3 version.This application invokes the pyttsx3.init() factory function to get a refernce to pyttsx3.It is very easy to use the tool to converts the entered text into speech.The pyttx3 module support two voice moth female and male which is provided by "sapi5" for windows.
It support three TTS engine:
1.sapi5 - SAPI on windows
2.nss - NSSpeechSynthesiaer on Mac
3.espeak?– eSpeak on every other platform
Installation:
To install the pyttsx3 module
pip install pyttsx3
Example:
import pyttsx3
engine = pyttsx3.init()
engine.say("Hello I am Dhanushkumar.")
engine.runAndWait()
Initialization (init()):
The init() function initializes the text-to-speech engine. You can call this function to set up the parameters for the speech synthesis. The initialization allows you to customize settings like the speech rate, volume, and voice selection.
import pyttsx3
engine = pyttsx3.init()
Changing Voice and Rate:
You can change the voice and speech rate using the setProperty() function.
Changing voice (if available)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
Changing speech rate
engine.setProperty('rate',150)
Converting Text to Speech (say()):
The say() function converts the provided text into speech and plays it.
engine.say("DhanushKumar is smart!")
engine.runAndWait()
Pausing and Resuming Speech:
engine.say("Dhanush is.....")
engine.pause()
#any code....
engine.resume()
Stopping Speech (stop()):
The 'stop()' function stops the ongoing speech:
engine.say("This is some speech.")
engine.runAndWait()
engine.stop()
Event Handling (connect()):
You can connect callback functions to events like the start and end of speech using the connect() method.
def onstart(name):
print("starting")
def onend(name,completed):
if completed:
print("completed")
else:
print("interrrupted")
engine.connect('start',onstart)
engine.connect('end',onend)
engine.say("Testing event")
engine.runAndWait()
Getting Information (getProperty()):
You can get information about the current engine properties using getProperty().
voice = engine.getProperty('voice')
rate = engine.getProperty('rate')
volume = engine.getProperty('volume')
Other Functionalities:
pyttsx3 also supports other functionalities like changing the volume and listing available voices.
engine.setProperty('volume', 0.5)
voices = engine.getProperty('voices'
for voice in voices:
print(voice.id, voice.name))
领英推è
Terminating the Engine:
stop() function to ensure any ongoing speech is stopped. Then, use the engine.endLoop()
engine.stop()
engine.endLoop()
the "Voice" metadata refers to the characteristics and properties of a specific speech synthesis voice. Each voice has associated attributes that you can access and manipulate to customize the speech output according to your preferences. Here are the main attributes of the Voice metadata in pyttsx3.
id:
The unique identifier for the voice. This is used to select a specific voice for synthesis.
voices = engine.getProperty('voices')
selected_voice_id = voices[0].id
engine.setProperty('voice', selected_voice_id)
name:
The name of the voice, which typically indicates its language and gender.
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.name)
languages:
A list of language codes that the voice supports.
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.languages)
gender:
The gender of the voice, which could be "male," "female," or "neutral."
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.gender)
age:
The age range of the voice, if available.
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.age)
variant:
A variant or style of the voice, if available.
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.variant)
description:
A description or additional information about the voice.
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.description)
quality:
A quality rating of the voice, if available.
voices = engine.getProperty('voices')
for voice in voices:
? ? print(voice.quality)
Not all voices may provide all of these attributes. The availability of these attributes can vary based on the installed speech synthesis engines and their capabilities. To select and use a specific voice, you can loop through the available voices using the getProperty('voices') method and access their attributes .
Saving Pyttsx3 audio file:
pyttsx3 does not have a direct method to save generated speech as an audio file. It's primarily designed for real-time speech synthesis and playback. If you need to save the speech as an audio file, you would typically need to use additional libraries like pydub or gtts
make sure you have pydub installed. You can install them using pip:
pip install pydub
import pyttsx3
from pydub import AudioSegment
from pydub.playback import play
# Initialize the pyttsx3 engine
engine = pyttsx3.init()
# Generate speech
text = "Dhanushkumar here"
engine.say(text)
engine.runAndWait()
# Save generated speech as an audio file
audio_data = AudioSegment.silent(duration=100)?
audio_data += AudioSegment.from_file("temp.wav")?
audio_data.export("output.mp3", format="mp3")?
Conclusion:
Hope this finds something useful to you.Thank you!!
Having 22+ Years of experience in various domains using different technologies. Canadian Citizen residing in India.
1 å¹´Very nicely presented complete features of the library! Excellent work! Thanks!