#!/usr/bin/env perl # MP3 FreeRADIO random mp3 player, v1.2 ## Start user config # $config = "/usr/local/etc/rc.mp3radio"; # web url to refresh $refresh = "http://localhost/radio/playing.html"; # input from mp3radio.server $usersel = "/nfs/usersel"; # remote playing file $coreplay = "/nfs/play"; # remote playing file HTML $corehtml = "/nfs/playing.html"; # lock for mp3radio.server $slock = "/nfs/serverlock"; # /dev/null set for your shell $null = ">/dev/null 2>&1"; # # End user config use File::Copy; use MPEG::MP3Info; sub Config { open C, $config || warn "\nCouldn't open $config: $!\nUsing defaults\n\n"; while () { chomp; next if /^\#/; next if /^$/; next if (!m/(.*)=\s*(.*)/); @line = split/\s*\=\s*/; push @{$conf{$line[0]}}, $line[1]; } } $conf = &Config; $conf{home}[0] = "/usr/local/lib/mp3radio" unless defined $conf{home}[0]; $conf{play_plan}[0] = "/home/playing/.plan" unless defined $conf{play_plan}[0]; $conf{mp3_link}[0] = "/mp3" unless defined $conf{mp3_link}[0]; $conf{playhtml}[0] = "/home/httpd/html/playing.html" unless defined $conf{playhtml}[0]; $conf{Log}[0] = "/nfs/.logs/Current_Month" unless defined $conf{Log}[0]; $conf{player}[0] = "mpg123 -b2 -q -m" unless defined $conf{player}[0]; $conf{nice}[0] = "nice --10" unless defined $conf{nice}[0]; $conf{aumix}[0] = "aumix" unless defined $conf{aumix}[0]; $conf{say}[0] = "say -x 1250" unless defined $conf{say}[0]; $home = $conf{home}[0]; $play_plan = $conf{play_plan}[0]; $mp3_link = $conf{mp3_link}[0]; $playhtml = $conf{playhtml}[0]; $Log = $conf{Log}[0]; $player = $conf{player}[0]; $nice = $conf{nice}[0]; $aumix = $conf{aumix}[0]; $say = $conf{say}[0]; $playing = "$home/playing"; $nope = "$home/nope"; $req = "$home/.req"; $id = "$home/id.mp3"; $mailcmd = "/bin/mail -s mp3radio admin"; # who gets server email $answer = 1; # Check for -nope argv # if ($ARGV[0] =~ /(-nope|-n\w*)/i) { &MP3_Radio_Nope; } # SysV argv thing. Comment out if it annoys you # else { $argv = "mp3radio"; for ($i = 1; $i < 80; $i++) { $argv = $argv . "\000"; } $0 = $argv; } unlink "$slock", "$playhtml", "$playing", "$coreplay", "$corehtml"; srand (time() ^ ($$ + ($$ >> 15))); open C, "$home/.count" || die "Can't open $home/.count: $!\n"; $count = ; close C; sub Do_It { if ((0xffff & system $args) != 0 ) { print STDERR "error: $!\n"; exit 1; } } sub MP3_Radio_Nope { print "\n\n\n>>>> Shutting down MP3 FreeRADIO after current playing MP3 is ". "finished.\n\n\n\a"; open F, ">$home/nope" || warn "Can't create nope file: $!\n"; exit 0; } sub Station_Id { # top of hour id, kludge but usually works if ($start_hour != $end_hour) { &Do_It($args = "$aumix -w 76 $null"); &Do_It($args = "$nice $player $id $null"); } } sub Test_It { $answer = grep $_ eq $num, @array; } sub Split { open L, "/bin/ls -l $mp3_link/$num|" || warn "Can't open $mp3_link/$num: $!\n"; while () { $k = (split)[10]; $k =~ s/.mp3//g; $k =~ tr/\///d; $k =~ tr/_/ /; ($l, $m) = split '-', $k; } close L; } sub Copy_Play { copy "$playing", "$play_plan"; copy "$playing", "$coreplay"; } sub PlayHTML { &Copy_Play; open PH, ">$playhtml" || warn "Can't open $playhtml: $!\n"; print PH "\n\nCurrent Playing Song\n". "\n". "\n
\n";
     open P, $playing || warn "Can't open $playing: $!\n";
     while (

) { print PH; } close P; print PH "

\n\n\n"; close PH; copy "$playhtml", "$corehtml"; } sub Play_Out { $info = get_mp3info "$mp3_link/$num"; $mins = $info->{MM}; $secs = $info->{SS}; $mp3secs = ($mins * 60) + $secs; print P "\n\n Artist ... Song Title\n ~~~~~~~~~~~~~~~~~~~~~~\n\n $l ... $m\n". "\n\n Song Info\n ~~~~~~~~~\n\n". " Number : $num\n"; printf P " Length : %d minute%s and %d second%s\n", $mins, ($mins == 1) ? "" : "s", $secs, ($secs == 1) ? "" : "s"; printf P " Encoding : MPEG %d layer %d, %d kbps @ %d kHz\n", $info->{VERSION}, $info->{LAYER}, $info->{BITRATE}, $info->{FREQUENCY}; } sub Log_It { push @array, $num; open LOG, ">>$Log" || warn "Can't open $Log: $!\n"; print LOG "$hour:$min:$sec GMT $mon/$mday/$year $num\t$l ... $m\n"; close LOG; } sub Play_It { &Do_It($args = "$aumix -w 80; $say number $num $null"); &Do_It($args = "$say $l $null"); &Do_It($args = "$say $m $null"); &Do_It($args = "$aumix -w 76; $nice $player $mp3_link/$num $null"); } sub Time { ($sec, $min, $hour, $mday, $mon, $year) = gmtime(time + $mp3length); $hour = sprintf "%02.0f", $hour; $min = sprintf "%02.0f", $min; $sec = sprintf "%02.0f", $sec; $mon = sprintf "%02.0f", $mon + 1; $mday = sprintf "%02.0f", $mday; $year = sprintf "%02.0f", $year + 1900; } sub MP3RadioNope { if (-z $nope) { open P, ">$playing" || warn "Can't open $playing: $!\n"; print P "\n\n\t\t\tDone.\n\n"; close P; &Copy_Play; open P, ">$playhtml" || warn "Can't open $playhtml: $!\n"; print P "\n\n". "\n". "

Done.
\n". "\n\n"; close P; copy "$playhtml", "$corehtml"; unlink "$usersel", "$slock", "$nope"; open F, "|$mailcmd" || warn "Can't open $mailcmd: $!\n"; print F scalar gmtime, "\n"; foreach (@array) { print F "$_\n"; } exit 0; } } sub MP3RadioSel { if (-f $usersel) { open U, $usersel || warn "Can't open $usersel: $!\n"; open R, ">>$req" || warn "Can't open $req: $!\n"; while () { print R; tr/0-9//csd; $sel = $_; } close R; close U; &Test_It($num = $sel); if ($answer == 0) { open P, ">$playing" || warn "Can't open $playing: $!\n"; &Split($num = $sel); &Play_Out($num = $sel); &Time($mp3length = 0); $start_hour = $hour; print P " Start time : $hour:$min:$sec $mon/$mday/$year GMT\n"; &Log_It($num = $sel); &Time($mp3length = $mp3secs); print P " End time : $hour:$min:$sec $mon/$mday/$year GMT\n". "\n\n This is an user requested song.\n\n\n"; close P; &PlayHTML; unlink "$slock", "$usersel"; &Play_It($num = $sel); &Time($mp3length = 0); $end_hour = $hour; &Station_Id; } else { unlink "$usersel", "$slock"; } } } # Do forever, or until there is a $nope file while () { &MP3RadioNope; &MP3RadioSel; &MP3RadioNope; &MP3RadioSel; $ran = int(rand $count) + 1; &Test_It($num = $ran); if ($answer == 0) { open P, ">$playing" || warn "Can't open $playing: $!\n"; &Split($num = $ran); &Play_Out($num = $ran); &Time($mp3length = 0); $start_hour = $hour; print P " Start time : $hour:$min:$sec $mon/$mday/$year GMT\n"; &Log_It($num = $ran); &Time($mp3length = $mp3secs); print P " End time : $hour:$min:$sec $mon/$mday/$year GMT\n". "\n\n"; close P; &PlayHTML; &Play_It($num = $ran); &Time($mp3length = 0); $end_hour = $hour; &Station_Id; } }