This website uses browsing/session and functional cookies to ensure you get the best experience. Learn More

Grabbing sound with PortAudio

From Wiki for iCub and Friends
Jump to: navigation, search


Grabbing sound with PortAudio


This technical note documents some of our experiences as users of YARP interfacing to the PortAudio software. Our main aim was to grab sound from microphones attached to a PC. Our system was working on windows. To access sound device, we used PortAudio. It is a free, cross platform, open-source, audio I/O library which lets you write simple programs in C to deal with audio [1]. We used to latest version (v19) in our application.


To install PortAudio, you need to install Steinberg's ASIOSDK, first. You can get ASIOSDK from [2]. After the successful installation of PortAudio, Yarp should be recompiled. Before compilation, make sure that you enable PortAudio in Cmake.

Getting sound within your module

Once Yarp is compiled successfully, you are ready to use PortAudio. There is an example showing how you could start your code in the Yarp wiki page. You should define a grabber, and once you check the validity of the device, you can grab the sound by using the grabber through the view method of your device.

#include <yarp/dev/AudioGrabberInterfaces.h>
#include <yarp/sig/Sound.h>

polyDriver              dev("portaudio");
IAudioGrabberSound	*grabber;
Sound sound;

if (grabber!=NULL)
    if (grabber->getSound(sound))

Streaming PC104 sound on a port

The sounds heard by the iCub are recorded with two microphones connected to the stereo line-in input. It is important to notice that line-in capture and associated volume has to be enabled, e.g. using the alsamixer on the PC104.

An easy way to access the data captured by the iCub ears (two microphones installed in the iCub head and captured with the line-in stereo input of the PC104) is to instantiate a yarpdev. In order to do so, you have to be sure that the driver is properly configured and in particular that the line-in capture and the associated volume are enabled. A graphical user interface for enabling and disabling the sound is the alsamixer (see the screenshot on the right). Alternatively, you can excute the following shell commands for setting the Capture volume:

amixer -c 0 sset Capture,0 80%,80% unmute cap

and setting the Line-in capture volume:

amixer -c 0 sset Line,0 80%,80% unmute cap

Once this is done, you should execute the following:

icub@pc104:~$ yarpdev --device portaudio --id 1 --c 2 --read --rate 48000

The above command will open a port (default name is "/grabber") streaming data read from the portaudio device number 1. In order to hear (or dump on a file) what is actually streamed on the port you can instantiate a yarphear device as follows:

icub@myMachine:~$ yarphear --name /my/yarphear

and then connect the yarpdev port with the yarphear port:

icub@myMachine:~$ yarp connect /grabber /my/hear

Linux tips and tricks

In order to open a portaudio yarpdev you need to be one of the users in the audio group. In order to do so:

root@myMachine:~$ adduser icub audio

Moreover, you need to be sure that the sound modules have been loaded in the linux kernel. In order to check if the modules have been loaded you can check the output of the following command:

cat /dev/sndstat

If the audio modules are not correctly loaded (i.e. Audio devices: NOT ENABLED IN CONFIG) you should type:

root@myMachine:~$ modprobe snd-pcm-oss
root@myMachine:~$ modprobe snd-mixer-oss


  1., 2007
  2., 2007
Personal tools