Raspberry Pi audio

Posted on 7th December 2019

My son and I are attempting to follow the instructions in Pi Mag 88 to build a voice activated LED-powered Christmas tree decoration. This is part of what I suspect shall be many posts documenting my attempts to actually use the couple of Raspberry Pis which we own.

Up first is recording audio on the Pi. I purchased this cheap USB microphone. It works, but it's rather poor quality. With a bit more research, I might have bought a Playstation Eye which is nearly as cheap, and apparently pretty much works out the box with the Pi. We played with the Microphone on our Pi 3 connected up to monitor, keyboard etc. but eventually we want to use my Pi 0 in headless mode.

Access a Pi via SSH

Install Raspberian and boot the Pi 0. With only one USB port and no working hub, you can run the initial setup with a keyboard quite easily. An internet search will reveal how to move the mouse cursor with the keyboard. Using the Preferences, enable SSH access. We can now run the Pi 0 headless and SSH in. I use Putty as a windows SSH client.

To use function keys (which we'll want to use shortly) you need to run Putty with the "keyboard" option as "Xterm R6" (and not "linux"...)

Checking the microphone

Plug in the USB microphone, reboot the Pi 0, and SSH in. Run

arecord -l

I got the following:

**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

The USB microphone shows up as "card 1" for me. Now run

alsamixer -c 1

to access the mixer for card 1. Here we need our function keys to work. Increase the capture level to 100.

To make a test recording:

arecord -D hw:1,0 -d 5 -f cd test.wav -c 1

This makes a 5 second recording, cd quality, mono "-c 1", using card 1, subdevice 0. Once copied to my computer and loaded into Audacity the result is okay, though very quiet.


We'll now need to install Portaudio. I am following the instructions here:

sudo apt-get install python-pyaudio python3-pyaudio sox

sudo pip3 install pyaudio

Maybe also:

sudo apt-get install libasound-dev

sudo apt-get install portaudio19-dev


aplay -l

Shows card 0, subdevice 0 as a suitable playback device (we won't actually use playback) and with card 1, subdevice 0 as the microphone (as above) we now edit (or create)

nano ~/.asoundrc

and change / create the file to have contents:

pcm.!default {
type asym
playback.pcm {
    type plug
    slave.pcm "hw:0,0"
capture.pcm {
    type plug
    slave.pcm "hw:1,0"

Then run

rec test.wav

and check it works.

Getting snowboy working

I wanted to play with SnowBoy which is an offline "hot word" detector (rather than following the Pi Mag's suggestion of an electrical button to make the Pi listen for commands).

First we download the file for Raspberry pis, then scp it over:

scp .\snowboy-rpi-arm-raspbian-8.0-1.1.1.tar.bz2 pi@

Then decompress and extract:

bzip2 -d snowboy.tar.bz2

tar xvf snowboy.tar

We'll need some more libraries:

sudo apt-get install libatlas-base-dev

This is Python 2 only, and I get a stream of error messages. A bit of debugging shows that we get a stream of non-fatal errors when initialising the pyaudio.PyAudio() object. The fatal error comes from trying to open the audio input at 16000Hz. 44kHz works fine. However, if I manually change the recording to 44kHz no speech recognition occurs: probably because the "model" and "data" are not incompatible.

At this point, I get bored with Python 2, and wonder if I can get Python 3 working, before we start some serious re-engineering of the project. The GitHub repo has Python 3 support, but it needs building. Thankfully sigmadel has nicely described the steps to do this. I followed this up until the point after running Swig. At this point, my aim is to extract a minimal working copy of the files needed for Python 3. I copied all the files from examples/Python3 to a new directory, and then fixed the missing symbolic links by copying the actual files over them. The next post will explain what I did then.

Recent posts