#!/usr/bin/perl -w # # Utterly Basic Audio Volume Set/Report Utility # # This can be used through the crontab to announce volume changes # throughout the day, or to change the volume silently. This is # what I use in my personal .crontab: # ########################################################## # # # turn the sound up to "daytime" level # 15 10 * * * mixer -s 67 # # turn the sound down to "afternoon-rest" level # 59 13 * * * mixer -s 60 # # turn the sound up to "daytime" level again # 15 17 * * * mixer -s 67 # # turn the sound down to "nighttime" level # 15 22 * * * mixer -s 55 # ########################################################## # # Last updated by rb on Wed Jan 14 00:30:56 EST 2004 # use strict; use Audio::Mixer; use Festival::Client; my $DEBUG = 0; #my $DEBUG = 1; my $VERSION = "1.0a"; my $COPYRIGHT = "mixer code is Copyright Ricky Buchanan , 2003."; sub usage { print < Set both speakers to % level mixer [-s] [-q] Set the left and right % levels seperately Options: -s Speak. Use Festival speech synth to announce volume level(s) after it has been set. -q Quiet. No output on STDOUT or STDERR, just returns average volume % level as the exit code. -v Report version and exit. -h Show this help screen and exit. $COPYRIGHT EOT } my $speak_results = 0; my $print_results = 1; my $exitcode_results = 0; $DEBUG && warn "Starting with only print_results flag.\n"; # boolean, needed because setting a volume to 0 is possible. my $set_volume = 0; # set both volumes the same my $set_second_volume = 0; # set volumes independantly my $volume = 0; # new volume setting my $second_volume = 0; # second new volume setting # # Main # my $arg = shift; if (defined($arg)) { if (lc($arg) eq "-h") { # help $DEBUG && warn "Help requested.\n"; usage(); exit 0; } elsif (lc($arg) eq "-v") { # version $DEBUG && warn "Version requested.\n"; print "mixer version $VERSION\n$COPYRIGHT\n"; exit 0; } } while (defined($arg)) { if (lc($arg) eq "-q") { # quiet $DEBUG && warn "Clearing print_results flag.\n"; $DEBUG && warn "Setting exitcode_results flag.\n"; $print_results = 0; $exitcode_results = 1; } elsif (lc($arg) eq "-s") { # speak $DEBUG && warn "Setting speak_results flag.\n"; $speak_results = 1; } elsif ($arg =~ m/^\d+$/) { # setting a volume, found a digit if (!$set_volume) { # set first volume $DEBUG && warn "Setting volume to '$arg'.\n"; $set_volume = 1; $volume = $arg; } else { # set second volume $DEBUG && warn "Setting second volume to '$arg'.\n"; $set_second_volume = 1; $second_volume = $arg; } } else { # Can't parse args, error out $DEBUG && warn "Error parsing args.\n"; print "I don't understand '$arg', sorry.\n"; usage(); exit 1; } # get next arg if any $arg = shift; } $DEBUG && warn "Finished parsing arguments.\n"; if ($set_volume) { # they're the same unless $set_second_volume flag set $second_volume = $volume unless $set_second_volume; # Do the actual setting. # ** NB it reports true on failure (!) if (Audio::Mixer::set_cval('vol', $volume, $second_volume)) { warn("Can't set audio mixer volume, do you have correct permissions?\n"); } } my $lvol; my $rvol; my $avgvol; my $volume_even; if ($speak_results | $print_results | $exitcode_results) { # We care about the results, better check what they are actually # Notice race condition - something else sets them between our # set and our check. ($lvol, $rvol) = Audio::Mixer::get_cval('vol'); $avgvol = ($lvol + $rvol) / 2; $volume_even = ($lvol == $rvol); } if ($speak_results > 0) { my $text = ""; if ($volume_even) { $text .= "The volume is $avgvol percent. Both speakers are even."; } else { $text .= "The volume for the left speaker is $lvol%. "; $text .= "The volume for the right speaker is $rvol%. "; $text .= "Average volume is $avgvol%."; } my $Festival = Festival::Client->new("localhost") or warn "Can't connect to festival server on 'localhost': $!\n"; if (defined($Festival)) { $Festival->say($text); } } if ($print_results > 0) { print "Left volume: $lvol%. Right volume: $rvol%.\n"; print "Average volume: $avgvol%.\n" unless $volume_even; } if ($exitcode_results > 0) { exit $avgvol; } exit 0; # # Ende. #