• Category
  • >Python Programming

Python Essentials for Voice Control : Part 2

  • Priyanshu Gupta
  • Mar 27, 2021
Python Essentials for Voice Control : Part 2 title banner

In our previous blog Python Essentials for Voice Control: Part 1 we have discussed Speech Recognition , which is the first step towards Voice Control. In this blog we are going to enhance our skills to develop a Personal Voice Assistant.

 

Let us move ahead now..

 

How to Communicate with a Machine?

 

Communication needs a medium for representing thoughts which can be in any form like Verbal or Non - Verbal.  We can express thoughts by speaking or writing. We can communicate with computers through speaking using Speech Recognition till now. But it is like one way communication , where the computer can listen to us but to make it in action it needs to express itself using Text , Speech  or Actions. 

 

Artificial Intelligence working towards putting responses about feelings and expression but not getting 100% accuracy till now.  

 

Now, we will  express thoughts using Text , where we will convert Text into  Speech in Real Time.

 

 

Hands-On with Voice Control  Libraries

 

Text to Speech Conversion makes us understand the response of the computer  in the form of Text. pyttsx3 library of Python will help us to achieve the Goal.

 


pip install pyttsx3

Install pyttsx3 library using CMD.

 

 

Challenge 1:  How can Machine convert Text into Speech? 

 

  • init() :

It is a class of pyttsx3 library. It contains several methods which helps us to convert text into speech. It initialises an Engine instance to convert Text to Speech. 

 

init(driverName=None , debug=False)

 

  1. Parameter driverName contains the specific name of the driver which we want to use to create Engine instances for Text to Speech Conversion. By default it will use the driver which is available in the Operating system.

 

  1. Parameter debug used to give debug output enabled or not. It is of Boolean Type and by default it is False.

 

Drivers :  By default available Drivers Operating System based are :

 


import pyttsx3

engine = pyttsx3.init()

 

Like this we can initialize the Engine for Text to Speech Conversion.

 

  • getProperty( ) : 

It is a function of class init(). Using it, we can get the current value of a Property to modify accordingly. In this case Property is an attribute or Characteristic of an Object. 

For Example: Voice Attributes can be defined like Voice of Male or Female, Speed of Voice, Volume of Voice etc.

 

getProperty(name)

Parameter name is defined as the String ID or name of Property which we want to modify.

 

  • setProperty( ) : 

It is a function of class init(). Using it, we can assign the values to the Properties of Objects. Attribute selection is done by getProperty() function while values assigning to the attribute is done by setProperty().

 

setProperty(name, value)

  1. Parameter name is defined as the name of the Property, which we want to modify. 
  2. Parameter value contains the value of the Property.

 


    voices = engine.getProperty('voices')

    engine.setProperty('voice',voices[1].id)

    engine.setProperty('rate', 125 )

    engine.setProperty('volume',1.0)

 

Code tells us that we have selected the voices as the property to modify using getProperty() function.

 

We assign the values to the  attributes of the voices Property using setProperty() function:

 

voice : voice id , ( 0 for Male Voice ) and  (1 for Female Voice).

rate : Speed of speaking (int type)

volume : Level of Volume (float type)

 

 

Challenge2 : How to manage operations in a Program?

 

  • say():

It is  a function of class init(). It is used to operate a computer to speak something as output to the user. It occurs just after a Preoperative is finished. It continues in a queue.

say(text , name=None)

 

  1. Parameter text is the Utterance which we need in execution of a program from the Computer Machine.
  2. Parameter name is a Notification of utterance, by default it is None.


 

  • runAndWait():

It is a function of class init() to manage Engine calls. It is used to run an event loop until all commands queued up until a method call completes in which we use this function. It Blocks all other next steps in the waiting area till the method call completes its operations.


def speak(text):

    engine = pyttsx3.init()

    voices = engine.getProperty('voices')

    engine.setProperty('voice',voices[1].id)

    engine.setProperty('rate', 125 )

    engine.setProperty('volume',1.0)

    engine.say(text)

    engine.runAndWait()



 

  • connect():

It is used to register a callback for notification on the given topic.

connect(topic, cb)

  1. Parameter topic contains the cause of callback to the method.
  2. Parameter cb contains the method to be called on occurrence of topic.

 

Valid Topics of callback for a method are conditions according to which keyword of topic should be occur:

 

Started-Utterance :

Method should be fired when the engine begins speaking an utterance.

 

onStartUtterance(name)

 

Started-Word :

Method should be fired when the engine begins speaking a word.

onStartWord(name, location, length)

 

Finished-Utterance:

Method should be fired when the engine ends speaking an utterance.

onFinishUtterance(name, completed)

 

Error:

Method should be fired when the engine throws an error.

onError(name, exception)

 


 

def onStart(name):

   print 'starting', name

def onWord(name, location, length):

   print 'word', name, location, length

def onEnd(name, completed):

   print 'finishing', name, completed

engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

 


 

  • disconnect() : 

It is used to unregister a notification callback.

 

disconnect(token)

  1. Parameter token is returned by connect() associated with the callback to be disconnected.

 

 

Conclusion

 

Python is healthy in almost every aspect of development in Technology. We can create wonderful applications in a small amount of codes. It gives us interesting implementations as well as industrial programs for projects.

 

Our thoughts can directly sync with the programs to develop something amazing. It is wonderful to make computers speak to us.

 

Using pyttsx3 we can develop many projects for the community. We can contribute to the luxury living of people and utilise time as well as resources in a healthier way.

 

We can make a computer to read a book for us using pyttsx3, which is like Amazon Audible, Audio Book Service.

Latest Comments