Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.7 KiB

  1. #!/bin/bash
  2. # Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>
  3. # Copyright (C) 2014 Alexander Keller <github@nycroth.com>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. #------------------------------------------------------------------------
  15. # The second parameter overrides the mixer selection
  16. # For PulseAudio users, use "pulse"
  17. # For Jack/Jack2 users, use "jackplug"
  18. # For ALSA users, you may use "default" for your primary card
  19. # or you may use hw:# where # is the number of the card desired
  20. MIXER="default"
  21. [ -n "$(lsmod | grep pulse)" ] && MIXER="pulse"
  22. [ -n "$(lsmod | grep jack)" ] && MIXER="jackplug"
  23. MIXER="${2:-$MIXER}"
  24. # The instance option sets the control to report and configure
  25. # This defaults to the first control of your selected mixer
  26. # For a list of the available, use `amixer -D $Your_Mixer scontrols`
  27. SCONTROL="${BLOCK_INSTANCE:-$(amixer -D $MIXER scontrols |
  28. sed -n "s/Simple mixer control '\([A-Za-z ]*\)',0/\1/p" |
  29. head -n1
  30. )}"
  31. # The first parameter sets the step to change the volume by (and units to display)
  32. # This may be in in % or dB (eg. 5% or 3dB)
  33. STEP="${1:-5%}"
  34. #------------------------------------------------------------------------
  35. capability() { # Return "Capture" if the device is a capture device
  36. amixer -D $MIXER get $SCONTROL |
  37. sed -n "s/ Capabilities:.*cvolume.*/Capture/p"
  38. }
  39. volume() {
  40. amixer -D $MIXER get $SCONTROL $(capability)
  41. }
  42. format() {
  43. perl_filter='if (/.*\[(\d+%)\] (\[(-?\d+.\d+dB)\] )?\[(on|off)\]/)'
  44. perl_filter+='{CORE::say $4 eq "off" ? "MUTE" : "'
  45. # If dB was selected, print that instead
  46. perl_filter+=$([[ $STEP = *dB ]] && echo '$3' || echo '$1')
  47. perl_filter+='"; exit}'
  48. perl -ne "$perl_filter"
  49. }
  50. #------------------------------------------------------------------------
  51. case $BLOCK_BUTTON in
  52. 3) amixer -q -D $MIXER sset $SCONTROL $(capability) toggle ;; # right click, mute/unmute
  53. 4) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}+ unmute ;; # scroll up, increase
  54. 5) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}- unmute ;; # scroll down, decrease
  55. esac
  56. volume | format