braingrove.com

Embedded | Heat Table | LCD | Power Control |UBW | SAM7 Board | Spectral Analysis | USB Thermometer | WiFi Spin | | Radio Tivo | Linux Kiosk | Designer Firewalls | Designer Print Servers

The Radio Tivo Project

Radio Tivo

Use Linux to record your favorite radio shows to mp3, even while you are asleep.

Using Linux to Record Scheduled Radio to MP3

The idea is to take a decent radio, pipe it's output to a soundcard of a computer and transform it to MP3. Easy, right? Sort of. Turns out, the MP3 recording was the easy part. It was the mixer settings that gave us headaches. First we recommend getting a decent radio. For AM recording we found the GE "Superadio" to be excellent despite its goofy name.

First Cron:

00 01 * * 1-5 /root/bin/start_radio.sh 2>&1 > /dev/null
00 05 * * 1-5 /root/bin/end_radio.sh 2>&1 > /dev/null      

We can start and stop the recordings easy enough. The start script performs the following.

#!/bin/bash
# start radio record
/usr/local/bin/sox -V -t ossdsp -r 16000 /dev/dsp /sound/ftp-pub/stuff_to_keep/radio/`date +\%F`_`date +\%s`.mp3

Easy enough, you can see the sox command above which does the heavy lifting... the stop script goes as follows.

#!/bin/bash
# end radio script
#  Here we kill any running radio records, and
#  copy files over to repository, setting permissions
#  A call is amde to a script to clean up the direcory.
DEST_DIR=/sound/ftp-pub/stuff_to_keep/radio
/usr/bin/killall /usr/local/bin/sox
chown -R root:soundwrite $DEST_DIR
chmod -R 770 $DEST_DIR
/usr/bin/perl /root/bin/clean_radio.pl

The above really kills the start process, changes the resulting files permissions, and the runs a final script called clean_radio to copy the file to a known location and delete any old files. The Clean_radio script follows.

#! /usr/bin/perl -w
use strict;
my %dates;
my %sizes;
my $radio_dir = '/sound/ftp-pub/stuff_to_keep/radio';
print process_dated_files();
sub process_dated_files
{
    my @list_of_all;
    #get list of files
    my @files = `find $radio_dir -type f`;
    foreach my $file (@files)
    {
        chomp($file);
        #target format 2005-02-05_1107583200.mp3
        if ($file !~ /[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{10}\.mp3$/)
        {
            print "*Ignoring $file...\n";
            next;
        }
        else
        {
            print "Including $file...\n";
            push (@list_of_all,$file);
        }
    }
    foreach my $file (@files)
    {
        my @parts = split(/\//, $file);
        #print @parts;
        my $last_part = $parts[$#parts]; # lose the path
        @parts = split(/_/, $last_part);
        $last_part = $parts[$#parts]; #lose stuff before epoch secs
        @parts = split(/\./, $last_part);
        $last_part = $parts[0]; #lose  extension
        print "$file has epoch secs $last_part\n";
        # add entries hash
        $dates{$file} = $last_part;
    }
    my @sorted;
    @sorted =  (sort { $dates{$a} <=> $dates{$b} } (keys(%dates)));
    foreach my $key (@sorted)
    {
        #       print ":$key\n";
    }
    my $keep_latest_val = 4;
    my @delete_list;
    #print "examining $target (set to keep latest $keep_latest_val):\n";
    for (my $i=0; $i<= $#sorted; $i++)
    {
        if ($i <= ($#sorted-$keep_latest_val))
        {
            #print "\t* $i:$sorted[$i] $sizes{$sorted[$i]}\n";
            print "\t* $sorted[$i]\n";
            push (@delete_list,$sorted[$i]);
        }
        else
        {
            print "\t  $sorted[$i]\n";
            #print "\t  $i:$sorted[$i] $sizes{$target}{$sorted[$i]}\n";
        }
    }
    print "\n";
    print "*** The items starred above are about to be DELETED!\n";
    print "hit ctrl-c now to abort...or ENTER to continue...\n";
    #<STDIN>;
    #print @delete_list;
    foreach my $item (@delete_list)
    {
        print "Removing $item...\n";
        #print '`rm -vr '."$item\n";
        `rm -vr $item`;
    }
}

Not the greatest script for cleaning up, but having been ripped out of a previous project, the fact that it works is beauty enough.

Now the Bad News

After all that good work, what could go wrong? The mixer settings, that's what. Each time you restart the system, the mixer settings need to be reapplied. But wait, there are several mixer utilities to choose from... which one to use? Well, start with the obvious Linux question, "Which ones actually WORK?" We used aumix and got this to work after way too much screwing around.

Use the 'play' command to replay and test your setup. Note that the radio Tivo work was performed on a CentOS 4 system. Any of you familiar with Redhat know they have some philosophical problems with MP3 (or perhaps more accurately, the license and ownership of MP3). We had to download all utilities for MP3 work (sox et al.) and recompile them for MP3 support.

Also, we do not quite have a handle on /dev/audio vs /dev/dsp and the mic vs input inputs. The mixer seems to represent these, but they do not seem to always do what they should.

 

 

About Us | Site Map | Privacy Policy | Contact Us | Administration(auth reqd) | ©2003 braingrove.com