May 29, 2008 at 4:15PM Stupid shell hacks: A poor man’s MusicPD.
I use ROX, and one of the extension scripts I wrote for it was something to allow me to play arbitrary audio files in the background without needing something like MusicPD.
#!/bin/sh
#
# play-track
# by Keith Gaughan <http://talideon.com/>
#
# Think of it as the poor man's MPD. :-) Specifically, I use it for playing
# audio files with ROX. Start it with a file to play that file and without
# to stop whatever the file currently being played from playing. Starting it
# with a directory name will play all the tracks in that directory (but none
# of its subdirectories). Starting it with no arguments will cause the copy
# currently executing in the background to exit.
#
# I, Keith Gaughan, hereby put this script in the public domain. It comes
# without warranty of any kind and the author disclaims responsibility for
# any mishap that may occur due to its use. You are requested, but not
# obligated, to leave the attribution of the original author in place and
# to append your name to the attribution if you distribute altered versions
# of this script.
#
PID_FILE=$HOME/.`basename "$0"`.pid
kill_existing () {
test -r $PID_FILE && kill `cat $PID_FILE`
}
play () {
kill_existing
for i in mplayer mpg321 mpg123 plaympeg; do
if which $i >/dev/null; then
$i "$@" 2>&1 >/dev/null &
PID=$!
echo $PID >$PID_FILE
if wait $PID; then
test -r $PID_FILE && rm $PID_FILE
return 0
fi
test -r $PID_FILE && rm $PID_FILE
return 1
fi
done
return 1
}
randomise_files () {
for i in "$@"/*; do
# jot is *BSD specific and generates numbers, much like seq. However,
# it's also able to generate sequences of *random* numbers.
echo "`jot -rn 1 1 100000`~$i"
done | sort -n | cut -f2 -d~
}
play_dir () {
randomise_files "$@" | while read j; do
play "$j" || exit
done
}
if [ "$@ " = " " ]; then
kill_existing
elif [ -d "$@" ]; then
play_dir "$@"
else
play "$@"
fi
The script has a few FreeBSD-isms, notably the use of jot to randomise the tracklisting. If you use bash, you can substitute $RANDOM for this.
Asides from demonstrating some nifty piping tricks, there’s an actual purpose behind me posting this up. Originally, it was only able to play individual tracks, but I extended it last night to support playing the contents of a directory. Why? Because I’ve became extraordinarily bad at getting up in the morning lately and I decided to have my laptop double as an alarm clock by setting up an at or cron job to trigger the alarm. I didn’t get to try it out properly last night, but I’m going to try it tonight and see if it helps.
Update: Well, it seems like Operation “Get The Hell Outta Bed” failed miserably. I’d set it up, but I was so anxious about the whole thing that I couldn’t get to sleep at all. Let’s see if I’ve better results on Monday.
No comments.