#!/usr/local/bin/perl -w # # Danger! Danger! Running with scissors! Carry handgrenades without pins! Driving without brakes! # # This code is a super simple super stupid web-based MP3 player concept program. It has *no* error # checking. It probably has security holes out the whazoo. It uses a template system so lame, the # person who wrote it should be shot (wait a minute!). If two users try to use it at the same time, # bad things will happen (because of the stupid HTTP::Daemon not supporting printing a string as a # response, not so much anything else). And worst of all, I wrote it using Windows(tm) based tools # (VisualSlickEdit and HotMetalPro ROCK!) # # On the other hand, it does demonstrate a standalone web-based application, using all those great # little CPAN modules. I'm sure [merlyn] and [tilly] will pump dozens of rounds into it, saying # how there's a better way to do it. On the third hand, I wrote it half an hour, it passes use strict # and -w, and I did it first. Nyah! (I also don't care that stuff goes past column 80) # # Last, and leastly, sometimes if you control-c out of the app (or Dog forbid, it should crash), you # may have to wait a few seconds for the socket to clear before restarting it. I don't know why the # socket stays allocated for a brief time after the app has exited. # use strict; use HTTP::Daemon; use HTTP::Status; use CGI; my $serverport = 2000; my %actions = ('Next' => \&mp3_next, 'Last' => \&mp3_last, 'Stop' => \&mp3_stop, 'Play' => \&mp3_play, 'Vol +10%' => \&mp3_volumeup, 'Vol -10%' => \&mp3_volumedown, 'Left 10%' => \&mp3_balanceleft, 'Right 10%' => \&mp3_balanceright, ); my @tracklist = ('Pink Floyd - Dark Side Of The Moon', 'Widespread Panic - Bombs & Butterflies', 'Allgood - Trilogy', 'Mox - Dr. Bombay', 'Soundtrack - Debbie Does Dallas', 'The Orb - Perpetual Dawn', 'Tangerine Dream - Underwater Sunlight' ); my $mode = 'Stopped'; my $curtrack = 0; my $volume = 50; my $balance = 50; # # # { my $htmltext = 0; # # Stupid HTTP::Daemon doesn't support sending a string as a response, only a file. So # we generate index.html, and keep the page in $htmltext for when we change parameters. # { local $/ = undef; $htmltext = ; } my $d = new HTTP::Daemon (LocalPort => $serverport) || die "Can't bind to port! ($serverport already in use?)"; print "Please contact me at: url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET') { ultralame_template_system ($htmltext); $c->send_file_response ("index.html"); } else { my $q = new CGI ($r->content); &{$actions {$q->param ('Action')}} if ($q->param ('Action')); ultralame_template_system ($htmltext); $c->send_file_response ("index.html"); } } $c->close; undef ($c); } } sub ultralame_template_system { my $temptext = shift; $temptext =~ s//@tracklist [$curtrack]/ei; $temptext =~ s//$mode/ei; $temptext =~ s//$volume/ei; $temptext =~ s//$balance/ei; open (FH, ">index.html") || die "Can't open file: $!"; print FH $temptext; close FH; } # # Normally, I don't condone oneliners like this, but for sample code, it'll work # sub mp3_next { $curtrack = ++$curtrack % scalar @tracklist; } sub mp3_last { $curtrack = --$curtrack % scalar @tracklist; } sub mp3_stop { $mode = 'Stopped'; } sub mp3_play { $mode = 'Playing'; } sub mp3_volumeup { $volume = min (100, $volume + 10); } sub mp3_volumedown { $volume = max (0, $volume - 10); } sub mp3_balanceleft { $balance = max (10, $balance - 10); } sub mp3_balanceright { $balance = min (100, $balance + 10); } # # # sub min { return ($_[0] < $_[1] ? $_[0] : $_[1]); } sub max { return ($_[0] > $_[1] ? $_[0] : $_[1]); } __DATA__ Stupid Sample Code For a Pico MP3 Server
Track Now Playing:

Mode:
Volume: %
Balance: %