[BACK]Return to mixer.pl CVS log [TXT][DIR] Up to [local] / mp3 / bin

Annotation of mp3/bin/mixer.pl, Revision 1.5

1.1       andrew      1: #!/usr/bin/perl -Tw
1.5     ! andrew      2: # $AFresh1: mixer.pl,v 1.4 2011/06/14 17:04:58 andrew Exp $
1.1       andrew      3: ########################################################################
                      4: # Mixer.pl *** Changes volume on the MP3 player
                      5: #
                      6: # 10/11/2002 4:26PM
                      7: # Written by andrew fresh <andrew@mad-techies.org>
                      8: ########################################################################
                      9: use strict;
1.4       andrew     10: use warnings;
1.1       andrew     11:
                     12: my $mixerctl = '/usr/bin/mixerctl';
                     13:
                     14: my $me = MyName();
                     15:
                     16: delete $ENV{PATH} if exists $ENV{PATH};
                     17:
1.4       andrew     18: my $paused_file = '/var/www/mp3/playlist/paused';
1.1       andrew     19:
                     20: my @Vols = (
1.5     ! andrew     21:     7,   15,  23,  31,  39,  47,  55,  63,  71,  79,  87,  95,
        !            22:     103, 111, 119, 127, 135, 143, 151, 159, 167, 175, 183, 191,
        !            23:     199, 207, 215, 223, 231, 239, 247, 255,
        !            24: );
1.1       andrew     25:
1.5     ! andrew     26: my $Cur_Vol = `$mixerctl outputs.master`;
1.1       andrew     27:
                     28: #»print "Cur_Vol $Cur_Vol<BR>\n";
                     29: $Cur_Vol =~ /outputs.master=(\d+),(\d+)/i;
1.5     ! andrew     30: $Cur_Vol = ( $1 + $2 ) / 2;
1.1       andrew     31:
1.4       andrew     32: my $pause_action = 'pause';
1.5     ! andrew     33: if ( -e $paused_file ) {
1.4       andrew     34:     $pause_action = 'play';
                     35: }
1.1       andrew     36: print "Content-Type: text/html\n\n";
                     37: print "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> ";
                     38:
1.5     ! andrew     39: my ( $prefix, $action ) = split /\&/, $ENV{'QUERY_STRING'};
1.4       andrew     40: $prefix ||= '';
                     41: if ($action) {
                     42:     $prefix =~ s/%(..)/pack("c",hex($1))/ge if $prefix;
                     43:     $action =~ s/%(..)/pack("c",hex($1))/ge;
                     44:
1.5     ! andrew     45:     print "<meta HTTP-EQUIV=Refresh CONTENT=\"1; URL=mixer.pl?"
        !            46:         . EncodeURL($prefix) . "\">";
1.3       andrew     47:
1.5     ! andrew     48:     if ( $action eq 'pause' ) { $pause_action = Pause($pause_action) }
        !            49:     elsif ( $action =~ /^(\d+)$/ ) { $Cur_Vol = Set_Vol( $Cur_Vol, $1 ); }
1.3       andrew     50:
1.1       andrew     51: }
                     52:
                     53: print <<EOL;
                     54:
                     55: <style type="text/css" media="screen">
                     56: <!--
                     57: a.down:link { color: red; text-decoration: none; }
                     58: a.down:visited { color: red; text-decoration: none; }
                     59: a.down:hover { color: red; text-decoration: none; }
                     60: a.down:active { color: red; text-decoration: none; }
                     61: a.up:link { color: blue; text-decoration: none; }
                     62: a.up:visited { color: blue; text-decoration: none; }
                     63: a.up:hover { color: blue; text-decoration: none; }
                     64: a.up:active { color: blue; text-decoration: none; }
                     65: -->
                     66: </style>
                     67:
                     68: EOL
                     69:
                     70: print "<title>Volume Mixer</title></head>\n<body>\n\n";
                     71:
                     72: Print_Nav();
                     73:
1.3       andrew     74: unless ($prefix) {
1.1       andrew     75:
1.5     ! andrew     76:     my @nav;
        !            77:     push @nav,
        !            78:           "<B><A HREF='$me?"
        !            79:         . EncodeURL($prefix) . '&'
        !            80:         . "pause' class='pause'>$pause_action</A></B>\n";
        !            81:     push @nav, " - Volume: ";
        !            82:
        !            83:     foreach my $vol (@Vols) {
        !            84:         my ( $char, $class ) = ( $vol, 'up' );
        !            85:
        !            86:         if    ( $vol > $Cur_Vol ) { ( $char, $class ) = ( '}', 'up' ); }
        !            87:         elsif ( $vol < $Cur_Vol ) { ( $char, $class ) = ( ']', 'down' ); }
        !            88:
        !            89:         push @nav,
        !            90:               "<B><A HREF='$me?"
        !            91:             . EncodeURL($prefix) . '&'
        !            92:             . "$vol' class='$class'>$char</A></B>\n";
1.1       andrew     93:
1.5     ! andrew     94:     }
        !            95:     print join " ", @nav;
1.4       andrew     96:
1.5     ! andrew     97:     #print "<br>\n";
1.3       andrew     98: }
1.1       andrew     99:
                    100: print "</body>\n</head>\n</html>\n";
                    101:
1.5     ! andrew    102: sub Set_Vol {
        !           103:     my ( $old, $new ) = @_;
1.1       andrew    104:
1.5     ! andrew    105:     my $Set_Vol;
        !           106:     foreach my $vol ( $old < $new ? $old .. $new : reverse $new .. $old ) {
        !           107:         $Set_Vol = `$mixerctl -w outputs.master=$vol,$vol`;
        !           108:     }
        !           109:     return $new;
1.4       andrew    110: }
                    111:
                    112: sub Pause {
                    113:     my ($action) = @_;
                    114:
1.5     ! andrew    115:     if ( $action eq 'play' ) {
1.4       andrew    116:         unlink $paused_file;
                    117:         return 'pause';
                    118:     }
                    119:     else {
                    120:         open my $fh, '>', $paused_file or die;
                    121:         print $fh time;
                    122:         close $fh;
                    123:         return 'play';
                    124:     }
1.1       andrew    125: }
                    126:
                    127: ########################################################################
                    128: # *** EncodeURL: %encodes the parameters of the URL
                    129: sub EncodeURL {
1.5     ! andrew    130:     my $strURL = shift || '';
        !           131:     $strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
        !           132:     return $strURL;
1.1       andrew    133: }
                    134:
                    135: #########################################################################
                    136: # My name
                    137: sub MyName {
1.5     ! andrew    138:     my @filename = ( split /\//, $0 );
        !           139:     pop @filename;
1.1       andrew    140: }
                    141:
                    142: #######################################################################
                    143: # Bail: this subrouting dies and displays the error to the browser.
1.5     ! andrew    144: # gotten from the example in the O'Reilly
1.1       andrew    145: # _Learning_Perl_on_Win32_Systems_
1.5     ! andrew    146: sub bail {
        !           147:     my $error = shift;
        !           148:     print "\n\nUnexpected Error: <b>$error</b><br>\n";
        !           149:     print "In " . __FILE__ . "\n";
        !           150:     die;
        !           151:     exit;
        !           152: }
        !           153:
        !           154: sub Print_Nav {
        !           155:     open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!";
        !           156:     while (<FILE>) {
        !           157:         s/%PREFIX%/$prefix/g;
        !           158:         print;
        !           159:     }
        !           160:     close FILE;
1.1       andrew    161: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>