#!/pro/bin/perl

# auto-mute.pl [ <default-volume>:30 [<alternate-volume>:45 [<max-volume>:100]]]
use strict;
use warnings;
use HTML::Entities;
use charnames ":alias" => ":pro";

# Playlist file for Radio Paradise and an alternate Radio Station
# was "http://www.radioparadise.com/modules/mini/mini_playlist.php?");
my $pls = "http://www2.radioparadise.com/nowplay_3.php";
my @pls = qw( radioparadise.pls pinkfloyd.pls );

use Getopt::Long;
my $opt_v = 0;
my $opt_x = 0;
my $opt_a = 0;
GetOptions (
    "a"		=> \$opt_a,	# Allow switch to alternate station
    "v"		=> \$opt_v,	# Verbose (more or less)
    "x|m|r"	=> \$opt_x,	# Don't start/switch xmms
    ) or die "usage: auto-mute.pl [-x] [<default_volume>]\n";

use Audio::Mixer;
use LWP::Simple;
use Time::HiRes qw( usleep );

system "clear";
binmode STDOUT, ":utf8";

my @def;
$def[0] = shift //  30;			# Default volume base
$def[1] = shift // 1.5 * $def[0];	# Volume alternate stream
$def[2] = shift // 100;			# Maximum volume

my $fad =  4;			# Fader time
my $cst = -1;			# Current station

my %vol = map { m/^\s*(\d+)\s+(.*)/; lc $2 => int ($1 * $def[0] / 30) } <DATA>;

sub volume ($)			# This fades in/out to the new volume
{
    my $v = shift;

    $v > $def[2] and $v = $def[2];

    my $c = (Audio::Mixer::get_cval ("pcm"))[0];
    $c == $v and return $v;

    my $x = ($v - $c) / (10 * $fad);	# .10 s steps
    Audio::Mixer::set_cval ("vol", 100, 100);
    while (($x > 0 ? $v - $c : $c - $v) > 0) {
	Audio::Mixer::set_cval ("pcm", $c, $c);
	usleep (100_000);
	$c += $x;
	}

    my $m = $v ? 100 : 0;
    Audio::Mixer::set_cval ("pcm", $v, $v);
    Audio::Mixer::set_cval ("vol", $m, $m);
    $v;
    } # volume

sub station ($)
{
    my $station = shift;
    $opt_x || $cst == $station and return;
    system "xmms $pls[$station] &";
    $cst = $station and volume ($def[$station] // $def[0]);
    } # station

#station (0);

# Prevent DNS calls
$pls =~ m{/([^/]+)/};
my $ip = join "." => unpack "CCCC", scalar gethostbyname $1;
$pls =~ s{/([^/]+)/}{/$ip/};

my $delay;
my $p = "";
while (1) {
    $opt_v and printf "   %02d:%02d fetch\n", (localtime)[1,0];
    my $page = get ($pls);
    $delay = 15;
    $page or next;
    $page =~ s/\s+/ /gs;
    $page =~ m/"refresh"\s+content="(\d+)"/ and $delay = $1 == 30 ? 5 : $1;
    my ($dm, $ds) = (int ($delay / 60), $delay % 60);
    my @p = ($page =~ m{(<a.*?)</a>}g);
    "@p" eq $p and next;
    $p[0] =~ m/>\s*(.*?)\s+-\s+(.*)/ or next;	# fetch error
    my ($g, $t) = map { decode_entities ($_) } ($1, $2);

    my @d = localtime;
    $p = "@p";
    my $vol = $vol{lc $g} // $def[0];
    printf "%02d:%02d:%02d %3d:%02d %2d %-28s - %s\n",
	@d[2,1,0], $dm, $ds, $vol, $g, $t;
    if ($vol == 0 && $opt_a) {
	station (1);
	next;
	}
    station (0);
    volume ($vol);
    } continue {
	sleep $delay;
	}
__END__
 0 Alarm
 0 Alpinestars
 0 Andy Stochansky
 0 Beatles
 0 Better Than Ezra
 0 Billy Bragg & Wilco
 0 Black Rebel Motorcycle Club
 0 Bono
 0 Bono & Adam Clayton
 0 Bono/Craig Armstrong
 0 Cake
 0 Chemical Bros.
 0 Chemical Brothers
 0 Coldplay
 0 Damien Rice
 0 David Byrne
 0 Death Cab For Cutie
 0 Delays
 0 Doves
 0 Echo & The Bunnymen
 0 Ellis Paul
 0 Evan Olson
 0 Fountains Of Wayne
 0 Franz Ferdinand
 0 George Harrison
 0 Gomez
 0 Grant Lee Buffalo
 0 Howie Day
 0 I Am Kloot
 0 Janah
 0 Jeff Buckley
 0 John Lennon
 0 Joseph Arthur
 0 Jump
 0 Keane
 0 Kent
 0 Mark Lanegan
 0 Muse
 0 My Morning Jacket
 0 Novastar
 0 Ours
 0 Paloalto
 0 Paul & Linda McCartney
 0 Paul McCartney
 0 Peter Murphy
 0 Placebo
 0 Radiohead
 0 Ryan Adams
 0 R.E.M. Michael Stipe & Bono
 0 Remy Zero
 0 Rufus Wainwright
 0 Simple Minds
 0 Slackshop
 0 South
 0 Starsailor
 0 Stevie Wonder
 0 The Alarm
 0 Travis
 0 U2
 0 Unified Theory
 0 Verve
20 Beck
50 Andreas Vollenweider
50 Air
50 Dire Straits
50 Garmarna
50 Ian Anderson
50 Jethro Tull
50 the Lash
50 Mark Knopfler
50 Natalie Merchant
50 Neil Young
50 Ozric Tentacles
50 Urban Trad
50 Zero 7
65 Asia
65 Camel
65 Chris Rea
65 Dire Straits
65 Hevia
65 Kate Bush
65 Loreena McKennitt
65 Mary Black
65 Peter Gabriel
65 Philip Glass
65 Pink Floyd
65 Roger Waters
65 Roger Waters/Sinead O'Connor
65 Rush
65 Sinead O'Connor
65 Solas
65 Supertramp
65 UK
65 Vienna Teng
