Saturday, September 10, 2011

Recording with PulseAudio

Sometimes I find that I want to record the sound coming out of Hal's speakers. Say that my automatic recording of Prairie Home Companion goes south because an earthquake/hurricane/remnants of a tropical storm knocked out my power. It would be nice to be able to go the Prairie Home web site, click on the archive for the show, play the streaming download and record it.

I've done this kind of thing for years, mostly without the earthquakes. Occasionally there have been problems, but it's always possible to find a solution. After which I write it down so that I'll remember how to fix things.

Except sometimes my notes aren't helpful, because somebody decided it was a good idea to change things. Take a look at the above link. To be able to record sound, I'm suppose to open Gnome's Sound preferences, click on Recording, and click on Capture. Let's look at gnome-volume-control as it's shipped with Ubuntu 11.04:

Gnome Sound Preferences

Looks nothing like what was described above, does it?

So what happened? Beats me. All I know is that someone changed things, and that the changes have something to do with PulseAudio, Ubuntu's choice for sound.

Fortunately, there is a Ubuntu Wiki article that solved things for me. Of course, the information you want is buried way down at the bottom of the article, so let me summarize it here, down at the bottom of this article. (Yes, I admit to being long-winded.)

First, you need to set up the PulseAudio volume control. For reasons known only to Mark Shuttleworth, this isn't installed by default in 11.04, and goes by the perfectly obvious name of pavucontrol. So install it first,

sudo apt-get install pavucontrol

and open it, either by typing pavucontrol from the command line or clicking "PulseAudio Volume Control" under Applications=>Sound & Video. You'll get something like this:

PulseAudio Volume Control (pavucontrol)

Not very helpful, is it? The problem is that there is no application open trying to record sound. So let's open up Gnome Sound Recorder:

Gnome Sound Recorder (with nothing recording)

Start something playing, and press Record. PulseAudio will now look something like this:

Pulse Audio Volume Control with Gnome Sound Recorder Operating

The key to making things work is the line that says:

Sound Recorder: Record Stream from: …

You may have several options, but what you want is Monitor of Internal Audio Analog Stereo. When this is selected Gnome Sound Recorder will record what's coming out of the speakers, and will look something like this:

Gnome Sound Recorder recording

You can see it is recording as the brown (surprise!) bar at the bottom moves in time with the music's volume.

We'll leave the rest of this, e.g., recording directly to .ogg or .mp3, converting from .flac to .ogg, using Audacity instead of Sound Recorder, etc., as an exercise for the reader. Just remember, to record sound with Ubuntu 11.04, you need to use Pulse Audio Volume Control to tell whichever application you are using to record to monitor the internal audio.

Until someone decides it's time for a change.

Sunday, March 23, 2008

A Programmable Audio Recorder

You might remember that I like to listen to A Prairie Home Companion at the gym. That link shows you how to take the Real Audio stream from the PHC Archive and turn it into an mp3 or ogg file.

And that works, everywhere except with Verizon FIOS, which for some reason only lets streams in RTSP be played by a real RealPlayer client. Probably a firewall issue. I've tried tracking it down, but haven't gotten up the energy to try to talk my way up the FIOS help staff to find someone who would tell me how to fix it.

But, as someone once said, “there's more than one way to do it.” For example, I could use Realplayer to play the file through the speakers, and then record the output either by invoking SoX as rec, using gnome-sound-recorder, or using Audacity (which I can't get to work in this mode). Any of these methods, of course, ties up the speakers for two hours, not very desirable.

Alternatively, one could stumble upon the fact that Minnesota Public Radio (MPR) plays as an http (port 80) continuous stream on the web, that Prairie Home Companion airs for two hours starting at 5pm Central Time (6pm Eastern) every Saturday Night, and that FIOS allows mplayer to listen to that stream. Oh, and that MPR puts on a 15-20 second advertisement when you start the download.

With that information, we construct this little script, which you should save in a directory that the /bin/sh shell can find. We'll call it mpr_record, and we thank Penguin Pete for the hint on how to put all of this in a box:

#! /bin/bash

# Get to the correct directory

cd $HOME/audio/podcasts/prairie_home_companion

# get year month date

year=`date +%y`
month=`date +%m`
day=`date +%d`

# Print out what we want to do:

echo Recording to phc_$year$month$day.flac

# Record for 122 minutes:  One minute before the official start,
# and one minute after the official end, just in case our clocks
# are off, and to account for the initial MPR ad.

# You don't have to use the flac format,
# Using .wav will end up giving you a smaller .mp3 file
# with a loss of quality that won't show up at the gym.

# You might not need the two-step process, just save directly
# to .mp3 or .ogg, but my notes say this isn't very efficient
# in terms of final file size.

# The 1> /dev/null makes sure all of the streaming information that
# mplayer continuously spits out doesn't make it into the final
# output.

# http://www.prairiehome.org/play/128.pls is linked to the MPR stream.

mplayer -endpos 2:02:00 -vc null -vo null -ao pcm:fast -ao pcm:file=phc_$year$month$day.flac http://www.prairiehome.org/play/128.pls 1> /dev/null

# Convert to mp3
# ogg produces a smaller file, but my player doesn't do ogg.

echo Converting to phc_$year$month$day.mp3

# Note that SoX must be recompiled to support MP3s, if that's
# what you want here.  Actually, that link doesn't work on Ubuntu,
# I had to recompile from source.  Someday I'll write that up.

sox phc_$year$month$day.flac phc_$year$month$day.mp3

# Of course, ffmpeg would work just as well:

# ffmpeg -i phc_$year$month$day.wav phc_$year$month$day.mp3

# Edit id3 info.  This adds both id3v1 and id3v2 data.
# My labeling scheme is designed to overcome the limits of my
# player.  You might want to label yours differently.  See
# 'man id3v2' for all the options.

echo Labeling phc_$year$month$day.mp3

id3v2 -t $year$month$day -g 100 -y 20$year -A NPR -a "Prairie Home Companion" -T 1 phc_$year$month$day.mp3


So, if you've read all the comments, you'll see that this records from MPR for two hours and two minutes. Now we just need to make sure this starts one minute before 6pm (Eastern US), every Saturday. If you've been following along this blog, you know this is a job for cron. The crontab entry we need is

59 17 * * 6 mpr_record > $HOME/audio/podcasts/prairie_home_companion/mpr.errors 2>&1

which starts the recording at 17:59 (5:59pm) local time (Eastern, here), on the sixth day of the week, running the program mpr_record and putting all of its output into an error file, just in case we need to track things down.

Of course, what we've done is turned the machine into the Internet Audio version of a programmable VCR. This can be used for any regularly scheduled broadcast the mplayer can play.

Thursday, January 31, 2008

Capturing Sound – or – a Case of Daemonic Possession

The long-time reader will remember that occasionally I record onto the computer. Usually using Audacity, but for many purposes gnome-sound-recorder does well enough.

Except when it doesn't. So I've been sitting here for the last two hours trying to figure out why, when I plug the tape deck into the line-in jack, start the tape, and press record, I don't get anything to record, even though I can hear the sound coming out of the computer's speakers, and even though things have worked in the past.

So after much searching, playing with JACK, tearing out my hair, yelling at the cats, etc., I finally found an appropriate post in Ubuntu forums. Note particularly the phrase “I had the Capture volume down.” Turns out I haven't done any sound recording since I upgraded to 7.10. Does Ubuntu automagically reset defaults when you upgrade? Anyway, following this post, I opened up the gnome-volume-control, clicked on Edit=>Preferences, clicked on line-in capture, and hit record.

Nothing.

OK, go back to the preferences menu. Look down. See an unchecked box labeled Capture. Click that.

Nada.

Sigh. But now the main gnome-volume-control panel has a tab on it that says “Recording.” Click on that. Up pops a panel that says “Capture,” with the Microphone symbol muted. Click that. Bring the capture volume up to full. Record.

It works.

HOW DID THIS HAPPEN? I had it all working before, you know, which meant that all those appropriate boxes were checked and buttons were pushed. AGGGH! Are there little elves running around my computer arbitrarily turning things on and off?

Well, anyway, I got it working, and this here post will remind me of it the next time it happens.