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!)

Thursday, May 10, 2012

Check for logged users

At office, we have a cluster made of several machines used for performance experiments. We are not a lot of people, so we explicitly avoided installing software for launching experiments in a controlled way.

Nevertheless, seldom happens that someone remotely logs in and launches stuff, without noticing, for example, if other users were logged or where running programs, thus invalidating any output from (long) runs.

So, at last, I wrote the following bash script which, upon ssh login to the servers, outputs a fancy colored message telling that someone else is logged in (if any).

#!/bin/bash

#for user in $(last | grep still | sort | uniq);
ME=`whoami`



for user in $(last -Rw | grep still | sed 's/ .*//' | sort | uniq);
do
        if [ "$ME" != "$user" ]
        then
                echo -en '\E[;31m'"\033[1mWARNING:\033[0m"   # Red
                echo -en '\E[;34m'"\033[1m \033[4m$user\033[0m \033[0m"
                tput sgr0
                echo -n "still logged in since "
                echo `last -RFw | grep $user | head -1 | awk '{print $4" "$5" "$6}'`
        fi
done

I've simply added this script to /etc/bash.bashrc, so that every user gets a warning message, if needed.