#!/bin/csh ############################################################### # This program will play all of the music files (.wav or .mp3) # in the directory whose name is passed in, or an any of the # subdirectories of that directory. It plays them in alphabetical # order and will only play each song once. It is set to work # correctly if new files are added while it is playing (the # new files will be played). # # It returns 0 on success, nonzero on failure. ############################################################## set device = '/dev/cdrom' ############################################################## # Argument checking # Make sure they passed in the name of a directory set directory = $1 if (0 == `echo -n "$directory" | wc -c`) then echo "Usage: $0 directory_to_play" exit 1 endif # Make sure that the directory exists if (-e "$directory") then else echo "Error: directory $directory does not exist" exit 1 endif ############################################################## # Store the name of the directory we are playing into the # currently-playing file within the state directory. First # delete the file, in case one was left over. set playstate_file = `musicbox_state_directory.txt`/now_playing \rm -f "$playstate_file" echo "$directory" > "$playstate_file" ############################################################## # Find a file under us that we have not yet played. Play the # songs in alphabetical order. Only play each song once. # Check for new songs after each song is played. This is done # by maintaining a playlist against which to compare. set playlist = /tmp/playlist set playtest = /tmp/playtest \rm -f $playlist touch $playlist \rm -f $playtest find "$directory" -name \*.wav -or -name \*.mp3 > $playtest set nextfile = `cat "$playlist" "$playtest" | sort | uniq -u | head -1` while ( 0 != `echo -n "$nextfile" | wc -c` ) echo "$nextfile" >> $playlist echo Playing $nextfile # If we found a WAV file, play it directly. Otherwise, use # 'lame' to decode it (MP3 file). if ( 0 == `echo -n $nextfile | grep -i .mp3 | wc -c` ) then play "$nextfile" else lame --quiet --decode "$nextfile" - | play -t wav - endif # Go looking for another file. \rm -f $playtest find "$directory" -name \*.wav -or -name \*.mp3 > $playtest set nextfile = `cat "$playlist" "$playtest" | sort | uniq -u | head -1` end ############################################################## # Delete the now_playing file because we just stopped it from # playing. set playstate_file = `musicbox_state_directory.txt`/now_playing \rm -f "$playstate_file" ############################################################## # Done rm $playlist exit 0