Voice is an essence of communication. It helps us transfer huge amounts of data in a very short period of time. It is certainly faster and easier than typing. Hence, more and more companies are working toward making systems that understands human voice and language and work according to them. It is certainly not easy because of the huge variations that are present in the language; however, we have come a considerable distance. So without much time, let's make our system get ready to recognize our voice.
So here, we would be using an API from Google Voice. As you may know, Google is really good at understanding what you say. Like, very literally. So it makes sense to use their API. Now, the way it works is very simple. We capture the voice, and we convert it into the text. Then, we compare if the text is similar to something we have defined in the configuration file. If it matches with anything, the bash command associated with it will be executed.
First, we need to check whether the microphone is connected. To do that, run the following command:
lsusb
This command will show you a list of devices connected on USB. If you see yours on the list, then thumbs up, you are on the right track. Otherwise, try finding it with the connection or maybe try another hardware.
We also need to set the recording volume to high. To do this, go ahead and type the following command on the serial:
alsamixer
Now once the GUI pops on to the screen, toggle the volume using the arrow keys.
It's best to hear the sound recorded by yourself rather than directly giving it down to the Raspberry. To do that first, we need to record our voice, so we need to run the following command:
arecord -l
This will check whether the webcam is on the list. Then, write the following command to record:
arecord -D plughw:1,0 First.wav
The sound will be recorded with the following name, First.wav.
Now we would also like to listen to what we just recorded. The simple way to do that is by typing the following command:
aplay test.wav
Check whether the voice is correct. If not, then you are free to make any adjustments to the system.
Once we are done with checking the sound and the microphone, it's time to install the real software for the job. There are simple ways with which you can do it. The following is a list of commands that you need to run:
wget –- no-check-certificate “http://goo.gl/KrwrBa” -O PiAUISuite.tar.gz
tar -xvzf PiAUISuite.tar.gz
cd PiAUISuite/Install/
sudo ./InstallAUISuite.sh
Now when you run this, very interesting things will start to happen. It will start to ask you various questions. Some of them will be straightforward. You can use your right mind to give the answers to it in the form of yes or no. Others could be very technical. As these questions might change over time, there seems to be no need to explicitly mention the answers that you need to fill, but as a general rule of thumb—Give it a yes unless it's something you really want to say no to.
Perfect then, we have installed the software. Now before you go any further in that software, let's go ahead and write the following programs:
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
LIGHT = 2
GPIO.setup(LIGHT,GPIO.OUT)
GPIO.output(LIGHT, GPIO.HIGH)
os.system('echo "LIGHTS TURNED ON "|festival --tts')
Whenever this program runs, the light that is connected on PIN number 2 will be turned on. Also, it will read out LIGHTS TURNED ON. Save this file with the name lighton.py:
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
LIGHT = 23
GPIO.setup(LIGHT,GPIO.OUT)
GPIO.output(LIGHT, GPIO.LOW)
os.system('echo "LIGHTS TURNED OFF "|festival --tts')
Similarly, in this program, the light would be turned off and it would read out LIGHTS TURNED OFF. Save it by the name lightoff.py:
import RPi.GPIO as GPIO
import time
Import os
GPIO.setmode(GPIO.BCM)
FAN = 22
GPIO.setup(FAN,GPIO.OUT)
GPIO.output(LIGHT, GPIO.HIGH)
os.system('echo "FAN TURNED ON "|festival --tts')
Now we are doing the same thing for the fan as well. In this one, the fan will be switched on; save it with the name fanon.py:
import RPi.GPIO as GPIO
import time
Import os
GPIO.setmode(GPIO.BCM)
FAN = 22
GPIO.setup(FAN,GPIO.OUT)
GPIO.output(LIGHT, GPIO.LOW)os.system('echo "FAN TURNED OFF "|festival --tts')
I don't need to explain the same thing for this do I? As you will have guessed, save it with the name fanoff.py.
All right! When all of this is done, then type the following command to check whether the software is installed properly:
voicecommand -c
Raspberry Pi responds to the wake word pi; let's change it to jarvis. All these changes can be made after opening the configuration file using the following command:
voicecommand -e.
In that file, enter the commands of your own. Here, let's add the following code:
LIGHT_ON
LIGHT_OFF
FAN_ON
FAN_OFF
Now for each command, define the action. The action would be to run the Python file that contains the code for switching the lights and fan on or off. The code is basic and simple to understand. Add the following to the file:
LIGHT ON = sudo python lighton.py
LIGHT OFF = sudo python lightoff.py
FAN ON = sudo python fanon.py
FAN OFF = sudo python fanoff.py
Now, let's see what we have done. Whenever you say Jarvis, light on
, it will convert your speed to text, compare it with the program that it has to run corresponding to it and will do whatever is there in the program. Hence, in this program, whenever we say Light on,
the lights will be turned on and similarly for the rest of the commands as well. Remember to make it listen to what you are saying. You would have to say the word, Jarvis,
which will make it attentive to the commands and ready to listen.