Saturday, January 12, 2013

Raspberry Pi: Programming with ALSA

As covered in a previous article, Raspberry Pi can be used to capture and playback audio using a USB sound card. One can do the capture using ALSA utility aplay and arecord, or programmatically using the ALSA API. 

Example recording can be found in list 4 here: 
http://www.linuxjournal.com/article/6735?page=0,2

To accommodate for my setup, I had to: 
  1. Change the hardware from "default" to "plughw:0,0" in snd_pcm_open(). 
  2. I also replaced the write() with an fwrite() to output to a file. 
To install the relevant headers, I had to install the following package: 

sudo apt-get install libasound2-dev

Finally, to do the compile, don't forget to link to the ALSA library using the -l option: 
gcc record.c -lasound

The example code performs the capture at 44100Hz, signed 16 bit, 2 channels, and output to simple raw format without any file headers. To verify the recording, I had to specify all the parameters using aplay: 
aplay -f s16_le -r 44100 -c 2 test.out

Thursday, January 10, 2013

Raspberry Pi Remote Camera Proof of Concept Implementation

As discussed before, the Raspberry Pi has a hardware accelerated H264 encoder and decoder. This allows us to build a remote camera solution. Given that the camera module has not been released, I faked the camera input using an rolling frame buffer.

Implementation can be seen here:
http://www.youtube.com/watch?v=5ac3nU-B4WQ

Implementation details:
  • 720p @ 30fps 
  • UDP transport
The end result is actually quite smooth. Clearly this example can be further extended into video conference application and such. 



Raspberry Pi recording/playback with Sound Blaster Play

Raspberry Pi doesn't come with an andio in jack. Audio in can only be supported using external USB sound card. I bought a Sound Blaster Play for this purpose. 

To enable USB audio output: 
sudo vi /etc/modprobe.d/alsa-base.conf

And comment out line: 
# options snd-usb-audio index=-2

This will enable USB audio output by default. Reboot now: 
sudo reboot

Now to record: 
arecord -D plughw:0,0 -f cd test.wav

To playback: 
aplay test.wav

Note that when recording using a non-standard format: 
arecord -D plughw:0,0 -f s16_le -r 8000 -c 1 -t raw test.raw

Don't forget to insert the -D plughw:0,0 option to force data translation: 
aplay -D plughw:0,0 -f s16_le -r 8000 -c 1 -t raw test.raw