added README
[mpdhero.git] / mpdnext
blob18b10eb70798f4cc444986bd65bbc46ef32cfbd9
1 #!/bin/bash
2 # mpdnext
4 # This program runs as a daemon and manages the current mpd playlist
5 # by changing the next song in the playlist to another similar song.
7 # To use this, select one song in your playlist and run this script.
9 # This script works best if your music is systematically ordered like this:
10 # GENRE/ARTIST/ALBUM/SONG
11 # for example:
12 # Reggae/Bob Marley/1984 - Legend/09 - I Shot The Sheriff.ogg
14 # Change the depth by typing:
15 # mpdnext <depth>
17 # The depth is a number that specifies how similar the next song will be.
18 # A depth of 2 means, pick a random song from "Reggae/Bob Marley". A depth
19 # of 0 would pick any random song.
21 # configuration
22 short_interval=0.1
23 long_interval=3
24 configfile="$HOME/.mpdnextdepth"
26 # argument handling
27 if [ "$1" == "--help" ]; then
28 echo "Usage: mpdnext [OPTION] [new depth]"
29 echo "Options:"
30 echo "--help print this text and exit"
31 echo "--depth print current depth and exit"
32 echo "no argument run in a loop"
33 exit
34 elif [ "$1" == "--depth" ]; then
35 cat "$configfile"
36 exit
37 elif [ "$1" ]; then
38 echo "$1" > "$configfile"
39 exit
42 # preparations
43 interval="$long_interval"
44 previous_depth=`cat "$configfile"`
45 counter=
46 last=
47 test -f "$configfile" || echo 1 > "$configfile"
48 mpc crop
49 mpc consume on
50 mpc random off
52 # main loop
53 while true; do
54 mpc idle playlist > /dev/null
56 # get the depth
57 depth=`cat "$configfile"`
58 case "$depth" in
59 any) depth=0;;
60 genre) depth=1;;
61 artist) depth=2;;
62 album) depth=3;;
63 stop) continue;;
64 esac
66 # delete the second song if the depth has changed
67 if [ "$previous_depth" -a "$depth" != "$previous_depth" ]; then
68 echo depth changed to $depth
69 mpc del 2
70 previous_depth="$depth"
73 # don't care if playlist is longer than 2
74 [ "`mpc playlist | head -2 | wc -l`" -gt 1 ] && continue
76 # keep the last "current song" in a buffer in case the playlist
77 # is cleared.
78 current="`mpc playlist -f '%file%' | head -1`"
79 if [ "$current" ]; then
80 last="$current"
81 else
82 current="$last"
85 # extract the path with the given depth
86 cmd='s/^\/\?\(\([^\/]*\/\?\)\{0,'"$depth"'\}\)[\/$].*$/\1/'
87 path=$(echo /"$current" | sed "$cmd")
89 # add one random song from that path
90 mpc listall "$path" | shuf -n 1 | mpc add
91 counter=50
92 interval="$short_interval"
93 done