aboutsummaryrefslogtreecommitdiffstats
path: root/_bin
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@gmail.com>2016-01-08 21:11:52 +0800
committerAaron LI <aaronly.me@gmail.com>2016-01-08 21:11:52 +0800
commite879719e6644cdace1e794e5471eedfb3b882310 (patch)
tree3793565d8f55fd96ddf0c2e143c1961424eb6cc4 /_bin
parent809155c33e7986cc1ff87781cc5892e7d534a589 (diff)
downloaddotfiles-e879719e6644cdace1e794e5471eedfb3b882310.tar.bz2
Add scripts `volume.sh` and `music-player.sh`
* `volume.sh`: support Linux (amixer/ALSA) and FreeBSD/DragonFly (mixer) * `music-player.sh`: support mpd and mocp
Diffstat (limited to '_bin')
-rwxr-xr-x_bin/music-player.sh145
-rwxr-xr-x_bin/volume.sh78
2 files changed, 223 insertions, 0 deletions
diff --git a/_bin/music-player.sh b/_bin/music-player.sh
new file mode 100755
index 0000000..e1e6979
--- /dev/null
+++ b/_bin/music-player.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+#
+# Music player control wrapper to support different players.
+#
+# Aaron LI
+# Created: 2016-01-07
+# Updated: 2016-01-08
+#
+
+PLAYERS="mpd mocp"
+#PLAYERS="mocp mpd"
+
+
+if [ $# -ne 1 ]; then
+ echo "Usage:"
+ echo " `basename $0` < state | info | play | pause | toggle | stop | previous | next >"
+ exit 1
+fi
+
+
+escape() {
+ echo "$1" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g;'
+}
+
+
+notify() {
+ title="$1"
+ text=`escape "$2"`
+ icon="$3"
+ notify-send -t 5000 --hint=int:transient:1 -i "${icon}" "${title}" "${text}"
+}
+
+# mpd/mpc
+player_mpd() {
+ case "$1" in
+ state)
+ status=`mpc status`
+ if echo "${status}" | grep -q '^\[playing\]'; then
+ echo "playing"
+ elif echo "${status}" | grep -q '^\[paused\]'; then
+ echo "paused"
+ else
+ echo "stopped"
+ fi
+ ;;
+ info)
+ state=`player_mpd state`
+ #echo "state: ${state}"
+ if [ "${state}" = "stopped" ]; then
+ notify "MPD" "stopped"
+ else
+ artist=`mpc current -f "%artist%"`
+ album=`mpc current -f "[%date% - ]%album%"`
+ title=`mpc current -f "%title%"`
+ notify "${title}" "${artist}\n${album}"
+ fi
+ ;;
+ play)
+ mpc play
+ ;;
+ pause)
+ mpc pause
+ ;;
+ toggle)
+ mpc toggle
+ ;;
+ stop)
+ mpc stop
+ ;;
+ previous)
+ mpc prev
+ ;;
+ next)
+ mpc next
+ ;;
+ *)
+ echo "ERROR: mpd/mpc: unknown command!"
+ exit 11
+ ;;
+ esac
+}
+
+
+# moc/mocp
+player_mocp() {
+ case "$1" in
+ state)
+ state=`mocp -Q "%state"`
+ [ "${state}" = "PLAY" ] && echo "playing"
+ [ "${state}" = "STOP" ] && echo "stopped"
+ [ "${state}" = "PAUSE" ] && echo "paused"
+ ;;
+ info)
+ state=`mocp -Q "%state"`
+ if [ "${state}" = "STOP" ]; then
+ notify "MOC" "stopped"
+ else
+ artist=`mocp -Q "%artist"`
+ album=`mocp -Q "%album"`
+ title=`mocp -Q "%song"`
+ notify "${title}" "${artist}\n${album}"
+ fi
+ ;;
+ play)
+ state=`mocp -Q "%state"`
+ [ "${state}" = "STOP" ] && mocp -p || mocp -U
+ ;;
+ pause)
+ mocp -P
+ ;;
+ toggle)
+ mocp -G
+ ;;
+ stop)
+ mocp -s
+ ;;
+ previous)
+ mocp -r
+ ;;
+ next)
+ mocp -f
+ ;;
+ *)
+ echo "ERROR: mocp: unknown command!"
+ exit 21
+ ;;
+ esac
+}
+
+
+for player in ${PLAYERS}; do
+ if pgrep -x ${player} >/dev/null 2>&1; then
+ player_cmd="player_${player}"
+ #echo "Found player: ${player}"
+ break
+ fi
+done
+
+if [ -n "${player_cmd}" ]; then
+ eval ${player_cmd} "$1"
+else
+ echo "ERROR: no known music player running/found"
+ exit 31
+fi
+
diff --git a/_bin/volume.sh b/_bin/volume.sh
new file mode 100755
index 0000000..f8558d9
--- /dev/null
+++ b/_bin/volume.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+#
+# Volume control wrapper to support different systems.
+#
+# Aaron LI
+# Created: 2016-01-07
+# Updated: 2016-01-07
+#
+
+# ALSA device/driver (Linux)
+ALSA_DEV="Master"
+# OSS device (FreeBSD/DragonFly)
+OSS_DEV="vol"
+
+
+if [ $# -ne 1 ]; then
+ echo "Usage:"
+ echo " `basename $0` < +% | -% | toggle | mute | unmute >"
+ exit 1
+fi
+
+
+# amixer: Linux ALSA
+vol_amixer() {
+ case "$1" in
+ toggle)
+ amixer set ${ALSA_DEV} toggle
+ ;;
+ mute)
+ amixer set ${ALSA_DEV} mute
+ ;;
+ unmute)
+ amixer set ${ALSA_DEV} unmute
+ ;;
+ +[1-9]*|-[1-9]*)
+ sign=`echo "$1" | cut -c1`
+ value="${1#?}"
+ amixer set ${ALSA_DEV} "${value}%${sign}" unmute
+ ;;
+ *)
+ echo "ERROR: amixer: unknown control command"
+ exit 11
+ ;;
+ esac
+}
+
+
+# mixer: FreeBSD/DragonFly OSS
+vol_mixer() {
+ # FIXME: how to toggle/mute/unmute ???
+ case "$1" in
+ +[1-9]*|-[1-9]*)
+ sign=`echo "$1" | cut -c1`
+ value="${1#?}"
+ mixer ${OSS_DEV} "${sign}${value}:${sign}${value}"
+ ;;
+ *)
+ echo "ERROR: mixer: unknown control command"
+ exit 21
+ ;;
+ esac
+}
+
+
+OS=`uname -s`
+case "${OS}" in
+ Linux)
+ vol_amixer "$1"
+ ;;
+ FreeBSD|DragonFly)
+ vol_mixer "$1"
+ ;;
+ *)
+ echo "ERROR: currently unsupport operating system"
+ exit 2
+ ;;
+esac
+