DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
My Handy Mp3 "alarm Clock" Written In Ruby.
// Simple tool to help wake me up in the morning. Not the cleanest code, but it took no time to write. Ruby rocks!
#!/bin/env ruby
#
# A helper to setup an alarm to slowly and leisurely wake up in the morning.
#
# $Id: mp3wakeup.rb 6 2007-01-31 16:02:57Z btek $
#
if ARGV.length < 1
puts "Usage: #{$0} <time> [mp3dir]"
exit 1
end
#############################
# Configuration parameters. #
#############################
NUM_STEPS = 15
STEP_DURATION = 60 * 1
FINAL_VOLUME = 95
INITIAL_VOLUME = 30
VOLUME_STEP = (FINAL_VOLUME - INITIAL_VOLUME) / NUM_STEPS
MIN_SEC_BEFORE_ALARM = NUM_STEPS * STEP_DURATION
#############################
#############################
time = Regexp.quote ARGV[0]
mp3dir = Regexp.quote(ARGV[1]) if ARGV.length > 1
def get_alarm_time(desired_hour, desired_minute)
now = Time.new
alarm_time = Time.local(now.year, now.month, now.day, desired_hour, desired_minute, 0, 0)
if alarm_time - now < MIN_SEC_BEFORE_ALARM
alarm_time = alarm_time + 60*60*24
end
return alarm_time
end
def run(cmd)
#puts cmd
system cmd
end
puts "Waking you up by #{time} with songs from #{mp3dir}."
match = time.match(/(\d+)(:(\d+))?/)
hour = match[1].to_i
minute = if (match.length > 1)
match[3].to_i
else
0
end
alarm_time = get_alarm_time(hour, minute)
for i in 0..NUM_STEPS
alert_time = alarm_time - (STEP_DURATION * i)
volume = FINAL_VOLUME - (VOLUME_STEP * i)
at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"
run "echo amixer sset PCM #{volume}% | #{at_cmd}"
if i == NUM_STEPS
# Start playing after time is confirmed set.
alert_time = alert_time + (60)
at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"
if mp3dir != nil
run "echo audacious #{mp3dir} | #{at_cmd}"
else
run "echo audtool playback-play | #{at_cmd}"
end
end
end





