Thomas Fischer's Weblog

Life, Linux, LaTeX

Archive for March 2010

Recording VoIP Phone Calls with ALSA

with one comment

Recently I stumbled upon the problem of how to record a phone call when using Skype (or any other VoIP technology) under Linux. For Windows and Mac OS X, there is a bunch of commercial software available, but nothing for Linux. But hey, it is open source and everything is documented, so why not figuring out how to do it anyway?

After searching for options, I found a pointer to the tee device, which works similar like the tee command on the command line: You can transparently pipe data through it and a copy of all data will be written to a file as well. A new ALSA output device can be configured in ~/.asoundrc using the tee device. It can either record the default device or a specific device:

pcm.tee00 {
        type empty
        slave.pcm "tee:'plughw:0,0','/tmp/out0.wav',wav"
}

Above example will save all audio recording going through plughw:0,0 to file /tmp/out0.wav. If you have different recording and playback devices (e.g. an USB microphone and standard speakers), add multiple section with the corresponding plughw number (also change the device’s name, e.g. pcm.tee10).
In the VoIP application, you have to set the new sound devices for microphone and speakers accordingly:

Skype Audio Settings
(numbers and names of devices may differ for you!)

If you make a test call, you will get two output files: one with your microphone’s recording and one with your speaker’s recording. Using a sound editor like Audacity or SoX, you can edit and merge both files.

This approach, however, has one drawback. Every time an application “opens” a sound device, the output file will be recreated and the previous recording will be lost. With Skype, the hangup sound at the end of the recording is such a case. I haven’t found out if you can configure ALSA’s tee device to append to a file instead of overwriting it, but the solution I came up with is to copy the output files to a different location before the phone call is over. To automatize the process, I wrote the following shell script:

#!/bin/sh

while true ; do
        timestamp=$(date '+%Y%m%d-%H%M%S')
        for wav in /tmp/out*.wav ; do
                test -f "$wav" || break
                newwav="$HOME/out/"$(echo "$wav" | sed -e 's/\/tmp\///;s/.wav$/-'$timestamp'.wav/')
                echo "Copying  $wav  to  $newwav"
                cp "$wav" "$newwav"
        done
        sleep 10
done

When running, it will copy every 10 seconds all output wav files from /tmp/ to the out/ directory in your home and append a time stamp to the file name. You simply start this script before starting the phone call and exit it with Ctrl+C. Then, the largest of files out0*.wav or out1*.wav, respectively, in the out directory will be your microphone or speaker recording, respectively.

Written by Thomas Fischer

March 20, 2010 at 12:00

Posted in Linux