TEXT-TO-SPEECH Using Pyttsx3

TEXT-TO-SPEECH Using Pyttsx3

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!!

Varshesh Badheka

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!

要查看或添加评论,请登录

Dhanushkumar R的更多文章

  • MLOPS -Getting Started .....

    MLOPS -Getting Started .....

    What is MLOps? MLOps (Machine Learning Operations) is a set of practices and principles that aim to streamline and…

    1 条评论
  • Pydub

    Pydub

    Audio files are a widespread means of transferring information. So let’s see how to work with audio files using Python.

    1 条评论
  • Introduction to Python libraries for image processing(Opencv):

    Introduction to Python libraries for image processing(Opencv):

    Image processing is a crucial field in computer science and technology that involves manipulating, analyzing, and…

    1 条评论
  • @tf.function

    @tf.function

    Learning Content:@tf.function @tf.

  • Web Scraping

    Web Scraping

    Web scraping is the process of collecting structured web data in an automated manner. It's also widely known as web…

  • TORCHAUDIO

    TORCHAUDIO

    Torchaudio is a library for audio and signal processing with PyTorch. It provides I/O, signal and data processing…

  • Getting Started With Hugging Face-Installation and setUp

    Getting Started With Hugging Face-Installation and setUp

    Hub Client Library : The huggingface_hub library allows us to interact with Hugging FaceHub wich is a ML platform…

  • Audio Features of ML

    Audio Features of ML

    Why audio? Description of sound Different features capture different aspects of sound Build intelligent audio system…

  • Learning Path: "Voice and Sound Recognition"

    Learning Path: "Voice and Sound Recognition"

    Chapter 1: SOUND AND WAVEFORMS The concept that I learnt from this content is fundamental concepts related to sound and…

  • Pytorch Learning -3 [TRANSFORMS]

    Pytorch Learning -3 [TRANSFORMS]

    TRANSFORMS Data does not always come in its final processed form that is required for training machine learning…

社区洞察

其他会员也浏览了