SHARE
TWEET

pavolume.sh

Volteos Oct 8th, 2017 894 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. # finds the active sink for pulse audio and increments the volume. useful when you have multiple audio outputs and have a key bound to vol-up and down
  4.  
  5. osd='no'
  6. inc='2'
  7. capvol='no'
  8. maxvol='200'
  9. autosync='yes'
  10.  
  11. # Muted status
  12. # yes: muted
  13. # no : not muted
  14. curStatus="no"
  15. active_sink=""
  16. limit=$((100 - inc))
  17. maxlimit=$((maxvol - inc))
  18.  
  19. reloadSink() {
  20.     active_sink=$(pacmd list-sinks | awk '/* index:/{print $3}')
  21. }
  22.  
  23. function volUp {
  24.  
  25.     getCurVol
  26.  
  27.     if [ "$capvol" = 'yes' ]
  28.     then
  29.         if [ "$curVol" -le 100 ] && [ "$curVol" -ge "$limit" ]
  30.         then
  31.             pactl set-sink-volume "$active_sink" -- 100%
  32.         elif [ "$curVol" -lt "$limit" ]
  33.         then
  34.             pactl set-sink-volume "$active_sink" -- "+$inc%"
  35.         fi
  36.     elif [ "$curVol" -le "$maxvol" ] && [ "$curVol" -ge "$maxlimit" ]
  37.     then
  38.         pactl set-sink-volume "$active_sink" "$maxvol%"
  39.     elif [ "$curVol" -lt "$maxlimit" ]
  40.     then
  41.         pactl set-sink-volume "$active_sink" "+$inc%"
  42.     fi
  43.  
  44.     getCurVol
  45.  
  46.     if [ ${osd} = 'yes' ]
  47.     then
  48.         qdbus org.kde.kded /modules/kosd showVolume "$curVol" 0
  49.     fi
  50.  
  51.     if [ ${autosync} = 'yes' ]
  52.     then
  53.         volSync
  54.     fi
  55. }
  56.  
  57. function volDown {
  58.  
  59.     pactl set-sink-volume "$active_sink" "-$inc%"
  60.     getCurVol
  61.  
  62.     if [ ${osd} = 'yes' ]
  63.     then
  64.         qdbus org.kde.kded /modules/kosd showVolume "$curVol" 0
  65.     fi
  66.  
  67.     if [ ${autosync} = 'yes' ]
  68.     then
  69.         volSync
  70.     fi
  71.  
  72. }
  73.  
  74. function getSinkInputs {
  75.     input_array=$(pacmd list-sink-inputs | grep -B 4 "sink: $1 " | awk '/index:/{print $2}')
  76. }
  77.  
  78. function volSync {
  79.     getSinkInputs "$active_sink"
  80.     getCurVol
  81.  
  82.     for each in $input_array
  83.     do
  84.         pactl set-sink-input-volume "$each" "$curVol%"
  85.     done
  86. }
  87.  
  88. function getCurVol {
  89.     curVol=$(pacmd list-sinks | grep -A 15 'index: '"$active_sink"'' | grep 'volume:' | grep -E -v 'base volume:' | awk -F : '{print $3}' | grep -o -P '.{0,3}%'| sed s/.$// | tr -d ' ')
  90. }
  91.  
  92. function volMute {
  93.     case "$1" in
  94.         mute)
  95.             pactl set-sink-mute "$active_sink" 1
  96.             curVol=0
  97.             status=1
  98.             ;;
  99.         unmute)
  100.             pactl set-sink-mute "$active_sink" 0
  101.             getCurVol
  102.             status=0
  103.             ;;
  104.     esac
  105.  
  106.     if [ ${osd} = 'yes' ]
  107.     then
  108.         qdbus org.kde.kded /modules/kosd showVolume ${curVol} ${status}
  109.     fi
  110.  
  111. }
  112.  
  113. function volMuteStatus {
  114.     curStatus=$(pacmd list-sinks | grep -A 15 "index: $active_sink" | awk '/muted/{ print $2}')
  115. }
  116.  
  117. # Prints output for bar
  118. # Listens for events for fast update speed
  119. function listen {
  120.     firstrun=0
  121.  
  122.     pactl subscribe 2>/dev/null | {
  123.         while true; do
  124.             {
  125.                 # If this is the first time just continue
  126.                 # and print the current state
  127.                 # Otherwise wait for events
  128.                 # This is to prevent the module being empty until
  129.                 # an event occurs
  130.                 if [ $firstrun -eq 0 ]
  131.                 then
  132.                     firstrun=1
  133.                 else
  134.                     read -r event || break
  135.                     if ! echo "$event" | grep -e "on card" -e "on sink"
  136.                     then
  137.                         # Avoid double events
  138.                         continue
  139.                     fi
  140.                 fi
  141.             } &>/dev/null
  142.             output
  143.         done
  144.     }
  145. }
  146.  
  147. function output() {
  148.     reloadSink
  149.     getCurVol
  150.     volMuteStatus
  151.     if [ "${curStatus}" = 'yes' ]
  152.     then
  153.         echo " $curVol%"
  154.     else
  155.         echo " $curVol%"
  156.     fi
  157. } #}}}
  158.  
  159. reloadSink
  160. case "$1" in
  161.     --up)
  162.         volUp
  163.         ;;
  164.     --down)
  165.         volDown
  166.         ;;
  167.     --togmute)
  168.         volMuteStatus
  169.         if [ "$curStatus" = 'yes' ]
  170.         then
  171.             volMute unmute
  172.         else
  173.             volMute mute
  174.         fi
  175.         ;;
  176.     --mute)
  177.         volMute mute
  178.         ;;
  179.     --unmute)
  180.         volMute unmute
  181.         ;;
  182.     --sync)
  183.         volSync
  184.         ;;
  185.     --listen)
  186.         # Listen for changes and immediately create new output for the bar
  187.         # This is faster than having the script on an interval
  188.         listen
  189.         ;;
  190.     *)
  191.         # By default print output for bar
  192.         output
  193.         ;;
  194. esac
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top