Advertisement
theroot

pavolume.sh

Jan 3rd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.68 KB | None | 0 0
  1. #!/bin/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='yes'
  6. inc='5'
  7. capvol='no'
  8. maxvol='200'
  9. tmpfile='/tmp/pasink.tmp'
  10. autosync='yes'
  11.  
  12. active_sink=`pacmd list-sinks |awk '/* index:/{print $3}'`
  13. limit=$(expr 100 - ${inc})
  14. maxlimit=$(expr ${maxvol} - ${inc})
  15.  
  16. function volUp {
  17.  
  18.         preVol=`pacmd list-sinks |grep -A 15 'index: '${active_sink}'' |awk '/volume: 0:/{ print $3}'|sed s/.$//`
  19.  
  20.         if [ ${capvol} = 'yes' ]
  21.         then
  22.                 if [ ${preVol} -le 100 -a ${preVol} -ge ${limit} ]
  23.                 then
  24.                         pactl set-sink-volume ${active_sink} -- 100%
  25.                 elif [ ${preVol} -lt ${limit} ]
  26.                 then
  27.                         pactl set-sink-volume ${active_sink} -- +${inc}%
  28.                 fi
  29.         elif [ ${preVol} -le ${maxvol} -a ${preVol} -ge ${maxlimit} ]
  30.         then
  31.                 pactl set-sink-volume ${active_sink} -- ${maxvol}%
  32.         elif [ ${preVol} -lt ${maxlimit} ]
  33.         then
  34.                 pactl set-sink-volume ${active_sink} -- +${inc}%
  35.         fi
  36.  
  37.         curVol=`pacmd list-sinks |grep -A 15 'index: '${active_sink}'' |awk '/volume: 0:/{ print $3}'|sed s/.$//`
  38.  
  39.         if [ ${osd} = 'yes' ]
  40.         then
  41.                 qdbus org.kde.kded /modules/kosd showVolume ${curVol} 0
  42.         fi
  43.  
  44.         if [ ${autosync} = 'yes' ]
  45.         then
  46.                 volSync
  47.         fi
  48. }
  49.  
  50. function volDown {
  51.  
  52.         pactl set-sink-volume ${active_sink} -- -${inc}%
  53.         curVol=`pacmd list-sinks |grep -A 15 'index: '${active_sink}'' |awk '/volume: 0:/{ print $3}'|sed s/.$//`
  54.         if [ ${osd} = 'yes' ]
  55.         then
  56.                 qdbus org.kde.kded /modules/kosd showVolume ${curVol} 0
  57.         fi
  58.  
  59.         if [ ${autosync} = 'yes']
  60.         then
  61.                 volSync
  62.         fi
  63.  
  64. }
  65.  
  66. function getSinkInputs {
  67.  
  68.         inputs=`pacmd list-sink-inputs |grep -B 4 'sink: '${1}' ' |awk '/index:/{print $2}' >${tmpfile}`
  69. #       input_array=( `cat "${tmpfile}"` )
  70.         input_array=`cat $tmpfile`
  71. }
  72.  
  73. function volSync {
  74.  
  75.         getSinkInputs ${active_sink}
  76.         curVol=`pacmd list-sinks |grep -A 15 'index: '${active_sink}'' |awk '/volume: 0:/{ print $3}'|sed s/.$//`
  77.  
  78.         for each in ${input_array}
  79.         do
  80.                 pactl set-sink-input-volume ${each} ${curVol}%
  81.         done
  82.  
  83. }
  84.  
  85. function volMute {
  86.  
  87.         case "$1" in
  88.                 mute)
  89.                         pactl set-sink-mute ${active_sink} 1
  90.                         curVol=0
  91.                         status=1
  92.                 ;;
  93.                 unmute)
  94.                         pactl set-sink-mute ${active_sink} 0
  95.                         curVol=`pacmd list-sinks |grep -A 15 'index: '${active_sink}'' |awk '/volume: 0:/{ print $3}'|sed s/.$//`
  96.                         status=0
  97.                 ;;
  98.         esac
  99.  
  100.         if [ ${osd} = 'yes' ]
  101.         then
  102.                 qdbus org.kde.kded /modules/kosd showVolume ${curVol} ${status}
  103.         fi
  104.  
  105. }
  106.  
  107. function volMuteStatus {
  108.  
  109.         curStatus=`pacmd list-sinks |grep -A 15 'index: '${active_sink}'' |awk '/muted/{ print $2}'`
  110.  
  111.         if [ ${curStatus} = 'yes' ]
  112.         then
  113.                 volMute unmute
  114.         else
  115.                 volMute mute
  116.         fi
  117.  
  118. }
  119.  
  120.  
  121. case "$1" in
  122.         --up)
  123.                 volUp
  124.         ;;
  125.         --down)
  126.                 volDown
  127.         ;;
  128.         --togmute)
  129.                 volMuteStatus
  130.         ;;
  131.         --mute)
  132.                 volMute mute
  133.         ;;
  134.         --unmute)
  135.                 volMute unmute
  136.         ;;
  137.         --sync)
  138.                 volSync
  139.         ;;
  140. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement