#!/usr/bin/perl #Wakeup by Dale Swanson Sept 6th 2012 #Gradual volume ramp up script for MPD. Used as my alarm. #use strict; #use warnings; use Cwd; my $rate = 1.03; #change in wait between each step, 1.03 = 103% my $wakeupstep = 60; #step on which wakeup typically occurs (volume ranges 0-100) my $wakeuptime = 15; #time in minutes a wakeup should take (from 0 volume to step listed above). my $debug=0; #set to 1 to get output, and prevent actual volume messing with my $pause = 3; #time in seconds between volume up steps my $speed=0; #speed of volume ramp up, 0-slow, #1-fast, slow~50mins, fast~4mins (2=enter speed) my $sleep; #time in seconds to sleep for my $endtime; #wakeup time my $time; #current time my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst); my $x; my $wakeuptimesec; my $tempwakeuptime; while ($speed == 0) {#main loop, will run until user gives us a 0 or 1 for slow or fast wakeup, also gets time in hours to delay ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); #current time $time = sprintf("%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900, $mon+1, $mday, $hour, $min, $sec); print "\nIt is currently: $time"; print "\nEnter number of hours to wait "; $sleep = ; #time in hours to sleep, we will make it seconds later chomp($sleep); #newlines print "\nIn $sleep hours it will be "; $sleep *= 60*60; #convert from hours to seconds ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time+$sleep); #wakeup time $endtime = sprintf("%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900, $mon+1, $mday, $hour, $min, $sec); print "$endtime"; print "\nEnter 1 to accept or 0 to change time "; $speed = ; #time in hours to sleep, we will make it seconds later chomp($speed); #newlines } system("clear"); print "Current - $time"; print "Wake up - $endtime"; $sleep -= (60*$wakeuptime); #deduct the wakeup time from sleep time print "\nGoing to sleep for ", $sleep/3600, " hours, followed by a $wakeuptime minute wakeup \n"; #go to sleep if ($sleep<1) {$sleep=0}; sleep ($sleep); #wakeup time if (!$debug) {#all the stuff that messes with foobar and volumes, skip in debug mode print "Wakeup!\n"; system "amixer set Master 0"; #mute system volume system "mpc stop"; #stop current music system "mpc play"; #start music, volume at 0 sleep (35); #wait 10 seconds to give it a chance to actually start before issuing commands system "unmuteall.sh"; #unmutes system volume system "mpc volume 0"; #set's mpd volume to 0 } $wakeuptimesec = $wakeuptime * 60; $tempwakeuptime = (1-($rate**$wakeupstep))/(1-$rate); #geometric series sum formula #we now know how long a wakeup will take with 1 sec starting interval, we will find the scaler needed for desired length $pause=($wakeuptimesec/$tempwakeuptime); #the begining pause length between steps (print "\nWUTS - $wakeuptimesec, TWUT - $tempwakeuptime \n") if ($debug); for ($x=1;$x<110;$x++) {#step up volume at our rate $pause *= $rate; #gradually increase pause between steps $pause = int($pause*1000)/1000; #round to 3 decimals places, just to keep the display nice print "\nStep - $x, Pause - $pause\n"; (system "amixer set Master 1+") if (!$debug && $x % 3 == 0); #only 31 master volume steps (system "mpc volume +1") if (!$debug); sleep($pause + 0.5); #sleep only takes the int, so we add 0.5 so it rounds rather than truncates to int }