Friday, January 28, 2011

Record speaker audio on Ubuntu

Ever wondered about recording audio playing on your computer speaker? Well, I did. There was this ringtone which i could play from a website but could not download it. So here you got and record it to your local file. I am using Ubuntu 9.04 (Karmic Koala). This has also been tested on various flavors of Ubuntu 10.04 and 10.10.

On the command line, install the following packages:
sudo apt-get install pulseaudio-utils
sudo apt-get install sox 
Open up your favorite editor (e.g. vi /tmp/record.sh) and paste the following code in it.
#!/bin/sh
WAV="$1" && [ -z "$WAV" ] && echo "Usage: $0 OUTPUT.WAV" && exit 1
rm -f "$WAV"
# Get sink monitor e.g. alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
MONITOR=$(pactl list | grep 'Monitor Source:' | awk '{print $3}')
[ -z "$MONITOR" ] && echo "Failed to fetch sink monitor" && exit 1
# Record it raw, and convert to a wav
echo "Recording to $WAV file ...Press CTRL-C to stop recording"
parec -d "$MONITOR" | sox -t raw -r 44k -sLb 16 -c 2 - "$WAV"
Save the file. I saved it in /tmp/record.sh. Change the permissions of /tmp/record.sh to 755.
chmod 755 /tmp/record.sh
/tmp/record.sh /tmp/filetostore.wav
Now play any song from your favorite website and  run /tmp/record.sh /tmp/filetostore.wav, it should record  the audio in wav format in /tmp/filetostore.wav.

The output file is in wav format. You may use audacity to convert the output file into the formats of your choice e.g. mp3. Audacity is available in ubuntu repositories and you can install it using: apt-get install audacity.

Enjoy!


No comments:

Post a Comment