*nohtml*

local/bin/music

   1 #!/bin/bash
   2 
   3 play_icon='>'
   4 pause_icon='||'
   5 
   6 print(){
   7     [[ $MUSIC_PLAYER ]] && printf '[%9s] ' $1
   8     echo "$icon $title (-$tl)"
   9 }
  10 
  11 # Moc {{{
  12 
  13 if [[ $(pgrep -x mocp) ]]; then
  14     case $1 in
  15         play) mocp --play; sleep 0.2;;
  16         stop) mocp --stop;;
  17        pause) mocp --toggle-pause;;
  18         prev) mocp --previous; sleep 0.1;;
  19         next) mocp --next; sleep 0.1;;
  20         seek) mocp --seek $2; sleep 0.1;;
  21     esac
  22 
  23     IFS='|' read status tt ct tl file title <<< \
  24     $(mocp --format '%state|%tt|%ct|%tl|%file|%artist - %song')
  25 
  26     if [[ $status != STOP ]]; then
  27         [[ $status == PLAY ]] &&
  28         icon=$play_icon || icon=$pause_icon
  29 
  30         (( ${#title} < 4 )) &&
  31         title=$(basename "$file")
  32 
  33         print mocp
  34     fi
  35 fi 2>&-
  36 
  37 # }}}
  38 # DBus {{{
  39 
  40 dbus(){ # {{{
  41     qdbus org.mpris.MediaPlayer2.$player \
  42     /org/mpris/MediaPlayer2 \
  43     org.mpris.MediaPlayer2.Player.$1 $2
  44 } # }}}
  45 
  46 for player in audacious rhythmbox spotify; do
  47     [[ ! $(pgrep -x $player) ]] && continue
  48 
  49     case $1 in
  50         play) dbus Play; sleep 0.2;;
  51         stop) dbus Stop;;
  52        pause) dbus PlayPause;;
  53         prev) dbus Previous;;
  54         next) dbus Next;;
  55         seek) dbus Seek $(( $2 * 1000000 ));;
  56     esac
  57 
  58     status=$(dbus PlaybackStatus)
  59     if [[ $status != Stopped ]]; then
  60         [[ $status == Playing ]] &&
  61         icon=$play_icon || icon=$pause_icon
  62 
  63         unset array
  64         declare -A array
  65         while read k v; do
  66             array[$k]=$v
  67         done <<< "$(dbus Metadata)"
  68 
  69         artist=${array[xesam:artist:]}
  70         title=${array[xesam:title:]}
  71 
  72         len=$(( ${array[mpris:length:]} / 1000000 ))
  73         pos=$(( $(dbus Position) / 1000000 ))
  74 
  75         tt=$(date -ud @$len +'%M:%S')
  76         ct=$(date -ud @$pos +'%M:%S')
  77         tl=$(date -ud @$(( $len - $pos )) +'%M:%S')
  78 
  79         [[ $artist ]] &&
  80         title="$artist - $title"
  81 
  82         print $player
  83     fi
  84 done
  85 
  86 # }}}
  87 # Cmus {{{
  88 
  89 if [[ $(pgrep -x cmus) ]]; then
  90     case $1 in
  91         play) cmus-remote --play; sleep 0.2;;
  92         stop) cmus-remote --stop;;
  93        pause) cmus-remote --pause;;
  94         prev) cmus-remote --prev;;
  95         next) cmus-remote --next;;
  96         seek) cmus-remote --seek $2;;
  97     esac
  98 
  99     unset array
 100     declare -A array
 101     while read k1 k2 v; do
 102         [[ $k1 == set || $k1 == tag ]] &&
 103         array[$k2]="$v" || array[$k1]="$k2 $v"
 104     done <<< "$(cmus-remote --query)"
 105 
 106     status=${array[status]}
 107     if [[ $status != stopped* ]]; then
 108         [[ $status == playing* ]] &&
 109         icon=$play_icon || icon=$pause_icon
 110 
 111         file=${array[file]}
 112         artist=${array[artist]}
 113         title=${array[title]}
 114 
 115         len=${array[duration]}
 116         pos=${array[position]}
 117 
 118         tt=$(date -ud @$len +'%M:%S')
 119         ct=$(date -ud @$pos +'%M:%S')
 120         tl=$(date -ud @$(( $len - $pos )) +'%M:%S')
 121 
 122         [[ $title ]] &&
 123         title="$artist - ${title}" ||
 124         title=$(basename "$file")
 125 
 126         print cmus
 127     fi
 128 fi
 129 
 130 # }}}
 131 # Mpd {{{
 132 
 133 if [[ $(pgrep -x mpd) ]]; then
 134     case $1 in
 135         play) mpc play;;
 136         stop) mpc stop;;
 137        pause) mpc toggle;;
 138         prev) mpc prev;;
 139         next) mpc next;;
 140         seek) mpc seek $2;;
 141     esac >&-
 142 
 143     unset array
 144     declare -a array
 145     while read line; do
 146         array+=("$line")
 147     done <<< "$(mpc --format '[%artist% - %title%]|[%file%]')"
 148 
 149     if [[ ${array[1]} ]]; then
 150         title=${array[0]##*/}
 151         read status _ tm _ <<< "${array[1]}"
 152 
 153         tt=${tm#*/}
 154         ct=${tm%/*}
 155 
 156         tl_t=$(date -ud $tt +%s)
 157         tl_c=$(date -ud $ct +%s)
 158         tl=$(date -ud @$(( $tl_t - $tl_c )) +%H:%M)
 159 
 160         [[ $status == \[playing\] ]] &&
 161         icon=$play_icon || icon=$pause_icon
 162 
 163         print mpd
 164     fi
 165 fi
 166 
 167 # }}}