Change Volume From a Bash Script
September 24th, 2008 by Mitch Frazier in
If you use ALSA for sound on your system the functions contained in the script presented here can be used to get and set the volume on your system. You might use this if you had a monitoring script running and wanted to raise the volume when you signal an alarm and then lower it again to the previous volume.
The get function uses amixer to output the information for the simple mixer control "Master" and then uses grep and cut to get the correct value from the output.
The set function also uses amixer to set the volume. It uses the "cset" option to amixer.
The script follows:
#!/bin/bash
#
#####################################################################
# Get current volume.
function get_volume()
{
mixer=$(amixer get Master | grep 'Front Left:')
echo $mixer | cut -d ' ' -f 4
}
#####################################################################
# Set volume.
function set_volume()
{
amixer cset iface=MIXER,name="Master Playback Volume" $1 >/dev/null
}
if [[ $(basename $0 .sh) == 'sound' ]]; then
sound_file=~/Documents/sounds/notify.wav
if [[ "$1" ]]; then sound_file="$1"; fi
ovol=$(get_volume)
echo "Current volume: $ovol"
aplay $sound_file
sleep 2
vol=40
echo "Playing at: $vol"
set_volume $vol
aplay $sound_file
sleep 2
echo "Again at: $ovol"
set_volume $ovol
aplay $sound_file
fi
# vim: tabstop=4: shiftwidth=4: noexpandtab:
# kate: tab-width 4; indent-width 4; replace-tabs false;
If you save the script as sound.sh and run it directly passing the name of a sound file it will play the file at the current volume, then it raises the volume and plays it again, and finally it restores the volume and plays it one last time. If you open your mixer controls before running the script you should see the master volume control move as the script changes the volume. You can adjust the value in the line vol=40 to get the volume you want.
__________________________Mitch Frazier is an Associate Editor at Linux Journal.
Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Sorry, offer available in the US only. International orders, click here.
Subscribe now!
The Latest
Featured Videos
Linux Journal Live - eBook Readers and DRM
November 14th, 2008 by Shawn Powers in
The November 13, 2008 edition of Linux Journal Live! Shawn Powers and special guest, Linux Journal Author Daniel Bartholomew, talk e-book readers and Daniel's Kindle, DRM, and other goodness.
Run Your Windows Partition Without Rebooting
November 13th, 2008 by Elliot Isaacson in
Dual booting is a necessary evil and very inconvenient. What if you could run your windows partition in a virtual machine, so you wouldn't have to worry about rebooting anymore? With VMWare Workstation, you can.
Recently Popular
From the Magazine
December 2008, #176
The Oxford English Dictionary says the word "gadget" is a placeholder name for a technical item whose precise name one can't remember. Like that book-reader thingy from Amazon...what's it called? Spindle, Gindle...Kindle, that's it. Check it out in this month's gadget issue.
Other gadgets covered include the Nokia tablets, the BlackBerry, the Neo FreeRunner, the Dash Express, the Roku Netflix Player, the Kangaroo TV, The TomTom GO 930 and the MooBella Ice Cream System. On the larger hardware front, read the reviews of the Acer Aspire One and the YDL PowerStation. On the software front, check out the articles and columns on memcached, Samba security, Mutt, desktop gadgets, bash and Puppet. To wrap it all up, read Doc's thoughts on Google and the browser platform.
Delicious
Digg
Reddit
Newsvine
Technorati







Just test code
On September 25th, 2008 Mitch Frazier says:
The "vol=40" is just for testing.
Although, I must admit that I'm not actually sure what the acceptable volume ranges are, or for that matter if they are the same on all systems.
ps. if you're playing the "Free Software Song" at 99 then you're probably already deaf and won't hear anything anyways. :)
__________________________Mitch Frazier is an Associate Editor at Linux Journal.
Great Article, but set_volume() must be scaled
On November 3rd, 2008 David Rankin (not verified) says:
Mitch,
Great article. The only problem is set_volume() doesn't work correctly because amixer volume range is 0-64, not 0-100. So a quick conversion is required for set_volume() to work:
# Set volume.
function set_volume()
{
## Mixer Volume Range (min=0 max=64);
## you must convert Vol. in % to 0-64 mixer volume range
mixerVol=$(($1*64/100))
amixer cset iface=MIXER,name="Master Playback Volume" $mixerVol >/dev/null
}
Also, for people running on a laptop, many times you will not have Left/Right volume but only a Mono setting. If that is the case, you will need to modify get_volume() as follows:
# Get current volume.
function get_volume()
{
mixer=$(amixer get Master | grep 'Mono:' | sed -e 's/^[^\[]*//' -e 's/^.//' -e 's/%.*$//')
echo $mixer
}
Good Idea
On November 4th, 2008 Mitch Frazier says:
I was just getting and setting raw values, not setting a percent of full. If you do it your way you should be scaling get_volume() also so that a value returned by get_volume() can later be passed to set_volume().
Is it always 64? Besides being a power of 2 is there any particular reason it's 64?
__________________________Mitch Frazier is an Associate Editor at Linux Journal.
Is it always 64? I
On November 10th, 2008 Anonymous (not verified) says:
Is it always 64?I have two soundcards.
Running the command:
amixer get MasterOne soundcard reports:
Limits: Playback 0 - 127The other sound card reports:
Limits: Playback 0 - 31Nice idea, but....
On September 24th, 2008 deifl (not verified) says:
What happens if master-volume is playing the "Free Software Song" with vol=99 ;-) Increasing volume seems better than setting to fixed volume (vol=40).
Isn't it better to save current volume -> play sound -> raise volume (enough to recognize) -> play sound -> restore volume -> play sound. Increasing volume can done with: amixer set 'Master" +
I also like to stop and continue playing music so I added mpc pause at the beginning and mpc toggle at the end of the script.
Post new comment