Showing posts with label audio. Show all posts
Showing posts with label audio. Show all posts

Friday, May 11, 2012

Record audio output

So, here's the story. We live in a web-radio era / audio-streaming world.
The old-days audiotape is no longer helpful in recording sounds around us.

Thus, after some headache, this is my modern "Linux audiotape", which I use to record.

Basically, it relies on pulseaudio (I'm running alsa with pulseaudio on my arch distro).
In particular, this script queries pulseaudio to know which is the first audio sink installed in the system (that is, the first audio source that is currently being directed to the output soundcard) and creates a dummy sink which is used to take the output and redirect it into a file.

Then, since we do not want to fill up our disk with raw pcm directed to the speakers, the signal is pipe'd into an ogg encoder, which silently compresses our data and stores it into disk.

#!/bin/bash

sinksIndex=`echo list-sink-inputs | pacmd | grep index | head -1 | sed 's/^.*index:\s\([0-9]\+\).*$/\1 /'` # Get wanted index

echo "Recording to audioRecord.ogg... CTRL+C to stop."

pactl load-module module-null-sink sink_name=audioRecord
pactl move-sink-input $sinksIndex audioRecord
parec -d audioRecord.monitor | oggenc -b 192 -o audioRecord.ogg --raw -

The only important thing to consider, is that to record exactly what we mean to, we must stop any audio stream in the system, start the one which we want to grab, and then launch this script.

This will work with youtube, webradios, audio streaming services, and so long and so forth. To restore the audio setting, after the recording is over, you must restart pulseadio with:

pulseaudio --kill

And furthermore, the program which was generating must be restarted as well. Otherwise, no sound will be heard from your machine.

And by the way, if you want to hear what's going on in your recording, you can just launch something like 'vlc audioRecord.ogg': it will play you the recording while it's still writing the file! (thank you, Unix File System!)