local/bin/music

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