Advertisement
Guest User

PulseAudio Volume Control

a guest
Oct 18th, 2010
3,666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.63 KB | None | 0 0
  1. #!/bin/sh
  2. #   pulsevol.sh
  3. #   PulseAudio Volume Control Script
  4. #   Original 2010-05-20 - Gary Hetzel <garyhetzel@gmail.com>
  5. #      
  6. #   Modified 2010-10-18 by Fisslefink <fisslefink@gmail.com>
  7. #    - Added support for multiple sinks and Ubuntu libnotify OSD
  8. #
  9. #   Usage:  pulsevol.sh [sink_index] [up|down|min|max|overmax|toggle|mute|unmute]
  10. #
  11.  
  12. EXPECTED_ARGS=2
  13. E_BADARGS=65
  14.  
  15. if [ $# -ne $EXPECTED_ARGS ]
  16. then
  17.     echo "Usage: `basename $0` [sink_index] [up|down|min|max|overmax|toggle|mute|unmute]"
  18.     exit $E_BADARGS
  19. fi
  20.  
  21. SINK=$1
  22. STEP=5
  23. MAXVOL=65537 # let's just assume this is the same all over
  24. #MAXVOL=`pacmd list-sinks | grep "volume steps" | cut -d: -f2 | tr -d "[:space:]"`
  25.  
  26. getperc(){
  27.     VOLPERC=`pacmd list-sinks | grep "volume" | head -n1 | cut -d: -f3 | cut -d% -f1 | tr -d "[:space:]"`
  28. }
  29.  
  30. getperc;
  31.  
  32. up(){
  33.     VOLSTEP="$(( $VOLPERC+$STEP ))";
  34. }
  35.  
  36. down(){
  37.     VOLSTEP="$(( $VOLPERC-$STEP ))";
  38. }
  39.  
  40. max(){
  41.     pacmd set-sink-volume $SINK $MAXVOL > /dev/null
  42. }
  43.  
  44. min(){
  45.     pacmd set-sink-volume $SINK 0 > /dev/null
  46. }
  47.  
  48. overmax(){
  49.     SKIPOVERCHECK=1
  50.     if [ $VOLPERC -lt 100 ]; then
  51.         max;
  52.         exit 0;
  53.     fi
  54.     up
  55. }
  56.  
  57. mute(){
  58.     pacmd set-sink-mute $SINK 1 > /dev/null
  59.     notify-send " " -i "notification-audio-volume-muted" -h int:value:0 -h string:synchronous:volume
  60. }
  61.  
  62. unmute(){
  63.     VOLSTEP="$(( $VOLPERC-0 ))";
  64.     SKIPOVERCHECK=1
  65.     pacmd set-sink-mute $SINK 0 > /dev/null
  66. }
  67.  
  68. toggle(){
  69.     M=`pacmd list-sinks | grep "muted" | cut -d: -f2 | tr -d "[:space:]"`
  70.     if [ "$M" = "no" ]; then
  71.         mute
  72.         exit 0;
  73.     else
  74.         unmute;
  75.     fi
  76. }
  77.  
  78. case $2 in
  79. up)
  80.     up;;
  81. down)
  82.     down;;
  83. max)
  84.     max
  85.     exit 0;;
  86. min)
  87.     min
  88.     exit 0;;
  89. overmax)
  90.     overmax;;
  91. toggle)
  92.     toggle;;
  93. mute)
  94.     mute;
  95.     exit 0;;
  96. unmute)
  97.     unmute;;
  98. *)
  99.     echo "Usage: `basename $0` [sink_index] [up|down|min|max|overmax|toggle|mute|unmute]"
  100.     exit 1;;
  101. esac
  102.  
  103. VOLUME="$(( ($MAXVOL/100) * $VOLSTEP ))"
  104.  
  105.  
  106. if [ -z $SKIPOVERCHECK ]; then
  107.     if [ $VOLUME -gt $MAXVOL ]; then
  108.         VOLUME=$MAXVOL
  109.     elif [ $VOLUME -lt 0 ]; then
  110.         VOLUME=0
  111.     fi
  112. fi
  113.  
  114.  
  115. pacmd set-sink-volume $SINK $VOLUME > /dev/null
  116.  
  117. VOLPERC=`pacmd list-sinks | grep "volume" | head -n1 | cut -d: -f3 | cut -d% -f1 | tr -d "[:space:]"`
  118.  
  119.  
  120. if [ "$VOLPERC" = "0" ]; then
  121.         icon_name="notification-audio-volume-off"
  122.     else
  123.         if [ "$VOLPERC" -lt "33" ]; then
  124.             icon_name="notification-audio-volume-low"
  125.         else
  126.             if [ "$VOLPERC" -lt "67" ]; then
  127.                 icon_name="notification-audio-volume-medium"
  128.             else
  129.                 icon_name="notification-audio-volume-high"
  130.             fi
  131.         fi
  132. fi
  133.  
  134. notify-send " " -i $icon_name -h int:value:$VOLPERC -h string:synchronous:volume
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement