Guest User

Untitled

a guest
Mar 21st, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.32 KB | None | 0 0
  1. #!/bin/bash
  2. DEBUG="${DEBUG-0}"
  3. FADING="${FADING-1}"
  4.  
  5. DEV_CHECK="/dev/snd/timer"
  6.  
  7.  
  8. FADE_DELAY="0.05"
  9. FADE_STEPS=5
  10.  
  11. DEFAULT_SOFTVOL=""
  12. EXIT=0
  13.  
  14. #####################################################################
  15. trap exit_handler SIGINT
  16. trap exit_handler EXIT
  17.  
  18. #### softvol handling ####
  19. exit_handler()
  20. {
  21.     if [[ "$EXIT" == "0" ]]; then
  22.         EXIT=1
  23.         if [[ "$FADING" == "1" ]]; then
  24.             is_empty "$DEFAULT_SOFTVOL" || set_softvol "$DEFAULT_SOFTVOL"
  25.         fi
  26.     fi
  27.     exit
  28. }
  29.  
  30. set_softvol()
  31. {
  32.     if [[ "$(get_softvol)" != "$1" ]]; then
  33.         debug "Setting softvol to $1"
  34.         cmus-remote -C "set softvol=$1"
  35.     fi
  36. }
  37.  
  38. get_softvol()
  39. {
  40.     cmus-remote -Q | grep softvol | cut -f3 -d\
  41. }
  42.  
  43. #####################################################################
  44. #### generic output functions ####
  45. error()
  46. {
  47.     echo "error: $1" > /dev/stderr
  48. }
  49.  
  50. debug()
  51. {
  52.     [[ "$DEBUG" == "1" ]] && echo "debug: $1"
  53. }
  54.  
  55. is_empty()
  56. {
  57.     [[ -z "$1" ]]
  58. }
  59.  
  60. #####################################################################
  61. #### cmus functions ####
  62. cmus_set_volume()
  63. {
  64.     vol="$1"
  65.     cmus-remote -v "$vol"
  66.     return 0
  67. }
  68.  
  69. cmus_get_volume()
  70. {
  71.     vol="$(cmus-remote -C status| grep vol_left | cut -f3 -d\   )"
  72.     is_empty "$vol" && return 1
  73.     echo "$vol"
  74.     return 0
  75. }
  76.  
  77.  
  78. cmus_play()
  79. {
  80.     cmus-remote --play
  81. }
  82.  
  83. cmus_stop()
  84. {
  85.     cmus-remote --stop
  86. }
  87.  
  88. cmus_pause()
  89. {
  90.     cmus-remote --pause
  91. }
  92.  
  93. cmus_status()
  94. {
  95.     cmus-remote -C status | head -n 1 | cut -f2 -d\
  96. }
  97.  
  98. cmus_paused()
  99. {
  100.     [[ "$(cmus_status)" == "paused" ]]
  101. }
  102.  
  103. cmus_playing()
  104. {
  105.     [[ "$(cmus_status)" == "playing" ]]
  106. }
  107.  
  108. cmus_stopped()
  109. {
  110.     [[ "$(cmus_status)" == "stopped" ]]
  111. }
  112.  
  113. cmus_running()
  114. {
  115.     [[ ! -z "$(cmus_pid)" ]]
  116. }
  117.  
  118. cmus_pid()
  119. {
  120.     pid="$(pidof cmus 2> /dev/null)"
  121.     is_empty "$pid" && return 1
  122.     echo "$pid"
  123.     return 0
  124. }
  125.  
  126.  
  127. #####################################################################
  128. #### generic cmus functions ####
  129. play()
  130. {
  131.     if cmus_paused; then
  132.         cmus_pause
  133.     elif cmus_stopped; then
  134.         cmus_play
  135.     fi
  136. }
  137.  
  138. pause()
  139. {
  140.     cmus_playing && cmus_pause
  141. }
  142.  
  143.  
  144. #####################################################################
  145. #### fading functions ####
  146.  
  147. fade_out()
  148. {
  149.     if [[ "$FADING" != "1" ]]; then
  150.         pause
  151.         return 0
  152.     fi
  153.  
  154.     debug "fading out"
  155.     vol="$(cmus_get_volume)"
  156.     init_vol="$vol"
  157.     is_empty "$vol" && return 0
  158.  
  159.     while (( vol > 0 )); do
  160.         cmus_set_volume "$((vol-=FADE_STEPS))"
  161.         sleep "$FADE_DELAY"
  162.     done
  163.  
  164.     pause
  165.     cmus_set_volume "$init_vol"
  166.  
  167.     return 0
  168. }
  169.  
  170. fade_in()
  171. {
  172.     if [[ "$FADING" != "1" ]]; then
  173.         play
  174.         return 0
  175.     fi
  176.  
  177.     debug "fading in"
  178.     target_vol="$(cmus_get_volume)"
  179.     is_empty "$target_vol" && return 0
  180.     vol=0
  181.  
  182.     cmus_set_volume "$vol"
  183.     play
  184.  
  185.     while (( vol < target_vol )); do
  186.         cmus_set_volume "$vol"
  187.         sleep "$FADE_DELAY"
  188.         (( vol+=3 ))
  189.     done
  190.  
  191.     cmus_set_volume "$target_vol"
  192.  
  193.     return 0
  194. }
  195.  
  196.  
  197. #####################################################################
  198. # either cmus should be playing  or not
  199. have_another_client()
  200. {
  201.     cmus="$(cmus_pid)"
  202.     is_empty "$cmus" && ( error "cmus is not running"; exit 1 )
  203.  
  204.     pids="$(fuser "$DEV_CHECK" 2> /dev/null)"
  205.  
  206.     for i in $pids; do
  207.         if [[ "$i" != "$cmus" ]]; then
  208.             debug "found at least one more client pid: $i (cmus: $cmus)"
  209.             return 0
  210.         fi
  211.     done
  212.  
  213.     return 1
  214. }
  215.  
  216.  
  217. # check if anything has changed on the usage of pcm devices
  218. AUDIO_UNDEFINED=0
  219. AUDIO_CLOSED=1
  220. AUDIO_OPENED=2
  221.  
  222. poll_statechange()
  223. {
  224.     ret="$(inotifywait -q --format="% e" -e close -e open "$DEV_CHECK")"
  225.     debug "inotifywait fired"
  226.     # the following should be useless for now
  227.     for i in $ret; do
  228.         [[ "$i" == "CLOSE" ]] && return $AUDIO_CLOSED
  229.         [[ "$i" == "OPEN" ]]  && return $AUDIO_OPENED
  230.     done
  231.     return $AUDIO_UNDEFINED
  232. }
  233.  
  234.  
  235. # set current state of cmus
  236. setup_cmus()
  237. {
  238.     # PID of cmus
  239.     cmus_running || ( error "cmus is not running"; exit 1 )
  240.  
  241.     if have_another_client; then
  242.         debug "another client"
  243.         cmus_playing && fade_out
  244.     else
  245.         debug "no other client"
  246.         cmus_playing || fade_in
  247.     fi
  248.  
  249.     return 1
  250. }
  251.  
  252. #####################################################################
  253. ###  Main  ###
  254. if [[ ! -e "$DEV_CHECK" ]]; then
  255.     error "$DEV_CHECK does not exist"
  256.     exit 1
  257. fi
  258.  
  259. if [[ "$FADING" == "1" ]]; then
  260.     DEFAULT_SOFTVOL="$(get_softvol)"
  261.     if [[ "$DEFAULT_SOFTVOL" == "false" ]]; then
  262.         set_softvol "true"
  263.     fi
  264. fi
  265.  
  266.  
  267. while true; do
  268.     poll_statechange &
  269.     setup_cmus
  270.     wait
  271. done
Advertisement
Add Comment
Please, Sign In to add comment