Random Song Player

I have hundreds of CDs, and having ripped most of them to my hard drive, I have thousands of tracks available. It's an eclectic collection, including 80s, Jazz, Classical, Baroque, Romantic, Nuevo Flamenco, Guitar, Punk, Metal ... You get the general idea.

I had the idea one day to write a script that would continuously pick random songs and play them. So we have the Random Song Player. I'll get to the code in a moment. With a line in my window manager menu that looks like this: rxvt -bg black -fg white -fn 10x20 +sb -geometry 101x12 -e randSongForever I get a window that looks like this:

Or this:

Notice the incredible juxtapositions of musical style - not to everyone's taste.

Here's the code:


#!/bin/bash
#
#   randSongForever - Play random songs forever
#   created 20020202

clear

#   the following means we skip a song on CTRL-C
trap " killall mpg123 " INT
#   It also means you have to send a "kill" to fully kill it - 
#   but if it's running in an xterm, killing the xterm will do the job

ResetColours="$(tput sgr0)"
#   15 usable colours:
DarkGrey="$(tput bold ; tput setaf 0)"
LightGrey="$(tput setaf 7)"
White="$(tput bold ; tput setaf 7)"
Red="$(tput setaf 1)"
LightRed="$(tput bold ; tput setaf 1)"
Green="$(tput setaf 2)"
LightGreen="$(tput bold ; tput setaf 2)"
Brown="$(tput setaf 3)"
Yellow="$(tput bold ; tput setaf 3)"
Blue="$(tput setaf 4)"
BrightBlue="$(tput bold ; tput setaf 4)"
Purple="$(tput setaf 5)"
Pink="$(tput bold ; tput setaf 5)"
Cyan="$(tput setaf 6)"
BrightCyan="$(tput bold ; tput setaf 6)"
numColours=15

function SelectColour {
	randColour="$(echo -e "scale=0 \n15*${RANDOM}/32767 \nquit" | bc)"
	Colour=$(echo -e "${DarkGrey}\n${LightGrey}\n${White}\n${Red}\n${LightRed}\n${Green}\n${LightGreen}\n${Brown}\n${Yellow}\n${Blue}\n${BrightBlue}\n${Purple}\n${Pink}\n${Cyan}\n${BrightCyan}" | awk 'NR=='${randColour}' {print $0}')
	echo -n ${Colour}
}

while $TRUE
do
	song=$(randsong)
	SelectColour
	echo ${Colour}$(eval basename ${song})${ResetColours}
	eval mpg321 "${song}" 2> /dev/null
	#sleep 3
done

The above calls "randsong," which is a script unto itself:

#!/bin/bash
#
#   randsong - Choose and play a random song or songs from my mp3s
#
#   Couldn't get awk to accept a variable (messing with those input
#   single quotes was causing problems), so I used head and tail
#
#   The current arrangement requires that you use:
#      eval mpg123 "$(randsong 3)" 
#   for mpg123 to accept the output of this.
#
mp3dir="/home/giles/music/"
numMp3s="$(find ${mp3dir} -name "*.mp3" -print | wc -l)"
if [ "x${1}" = "x" ]
then
	numOfPlays="1"
else
	numOfPlays="${1}"
fi

function SelectSong {
	randMp3="$(echo -e "scale=0 \n${numMp3s}*${RANDOM}/32767 \nquit" | bc)"
	mp3Name="$(echo "$(find ${mp3dir} -name "*.mp3" -print | awk 'NR=='${randMp3}' {print $0}')")"
	echo -n "\"${mp3Name}\" "
}

while [ "${numOfPlays}" -gt "0" ]
do
	SelectSong
	numOfPlays="$((${numOfPlays}-1))"
done


http://gilesorr.org/code/randsong.html (3.4Kb)
Last modified 20060516 by giles