P-Brain & Microphone setup on Rasp Pi 2

After having some problems setting up my microphone on my Raspberry Pi 2 I decided to document my experiences in the hope that it will help someone else who might get frustrated.

After seeing P-Brains brilliant video’s on YouTube and decided to give it a go.  I downloaded and installed per the P-Brain Git hub site here.

I had a Raspberry Pi 2 knocking around and decided to give it a try.

Setting up the microphone

First I did a fresh install of Raspian (NOOBS)

We’ll need to update the firmware, this requires Internet access but only takes 15 minutes or so. You might want to run sudo apt-get update first if you haven’t lately. Then run the following commands in order:

sudo apt-get update
sudo apt-get upgrade
sudo reboot

Plugin your USB microphone.  I got mine from Adafruit here

If the steps below for configuring the microphone do not work for you then take a look at Adafruit’s excellent tutorial here.

Now see what USB devices are present with command:

lsusb
Bus 001 Device 006: ID 19ff:0225 Dynex
Bus 001 Device 005: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter
Bus 001 Device 004: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Then list your recording device:

arecord -l
**** 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

Here the recording device is card 1, device 0, or hw1:0.

Change your ~/.asoundrc file using command:

sudo nano ~/.asoundrc

And copy/paste below:

pcm.!default {
 type plug
 slave.pcm "dsnooped"
}
ctl.!default {
 type hw
 card 1
}

pcm.dsnooped {
 type dsnoop
 ipc_key 125
 slave.pcm "hw:1,0"
}

The type dsnoop and ipc_key allow you to record from the microphone concurrently from 2 different processes.  I think P-Brain requires you to use PulseAudio to solve this concurrency, but I had problems PulseAudio not starting on reboot and did not want to fix it.

You should now be able to record audio:

rec test.wav

and playback into headphone jack with:

aplay test.wav

You can adjust the gain of the microphone using alsamixer tool.

alsamixer

Problems with background noise

P-Brain uses a feature if the microphone recording sotfware “rec” to detect periods of silence in the audio so it knows when to stop recording and send the audio question to Wit.ai for natural language parsing.

I had a problem where there was so much background high pitched sound that the audio recording never stopped.

Enter the command below to test audio recording to see if it can detect the silence in the audio stream.

rec test.wav rate 16k silence 1 0.1 3% 1 2.0 3%

This command will detect speech and after 2 seconds of silence will terminate.  You can play back the test.wav file with:

aplay test.wav

If it does not terminate and the audio level gauge at the bottom of the terminal is moving up and down, even when the room is quiet, then you may also have the same problem with background high pitched sound (1 kHz).

 

After lots if googling it seems that the Raspberry Pi has problems with power noise on the USB ports that may producing the high pitched sound.

What worked for me was to reduce this power noise by moving the USB microphone to a USB Hub, that has its own power supply and try again.  That fixed my problem enough that the “rec” command above worked as expected.

Now onto trying out my P-Brain again.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s