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
12 # Reggae/Bob Marley/1984 - Legend/09 - I Shot The Sheriff.ogg
14 # Change the depth by typing:
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.
24 configfile
="$HOME/.mpdnextdepth"
27 if [ "$1" == "--help" ]; then
28 echo "Usage: mpdnext [OPTION] [new depth]"
30 echo "--help print this text and exit"
31 echo "--depth print current depth and exit"
32 echo "no argument run in a loop"
34 elif [ "$1" == "--depth" ]; then
38 echo "$1" > "$configfile"
43 interval
="$long_interval"
44 previous_depth
=`cat "$configfile"`
47 test -f "$configfile" ||
echo 1 > "$configfile"
54 mpc idle playlist
> /dev
/null
57 depth
=`cat "$configfile"`
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
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
78 current
="`mpc playlist -f '%file%' | head -1`"
79 if [ "$current" ]; then
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
92 interval
="$short_interval"