version 1.3, 2007/02/08 20:04:03 |
version 1.5, 2011/06/14 18:05:23 |
|
|
#!/usr/bin/perl -Tw |
#!/usr/bin/perl -Tw |
# $RedRiver: mixer.pl,v 1.2 2006/08/12 00:14:53 andrew Exp $ |
# $AFresh1: mixer.pl,v 1.4 2011/06/14 17:04:58 andrew Exp $ |
######################################################################## |
######################################################################## |
# Mixer.pl *** Changes volume on the MP3 player |
# Mixer.pl *** Changes volume on the MP3 player |
# |
# |
|
|
# Written by andrew fresh <andrew@mad-techies.org> |
# Written by andrew fresh <andrew@mad-techies.org> |
######################################################################## |
######################################################################## |
use strict; |
use strict; |
use diagnostics; |
use warnings; |
|
|
my $mixerctl = '/usr/bin/mixerctl'; |
my $mixerctl = '/usr/bin/mixerctl'; |
|
|
|
|
|
|
delete $ENV{PATH} if exists $ENV{PATH}; |
delete $ENV{PATH} if exists $ENV{PATH}; |
|
|
|
my $paused_file = '/var/www/mp3/playlist/paused'; |
|
|
my @Vols = ( |
my @Vols = ( |
[ 7, 7], |
7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, |
[ 15, 15], |
103, 111, 119, 127, 135, 143, 151, 159, 167, 175, 183, 191, |
[ 23, 23], |
199, 207, 215, 223, 231, 239, 247, 255, |
[ 31, 31], |
); |
[ 39, 39], |
|
[ 47, 47], |
|
[ 55, 55], |
|
[ 63, 63], |
|
[ 71, 71], |
|
[ 79, 79], |
|
[ 87, 87], |
|
[ 95, 95], |
|
[103, 103], |
|
[111, 111], |
|
[119, 119], |
|
[127, 127], |
|
[135, 135], |
|
[143, 143], |
|
[151, 151], |
|
[159, 159], |
|
[167, 167], |
|
[175, 175], |
|
[183, 183], |
|
[191, 191], |
|
[199, 199], |
|
[207, 207], |
|
[215, 215], |
|
[223, 223], |
|
[231, 231], |
|
[239, 239], |
|
[247, 247], |
|
[255, 255], |
|
); |
|
|
|
|
|
my $Cur_Vol = `$mixerctl outputs.master`; |
my $Cur_Vol = `$mixerctl outputs.master`; |
|
|
#»print "Cur_Vol $Cur_Vol<BR>\n"; |
#»print "Cur_Vol $Cur_Vol<BR>\n"; |
$Cur_Vol =~ /outputs.master=(\d+),(\d+)/i; |
$Cur_Vol =~ /outputs.master=(\d+),(\d+)/i; |
my $Cur_Vol_Left = $1; |
$Cur_Vol = ( $1 + $2 ) / 2; |
my $Cur_Vol_Right = $2; |
|
|
|
|
my $pause_action = 'pause'; |
|
if ( -e $paused_file ) { |
|
$pause_action = 'play'; |
|
} |
print "Content-Type: text/html\n\n"; |
print "Content-Type: text/html\n\n"; |
print "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> "; |
print "<html>\n<head>\n\t<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'> "; |
|
|
my ($prefix, $New_Vol_Left, $New_Vol_Right) = split /\&/, $ENV{'QUERY_STRING'}; |
my ( $prefix, $action ) = split /\&/, $ENV{'QUERY_STRING'}; |
if ($New_Vol_Left || $New_Vol_Right) { |
$prefix ||= ''; |
$New_Vol_Left =~ s/%(..)/pack("c",hex($1))/ge; |
if ($action) { |
$New_Vol_Right =~ s/%(..)/pack("c",hex($1))/ge; |
$prefix =~ s/%(..)/pack("c",hex($1))/ge if $prefix; |
print "<meta HTTP-EQUIV=Refresh CONTENT=\"1; URL=mixer.pl?" . EncodeURL($prefix) . "\">"; |
$action =~ s/%(..)/pack("c",hex($1))/ge; |
} |
|
|
|
$New_Vol_Left ||= $Cur_Vol_Left; |
print "<meta HTTP-EQUIV=Refresh CONTENT=\"1; URL=mixer.pl?" |
$New_Vol_Right ||= $Cur_Vol_Right; |
. EncodeURL($prefix) . "\">"; |
|
|
if ($New_Vol_Left =~ /(\d+)/) { $New_Vol_Left = $1; } |
if ( $action eq 'pause' ) { $pause_action = Pause($pause_action) } |
if ($New_Vol_Right =~ /(\d+)/) { $New_Vol_Right = $1; } |
elsif ( $action =~ /^(\d+)$/ ) { $Cur_Vol = Set_Vol( $Cur_Vol, $1 ); } |
|
|
if ($prefix) { |
|
$prefix =~ s/%(..)/pack("c",hex($1))/ge; |
|
} else { |
|
$prefix =''; |
|
} |
} |
|
|
print <<EOL; |
print <<EOL; |
|
|
Print_Nav(); |
Print_Nav(); |
|
|
unless ($prefix) { |
unless ($prefix) { |
print "Volume - "; |
|
|
|
#print "New_Vol $New_Vol_Left:$New_Vol_Right<BR>\n"; |
my @nav; |
|
push @nav, |
|
"<B><A HREF='$me?" |
|
. EncodeURL($prefix) . '&' |
|
. "pause' class='pause'>$pause_action</A></B>\n"; |
|
push @nav, " - Volume: "; |
|
|
my @nav; |
foreach my $vol (@Vols) { |
foreach my $vol (@Vols) { |
my ( $char, $class ) = ( $vol, 'up' ); |
if ($$vol[0] == $New_Vol_Left) { |
|
push @nav, "<B><A HREF='$me?" . EncodeURL($prefix) . '&' . "$$vol[0]&$$vol[1]' class=up>$$vol[0]</A></B>\n"; |
|
} elsif ($$vol[0] < $New_Vol_Left) { |
|
push @nav, "<A HREF='$me?" . EncodeURL($prefix) . '&' . "$$vol[0]&$$vol[1]' class=up>}</A>\n"; |
|
} elsif ($$vol[0] > $New_Vol_Left) { |
|
push @nav, "<A HREF='$me?" . EncodeURL($prefix) . '&' . "$$vol[0]&$$vol[1]' class=down>]</A>\n"; |
|
} |
|
#print "$vol - $$vol[0] - $$vol[1]<br>\n"; |
|
} |
|
print join " ", @nav; |
|
#print "<br>\n"; |
|
|
|
Set_Vol($Cur_Vol_Left, $Cur_Vol_Right, $New_Vol_Left, $New_Vol_Right); |
if ( $vol > $Cur_Vol ) { ( $char, $class ) = ( '}', 'up' ); } |
|
elsif ( $vol < $Cur_Vol ) { ( $char, $class ) = ( ']', 'down' ); } |
|
|
|
push @nav, |
|
"<B><A HREF='$me?" |
|
. EncodeURL($prefix) . '&' |
|
. "$vol' class='$class'>$char</A></B>\n"; |
|
|
|
} |
|
print join " ", @nav; |
|
|
|
#print "<br>\n"; |
} |
} |
|
|
print "</body>\n</head>\n</html>\n"; |
print "</body>\n</head>\n</html>\n"; |
|
|
|
sub Set_Vol { |
|
my ( $old, $new ) = @_; |
|
|
|
my $Set_Vol; |
|
foreach my $vol ( $old < $new ? $old .. $new : reverse $new .. $old ) { |
|
$Set_Vol = `$mixerctl -w outputs.master=$vol,$vol`; |
|
} |
|
return $new; |
|
} |
|
|
sub Set_Vol |
sub Pause { |
{ |
my ($action) = @_; |
my $old_vol_right = shift; |
|
my $old_vol_left = shift; |
|
my $new_vol_left = shift; |
|
my $new_vol_right = shift; |
|
|
|
my $old_vol; |
|
my $new_vol; |
|
if (($old_vol_left + $old_vol_right) > ($new_vol_left + $new_vol_right)) { |
|
$old_vol = $old_vol_left > $old_vol_right ? $old_vol_left : $old_vol_right; |
|
$new_vol = $new_vol_left < $new_vol_right ? $new_vol_left : $new_vol_right; |
|
} else { |
|
$old_vol = $old_vol_left < $old_vol_right ? $old_vol_left : $old_vol_right; |
|
$new_vol = $new_vol_left > $new_vol_right ? $new_vol_left : $new_vol_right; |
|
} |
|
|
|
#print "Old: $old_vol - New: $new_vol<br>\n"; |
if ( $action eq 'play' ) { |
|
unlink $paused_file; |
my $Set_Vol; |
return 'pause'; |
|
} |
foreach my $vol ($old_vol < $new_vol ? $old_vol..$new_vol : reverse $new_vol..$old_vol) { |
else { |
#print "$vol<br>\n"; |
open my $fh, '>', $paused_file or die; |
$Set_Vol = `$mixerctl -w outputs.master=$vol,$vol`; |
print $fh time; |
#print $Set_Vol . "<br>\n"; |
close $fh; |
} |
return 'play'; |
return $Set_Vol; |
} |
} |
} |
|
|
|
|
|
|
######################################################################## |
######################################################################## |
# *** EncodeURL: %encodes the parameters of the URL |
# *** EncodeURL: %encodes the parameters of the URL |
sub EncodeURL { |
sub EncodeURL { |
my $strURL = shift; |
my $strURL = shift || ''; |
$strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg; |
$strURL =~ s/(\W)/sprintf("%%%x", ord($1))/eg; |
return $strURL; |
return $strURL; |
} |
} |
|
|
######################################################################### |
######################################################################### |
# My name |
# My name |
sub MyName { |
sub MyName { |
my @filename = (split /\//, $0); |
my @filename = ( split /\//, $0 ); |
pop @filename; |
pop @filename; |
} |
} |
|
|
|
|
####################################################################### |
####################################################################### |
# Bail: this subrouting dies and displays the error to the browser. |
# Bail: this subrouting dies and displays the error to the browser. |
# gotten from the example in the O'Reilly |
# gotten from the example in the O'Reilly |
# _Learning_Perl_on_Win32_Systems_ |
# _Learning_Perl_on_Win32_Systems_ |
sub bail { |
sub bail { |
my $error = shift; |
my $error = shift; |
print "\n\nUnexpected Error: <b>$error</b><br>\n"; |
print "\n\nUnexpected Error: <b>$error</b><br>\n"; |
print "In " . __FILE__ . "\n"; |
print "In " . __FILE__ . "\n"; |
die; |
die; |
exit; |
exit; |
} |
} |
sub Print_Nav |
|
{ |
sub Print_Nav { |
open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!"; |
open FILE, 'nav.inc' or die "\n\ncouldn't open FILE nav.inc: $!"; |
while (<FILE>) { |
while (<FILE>) { |
s/%PREFIX%/$prefix/g; |
s/%PREFIX%/$prefix/g; |
print; |
print; |
} |
} |
close FILE; |
close FILE; |
} |
} |