Snowboy is a hotword detection engine from Kitt ai. They have prepared a easy to use vocal command recogniton that can be used straight in you project. If you have a company or a start up contact them.

What we want to do

This post is the first part of the guide. We will first set up the snowboy environment, the python wrapper and finally we will run the demo provided by the team. You will see how easy can be to get a hotword detection software. In the second part we will train the classifer with our own voice using the web interface and prepare a simple python script to run the voice detector.
For this guide we will be using a Raspberry Py 3 with an external sound card.

Prerequisites

We will need to install some packages. First, install Swig, which is an interface compiler that connects programs written in C and C++ with scripting languages such as Perl, Python, Ruby.

We will follow the steps provided in this official page taking advantage of git.

Let’s install all required packages to compile Swig. Run on a terminal:

userk@dopamine:~$ sudo apt-get install git autoconf automake gcc bison libpcre3

Now, build the latest version from the master branch of the swig repository:

userk@dopamine:~$ cd && mkdir -p programs/libraries/ && cd programs/libraries
userk@dopamine:~/programs/libraries$ git clone https://github.com/swig/swig.git
userk@dopamine:~/programs/libraries$ cd swig
userk@dopamine:~/programs/libraries/swig$ ./autogen.sh
userk@dopamine:~/programs/libraries/swig$ ./configure
userk@dopamine:~/programs/libraries/swig$ make
userk@dopamine:~/programs/libraries/swig$ sudo make install

errors?
If you get an error during the configuration step caused by libpcre3, then install try
./configure –without-pcre

To run the snowboy demo you will need the following, depending on which demo you use and what platform you are working with:

– SoX (audio conversion)
– PortAudio or PyAudio (audio capturing)
– ATLAS or OpenBLAS (matrix computation)


You can also find the exact commands you need to install the dependencies on Ubuntu or Raspberry Pi below:

userk@dopamine:~$ sudo apt-get install python-pip python-setuptools python-pyaudio python3-pyaudio sox libatlas-base-dev
userk@dopamine:~$  pip install pyaudio

Ok, now try to record an audio sample with

userk@dopamine:~$ rec t.wav
userk@dopamine:~$ play t.wav

You should hear something now.

errors?

If you see errors like: ALSA lib confmisc.c:767:(parse_card) cannot find card ‘0’
Try the following steps before going further:
– Check the lsusb command and make sure that the external USB sound card is detected. Plug in the usb adapter, execute lsusb, remove it, execute it and compare the outputs. Can you see a difference?
– Try to list all available sound cards. Do you get an USB audio device?

userk@dopamine:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Edit the alsa.conf file and set the USB audio adapter as default card sudo vim /usr/share/alsa/alsa.conf and change 0 to 1 so that the default card is card: 1

defaults.ctl.card 1
defaults.pcm.card 1

If with the previous command you get:

userk@dopamine:~ $ arecord -l
arecord: device_list:270: no soundcards found...
userk@dopamine:~ $ sudo !!
sudo arecord -l
sudo: unable to resolve host epinephrine
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Try with root privileges. If it shows a hardware device then you have to add your user to the audio group

userk@dopamine:~ $ sudo usermod -aG audio $USER

Get hands dirty

Well done, now let’s clone the git repo and compile a python wrapper.

userk@dopamine:~$ cd ~/programs
userk@dopamine:~/programs$ git clone https://github.com/Kitt-AI/snowboy.git && cd snowboy
userk@dopamine:~/programs/snowboy$ cd swig/Python
userk@dopamine:~/programs/snowboy/swig/Python$ make

Now you should see the following content in the Python folder:

userk@dopamine:~/programs/snowboy/swig/Python$ l
Makefile            snowboy-detect-swig.cc  snowboy-detect-swig.o
_snowboydetect.so*  snowboy-detect-swig.i   snowboydetect.py
userk@dopamine:~/programs/snowboy/swig/Python$ cd ../..
userk@dopamine:~/programs/snowboy$

SWIG will generate a _snowboydetect.so file and a simple python wrapper snowboydetect.py

Test the demo
Make sure you have the snowboy.umdl universal model in the resources folder under the snowboy directory

userk@dopamine:~/programs/snowboy$ ll resources/
total 2300
drwxr-xr-x 3 userk userk   4096 Oct  9 18:27 alexa
-rw-r--r-- 1 userk userk 975564 Oct  9 18:27 alexa.umdl
-rw-r--r-- 1 userk userk 487498 Oct  9 18:27 common.res
-rw-r--r-- 1 userk userk  15782 Oct  9 18:27 ding.wav
-rw-r--r-- 1 userk userk  12996 Oct  9 18:27 dong.wav
-rw-r--r-- 1 userk userk 811554 Oct  9 18:27 snowboy.umdl
-rw-r--r-- 1 userk userk  30030 Oct  9 18:27 snowboy.wav

Daje, let’s test the demo by providing the universal model of the Snowboy hotkey.

userk@dopamine:~/programs/snowboy$ cd examples/Python
userk@dopamine:~/programs/snowboy/examples/Python $ ls
__init__.py         demo_arecord.py    snowboydecoder.pyc
_snowboydetect.so@  demo_threaded.py   snowboydecoder_arecord.py
demo.py             requirements.txt   snowboydetect.py@
demo2.py            resources@         snowboydetect.pyc
demo3.py            snowboydecoder.py  snowboythreaded.py
userk@dopamine:~/programs/snowboy/examples/Python $ python demo.py ../../resources/snowboy.umdl
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
Listening... Press Ctrl+C to exit

say snowboy!

INFO: Keyword 1 detected at time: 2017-10-10 14:41:44

As you may have noticed, we are getting some warnings from the script. Kill pulseaudio and start the jack_control

userk@dopamine:~/programs/snowboy/examples/Python $ pulseaudio --kill
-bash: pulseaudio: command not found
userk@dopamine:~/programs/snowboy/examples/Python $ sudo apt-get install pulseaudio
userk@dopamine:~/programs/snowboy/examples/Python $ pulseaudio --kill
userk@dopamine:~/programs/snowboy/examples/Python $ sudo apt-get install jackd2
userk@dopamine:~/programs/snowboy/examples/Python $ jack_control start

If jack_control returns exceptions or errors, the carry on and leave the warnings. This step is not compulsory.

If you are getting other warning messages like:

ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline

Then open /usr/share/alsa/alsa.conf and comment out each line in the PCM INTERFACE section of the file related to the card you have got problem from. For example, let’s consider the first warning linked to cards.pcm.center_lfe. Find the line below and comment it.
From this:

pcm.center_lfe cards.pcm.center_lfe

To:

#pcm.center_lfe cards.pcm.center_lfe

Ok the first part is over. Please comment below if you need help.

Check out the second one!