#!/bin/csh ############################################################### # This script watches for CD insertion. When one is inserted, # it does the things the musicbox should do: # Kills any currently-playing song # If the CD is already in the catalog # Tell the user we recognize it # Start playing the CD from the hard drive # Eject the CD # Otherwise # Start ripping the songs to a temporary dir # Ask the user where the songs should end up # ############################################################## 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_put_files_in catalog_file" exit 3 endif # Make sure that the directory exists if (-e "$directory") then else echo "Error: directory $directory does not exist" exit 3 endif # Make sure they passed in the name of a directory set catalog = $2 if (0 == `echo -n "$catalog" | wc -c`) then echo "Usage: $0 directory_to_put_files_in catalog_file" exit 3 endif # Make sure that the catalog file exists if (-e "$catalog") then else echo "Error: catalog file $catalog does not exist" exit 3 endif ############################################################## # Media and existence checking # Find out the ID of the disc that is in the CD device. # Make sure that it is valid. set discid = `musicbox_get_disc_id.txt` if ( 0 != $status) then echo "Could not read discID from CD" exit 3 endif # See if we've already read this disc (it will be in the catalog # if so). If so, return code 1. set entry = `grep $discid $catalog` if ( 0 != `echo -n $entry | wc -c` ) then echo "This disc has already been cataloged" exit 1 endif # Check to see if there is an ID file in the directory already. # If so, then return an error code. if ( -e $directory/id ) then echo "There is already another disc in $directory" exit 2 endif ############################################################## # All of the checks have been done and we're ready to do our # thing. Start by going into the directory that we are going # to fill up and writing the ID file. Then start saving the # .wav files into that directory. pushd $directory echo $discid > id # Find out how many tracks there are on the disc. This is done # by running cdda2wav again with the -J parameter, which # has the side effect of creating index files for each of # the tracks on the CD. cdda2wav -D $device -I cooked_ioctl -v disable -J >& /dev/null set numtracks = `ls audio_??.inf | wc -w` echo $numtracks tracks found if ( 0 == $numtracks ) then echo "Found no audio tracks on the CD" exit 3 endif # Now convert each track into an MP3 file. # Launch the play script after track one has been completed. # Use the 'lame' program for the conversion. set track = 1 while ( $track <= $numtracks ) set prettytrack = $track if ( $track < 10 ) set prettytrack = 0$track # -S 0 is default (maximum) speed for the device. # -n 100 makes it read 100 sectors whenever it wants one # -l 200 creates a 200-entry circular buffer of sector storage # -H means no info-file # XXX -P 0 will fail to correct for bad reads, but is faster cdda2wav -D $device -I cooked_ioctl -v disable -H -t $track -P 0 -n 100 -S 0 -l 200 -Q track_$prettytrack.wav set cddastatus = $status if ( $cddastatus != 0 ) then echo "Error $cddastatus when trying to copy track $track" rm * exit 3 endif @ track ++ end ############################################################## # Delete the information tracks. \rm -f audio_??.inf ############################################################## # Put an entry into the catalog for this CD popd echo $discid $directory >> $catalog exit 0