Using your voice to control things

Starting and stopping our listening device to record what's going on might be a bit tricky when our device is hidden, so wouldn't it be great if we could use a voice command to start recording when something interesting is happening?

Well, using a tool called Voice Command installed on our Pi allows us to do this. It was created for the Raspberry Pi by a guy called Steven Hickson and it's based on the Google Voice API, and as such is pretty reliable and accurate.

Because it uses the Google Voice API, Voice Command requires Internet access to be able to process your voice. If you want to use offline voice recognition, then look at something like Jasper (http://jasperproject.github.io), although it needs plenty of storage space and is not as accurate as online systems.

Give your Pi some ears

There's no install package for Voice Command so we need download the code and build it on our Pi. To do this, we need to ensure that we have git-core installed so that we can download it from the Git repository, although on the latest version of Raspbian Jessie you should already have it. Check and install with the following command:

pi@raspberrypi ~ $ sudo apt-get install git-core

It also needs this library, as it doesn't automatically include it in its dependency script:

pi@raspberrypi ~ $ sudo apt-get install libboost-regex1.49.0

Now we can download the Voice Command files:

pi@raspberrypi ~ $ git clone git://github.com/StevenHickson/PiAUISuite.git

Once downloaded we can install it:

pi@raspberrypi ~ $ cd PiAUISuite/Install/
pi@raspberrypi ~ $ ./InstallAUISuite.sh

As part of the installation, it will ask if you want to install dependencies too. Press Y when you're asked this. It will then download a bunch of libraries it requires to run.

The installer will also ask lots of questions about installing other stuff as well. Say N to not to install them and just say Y to the voicecommand option at the end.

When Voice Command has finished installing (it may take a while), it will ask you if you want it to help you set it up. Say Y to this. You can also do this at any time by using the voicecommand -s command if you want to change the options.

The configuration process will ask you the following questions. Respond to them as shown:

pi@raspberrypi:~ $ voicecommand -s
Opening config file...
Do you want to permanently set the continuous flag so that it always runs continuously? (y/n)
n
Do you want to permanently set the verify flag so that it always verifies the keyword? (y/n)
y
Do you want to permanently set the ignore flag so that it never looks for answers outside the config file? (y/n)
y
Do you want to permanently set the quiet flag so that it never uses audio out to speak? (y/n)
n
Do you want to permanently change the default duration of the speech recognition (3 seconds)? (y/n)
n
Do you want to permanently change the default command duration of the speech recognition (2 seconds)? (y/n)
n
Do you want to set up and check the text to speech options? (y/n)
n
Do you want to set up and check the speech recognition options? (y/n)
y
First I'm going to make sure you have the correct hardware device
Everything seems right with the hardware config
Would you like me to try to get the proper audio threshold? (y/n)
y
I'm going to record you once while you are silent and then once while you say the command in order to determine the threshold
Getting ready for silent recording, just don't say anything while this is happening, press any key when ready
Recording WAVE '/dev/shm/noise.wav' : Signed 16 bit Little Endian, Rate 16000 Hz, Stereo
Getting ready for command recording, try saying the command while this is happening, press any key when ready
Recording WAVE '/dev/shm/noise.wav' : Signed 16 bit Little Endian, Rate 16000 Hz, Stereo
I detected that your default thresh: 12.180400 is different than the thresh I detected that you should use: 1.492800
Should I set that in the config file? (y/n)
y
The default keyword of the system is "pi"
Do you want to change the keyword? (y/n)
n
Done setting everything up!
pi@raspberrypi:~ $

We can now configure Voice Command to respond to our, umm, voice commands:

To edit the configuration file, type:

pi@raspberrypi ~ $ voicecommand -e

After displaying some blurb, it will take you into Nano so that you can change its settings. You might want to read the documentation for Voice Command by running the following command, which loads the manual pages:

pi@raspberrypi ~ $ voicecommand -h

However, here's a basic configuration file I have set up which will get you started. It's a little bit more straightforward and simpler than the default one that's set up during installation. You can replace that one with this:

Give your Pi some ears
Voice Command configuration file

If you take a look at the highlighted part of the screenshot, you'll see that I've added four voice commands. Add these commands to your file so that you can try them for yourself. The format of the configuration is voice_command==action, so in the first example, by saying display it will echo to the console whatever is said after display - that's what the ellipses (...) means. So if I said display hello world, I would expect the words hello world to be output onto the terminal console.

The second command will have espeak say back whatever I say after the command parrot.

The third command will simply reboot the Pi after saying reboot.

And the last one is just an example of how to run a given script, passing in the parameter that I give after saying the word switch. So if I say switch on it will run the equivalent of running this:

/home/pi/switch.sh on

Let's take a look at a basic switch.sh script that responds to our voice commands. When we pass in the parameter on, it will begin to record in the background using the sox command we explored earlier. When we use the parameter off, it will kill the sox process if it's running:

#!/bin/bash 
# switch.sh $1 
 
#get the parameter that was passed in 
case "$1" in  
 "on" )  
   #record using SoX 
   sox -t alsa plughw:1 -t wav - | lame - myrec.mp3 & 
 ;;  
 
 "off" )  
   pkill sox 
 ;;  
 
 * ) 
   echo "Option not recognised" 
esac
Voice controlled switch.sh script

The keyword to activate the voice commander is pi by default, so in our switch example we operate it like this:

  1. Start off by running Voice Command in continuous mode:
    pi@raspberrypi ~ $ voicecommand -c
    
  2. Say pi clearly into your microphone
  3. Wait for the Pi to response with Yes Sir?
  4. Say switch on. Your switch script should now run, switching on whatever it's supposed to do - in this case the recording command from earlier.
  5. Say switch off. Your switch script should now run switching off the recording process by killing the process.

As you can see, this has massive scope for us secret agents to be mischievous in a very covert manner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.196.172