*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     mapfile -t array < <(mpc -f '%file%\n%artist%\n%album%\n%title%')
  96 
  97     read status _ time _ <<< "${array[4]}"
  98     [[ $status ]] || return 1
  99     [[ $status == \[playing\] ]] && status=play || status=pause
 100 
 101     file=${array[0]}
 102     artist=${array[1]}
 103     album=${array[2]}
 104     title=${array[3]}
 105 
 106     tt=${time#*/}
 107     tti=$(date -ud $tt +%s)
 108 
 109     ct=${time%/*}
 110     cti=$(date -ud $ct +%s)
 111 
 112     tl=$(date -ud @$(( tti - cti )) +%H:%M)
 113 } # }}}
 114 
 115 dbus(){ # {{{
 116     qdbus org.mpris.MediaPlayer2.$1 \
 117     /org/mpris/MediaPlayer2 \
 118     org.mpris.MediaPlayer2.Player.$2 $3
 119 } # }}}
 120 dbus_control(){ # {{{
 121     case $2 in
 122         play) dbus $1 Play; sleep 0.2;;
 123         stop) dbus $1 Stop;;
 124        pause) dbus $1 PlayPause;;
 125         prev) dbus $1 Previous;;
 126         next) dbus $1 Next;;
 127         seek) dbus $1 Seek $(( $3 * 1000000 ));;
 128     esac
 129 } # }}}
 130 dbus_status(){ # {{{
 131     status=$(dbus $1 PlaybackStatus)
 132 
 133     [[ $status == Stopped ]] && return 1
 134     [[ $status == Playing ]] && status=play || status=pause
 135 
 136     declare -A array
 137     while read k v; do
 138         array[$k]=$v
 139     done <<< "$(dbus $1 Metadata)"
 140 
 141     artist=${array[xesam:artist:]}
 142     album=${array[xesam:album:]}
 143     title=${array[xesam:title:]}
 144 
 145     file=${array[xesam:url:]#file://}
 146     file=$(echo -e "${file//%/\\x}")
 147 
 148     tti=$(( ${array[mpris:length:]} / 1000000 ))
 149     tt=$(date -ud @$tti +'%M:%S')
 150 
 151     cti=$(( $(dbus $1 Position) / 1000000 ))
 152     ct=$(date -ud @$cti +'%M:%S')
 153 
 154     tl=$(date -ud @$(( tti - cti )) +'%M:%S')
 155 } # }}}
 156 
 157 rhythmbox_control(){ # {{{
 158     dbus_control rhythmbox "$@"
 159 } # }}}
 160 rhythmbox_status(){ # {{{
 161     dbus_status rhythmbox
 162 } # }}}
 163 
 164 export player icon file artist album title tt ct tl
 165 for player in mocp audacious cmus mpd rhythmbox; do
 166     [[ $(pidof $player) ]] || continue
 167     ${player}_control "$@" && ${player}_status || continue
 168 
 169     [[ $title ]] || title=${file##*/}
 170     [[ $status == play  ]] && icon=$play_icon
 171     [[ $status == pause ]] && icon=$pause_icon
 172 
 173     [[ $MUSIC_PLAYER ]] && printf '[%9s] ' $player
 174     [[ $MUSIC_FORMAT ]] && echo $MUSIC_FORMAT | envsubst ||
 175     echo "$icon $artist${artist:+ - }$title (-$tl)"
 176 done