How do I toggle sound with amixer?

This works for me on 13.04, both mute and unmute:

amixer -D pulse set Master 1+ toggle

It specifies pulse audio to ensure unmute, unmutes everything.


I'm using this script as a workaround:

#!/bin/bash

CURRENT_STATE=`amixer get Master | egrep 'Playback.*?\[o' | egrep -o '\[o.+\]'`

if [[ $CURRENT_STATE == '[on]' ]]; then
    amixer set Master mute
else
    amixer set Master unmute
    amixer set Front unmute
    amixer set Headphone unmute
fi

I did a diff of amixer scontents before calling amixer set Master mute and after calling it and unmuting everything using the GUI to figure out what needed to be unmuted.


  1. With sound on type amixer scontents > ~/before (you'll get a file with the status of all sound chanels)
  2. Then toggle volume with amixer set Master toggle
  3. Create a second chanel status file with amixer scontents > ~/after
  4. Toggle sound again with amixer set Master toggle which is supposed to turn the volume back to the level before the first toggle command
  5. Create a third file with amixer scontents > ~/afterafter

Now you have three files telling you which tell you the status of the sound chanels used for normal sound, which are muted by amixer set Master toggle and which aren't turned on again by again unmuting with the same command.

To easily compare the files and see the differences (the chanels that get muted and won't unmute afterwards) you can use meld from the Software Centre. Start it, open the three files and on the scrollbar you can see where there are differences between the files. Use the found chanel names to add them to the above described script.


I couldn't get sound muting to toggle correctly. Whether I used CLI and type in Amixer sset Master toggle or hit the HP pavillion media toggle key, I get the same result: If sound is on and working, it will automatically turn off Master channel AND PCM channel. Then when I hit the toggle again (cli or key) it ALWAYS turns on Master, but leaves PCM muted. For my rig (HP Pavilion DV6 running Xubuntu Oneric), this means sound is off even with master on. When run from CLI - same results. If, with cli, I toggle PCM, it also turns off master channel and again won't turn it back on, though it toggles PCM correctly.

The script that worked was:

#!/bin/bash
    if amixer -c 0 get Master | grep -q off
then
    amixer set Master unmute
    amixer set PCM unmute

else
    amixer set Master mute
fi

Then I used Xubuntu keybindings (settings> settings manager> keyboard> application shortcuts) to browse to the script (I called it sndfx.sh and set it to executable by right clicking in thunar and under permissions ticked make executable). Then I assigned the HP Pavilion quickkey to it by tapping that key. Now, it toggles correctly - Awesome. Hope this helps someone else.

Peace