*nohtml*

local/bin/music

   1 #!/bin/bash
   2 
   3  play_icon=${MUSIC_PLAY_ICON:-'>'}
   4 pause_icon=${MUSIC_PAUSE_ICON:-'||'}
   5 
   6 mocp_control(){ # {{{
   7     case $1 in
   8         play) mocp --play; sleep 0.2;;
   9         stop) mocp --stop;;
  10        pause) mocp --toggle-pause;;
  11         prev) mocp --previous; sleep 0.1;;
  12         next) mocp --next; sleep 0.1;;
  13         seek) mocp --seek $2; sleep 0.1;;
  14     esac 2>&-
  15 } # }}}
  16 mocp_status(){ # {{{
  17     IFS='|' read status tt ct tl file artist album title <<< \
  18     $(mocp -Q '%state|%tt|%ct|%tl|%file|%artist|%album|%song' 2>&-)
  19 
  20     [[ $status == STOP ]] && return 1
  21     [[ $status == PLAY ]] && status=play || status=pause
  22 } # }}}
  23 
  24 audacious_control(){ # {{{
  25     case $1 in
  26         play) audtool playback-play;;
  27         stop) audtool playback-stop;;
  28        pause) audtool playback-playpause;;
  29         prev) audtool playlist-reverse;;
  30         next) audtool playlist-advance;;
  31         seek) audtool playback-seek-relative $2;;
  32     esac
  33 } # }}}
  34 audacious_status(){ # {{{
  35     IFS='|' read status tt ct tti cti file artist album title <<< $(audtool \
  36         playback-status \
  37         current-song-length current-song-output-length \
  38         current-song-length-seconds current-song-output-length-seconds \
  39         current-song-filename current-song-tuple-data artist \
  40         current-song-tuple-data album current-song-tuple-data title |
  41         tr \\n \|
  42     )
  43 
  44     [[ $status == stopped ]] && return 1
  45     [[ $status == playing ]] && status=play || status=pause
  46 
  47     tl=$(date -ud @$(( $tti - $cti )) +'%M:%S')
  48 } # }}}
  49 
  50 cmus_control(){ # {{{
  51     case $1 in
  52         play) cmus-remote --play; sleep 0.2;;
  53         stop) cmus-remote --stop;;
  54        pause) cmus-remote --pause;;
  55         prev) cmus-remote --prev;;
  56         next) cmus-remote --next;;
  57         seek) cmus-remote --seek $2;;
  58     esac
  59 } # }}}
  60 cmus_status(){ # {{{
  61     declare -A array
  62     while read k1 k2 v; do
  63         [[ $k1 == set || $k1 == tag ]] &&
  64         array[$k2]="$v" || array[$k1]="$k2 $v"
  65     done <<< "$(cmus-remote --query)"
  66 
  67     status=${array[status]}
  68     [[ $status == stopped* ]] && return 1
  69     [[ $status == playing* ]] && status=play || status=pause
  70 
  71     file=${array[file]}
  72     artist=${array[artist]}
  73     album=${array[album]}
  74     title=${array[title]}
  75 
  76     tti=${array[duration]}
  77     cti=${array[position]}
  78 
  79     tt=$(date -ud @$tti +'%M:%S')
  80     ct=$(date -ud @$cti +'%M:%S')
  81     tl=$(date -ud @$(( $tti - $cti )) +'%M:%S')
  82 } # }}}
  83 
  84 mpd_control(){ # {{{
  85     case $1 in
  86         play) mpc play;;
  87         stop) mpc stop;;
  88        pause) mpc toggle;;
  89         prev) mpc prev;;
  90         next) mpc next;;
  91         seek) mpc seek $2;;
  92     esac >&-
  93 } # }}}
  94 mpd_status(){ # {{{
  95     declare -a array
  96     while read line; do
  97         array+=("$line")
  98     done <<< "$(mpc -f '%file%\n%artist%\n%album%\n%title%')"
  99 
 100     read status _ time _ <<< "${array[3]}"
 101     [[ $status ]] || return 1
 102     [[ $status == \[playing\] ]] && status=play || status=pause
 103 
 104     file=${array[0]}
 105     artist=${array[1]}
 106     album=${array[2]}
 107     title=${array[3]}
 108 
 109     tt=${time#*/}
 110     tti=$(date -ud $tt +%s)
 111 
 112     ct=${time%/*}
 113     cti=$(date -ud $ct +%s)
 114 
 115     tl=$(date -ud @$(( $tti - $cti )) +%H:%M)
 116 } # }}}
 117 
 118 dbus(){ # {{{
 119     qdbus org.mpris.MediaPlayer2.$1 \
 120     /org/mpris/MediaPlayer2 \
 121     org.mpris.MediaPlayer2.Player.$2 $3
 122 } # }}}
 123 dbus_control(){ # {{{
 124     case $2 in
 125         play) dbus $1 Play; sleep 0.2;;
 126         stop) dbus $1 Stop;;
 127        pause) dbus $1 PlayPause;;
 128         prev) dbus $1 Previous;;
 129         next) dbus $1 Next;;
 130         seek) dbus $1 Seek $(( $3 * 1000000 ));;
 131     esac
 132 } # }}}
 133 dbus_status(){ # {{{
 134     status=$(dbus $1 PlaybackStatus)
 135 
 136     [[ $status == Stopped ]] && return 1
 137     [[ $status == Playing ]] && status=play || status=pause
 138 
 139     declare -A array
 140     while read k v; do
 141         array[$k]=$v
 142     done <<< "$(dbus $1 Metadata)"
 143 
 144     artist=${array[xesam:artist:]}
 145     album=${array[xesam:album:]}
 146     title=${array[xesam:title:]}
 147 
 148     file=${array[xesam:url:]#file://}
 149     file=$(echo -e "${file//%/\\x}")
 150 
 151     tti=$(( ${array[mpris:length:]} / 1000000 ))
 152     tt=$(date -ud @$tti +'%M:%S')
 153 
 154     cti=$(( $(dbus $1 Position) / 1000000 ))
 155     ct=$(date -ud @$cti +'%M:%S')
 156 
 157     tl=$(date -ud @$(( $tti - $cti )) +'%M:%S')
 158 } # }}}
 159 
 160 rhythmbox_control(){ # {{{
 161     dbus_control rhythmbox "$@"
 162 } # }}}
 163 rhythmbox_status(){ # {{{
 164     dbus_status rhythmbox
 165 } # }}}
 166 
 167 export player icon file artist album title tt ct tl
 168 for player in mocp audacious cmus mpd rhythmbox; do
 169     [[ $(pgrep -x $player) ]] || continue
 170     ${player}_control "$@" && ${player}_status || continue
 171 
 172     [[ $title ]] || title=${file##*/}
 173     [[ $status == play  ]] && icon=$play_icon
 174     [[ $status == pause ]] && icon=$pause_icon
 175 
 176     [[ $MUSIC_PLAYER ]] && printf '[%9s] ' $player
 177     [[ $MUSIC_FORMAT ]] && echo $MUSIC_FORMAT | envsubst ||
 178     echo "$icon $artist${artist:+ - }$title (-$tl)"
 179 done