Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- DEBUG="${DEBUG-0}"
- FADING="${FADING-1}"
- DEV_CHECK="/dev/snd/timer"
- FADE_DELAY="0.05"
- FADE_STEPS=5
- DEFAULT_SOFTVOL=""
- EXIT=0
- #####################################################################
- trap exit_handler SIGINT
- trap exit_handler EXIT
- #### softvol handling ####
- exit_handler()
- {
- if [[ "$EXIT" == "0" ]]; then
- EXIT=1
- if [[ "$FADING" == "1" ]]; then
- is_empty "$DEFAULT_SOFTVOL" || set_softvol "$DEFAULT_SOFTVOL"
- fi
- fi
- exit
- }
- set_softvol()
- {
- if [[ "$(get_softvol)" != "$1" ]]; then
- debug "Setting softvol to $1"
- cmus-remote -C "set softvol=$1"
- fi
- }
- get_softvol()
- {
- cmus-remote -Q | grep softvol | cut -f3 -d\
- }
- #####################################################################
- #### generic output functions ####
- error()
- {
- echo "error: $1" > /dev/stderr
- }
- debug()
- {
- [[ "$DEBUG" == "1" ]] && echo "debug: $1"
- }
- is_empty()
- {
- [[ -z "$1" ]]
- }
- #####################################################################
- #### cmus functions ####
- cmus_set_volume()
- {
- vol="$1"
- cmus-remote -v "$vol"
- return 0
- }
- cmus_get_volume()
- {
- vol="$(cmus-remote -C status| grep vol_left | cut -f3 -d\ )"
- is_empty "$vol" && return 1
- echo "$vol"
- return 0
- }
- cmus_play()
- {
- cmus-remote --play
- }
- cmus_stop()
- {
- cmus-remote --stop
- }
- cmus_pause()
- {
- cmus-remote --pause
- }
- cmus_status()
- {
- cmus-remote -C status | head -n 1 | cut -f2 -d\
- }
- cmus_paused()
- {
- [[ "$(cmus_status)" == "paused" ]]
- }
- cmus_playing()
- {
- [[ "$(cmus_status)" == "playing" ]]
- }
- cmus_stopped()
- {
- [[ "$(cmus_status)" == "stopped" ]]
- }
- cmus_running()
- {
- [[ ! -z "$(cmus_pid)" ]]
- }
- cmus_pid()
- {
- pid="$(pidof cmus 2> /dev/null)"
- is_empty "$pid" && return 1
- echo "$pid"
- return 0
- }
- #####################################################################
- #### generic cmus functions ####
- play()
- {
- if cmus_paused; then
- cmus_pause
- elif cmus_stopped; then
- cmus_play
- fi
- }
- pause()
- {
- cmus_playing && cmus_pause
- }
- #####################################################################
- #### fading functions ####
- fade_out()
- {
- if [[ "$FADING" != "1" ]]; then
- pause
- return 0
- fi
- debug "fading out"
- vol="$(cmus_get_volume)"
- init_vol="$vol"
- is_empty "$vol" && return 0
- while (( vol > 0 )); do
- cmus_set_volume "$((vol-=FADE_STEPS))"
- sleep "$FADE_DELAY"
- done
- pause
- cmus_set_volume "$init_vol"
- return 0
- }
- fade_in()
- {
- if [[ "$FADING" != "1" ]]; then
- play
- return 0
- fi
- debug "fading in"
- target_vol="$(cmus_get_volume)"
- is_empty "$target_vol" && return 0
- vol=0
- cmus_set_volume "$vol"
- play
- while (( vol < target_vol )); do
- cmus_set_volume "$vol"
- sleep "$FADE_DELAY"
- (( vol+=3 ))
- done
- cmus_set_volume "$target_vol"
- return 0
- }
- #####################################################################
- # either cmus should be playing or not
- have_another_client()
- {
- cmus="$(cmus_pid)"
- is_empty "$cmus" && ( error "cmus is not running"; exit 1 )
- pids="$(fuser "$DEV_CHECK" 2> /dev/null)"
- for i in $pids; do
- if [[ "$i" != "$cmus" ]]; then
- debug "found at least one more client pid: $i (cmus: $cmus)"
- return 0
- fi
- done
- return 1
- }
- # check if anything has changed on the usage of pcm devices
- AUDIO_UNDEFINED=0
- AUDIO_CLOSED=1
- AUDIO_OPENED=2
- poll_statechange()
- {
- ret="$(inotifywait -q --format="% e" -e close -e open "$DEV_CHECK")"
- debug "inotifywait fired"
- # the following should be useless for now
- for i in $ret; do
- [[ "$i" == "CLOSE" ]] && return $AUDIO_CLOSED
- [[ "$i" == "OPEN" ]] && return $AUDIO_OPENED
- done
- return $AUDIO_UNDEFINED
- }
- # set current state of cmus
- setup_cmus()
- {
- # PID of cmus
- cmus_running || ( error "cmus is not running"; exit 1 )
- if have_another_client; then
- debug "another client"
- cmus_playing && fade_out
- else
- debug "no other client"
- cmus_playing || fade_in
- fi
- return 1
- }
- #####################################################################
- ### Main ###
- if [[ ! -e "$DEV_CHECK" ]]; then
- error "$DEV_CHECK does not exist"
- exit 1
- fi
- if [[ "$FADING" == "1" ]]; then
- DEFAULT_SOFTVOL="$(get_softvol)"
- if [[ "$DEFAULT_SOFTVOL" == "false" ]]; then
- set_softvol "true"
- fi
- fi
- while true; do
- poll_statechange &
- setup_cmus
- wait
- done
Advertisement
Add Comment
Please, Sign In to add comment