volume 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, eventually 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. if [[ -z "$MIXER" ]] ; then
  21. MIXER="default"
  22. if command -v pulseaudio >/dev/null 2>&1 && pulseaudio --check ; then
  23. # pulseaudio is running, but not all installations use "pulse"
  24. if amixer -D pulse info >/dev/null 2>&1 ; then
  25. MIXER="pulse"
  26. fi
  27. fi
  28. [ -n "$(lsmod | grep jack)" ] && MIXER="jackplug"
  29. MIXER="${2:-$MIXER}"
  30. fi
  31. # The instance option sets the control to report and configure
  32. # This defaults to the first control of your selected mixer
  33. # For a list of the available, use `amixer -D $Your_Mixer scontrols`
  34. if [[ -z "$SCONTROL" ]] ; then
  35. SCONTROL="${BLOCK_INSTANCE:-$(amixer -D $MIXER scontrols |
  36. sed -n "s/Simple mixer control '\([^']*\)',0/\1/p" |
  37. head -n1
  38. )}"
  39. fi
  40. # The first parameter sets the step to change the volume by (and units to display)
  41. # This may be in in % or dB (eg. 5% or 3dB)
  42. if [[ -z "$STEP" ]] ; then
  43. STEP="${1:-5%}"
  44. fi
  45. #------------------------------------------------------------------------
  46. capability() { # Return "Capture" if the device is a capture device
  47. amixer -D $MIXER get $SCONTROL |
  48. sed -n "s/ Capabilities:.*cvolume.*/Capture/p"
  49. }
  50. volume() {
  51. amixer -D $MIXER get $SCONTROL $(capability)
  52. }
  53. format() {
  54. perl_filter='if (/.*\[(\d+%)\] (\[(-?\d+.\d+dB)\] )?\[(on|off)\]/)'
  55. perl_filter+='{CORE::say $4 eq "off" ? "MUTE" : "'
  56. # If dB was selected, print that instead
  57. perl_filter+=$([[ $STEP = *dB ]] && echo '$3' || echo '$1')
  58. perl_filter+='"; exit}'
  59. output=$(perl -ne "$perl_filter")
  60. echo "$LABEL$output"
  61. }
  62. #------------------------------------------------------------------------
  63. case $BLOCK_BUTTON in
  64. 3) amixer -q -D $MIXER sset $SCONTROL $(capability) toggle ;; # right click, mute/unmute
  65. 4) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}+ unmute ;; # scroll up, increase
  66. 5) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}- unmute ;; # scroll down, decrease
  67. esac
  68. volume | format