Controlling your TV with voice and Raspberry Pi
What’s cooler than controlling your TV with voice commands?
A billion dollars!
But for this post,
it’s controlling your TV with voice commands through Raspberry Pi!
(and a billion dollars…)
So, down to business.
Full code can be found on GitHub.
Hardware requirements:
- TV set with enabled HDMI-CEC / Anynet+ (Samsung) - You’ll have to check your own TV set and make sure that you enable the HDMI-CEC control.
- Raspberry Pi - I recommend the CanaKit Raspberry Pi 2. For this tutorial, the Raspberry Pi should be connected to the TV set via HDMI.
- USB microphone - I used C-Media microphone USB
Install usb microphone on Raspberry Pi
It seems that just connecting the USB microphone is not enough with the Raspberry Pi, you actually have to enable it on the Raspberry Pi OS in order to use it:
1) Edit /etc/modprobe.d/alsa-base.conf with your favorite editor
1.1) Set the value options snd-usb-audio index=-2 to be options snd-usb-audio index=0
1.2) On the next line add: options snd_bcm2835 index=1
2) Reboot your Raspberry Pi
3) From the Raspberry Pi terminal, run lsusb.
Make sure that you can see your device in the list (in my case “C-Media Electronics, Inc. CM108 Audio Controller”):
4) Run “cat /proc/asound/cards” and verify that your USB mic is set on device 0:
Install requirements (on the Raspberry Pi)
Install basics
Install pyaudio
1) Clone the pyaudio git repository
2) Go to the pyaudio folder and run the install command
Install flac
Flac is an audio compression format (like MP3).
If you don’t have it, pyaudio will not be able to run.
Install required python modules
Last step before creating our script is to install a few python packages. I usually like to create virtual environments for these projects.
In your virtual environment install the following packages:
- Speechrecognition - Library for performing speech recognition with the Google Speech Recognition API.
- pyaudio - provides Python bindings for PortAudio, the cross-platform audio I/O library
- python cec - Python bindings for libcec. This will be used to control the TV through HDMI.
Creating the script
This is a very simple script, which performs the following steps:
- Initialize the required components
- Start running an infinite loop until there’s a stop command:
- Record audio through the microphone
- Translate the audio to text
- Check if text contains a command
- Perform the required command
Step 1: Initialize required components
We’ll start by importing the required packages:
We need to define the commands that we want to use. For this example I chose the following commands:
- Turn the TV on
- Turn the TV off
- Close program
Initialize CEC control
Create speech recognition object
Step 2: Record and analyze audio
In an infinite loop, record audio through the microphone
Translate the audio to text
Check if the command is to turn the TV on
Check if the command is to turn the TV off
Check if the command is to stop the script
This is how the full code looks like:
If you run the script, you can say the commands and check in the script output if it was able to translate your speech correctly and run the correct command.
That’s it!
Full code can be found on GitHub.
So, what’s cooler than controlling your TV with voice commands?
A billion dollars!