Turn Volume Up or Down Automatically on Selected Hours

easy
  11 years ago
  7

This is a tutorial to set up a script that turns the volume up to awake-volume at awake-hour, and turns it down to sleep-volume at sleep-hour automatically.

It should be run every hour (automated, see below) and it checks if it's awake-hour or sleep-hour and sets the volume accordingly. It'll check if the current hour is exatly the given awake-hour or sleep-hour, and it'll set the volume to the given awake-volume or sleep-volume. The reason I've made it this way is because otherwise it could be really annoying: imagine you're watching a movie, and it sets the volume to 50% every hour. So I've avoided this situation. smiley

This is how you can set it up:

  1. You need to have 'amixer'. Just open a terminal and type 'amixer' without the quotes of course. If it writes out data then it's okay. If you don't have it installed, type 'sudo apt-get install amixer'.
  2. Next thing is to create the script itself. Open gedit and copy-paste the following script. Change 'awake_hour' value to the desired hour (0-24) from when it should be loud. Set 'awake_volume' too (it's the percentage of the volume). Set the 'sleep_hour' and 'sleep_volume' in the similar way.
  3. #!/bin/bash
    # Turns the volume down once at sleep-hour,
    # and turns it back up once at awake-hour.
    echo "Volume manager"
    
    current_hour=`date +%H`
    awake_hour=9
    sleep_hour=22
    awake_volume=70
    sleep_volume=30
    
    if (( "$current_hour" == "$awake_hour" )); then
     echo "Turning volume up..."
     amixer set Master $awake_volume
    elif (( "$current_hour" == "$sleep_hour" )); then
     echo "Turning volume down..."
     amixer set Master $sleep_volume
    else
     echo "Doing nothing."
    fi
    
  4. Save it as 'volumemanager.sh' into your home directory!
  5. Now we make it executable. Open a terminal and type 'chmod +x ~/volumemanager.sh'.
  6. Now we automate it to run it every hour. Open a terminal and type 'crontab -e'. It might ask you which editor you want to use, choose 'nano'. Now go all the way down with your cursor and write the following to the last empty line: '0 * * * * ~/volumemanager.sh'. Then press CTRL-X to quit and save it.
  7. To check if the crontab is set up, type 'crontab -l'. It should echo what we have just done.

Well done, you've set it up! From now on, on hourly basis the script will run, and it'll turn down/up your volume automatically as you wish. You can alter the hour and volume settings any time by editing the script.

(...If you want to undo the whole thing, just use 'crontab -e', and delete the last line. Save and exit.)

Also it was a great excersise for me to learn a little bash scripting! smiley

P.S. Let me know if there is any problems!